Syntax highlight code

Syntax highlight code

I just had a go at bodging in a syntax highlighter for ruby code. Patch below against the WikiRedcloth class in the soks-view.rb file. You need to have Syntax installed, preferably as a gem. You also need to add some appropriate definitions to your css in the attachment directory of your wiki. Copying and pasting those from here would do.

Unfortunately it doesn’t work quite correctly because of the escaping that Redcloth does1. It is also very slow. I therefore probably won’t put it in the next release, but it is here in case anyone wants to adapt and improve.

Index: soks-view.rb
===================================================================
RCS file: /var/cvs/soks/soks/lib/soks-view.rb,v
retrieving revision 1.43
diff -r1.43 soks-view.rb
139c139
<     RULES = [:refs_soks_bracketed_link, :refs_textile, :block_textile_table, :block_textile_lists,:block_textile_prefix, :inline_textile_image, :inline_textile_link, :inline_textile_code, :inline_soks, :inline_textile_glyphs, :inline_textile_span, :refs_markdown, :block_markdown_setext, :block_markdown_atx, :block_markdown_rule, :block_markdown_bq, :block_markdown_lists, :inline_markdown_reflink, :inline_markdown_link ]
---
>     RULES = [:refs_soks_bracketed_link, :refs_textile, :block_textile_table, :block_textile_lists,:block_textile_prefix, :inline_textile_image, :inline_textile_link, :inline_textile_code, :inline_soks, :inline_textile_glyphs, :inline_textile_span, :refs_markdown, :block_markdown_setext, :block_markdown_atx, :block_markdown_rule, :block_markdown_bq, :block_markdown_lists, :inline_markdown_reflink, :inline_markdown_link, :block_code_syntax ]
253a254,265
>     require 'rubygems'
>     require_gem 'syntax'
>     require 'syntax/convertors/html'
>     
>     def block_code_syntax( text )
>         @pre_list.each_with_index do |preserved_code,index|
>             next unless preserved_code =~ /^(.*)/m
>             convertor = Syntax::Convertors::HTML.for_syntax $1
>             @pre_list[index] = "<div class='ruby'>#{convertor.convert( $2 )}</div>" 
>         end
>         text
>     end

1 Because I’m not very bright, I find it very hard to figure out the millions of Regexps in _why’s Redcloth code so I haven’t yet worked out where it does this and how to stop it…

Edit this page or watch for changes using RSS.