# Tom Moertel # 2006-10-31 # Haskell code filter for Typo # Based on the filter. class Plugins::Textfilters::HaskellController < TextFilterPlugin::MacroPre plugin_display_name "Haskell" plugin_description "Apply syntax highlighting to a block of Haskell code" def self.help_text %{ You can use `` to include syntax-highlighted blocks of Haskell code. Example: let fibs = 0 : 1 : zipWith (+) fibs (tail fibs) in take 10 fibs This filter uses hscolour (http://www.cs.york.ac.uk/fp/darcs/hscolour/) by Malcolm Wallace. (The HsColour tool must be executable by the user running the Typo blog.) Options: * **linenumber**. Turns on line numbering. Use `linenumber="true"` to enable. * **title**. Adds a title block to the top of the code block. * **class**. Adds a new CSS class to the wrapper around the code block. } end def self.macrofilter(controller,content,attrib,params,text="") lang = attrib['lang'] || 'haskell' title = attrib['title'] cssclass = attrib['class'] linenumber = attrib['linenumber'] text = text.to_s.gsub(/\r/,'').gsub(/\A\n/,'').chomp IO.popen("HsColour -css", "r+") do |f| pid = fork { f.write text; f.close; Kernel.exit!(0) } f.close_write text = f.read Process.waitpid pid end text.gsub!(/.*
/m,"
")
    text.gsub!(/<\/pre>.*/m,"\n
") if(linenumber) lines = text.split(/\n/).size linenumbers = (1..lines).to_a.collect{|line| line.to_s}.join("\n") text = "
\n
\n#{linenumbers}\n
\n
#{text}
" end if(title) titlecode="
#{title}
" else titlecode='' end "
#{titlecode}#{text}
" end end