Sunday, April 27, 2008

Blogging Code



Blogging Code


I quite often find myself blogging about program source code, that code is typically stored in source files which I then run through a pretty printer (something like source-highlight). Combining everyting together means some copy and pasting - not the most repeatable process and quite often the code and article evolve together - so I end up copying and pasting quite often.


So I came up with mashup. A small ruby program to process html files and handle include directives to do inline include of another file and for this purpose the results of a process


The following source was include with



<x:include value="source-highlight -o STDOUT ~/projects/mashup/mashup"/>

By running:



mashup blogging-code.html > blogging-code-publish.html


#!/usr/bin/ruby

ARGV.each do |arg|
contents = File.new(arg).read()

contents.sub!(/<s:include\s+value="([^"]*)"\s*\/>/) do |match|
replacement = File.new($1).read()
replacement.gsub!(/.*<body>/m, '')
replacement.gsub!(/<\/body>.*/m, '')
replacement
end

contents.sub!(/<x:include\s+value="([^"]*)"\s*\/>/) do |match|
replacement = `#{$1}`
replacement.gsub!(/.*<body>/m, '')
replacement.gsub!(/<\/body>.*/m, '')
replacement
end

puts contents
end



No comments: