1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#!/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