Class: Haml2Slim::Converter

Inherits:
Object
  • Object
show all
Defined in:
lib/haml2slim/converter.rb

Instance Method Summary collapse

Constructor Details

#initialize(haml) ⇒ Converter

Returns a new instance of Converter.



3
4
5
6
7
8
9
# File 'lib/haml2slim/converter.rb', line 3

def initialize(haml)
  @slim = ""

  haml.each_line do |line|
    @slim << parse_line(line)
  end
end

Instance Method Details

#parse_line(line) ⇒ Object



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
# File 'lib/haml2slim/converter.rb', line 15

def parse_line(line)
  indent = line[/^[ \t]*/]
  line.strip!

  converted = case line[0, 2]
    when '&=' then line.sub(/^&=/, '==')
    when '!=' then line.sub(/^!=/, '=')
    when '-#' then line.sub(/^-#/, '/')
    else
      case line[0]
        when ?%, ?., ?# then parse_tag(line)
        when ?:         then "#{line[1..-1]}:"
        when ?!         then line.sub(/^!!!/, '! doctype')
        when ?-, ?=     then line
        when ?~         then line.sub(/^~/, '=')
        when ?/         then line
        when ?\         then line.sub(/^\\/, '|')
        when nil        then ""
        else "| #{line}"
      end
  end

  if converted.chomp!(' |')
    converted.sub!(/^\| /, '')
    converted << ' \\'
  end

  "#{indent}#{converted}\n"
end

#parse_tag(tag_line) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/haml2slim/converter.rb', line 45

def parse_tag(tag_line)
  tag_line.sub!(/^%/, '')

  if tag_line_contains_attr = tag_line.match(/(.+)\{(.+)\}/)
    tag, attrs = *tag_line_contains_attr[1..2]

    attrs.gsub!(/,?( ?):?"?([^"'{ ]+)"? ?=> ?/, '\1\2=')
    attrs.gsub!(/=([^"']+)(?: |$)/, '=(\1)')

    "#{tag} #{attrs}"
  else
    tag_line
  end
end

#to_sObject



11
12
13
# File 'lib/haml2slim/converter.rb', line 11

def to_s
  @slim
end