tweak / rhighlightjs forked from romanvbabenko/rhighlightjs

rails plugin to provide the syntax code highlighter rhighlight.js

This URL has Read+Write access

README.textile

Rhighlightjs

Rhighlightjs is a rails plugin which implements Ivan Salagaev’s highlight.js (currently v5.5)

highlight.js highlights syntax in code examples on blogs, forums and in fact on any web pages. It’s very easy to use because it works automatically: finds blocks of code, detects a language, highlights it.

Syntax highlighting is supported for the following languages:

Installation

./script/plugin install git://github.com/tweak/rhighlightjs.git

OR

git submodule add git://github.com/tweak/rhighlightjs.git vendor/plugins/rhighlightjs

After the plugin has installed, run the following rake task to copy the required assets into your rails project.

rake rhighlightjs:install

Adding to Template

Place this line just above the </body> tag in your template:

<%= highlighter %>

You can also pass the theme to use, thusly:

<%= highlighter 'dark' %>

Usage

To have a block of code highlighted, simply wrap it in <pre> and <code> tags like this:

<pre><code class="ruby">
    class Person
        def say
            puts "hello world"
        end
    end
</code></pre>

Heuristics

Autodetection of a code’s language is done with a simple heuristics: the program tries to highlight a fragment with all available languages and counts all syntactic structures that it finds along the way. The language with greatest count wins.

This means that in short fragments of code the probability of an error is much higher than with larger blocks. In this case you can set the fragment’s language explicitly by assigning a class to the <code> element:

<pre><code class="html">...</code></pre>

To disable highlighting of a fragment altogether use “no-highlight” class:

<pre><code class="no-highlight">...</code></pre>

Custom Themes

Creating a custom theme is as simple as dropping a stylesheet into the /public/stylesheets/ directory of your application, and changing the call to highlighter. For example, the following will load custom.css:

<%= highlighter 'custom' %>

Uninstallation

Running the following rake task will clear up the installed assets. NOTE: The uninstall task will also remove any custom theme stylesheets you’ve added, so make backups!

rake rhighlightjs:uninstall

Copyright © 2009 tweak released under the MIT license