Syntax highlight plugin using CodeRay

Posted on Sun Jul 01 @ 15:10:45

A couple of days ago I’ve implemented a simple syntax highlighting plugin using CodeRay. It is implemented as an after filter. Here is how you use it. In the controller add the after filter using highlight_coderay.


       class PostController < ApplicationController
           #highlight_with_coderay :only => ['show']
           highlight_with_coderay :except =>
               ['posts_feed','comments_feed',
                'comment_report_spam',
                'add_comment', 'delete_comment']

            ...
        end

After that when you add your content in the html just add <pre lang=”[language]”>[code]</pre> and your done. The after filter will replace this pre tag with the correct highlighting.

You can also customize the style coderay uses by adding this line of code to config/environment.rb:



    SyntaxHighlight::Filters::CodeRay.stylesheet = :bright


There are three styles you can use and they are cycnus, murphy which are part of coderay or bright which is part of the plugin. Also included is a javascript scanner i found here,

Installlation


     script/plugin install http://svn.munitic.com.hr/plugins/syntax_highlight

Enjoy!

UPDATE: I now added helper methods too. So now you can choose if you want to use the after filter or the view helpers. The view helpers are:



    <%= coderay_stylesheet_tag :bright %>


This prints out the stylesheet coderay uses.



    <%= highlight_with_coderay post.body %>


This actually does the highlighting

Posted in Rails, 1 Comments Comments

Comments

GERMANY rofovnifo on 04 Jul at 23:50:55

Hi Looks good! Very useful, good stuff. Good resources here. Thanks much! Bye

COMMENTS ARE DISABLED

About me

Me
Full name: Ivica Munitic Age: 30
Profession: Developer
Email: ivica@munitic.com.hr

Latest articles

Categories

Archive

Feeds

Log in



 

Webfaction