Class: HtmlToHaml::Html::CommentConversionUseCase

Inherits:
Object
  • Object
show all
Defined in:
lib/html_to_haml/use_cases/html/comment_conversion_use_case.rb

Constant Summary collapse

HTML_COMMENT_REGEX =
"<!--(.|\n)*?-->"
HTML_USING_HAML_COMMENT_SYNTAX =
"^\s*\/"

Instance Method Summary collapse

Constructor Details

#initialize(html) ⇒ CommentConversionUseCase

Returns a new instance of CommentConversionUseCase.



8
9
10
# File 'lib/html_to_haml/use_cases/html/comment_conversion_use_case.rb', line 8

def initialize(html)
  @html = html
end

Instance Method Details

#convertObject



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/html_to_haml/use_cases/html/comment_conversion_use_case.rb', line 12

def convert
  haml = @html.gsub(/#{HTML_COMMENT_REGEX}|#{HTML_USING_HAML_COMMENT_SYNTAX}/) do |comment|
    case comment
      when /#{HTML_COMMENT_REGEX}/
        "\n/ #{format_html_comment_for_haml(comment: comment)}\n"
      when /#{HTML_USING_HAML_COMMENT_SYNTAX}/
        escape_misleading_forward_slash(comment: comment)
    end
  end
  haml.gsub(/\n\s*\n/, "\n")
end