Sunday, December 2, 2007

Formatting Ruby and HTML code for blog posting

Posting Ruby and HTML that looks good here on Blogspot can be a pain. But I found a ruby gem called Syntax that will format Ruby code to look right on web pages. It can also do the same thing for HTML/XML. I found out about this at http://blog.wolfman.com/articles/2006/05/26/howto-format-ruby-code-for-blogs. To install it, just type:
gem install syntax

at the command prompt. Then add this to your CSS declaration.

pre {
background-color: #f1f1f3;
color: #112;
padding: 5px;
font-family:"bitstream vera sans mono",monaco,"lucida console","courier new",courier,serif;
font-size: 0.9em;
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; }

For blogspot, just click Template, and Edit HTML.

I wrote up a Ruby script using this gem for Windows that will read Ruby code from the clipboard, format it for HTML, and paste the HTML back to the clipboard. Here is the code:
require 'syntax/convertors/html'
require 'Win32API'
require 'win32/clipboard'
include Win32

in_data = Clipboard.data
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
code_html = convertor.convert(in_data)
Clipboard.set_data(code_html)
And I also wrote one for formatting HTML or XML code. Notice that the only thing different is the for_syntax line.
require 'syntax/convertors/html'
require 'Win32API'
require 'win32/clipboard'
include Win32

in_data = Clipboard.data
convertor = Syntax::Convertors::HTML.for_syntax "xml"
code_html = convertor.convert(in_data)
Clipboard.set_data(code_html)
The only complaint is that sometimes line breaks are displayed on the web page in your code in Internet Explorer when there shouldn't be. The scroll bars show up, but it won't scroll over to the right for more than one word. This is more of an IE specific issue though, it displays fine in Firefox.

9 comments:

bryan said...

thanks, brent! works like a charm :)

xiaobozz said...

Thank you for this good help :-)

Ashish Chatterjee said...

a small bug/update needed...
the code snippet for ruby2html should now be

require 'syntax/convertors/html'
require 'Win32API'
require 'win32/clipboard'
include Win32
...
...
...

as without including that line you get an error like
"..../ruby/lib/ruby/gems/1.8/gems/win32-clipboard-0.4.3/lib/win32/clipboard.rb:25: uninitialized constant Win32::Clipboard::Win32API (NameError)"

thanks to Axel for saving my time :)

Brent said...

Thanks for catching that Ashish! It works fine for me without Win32API, you must have different version of Ruby that me. I'm running 1.8.5. Adding the require Win32API for me works the same, so I've updated the code in the posting to have this require.

AgileTester said...

why not use the XMP tag for XML => HTML?

Stefan said...

Good Job! :)

Dugeen said...

Thanks for the Win32API tip Ashish

Gishu said...

Nice. Thanks for taking the effort to post this.

Dan said...

The error is NOT a Ruby version error, the error message reported by Ashish is clear, it was on line 25 of clipboard.rb, in the package: win32-clipboard-0.4.3 so; The need to "require 'WIN32API'" must have been a typo in win32-clipboard-0.4.3.

Try making the code, more like:

require 'syntax/convertors/html'
require 'win32/clipboard'
include Win32
require 'Win32API' if Clipboard::VERSION == '0.4.3'
(then the rest of your code, in each sample.)

Ideally it would be nice to know when the error was removed; it is NOT in ver 0.5.2, so the conditional version line could also read:

require 'Win32API' if Clipboard::VERSION < '0.5.2'