This is the approach I settled on for publishing code snippets in Blogger. If there is a better way, I'd love to hear about it.
First, about CodeRay (
http://coderay.rubychan.de/) -- CodeRay is a tool that takes snippets of code, and formats the result as HTML, using CSS styles to create an attractive result. Here's an example.
1 2 3 4 5 6 7 8 9 10
| # # Handle login requests # def login # Attempt to do the login self.current_user = User.authenticate(params[:login], params[:password]) if logged_in? if params[:remember_me] == "1" self.current_user.remember_me |
Some blogging engines have plugins that make it easy to generate results like this, but I haven't found one for Blogger. The approach I chose has two elements:
1) The CSS styles.
There are a lot of CodeRay stylesheets out there. I suggest you find one that you like and start from there. You need to paste the CSS into your template, and from there you can see how it looks on your blog, and then tweak it until you get the result you like.
You will want to save the CSS somewhere (outside of Blogger). I've noticed that when you change templates, you lose the custom CSS that you stuck in the template, so any carefully tweaked values will be lost.
2) Actually generating the formatted code.
This second part turns out to not be terribly difficult, but I wanted a simple tool that would allow me to copy code from my editor, and then produce a formatted result that I can paste into the "Edit Html" tab in Blogger. Here is the script:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | #!c:/ruby/bin/ruby.exe -rubygems require 'coderay' require 'win32/clipboard' include Win32 # Fix the formatted code to work nicely in blogger. def format_for_blogger(str) # Blogger has this quirk where it transforms newlines to <br /> # tags. This is great, except when you don't want it, for instance # the HTML <tr><br /><td> is illegal. This code finds all of the # places where things like <tr> exist with a newline after them, # and removes the newline blogger_result = str.gsub(/(\s*\n\s*)(<|<\/)(table|tr|td)/i) { |x| x.gsub("\n"," ") } # CodeRay produces <tt> elements that have nothing but a newline in them. # Replacing these with simple \n (which blogger transforms to <br />) # also helps in making things line up nicely. Note the space after the # newline. That turns out to be important to get nice results in IE. blogger_result.gsub(/<tt>\s*\n\s*<\/tt>/i, "\n ") end # Quit if there's no clipboard print "The clipboard was empty" and exit unless Clipboard.data # Format it via CodeRay, and process the result specially for blogger formatted_code = CodeRay.scan(Clipboard.data, :ruby).div({ :line_numbers => :table, :bold_every => 5, :line_number_start => 1 } ) blogger_code = format_for_blogger(formatted_code) # Set the clipboard contents and show the user the result Clipboard.set_data blogger_code print blogger_code |
This relies on two gems, which you can install with:
gem install coderay
gem install win32-clipboard
The script takes the contents of the clipboard, runs it through the CodeRay formatter (passing options to get a table with line numbers) and then puts the result back onto the clipboard. This makes it possible to copy code, run the script and then paste into the "Edit Html" tab of the editor in Blogger.
0 comments:
Post a Comment