class HtmlToHaml::Converter

Public Class Methods

new(html) click to toggle source
# File lib/html_to_haml/converter.rb, line 8
def initialize(html)
  @html = html
end

Public Instance Methods

convert() click to toggle source
# File lib/html_to_haml/converter.rb, line 12
def convert
  whitespace_free_html = remove_html_whitespace(html: @html)
  erb_converted_haml = Erb::BasicConversionUseCase.new(whitespace_free_html).convert
  haml = NonHtmlSelectorBlocks::StyleConversionUseCase.new(erb_converted_haml).convert
  haml = NonHtmlSelectorBlocks::ScriptConversionUseCase.new(haml).convert
  Html::ConversionUseCase.new(haml, remove_whitespace: false).convert
end

Private Instance Methods

html_with_important_whitespace() click to toggle source
# File lib/html_to_haml/converter.rb, line 34
def html_with_important_whitespace
  important_whitespace_classes.map do |klass|
    "^\\s*<#{klass::HTML_TAG_NAME}.*?>(.|\n)*?<\/#{klass::HTML_TAG_NAME}>"
  end.join("|")
end
important_whitespace_classes() click to toggle source
# File lib/html_to_haml/converter.rb, line 40
def important_whitespace_classes
  [NonHtmlSelectorBlocks::ScriptConversionUseCase,
  NonHtmlSelectorBlocks::StyleConversionUseCase]
end
remove_html_whitespace(html:) click to toggle source
# File lib/html_to_haml/converter.rb, line 22
def remove_html_whitespace(html:)
  html.gsub(/#{html_with_important_whitespace}|^\s*|\n/) do |matching_html|
    case matching_html
      when /#{html_with_important_whitespace}/
        initial_indentation = matching_html.gsub("\n", '').match(/^\s*/).to_s
        matching_html.gsub(/^#{initial_indentation}/, "\n")
      else
        ""
    end
  end
end