Sunday, December 2, 2007

Formatting Ruby and HTML code for blog posting

I just found a better way to format code for blogs. Read my blog post Better way to format code for blog posts to see how. I'll leave the old way here, in case you want to still use this way.

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 in Windows, format it for HTML, and paste the HTML back to the clipboard. For some later Ruby versions you'll need to run "gem install win32-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)


For other OSes, here is code that will read a file "input.rb" and convert it to HTML in the file output.html:
require 'syntax/convertors/html'

in_data = File.read('input.rb')
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
code_html = convertor.convert(in_data)
File.open('output.html', 'w') {|f| f << code_html }

And here is the code to convert HTML or XML from the file input.html into output.html:
require 'syntax/convertors/html'

in_data = File.read('input.html')
convertor = Syntax::Convertors::HTML.for_syntax "xml"
code_html = convertor.convert(in_data)
File.open('output.html', 'w') {|f| f << 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.

11 comments:

bryan said...

thanks, brent! works like a charm :)

xiaobozz said...

Thank you for this good help :-)

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'

Brent said...

I just updated the post to include code for reading the input from a file and outputting to another file - useful for OSes other than Windows.

Brent said...

I just tried the example Windows code again and noticed that with the latest Ruby 1.8.7 and Ruby 1.9.2 install packages, the Win32 gems are not installed. You'll get an error that it couldn't load win32/clipboard. To fix this run "gem install win32-clipboard".

Brent said...

I found a better way to post code to blogs. See my post Better way to format code for blog posts to see how.