Format ruby code for your blog
I was looking for a ruby syntax highlighter for my blog and found syntax and found 3 good usage tips but all tips for a specific platform;
- MacOSX from Andi Schacke
- KDE DCOP from Wolfman Howlings (can't usable KDE 4 using DBus)
- WIN32API from Brent
First install syntax and add CSS style to your site as told all three blogs beginning and forget the other parts of them.
#!/usr/bin/env ruby
require 'rubygems'
require 'syntax/convertors/html'
if ARGV.size == 0 then
require 'Qt4'
app = Qt::Application.new []
clipboard = Qt::Application.clipboard
@code = clipboard.text
elsif ARGV.size == 1
@code = File.read(ARGV[0])
else
puts "without argument converts clipboard content\nUsage:\n"
puts "cbtohtml.rb [filename.rb]\ncbtohtml.rb [filename.rb] > output.html\n"
exit
end
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
puts convertor.convert(@code)
Copy code block and save as "cbtohtml.rb" and give permission to run on unix type oses "chmod +x cbtohtml.rb". Without parameter using Qt4 to get clipboard text and it's the most crossplatformed style :) . With parameter using standart stdout that you can pipe to a file. Loading Qt4 when needed can be good for performance as can see below when converting files. Choosing CLI with parameter for really big files maybe a smart thing.
"cbtohtml.rb" copied clipboard:
[slmn@azrael cbtohtml]$ time ./cbtohtml.rb &>/dev/null
real 0m0.726s
user 0m0.313s
sys 0m0.043s
from file:
[slmn@azrael cbtohtml]$ time ./cbtohtml.rb cbtohtml.rb &>/dev/null
real 0m0.189s
user 0m0.133s
sys 0m0.023s
Now some KDE 4 plasma magic; Unlock Widgets > Add Widgets ... then add "Paste" plasmoid to you panel or desktop. enter Paste Settings look the shot below and give the path like in your computer.
You only need copy the ruby code want to convert then click "cbtohtml" from Paste plasmoid. Long live KDE :)
