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.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/html2slim/converter.rb', line 15

def initialize(file)
  # open.read makes it works for files & IO
  erb = File.exist?(file) ? open(file).read : file

  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*)-?%>/){ %(<ruby code="#{$2.gsub(/"/, '&quot;')}">) }
  # else
  erb.gsub!(/<%-?\s*else\s*-?%>/, %(</ruby><ruby code="else">))
  # elsif
  erb.gsub!(/<%-?\s*(elsif .+?)\s*-?%>/){ %(</ruby><ruby code="#{$1.gsub(/"/, '&quot;')}">) }
  # when
  erb.gsub!(/<%-?\s*(when .+?)\s*-?%>/){ %(</ruby><ruby code="#{$1.gsub(/"/, '&quot;')}">) }
  erb.gsub!(/<%\s*(end|}|end\s+-)\s*%>/, %(</ruby>))
  erb.gsub!(/<%-?(.+?)\s*-?%>/m){ %(<ruby code="#{$1.gsub(/"/, '&quot;')}"></ruby>) }
  @slim ||= Hpricot(erb).to_slim
end