class HtmlToHaml::Erb::IndentationConversionUseCase

Constants

SNARKY_COMMENT_FOR_HAVING_NESTED_CASE_STATEMENTS

Public Instance Methods

convert_indentation(erb:) click to toggle source
# File lib/html_to_haml/use_cases/erb/indentation_conversion_use_case.rb, line 19
def convert_indentation(erb:)
  indentation_converter = new_indentation_converter
  erb.split("\n").map do |erb_line|
    indentation = indentation_for_line_or_error(erb: erb_line, indentation_level: indentation_converter.indentation_level)
    adjust_indentation_level(erb: erb_line, indentation_converter: indentation_converter)
    construct_haml_line(erb: erb_line, indentation: indentation, indentation_converter: indentation_converter)
  end.join
end

Private Instance Methods

adjust_indentation_level(erb:, indentation_converter:) click to toggle source
# File lib/html_to_haml/use_cases/erb/indentation_conversion_use_case.rb, line 34
def adjust_indentation_level(erb:, indentation_converter:)
  if begin_case_statement?(erb: erb)
    indentation_converter.begin_case_statement
  elsif begin_indented_control_flow?(erb: erb)
    indentation_converter.add_indentation
  elsif end_of_block?(erb: erb)
    indentation_converter.end_block
  end
end
adjusted_indentation_level_for_line(erb:, indentation_level:) click to toggle source
# File lib/html_to_haml/use_cases/erb/indentation_conversion_use_case.rb, line 53
def adjusted_indentation_level_for_line(erb:, indentation_level:)
  if continue_indented_control_flow?(erb: erb)
    indentation_level - HtmlToHaml::INDENTATION_AMOUNT
  else
    indentation_level
  end
end
begin_nested_case_statement?(erb:, indentation_converter:) click to toggle source
# File lib/html_to_haml/use_cases/erb/indentation_conversion_use_case.rb, line 69
def begin_nested_case_statement?(erb:, indentation_converter:)
  begin_case_statement?(erb: erb) && indentation_converter.nested_case_statement?
end
construct_haml_line(erb:, indentation:, indentation_converter:) click to toggle source
# File lib/html_to_haml/use_cases/erb/indentation_conversion_use_case.rb, line 61
def construct_haml_line(erb:, indentation:, indentation_converter:)
  indentation_adjusted_haml_line = "#{indentation}"
  if begin_nested_case_statement?(erb: erb, indentation_converter: indentation_converter)
    indentation_adjusted_haml_line << nested_case_statement_commentary(indentation: indentation)
  end
  indentation_adjusted_haml_line << "#{erb}\n" unless end_of_block?(erb: erb)
end
control_flow_matcher() click to toggle source
# File lib/html_to_haml/use_cases/erb/indentation_conversion_use_case.rb, line 81
def control_flow_matcher
  ControlFlowMatcher.instance
end
indentation_for_line_or_error(erb:, indentation_level:) click to toggle source
# File lib/html_to_haml/use_cases/erb/indentation_conversion_use_case.rb, line 44
def indentation_for_line_or_error(erb:, indentation_level:)
  adjusted_indentation_level = adjusted_indentation_level_for_line(erb: erb, indentation_level: indentation_level)
  if adjusted_indentation_level < 0
    raise ParseError, 'The erb is malformed. Please make sure you have the correct number of "end" statements'
  else
    " " * adjusted_indentation_level
  end
end
nested_case_statement_commentary(indentation:) click to toggle source
# File lib/html_to_haml/use_cases/erb/indentation_conversion_use_case.rb, line 73
def nested_case_statement_commentary(indentation:)
  SNARKY_COMMENT_FOR_HAVING_NESTED_CASE_STATEMENTS.gsub("\n", "\n#{indentation}")
end
new_indentation_converter() click to toggle source
# File lib/html_to_haml/use_cases/erb/indentation_conversion_use_case.rb, line 77
def new_indentation_converter
  IndentationTracker.new(indentation_level: 0, indentation_amount: HtmlToHaml::INDENTATION_AMOUNT)
end