Notional Slurry Logo

Testing TextMate-generated code and styles

Like all the best geek tools, TextMate will gladly do anything you want it to do, and a lot more. One interesting prospect for the programmy blogger is the ability (in the TextMate bundle) to generate HTML from code, and also generate a stylesheet from the theme you’re using. Let’s see how that works:

#!/usr/bin/env ruby

myPoem = ENV['TM_SELECTED_TEXT']

newPoem = "<div class=\\"poem\\">\\n<div class=\\"stanza\\">\\n"
betweenStanzas = "</div>\\n\\n<div class=\\"stanza\\">\\n"
myPoem.each do |line|
  if line =~ /(^ +)\\S/
    indent = $1.length
    if [2,4,6,8,10,12,14].include?(indent)
      classing = "<p class=\"i#{indent.to_s}\\">"
    else
      classing = "<p class=\\"FIXME\\">"
    end
    newPoem += line.chomp.sub(/^ +/,classing) + "</p>\\n"
  elsif line =~ /^( *)$/
    newPoem += betweenStanzas
  else
    newPoem += "<p>" + line.chomp + "</p>n"
  end
end
newPoem += "</div>\\n</div>"

print newPoem

It does go, but some observations: The code sucks (okay, it was just the first Ruby I had on hand). The backslashes need to be escaped for some reason: either MarsEdit or WordPress? There’s still that annoying tendency all browsers have, to let <pre> markup get too big for its bounding box.

Leave a Comment