#!/usr/local/bin/ruby

##
# USE AT YOUR OWN RISK
#
# Orginially hacked together by Geoffrey Grosenbach
# http://topfunky.com
#
# A prototype for a Markaby to Haml converter.
# Does not produce production-ready Haml, but it does
# do most of the work to get it there.
#
# mab2haml.rm input.mab
# => Prints approximation of file in HAML
#
# If you use TextMate, create a command like this:
#
# #!/bin/sh
# /somewhere/bin/mab2haml.rb $TM_FILEPATH
#

require 'rubygems'
require 'markaby'

xhtml_tags = Markaby::XHTMLStrict.tags.map {|t| t.to_s}.join('|')

File.open(ARGV.first).readlines.each do |line|
case line
when /^(\s+)(#|if|else)(.*)/
# Conditional
puts [$1, '- ', $2, $3].join
when /^(\s*)(#{xhtml_tags})([. ])(.*)/
# XHTML Tag
(space, tag, punc, remainder) = $1, $2, $3, $4
if remainder =~ /^(\s*)(\{)(.*)(\})\s*$/
puts [space, '%', tag, punc].join
puts [space, ' = ', $3.strip].join
else
remainder.gsub!(/\s(do|\{)\s*$/, '')
puts [space, '%', tag, punc, remainder].join
end
when /^\s*(end|\})\s*/
# Closing tag...do nothing
else
puts line
end
end