CodeColorer
CodeColorer is the plugin which allows you to insert code snippets into the post with nice syntax highlighting. I want to show you example first:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | class Feed < ActiveRecord::Base SourceTypes = { :category => 0, :tag => 1 } define_enum :source_type, :raise_on_invalid => true #------------------------------------------------- # Accessors def video_ids v = self[:video_ids] v ? v.split(':').map(&:to_i) : [] end end |
- syntax highlighting in RSS feeds
- syntax highlighting of single line of code (inline)
- syntax highlighting of code in comments
- line numbers
- automatic links to the documentation inserting
- code block intelligent scroll detection (short code would have short block, for long one block height would be fixed and scrollbar would appear)
- predefined color themes (Slush & Poppies, Blackboard, Dawn, Mac Classic, Twitlight, Vibrant Ink)
- syntax colors customization in CSS file
- code protect from mangling by WordPress (for example, quotes, double-dashes, etc would look just right as you entered)
Installation
- Download and unpack plugin files to wp-content/plugins/codecolorer directory.
- Enable CodeColorer plugin on your Plugins page in Site Admin.
- Go to the Options/CodeColorer page in Site Admin and change plugin’s options as you wish.
- Use
[cc lang="lang"]code[/cc]
or<code lang="lang">code</code>
syntax to insert code snippet into the post (you could skiplang="lang"
, in this case code would be in code block, but without syntax highlighting). Also you can use[ccilang="lang"]code[/cci]
to format inline code (see the “inline” option description). The list of available languages you could find below. - Have fun!
Syntax
To insert code snippet into your post (or comment) you should use
[cc lang="lang"]code[/cc]
or<code lang="lang">code</code>
syntax. Starting from version 0.6.0 you could specify additional CodeColorer options inside [cc]
tag:1 | [cc lang="php" tab_size="2" lines="40"]// some code[/cc] |
Note: You should always use double quotes or single quotes around the parameter value. Boolean values could be passed using string true or false, on or off, number 1 or 0.
Short codes
Starting from CodeColorer 0.8.6 you can use short codes to insert code snippets. The short code in common looks like
[ccMODE_LANG]
, where LANG is your programming language, and MODE is the one or more of following modes:- i – inline
- e – escaped
- s – strict
- n – line_numbers
- b – no_border
- w – no_wrap
- l – no_links
Small letter means enabled, capital – disabled. Examples:
PHP code with links enabled and line numbers disabled:
1 2 3 | [cclN_php] echo "hello" [/cclN_php] |
Already escaped HTML code:
1 | [ccie_html]<html>[/ccie_html] |
Ruby code without wrapping having tab size equal to 4:
1 2 3 | [ccW_ruby tab_size="4"] attr_accessor :title [/ccW_ruby] |
More examples could be found on the CodeColorer Examples page. You can find modes explained below.
Possible parameters
- lang (string) – source language.
- tab_size (integer) – how many spaces would represent TAB symbol.
- line_numbers (boolean) – when true line numbers will be added.
- first_line (integer) – a number of the first line in the block.
- highlight (string) – a comma-separated list of line numbers to highlight (e.g.
1,5,8,9
). - no_links (boolean) – when false keywords will be represented as links to manual.
- lines (integer) – how many lines would be block height without scroll; could be set to -1 to remove vertical scrollbar.
- width (integer or string) – block width.
- height (integer or string) – height in pixels; used when lines number is greater then “lines” value.
- rss_width (integer or string) – block width in RSS feeds.
- theme (string) – color theme (default, blackboard, dawn, mac-classic, twitlight, vibrant).
- inline (boolean) – when true forces code block to render inside
<code></code>
tag. Used to paste a single line of code into the regular text. - strict (boolean) – when true strict mode will be enabled. By default CodeColorer tries to guess whether strict mode is needed, so this option allows to force it on or off when automatic suggestion is wrong.
- nowrap (boolean) – when false no horizontal scrollbar will be shown; instead code will be wrapped in the end of code box.
- noborder (boolean) – when true no border will be shown around the code block.
- no_cc (boolean) – when true the syntax in code block will not be highlighted, code will be rendered inside
<code></code>
tag. - class (string) – additional CSS classes to add to the wrapper HTML element.
You can use special tag
[cci]
instead of [cc]
to force inline mode: [ccilang="lang"]code[/cci]
Most of these parameters could be configured via the CodeColorer options page.
Supported languages
Here is list of supported by CodeColorer languages: abap, actionscript, actionscript3, ada, apache, applescript, apt_sources, asm, asp, autoit, avisynth, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_mac, caddcl, cadlisp, cfdg, cfm, cil, cmake, cobol, cpp-qt, cpp, csharp, css, d, dcs, delphi, diff, div, dos, dot, eiffel, email, erlang, fo, fortran, freebasic, genero, gettext, glsl, gml, gnuplot, groovy, haskell, hq9plus, html4strict, idl, ini, inno, intercal, io, java, java5, javascript, kixtart, klonec, klonecpp, latex, lisp, locobasic, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, make, matlab, mirc, modula3, mpasm, mxml, mysql, nsis, oberon2, objc, ocaml-brief, ocaml, oobas, oracle11, oracle8, pascal, per, perl, php-brief, php, pic16, pixelbender, plsql, povray, powershell, progress, prolog, properties, providex, python, qbasic, rails, rebol, reg, robots, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, tcl, teraterm, text, thinbasic, tsql, typoscript, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xml, xorg_conf, xpp, yaml, z80.
Requirements
Plugin works with WordPress 2.7.0 – 2.8.2 as well. Hey users of WordPress 2.7.0, could you explain why do you use so old buggy crap? Use new one instead, at least it looks more beautiful!
Download
The latest version of the CodeColorer plugin is 0.9.8, and you can download it here:
Also you can download older versions from plugin home page on WordPress.org (but do you really need this old stuff?).
Translations
Thank you all guys, who submitted translations to your language. CodeColorer is currently available in following languages:
- English
- Russian – Русский
- Ukrainian – Українська
- Arabic – العربية (thanks to Amine Roukh)
- Belarusian – Беларуский (thanks to И. Фадков)
- Czech – Čeština (thanks to Lelkoun)
- Danish – Dansk (thanks to Hans Klysner)
- Dutch – Nederlands (thanks to Martijn van Iersel)
- French – Français (thanks to Valentin PRUGNAUD, fanta78, Sylvain Zabé, and Whiler)
- Georgian – ქართული ენა (thanks to Nika Chkhikvishvili)
- German – German (Deutsch) (thanks to Fabian Schulz and Michael Gutbier)
- Hebrew – עִבְרִית (thanks to Yaron Ofer)
- Italian – Italiano (thanks to CodeSnippet)
- Japanese – 日本語 (thanks to Kuroneko Square)
- Persian – فارسی, پارسی, دری (thanks to Hamed Momeni)
- Polish – Polski (thanks to Andrzej Pindor)
- Brazilian Portuguese – Português Brasileiro (thanks to Paulo César M. Jeveaux, Fabricio Bortoluzzi, and Rodolfo Leão)
- Simplified Chinese – 简化字 (thanks to liuxiangqian)
- Spanish – Español (thanks to Sergio Díaz)
- Spanish – Español (Argentina) (thanks to Diego Sucaria)
- Spanish – Español (Colombia) (thanks to Diego Alberto Bernal)
- Swedish – Svenska (thanks to LHLI)
- Turkish – Türkçe (thanks to Hasan Akgöz)
Want to help me with translation? It’s easy!
- Install Poedit.
- Download codecolorer.pot file.
- Click File/New catalog from .pot file and select codecolorer.pop you’ve just downloaded.
- Enter project name (something like CodeColorer 0.9.8), your name and email address, select a language you want to translate to and click OK.
- Enter a filename like codecolorer-en_EN.po and click Save.
- Translate all strings one by one.
- Send me a .po file with a translation to kpumuk@kpumuk.info. Do not forget a link to add to CodeColorer project home page.
- Thank you!
To fix existing translation, just open corresponding .po file from codecolorer/languages folder in Poedit, and add missing or update existing strings.
Customization
Syntax coloring is highly customizable: you could change color scheme for all
languages or for specific language. You could find CodeColorer CSS in
wp-content/plugins/codecolorer/codecolorer.css file. To change colors for
all languages edit lines below Color scheme section.
languages or for specific language. You could find CodeColorer CSS in
wp-content/plugins/codecolorer/codecolorer.css file. To change colors for
all languages edit lines below Color scheme section.
There is simple mapping exists between Textmate color themes and CodeColorer
ones:
ones:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | /* "Slush & Poppies" color scheme (default) */ .codecolorer-container, .codecolorer { color: #000000; background-color: #F1F1F1; } /* Comment */ .codecolorer .co0, .codecolorer .co1, .codecolorer .co2, .codecolorer .co3, .codecolorer .co4, .codecolorer .coMULTI { color: #406040; font-style: italic; } /* Constant */ .codecolorer .nu0, .codecolorer .re3 { color: #0080A0; } /* String */ .codecolorer .st0, .codecolorer .st_h, .codecolorer .es0, .codecolorer .es1 { color: #C03030; } /* Entity */ .codecolorer .me1, .codecolorer .me2 { color: #0080FF; } /* Keyword */ .codecolorer .kw1, .codecolorer .kw2, .codecolorer .sy1 { color: #2060A0; } /* Storage */ .codecolorer .kw3, .codecolorer .kw4, .codecolorer .kw5, .codecolorer .re2 { color: #008080; } /* Variable */ .codecolorer .re0, .codecolorer .re1 { color: #A08000; } /* Global color */ .codecolorer .br0, .codecolorer .sy0 { color: #000000; } |
Check the codecolorer.css file to get more examples.
Frequently Asked Questions
Q. How do I can customize CodeColorer CSS rules?
A. Go to the Options/CodeColorer page in Site Admin and change the “Custom CSS Styles” option.
A. Go to the Options/CodeColorer page in Site Admin and change the “Custom CSS Styles” option.
Q. I see < instead of < (or other HTML entities like >, &, ") in my code.
A. You should use
A. You should use
[cc escaped="true"]
or [cce]
in the visual editor when inserting code into the post.Q. Does it highlights my code on server or client side?
A. CodeColorer performs code highlighting on the server, you could see HTML of the highlighted code in page source.
A. CodeColorer performs code highlighting on the server, you could see HTML of the highlighted code in page source.
Q. Is it produces valid XHTML source?
A. Yes, resulting XHTML is completely valid.
A. Yes, resulting XHTML is completely valid.
Q. Could my visitors insert their code snippets in comments?
A. Yes, CodeColorer supports code highlighting in comments using the same syntax, as you use in your blog posts.
A. Yes, CodeColorer supports code highlighting in comments using the same syntax, as you use in your blog posts.
Q. How can I disable syntax highlighting for a particular
A. Use
<code>
block?A. Use
no_cc="true"
option for your code block.Q. I have updated the plugin to the newest version and now I keep getting following warnings:
1 | Warning: array_keys() [function.array-keys]: The first argument should be an array in /home/wordpress/wp-content/plugins/codecolorer/lib/geshi.php on line 3599 |
A. Remove all files from wp-content/plugins/codecolorer folder and unpack an archive with plugin again (thanks to Anatoliy ‘TLK’ Kolesnick).
Q. I love this plugin. How to do a favor to the author?
A. Just vote for this plugin on WordPress.org. And thank you!
A. Just vote for this plugin on WordPress.org. And thank you!
Support
If you have any suggestions, found a bug, wanted to contribute a
translation to your language, or just wanted to say “thank you”,– feel free to contact me. Promise, I will answer to every message.
translation to your language, or just wanted to say “thank you”,– feel free to contact me. Promise, I will answer to every message.
If you want to contribute your code, see the Development section below.
Development
Sources of this plugin are available both in SVN and Git:
Feel free to check them out, make your changes and send me patches. Promise, I will apply every patch (of course, if they add a value to the product). Email for patches, suggestions, or bug reports:kpumuk@kpumuk.info. Also you have much more ways to contact me.
Changelog
- 0.9.8 (March 23, 2010)
- Added an icon to the admin options page.
- Updated Arabic translation (thanks to Amine Roukh).
- GeSHi updated to 1.0.8.6.
- Added ability to highlight specified lines (thanks to DELTA NOVA).
- Added Czech translation (thanks to Lelkoun).
- Added Georgian translation (thanks to Nika Chkhikvishvili).
- Added Persian translation (thanks to Hamed Momeni).
- Some unit tests added.
- 0.9.7 (December 19, 2009)
- Fixed
theme="geshi"
attribute bug. - Added ability to highlight arbitary piece of code from PHP.
- Use
wp_enqueue_style
instead of echoing plain HTML. - Fixed problem with escaped code blocks, when some entities were not unescaped.
- Fixed compatibility with WordPress 2.9.
- Fixed
- 0.9.6 (December 18, 2009)
- Added French translation (thanks to Valentin PRUGNAUD, fanta78, Sylvain Zabé, and Whiler).
- Added Brazilian Portuguese translation (thanks to Paulo César M. Jeveaux, Fabricio Bortoluzzi, and Rodolfo Leão).
- Added Swedish translation (thanks to LHLI).
- Fixed XHTML validation problems on the CodeColorer options page (thanks to Brett Zamir).
- Added Japanese translation (thanks to Kuroneko Square).
- Added Danish translation (thanks to Hans Klysner).
- Added GeSHi theme.
- Added ability to specify custom CSS class for the wrapper HTML element.
- 0.9.5 (August 27, 2009)
- Added Dutch translation (thanks to Martijn van Iersel).
- Added Spanish (Argentina) translation (thanks to Diego Sucaria).
- Added Arabic translation (thanks to Amine Roukh).
- Fixed bug in Safari 4 caused by text-align=justify in parent container.
Complete changelog could be found on the CodeColorer History page.
Other plugins
Full list of plugins I have implemented is available here.
1. When I use
<code>
it removes my text from my page.2. When I set a theme I still get no colors on the code on my page.
1. When I use
no_cc="true"
my text is removed from my page.{code lang=”"}тут текст{code} (специально заменил)
не вылазила ошибка, вот такая:
The second is to use the Geshi methods to show the lines:
2
$geshi->enable_line_numbers(GESHI_FANCY_LINE_NUMBERS);
2
3
4
5
6
7
8
position: relative;
width: 98%;
}
.codecolorer-container .codecolorer{
position: absolute;
top:0;
}
.codecolorer
‘s position to absolute “locks” it inside the first HTML element whose position is not static (because its position is based on that element), so setting.codecolorer-container
to relative, makes it the element to where.codecolorer
will be based.position
allows us to change an element’s original position and area.position: relative;
allows us to change it based on its original position. Since no top and left properties are set, they remain on their original position, but.codecolorer
now can’t go beyond.codecolorer-container
.width: 98%;
to define its width, because the whole idea of using these styles is so that I can use % width.GetCodeHighlighted
:2
3
4
5
6
$codeColorer = &CodeColorer::GetInstance();
echo $codeColorer->GetCodeHighlighted($string);
}
$string
is a normal string we use in posts and comments, it can have normal text with HTML and they won’t be highlighted, and we just use normal[ cc][ /cc]
<code></code>
and anything inside them will be highlighted!codecolorer_highlight
global function. It is defined incodecolorer.php
and used just to do that (starting from version 0.9.7).