Sunday, January 6, 2008

Ruby code formatting in Blogger on Mac

How do you format ruby code for blogger?

Update


I am now formatting using syntaxhighlighter



I followed this nice posting and tweaked it for the mac. Looks like Wolfman is running KDE.

My codetohtml.rb now looks like this:

require 'rio'
require 'rubygems'
require 'syntax/convertors/html'

if ARGV.size > 0
code = File.read(ARGV[0])
else
code = `pbpaste`
end

convertor = Syntax::Convertors::HTML.for_syntax "ruby"
@code_html = convertor.convert( code )

puts @code_html

if ARGV.size > 0
fn = "#{File.basename(ARGV[0], File.extname(ARGV[0]))}.html"
rio(fn) << @code_html
else
#put the results back on the clipboard, NB this may fail if there are shell specific characters
system("echo \"#{@code_html}\" | pbcopy ")
end


Then you post the CSS in the html under 'Layout' -> 'Edit Html'

pre {
background-color: #f1f1f3;
color: #112;
padding: 10px;
font-size: 1.1em;
overflow: auto;
margin: 4px 0px;
width: 95%;
}

/* Syntax highlighting */
pre .normal {}
pre .comment { color: #005; font-style: italic; }
pre .keyword { color: #A00; font-weight: bold; }
pre .method { color: #077; }
pre .class { color: #074; }
pre .module { color: #050; }
pre .punct { color: #447; font-weight: bold; }
pre .symbol { color: #099; }
pre .string { color: #944; background: #FFE; }
pre .char { color: #F07; }
pre .ident { color: #004; }
pre .constant { color: #07F; }
pre .regex { color: #B66; background: #FEF; }
pre .number { color: #F99; }
pre .attribute { color: #5bb; }
pre .global { color: #7FB; }
pre .expr { color: #227; }
pre .escape { color: #277; }

3 comments:

rhacket said...

Thanks! Just what I was looking for.

Anonymous said...

www.fromjavatoruby.com is very informative. The article is very professionally written. I enjoy reading www.fromjavatoruby.com every day.
bad credit cash loans
payday loans online

Anonymous said...

die ausgezeichnete und termingemäße Mitteilung. cialis kaufen viagra generika [url=http//t7-isis.org]levitra ohne rezept[/url]