Home

Awesome

Crayon Syntax Highlighter

Supports multiple languages, themes, fonts, highlighting from a URL, local file or post text.

snapshot

Written in PHP and jQuery. Crayon is a Wordpress plugin, but can be used in any PHP environment.

Download

Tag Editor

The Tag Editor makes adding code, changing settings and all that much easier with a simple dialog. I'd recommend it over the manual approach, since you can fine-tune the Crayon after the editor generates it :)

Pre-formatted Tags

You can also use <pre class="attributes" title="something">...</pre> so that:

The Tag Editor generates tags like these. Attributes names are separated from values by either : (default) or _. E.g. <pre class="lang:php theme:twilight mark:1,2-4" title="something">...</pre> You can optionally provide the following attributes:

Attributes

NameDescriptionExample
langSpecify a Language ID, these are the folders in the langs directory and appears in the list of Languages in Settings.lang="java"
urlLoad a file from the web or a local path. You give a relative local path instead of absolute (see Files). For languages with defined extensions (see Languages in Settings) you don't even need to provide the lang attribute, as this will be detected if your file ends with it, as in the example.url="http://example.com/code.java" or url="java/code.java"
titleGive a title for your code snippet. Appears in the toolbar.title="Sample"
markMark some lines as important so they appear highlighted. You can specify single numbers, comma separted, a range, or a combination.mark="5-10,12"
rangeSpecify the range of lines from the input code to use in the output. You can specify a single number or a single range.range="3-5", range="3"

Mixed Highlighting

You can even mix code together like on a real HTML page, by having <script>, <style> and <?php...?> tags all in a single Crayon and setting the language to HTML.

Languages

Language information is found here in the Wordpress Admin: Settings > Crayon > Languages > Show Languages You can customise and create new languages and define how to capture each element (keywords, comments, strings, etc.) with regular expressions. Languages are structured langs/lang-name/lang-name.txt. Take a look at langs/default/default.txt and check out the neat regex of the default/generic language. See the readme in langs/readme.md for more information about the language file syntax.

You can add custom languages in wp-content/uploads/crayon-syntax-highlighter/languages with the same format as those in the plugin directory and they will remain after plugin updates.

Themes

Crayon comes with built-in Themes to style your code. See a sample of the current set of themes. Themes are structured themes/theme-name/theme-name.css. If you know CSS, take a look at themes/default/default.css to get an idea of how they work and how you can change/create them. The specification for CSS classes is here.

You can add custom themes in wp-content/uploads/crayon-syntax-highlighter/themes with the same format as those in the plugin directory and they will remain after plugin updates. This is where user themes are stored when you customise stock themes in the Theme Editor.

Comments

You can enable support for Crayon comments by adding TinyMCE to the comment box. Add this code at the end of your theme's functions.php file. This requires at least version 3.3 of Wordpress.

add_filter('comment_form_defaults', 'tinymce_comment_enable');
function tinymce_comment_enable ( $args ) {
    ob_start();
    wp_editor('', 'comment', array('tinymce'));
    $args['comment_field'] = ob_get_clean();
    return $args;
}

Then enable these settings in Wordpress Admin > Settings > Crayon:

Internationalization

Crayon comes translated in several languages already, and if yours is included it will translate Crayon given you've followed instructions about how to show Wordpress in Your Language. If Crayon doesn't support your language or you'd like to help improve it:

Fonts

You can define fonts and font-sizes within Themes, but you can also override the theme's font with those inside fonts/ and also provide @font-face fonts just like in themes - it's just CSS after all.

You can add custom fonts in wp-content/uploads/crayon-syntax-highlighter/fonts with the same format as those in the plugin directory and they will remain after plugin updates.

Disable Highlighting

You can temporarily disable highlighting for a piece of code using the highlight="false" attribute. You can also prevent Crayon from touching any kind of tag by using the crayon="false" attribute. If using pre tags, put crayon:false in the class.

Minification

Crayon's CSS and JavaScript resources is already minified (see css and js).

<!-- ### Use in Other Environments TODO ### Other Uses in Wordpress TODO -->

Settings

Crayon is versatile so you can override global settings for individual Crayons with attributes. The Tag Editor lets you do all this using a simple dialog!

SettingAllowed ValueDescription
themestringID of the theme to use
fontstringID of the font to use
font-sizenumber > 0Custom font size in pixels
min-height/max-heightnumber > 0 followed by px/%Minimum/maximum height in units
heightnumber > 0 followed by px/%Height in units
min-width/max-widthnumber > 0 followed by px/%Minimum/maximum width in units
widthnumber > 0 followed by px/%Width in units
toolbartrue/false/"always"Show or hide the toolbar. "always" shows always (without mouseover).
top-marginnumber >= 0Top margin in pixels
bottom-marginnumber >= 0Bottom margin in pixels
left-marginnumber >= 0Left margin in pixels
right-marginnumber >= 0Right margin in pixels
h-align"none/left/right/center"Horizontal alignment
float-enabletrue/falseAllow floating elements to surround Crayon
toolbar-overlaytrue/falseOverlay the toolbar on code rather than push it down when possible
toolbar-hidetrue/falseToggle the toolbar on single click when it is overlayed
toolbar-delaytrue/falseDelay hiding the toolbar on MouseOut
show-titletrue/falseDisplay the title when provided
show-lang"found/always/never"When to display the language.
stripedtrue/falseDisplay striped code lines
markingtrue/falseEnable line marking for important lines
numstrue/falseDisplay line numbers by default
nums-toggletrue/falseEnable line number toggling
plaintrue/falseEnable plain code. Disabling will also disable plain toggling and copy/paste which use the plain code.
crayonfalsePrevent from turning the tag into a Crayon.
highlighttrue/falsePrevent from highlighting the code, treats it as plain text instead.
plain-toggletrue/falseEnable plain code toggling
show-plain-defaulttrue/falseShow the plain code by default instead of the highlighted code
copytrue/falseEnable code copy/paste
popuptrue/falseEnable opening code in a window
scrolltrue/falseShow scrollbar on mouseover
tab-sizenumber >= 0Tab size
trim-whitespacetrue/falseTrim the whitespace around the code
mixedtrue/falseEnable mixed highlighting (multiple languages in code)
show_mixedtrue/falseShow the mixed highlighting plus sign
start-linenumber >= 0When to start line numbers from
fallback-langstringID of the language to use when none is detected
local-pathstringLocal path to load file from
touchscreentrue/falseDisable mouse gestures for touchscreen devices (eg. MouseOver)
disable-animtrue/falseDisable animations
runtimetrue/falseDisable runtime stats
error-logtrue/falseLog errors for individual Crayons
error-log-systrue/falseLog system-wide errors
error-msg-showtrue/falseDisplay custom message for errors
error-msgstringThe error message to show for errors
mixedtrue/falseAllow mixed languages using delimiters and tags

Legacy Tags

Using the legacy tag [crayon attributes] some code [/crayon] is possible but I highly recommend using <pre> tags instead. You can also convert legacy tags automatically in the Wordpress settings screen.

Mini Tags

Using Mini Tags like [php]some code[/php] is supported but also deprecated in favour of <pre> tags.

Licence

Crayon is released under the GPLv3 licence. See license.txt.