Class: HtmlToHaml::Html::DefaultConversionUseCase
- Inherits:
-
Object
- Object
- HtmlToHaml::Html::DefaultConversionUseCase
- Includes:
- HtmlToHaml::HamlWhitespaceCleaner
- Defined in:
- lib/html_to_haml/use_cases/html/default_conversion_use_case.rb
Constant Summary collapse
- HAML_SYMBOL_REGEX =
">\s*(\/|-|=)"- ERB_LINE_REGEX =
"\n\s*(-|=).*$"- CLOSING_HTML_REGEX =
"<\/.*?>"- DOCTYPE_REGEX =
"<!DOCTYPE.*?>"- SELF_CLOSING_HTML_REGEX =
For self-closing html tags that aren’t self-closing by default
"\/>"- SELF_CLOSING_TAGS =
%w(area base br col command embed hr img input keygen link meta param source track wbr)
Instance Method Summary collapse
- #convert ⇒ Object
-
#initialize(html, remove_whitespace: true) ⇒ DefaultConversionUseCase
constructor
A new instance of DefaultConversionUseCase.
- #self_closing_tag_regex ⇒ Object
Constructor Details
#initialize(html, remove_whitespace: true) ⇒ DefaultConversionUseCase
17 18 19 20 21 22 23 24 25 26 |
# File 'lib/html_to_haml/use_cases/html/default_conversion_use_case.rb', line 17 def initialize(html, remove_whitespace: true) # Since Haml uses whitespace in a way html doesn't, this starts by stripping # whitespace to start the next gsub with a clean slate. Unless the caller opts # out. @html = if remove_whitespace remove_html_whitespace(html: html) else html end end |
Instance Method Details
#convert ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/html_to_haml/use_cases/html/default_conversion_use_case.rb', line 28 def convert indentation_tracker = IndentationTracker.new(indentation_amount: HtmlToHaml::INDENTATION_AMOUNT) haml = @html.gsub(/#{DOCTYPE_REGEX}|#{ERB_LINE_REGEX}|#{CLOSING_HTML_REGEX}|#{SELF_CLOSING_HTML_REGEX}|#{self_closing_tag_regex}|#{HAML_SYMBOL_REGEX}|<|>|\n/) do |matched_elem| adjust_indentation_level(html: matched_elem, indentation_tracker: indentation_tracker) start_of_line = "\n#{indentation_tracker.indentation}" case matched_elem when /#{DOCTYPE_REGEX}/ "#{matched_elem}\n" when /#{ERB_LINE_REGEX}/ "#{start_of_line}#{matched_elem[1..-1]}" when /#{self_closing_tag_regex}/ "#{start_of_line}%#{matched_elem[1..-1]}" when /#{HAML_SYMBOL_REGEX}/ "#{start_of_line}#{matched_elem[1..-1].insert(-2, "\\")}" when "<" "#{start_of_line}%" else start_of_line end end remove_haml_whitespace(haml: haml) end |
#self_closing_tag_regex ⇒ Object
51 52 53 |
# File 'lib/html_to_haml/use_cases/html/default_conversion_use_case.rb', line 51 def self_closing_tag_regex "<#{SELF_CLOSING_TAGS.join('|<')}\\s" end |