Found out about this nice gem that does syntax highlighting of Ruby code (and C, Delphi, HML, RHML and Nitro-HTML for now) and outputs it as a stand-alone HTML. Perfect for blogging. I had found some other one some time ago that needed javascript, and it’s not every blogging service that lets you add that kinda stuff to your posts.
There are many configurable options, other types of output and stuff, you can find all that (very well documented) at CodeRay’s website.
So as a try-out, here’s the output of a little script to color ruby code, run on itself:
1 #!/usr/bin/env ruby 2 require 'rubygems' 3 require 'coderay' 4 5 if ARGV.length != 1 6 puts "Wrong number of arguments. Use: color_html <source_file>" 7 exit 8 end 9 10 rb_file = File.expand_path(ARGV[0]) 11 if rb_file.split('.')[-1] != 'rb' 12 puts "Not a .rb file" 13 exit 14 end 15 16 html_file = rb_file.split('.')[0..-2].join('.') << '.html' 17 if File.exists? html_file 18 puts "File #{html_file} exists. nOverwrite it? (y/N)" 19 # need to make STDIN explicit because there's a filename in the 20 # command-line argument (gets defaults to reading from it) 21 if STDIN.gets.strip == 'y' 22 File.delete(html_file) 23 else 24 exit(true) 25 end 26 end 27 28 File.open(html_file,'w') do |f| 29 f << CodeRay.encode( 30 File.read(rb_file), 31 :ruby, 32 :html, 33 :line_numbers => :inline, 34 :hint => :info, 35 :css => :style, 36 :wrap => :div 37 ) 38 end 39 40 puts "Done! Thanks for coming! =)"
June 3, 2007 at 4:26 pm
http://search.cpan.org/~geoffr/Text-VimColor-0.11/lib/Text/VimColor.pm
If you use VIM and like the way it syntax highlight stuff for you, there’s an alterantive. This Perl (!) module uses VIM highlight syntax capabilities to generate the piece of XHTML you want, so every file that VIM knows how to
highlight is supported. It includes a wrapper called “text-vimcolor” that accepts format and other options and call the module.
June 3, 2007 at 8:01 pm
Thanks Anonymous Coward =)
There’s also this other one that I was trying to find that does all sorts of fancy things, even with some mouse-over special effects for the code, highlighting occurences of variables and stuff like that. Couldn’t find it though.
June 16, 2007 at 4:13 pm
Testing coComment. Or not.
June 16, 2007 at 4:22 pm
Does it work? ;-)