Rails Against The Machine

Just a mind dump. Why are you even reading this?

Wednesday, 2 January 2008

 

Posting nicely formatted code

I neglected this blog partly due to lack of enthusiasm, partly because I've been writing java for the last few months and partly because it was so hard to post code / html samples to blogger. One would have thought that the might of google would have been capable of creating something along the lines of

<code language=ruby>
some ruby code
</code>


Or even providing some help on how to post code, but no it was not to be. ....Here is a rake task to help I adapted it from this article .

First install the syntax plugin: gem install syntax and then create the following file to

lib\tasks\blog.rake
require 'syntax/convertors/html'
require 'win32/clipboard'
include Win32

namespace :format do
desc "This task will format code in the clipboard or file specified by file=\"filename\" as html"
task :ruby4display do
BloggingTools.ruby4display
end

desc "This task will format code in the clipboard or file specified by file=\"filename\" as html"
task :html4display do
BloggingTools.html4display
end
end

#Actual functions are defined in a separate class to ease testing and keep things clean and dry
class BloggingTools

def self.ruby4display
convertor = Syntax::Convertors::HTML.for_syntax "ruby"
code_html = convertor.convert(input)
output code_html
end

def self.html4display
convertor = Syntax::Convertors::HTML.for_syntax "xml"
code_html = convertor.convert(input)
output code_html
end

def self.input
if ENV['file']
puts "#{ENV['file']} will be processed and copied to the clipboard"
in_data = File.read(ENV['file'])
else
puts "no arguments supplied data will be taken from clipboard"
in_data = Clipboard.data
end
end

def self.output code_html
if ENV['file']
fn= "#{File.basename(ENV['file'], File.extname(ENV['file']))}.html"
puts "output to file #{fn}"
in_data = File.open(fn, File::WRONLY|File::TRUNC|File::CREAT)
in_data.puts code_html
else
puts "output to clipboard"
Clipboard.set_data(code_html)
end

end


end



If you run the rake task without arguments it copies from and to the clipboard, if you provide a file name then it takes that file as input and produces a similarly named html file as output.

Why a rake task? Well its easy to run from within netbeans and we can with minimal adaptation use it in the future as part of our "build process" e.g. automatically upload changes to a "change blog" so we can tell at a glance what the other developers have been up to e.g. in pseudocode

find modified files:
update svn
send svn link and code for all files with time and author to our internal blog!

Incidently ENV['key'] is how you access arguments within rake not ARGV[n]

Comments: Post a Comment

Subscribe to Post Comments [Atom]





<< Home

Archives

July 2007   August 2007   September 2007   December 2007   January 2008   February 2008   March 2008   April 2008   June 2008   July 2008   August 2008   October 2008   November 2008   January 2009  

This page is powered by Blogger. Isn't yours?

Subscribe to Comments [Atom]