Class: HTML2Slim::ERBConverter

Inherits:
Converter show all
Defined in:
lib/html2slim/converter.rb

Instance Method Summary collapse

Methods inherited from Converter

#to_s

Constructor Details

#initialize(file) ⇒ ERBConverter

Returns a new instance of ERBConverter.



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/html2slim/converter.rb', line 23

def initialize(file)
  erb = file.read

  erb.gsub!(/<%(.+?)\s*\{\s*(\|.+?\|)?\s*%>/) { %(<%#{$1} do #{$2}%>) }
  # case, if, for, unless, until, while, and blocks...
  erb.gsub!(/<%(-\s+)?((\s*(case|if|for|unless|until|while) .+?)|.+?do\s*(\|.+?\|)?\s*)-?%>/) do
    %(<ruby code="#{escape($2)}">)
  end
  # else
  erb.gsub!(/<%-?\s*else\s*-?%>/, %(</ruby><ruby code="else">))
  # elsif
  erb.gsub!(/<%-?\s*(elsif .+?)\s*-?%>/) { %(</ruby><ruby code="#{escape($1)}">) }
  # when
  erb.gsub!(/<%-?\s*(when .+?)\s*-?%>/) { %(</ruby><ruby code="#{escape($1)}">) }
  erb.gsub!(/<%\s*(end|}|end\s+-)\s*%>/, %(</ruby>))
  erb.gsub!(/<%-?\n?(.+?)\s*-?%>/m) { %(<ruby code="#{escape($1)}"></ruby>) }

  super(erb)
end