Sha256: bd674c71054b98936f9c0794b93cb6abe9f3c2843797a32cb68ed6e068ca1126

Contents?: true

Size: 1.06 KB

Versions: 2

Compression:

Stored size: 1.06 KB

Contents

require_relative '../../html_to_haml'
require_relative '../../helpers/haml_whitespace_cleaner'
require_relative 'indentation_conversion_use_case'

module HtmlToHaml::Erb
  class BasicConversionUseCase
    include HtmlToHaml::HamlWhitespaceCleaner

    def initialize(erb)
      @erb = erb
    end

    def convert
      sanitized_erb = remove_newlines_within_erb_statements(erb: @erb)
      erb = convert_syntax(erb: sanitized_erb)
      haml = convert_indentation(erb: erb)
      remove_haml_whitespace(haml: haml)
    end

    private

    def remove_newlines_within_erb_statements(erb:)
      erb.gsub(/<%(.*?)\n(.*?)%>/) do |erb_statement|
        erb_statement.gsub("\n", " ")
      end
    end

    def convert_syntax(erb:)
      erb.gsub(/\s*?\n?(<%=|<%-|<%)\s?/) do |erb_selector|
        erb_selector_index = erb_selector =~ /-|=/
        erb_selector_index ? "\n#{erb_selector[erb_selector_index]} " : "\n- "
      end.gsub(/\s?(-%>|%>)/, "\n")
    end

    def convert_indentation(erb:)
      IndentationConversionUseCase.instance.convert_indentation(erb: erb)
    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/erb/basic_conversion_use_case.rb
html-to-haml-0.0.5 lib/html_to_haml/use_cases/erb/basic_conversion_use_case.rb