Sha256: 0f420272e2d5630641ded7acf66117d405ed5c44738d624e87d67281758953e1

Contents?: true

Size: 883 Bytes

Versions: 2

Compression:

Stored size: 883 Bytes

Contents

require_relative '../../html_to_haml'

module HtmlToHaml::Html
  class CommentConversionUseCase
    HTML_COMMENT_REGEX = "<!--(.|\n)*?-->"
    HTML_USING_HAML_COMMENT_SYNTAX = "^\s*\/"

    def initialize(html)
      @html = html
    end

    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

    private

    def format_html_comment_for_haml(comment:)
      comment.gsub(/\n\s*/, "\n/ ")[4..-4].strip
    end

    def escape_misleading_forward_slash(comment:)
      comment.gsub(/^(\s*)\//, '\1\/')
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
html-to-haml-0.0.6 lib/html_to_haml/use_cases/html/comment_conversion_use_case.rb
html-to-haml-0.0.5 lib/html_to_haml/use_cases/html/comment_conversion_use_case.rb