class RailsRouteChecker::Parsers::HamlParser::Document

Attributes

source[R]
source_lines[R]
tree[R]

Public Class Methods

new(source) click to toggle source
# File lib/rails-route-checker/parsers/haml_parser/document.rb, line 9
def initialize(source)
  @source = source.force_encoding(Encoding::UTF_8)
  @source_lines = @source.split(/\r\n|\r|\n/)

  version = Gem::Version.new(Haml::VERSION).approximate_recommendation
  options = Haml::Options.new
  original_tree = case version
                  when '~> 4.0', '~> 4.1' then Haml::Parser.new(@source, options).parse
                  when '~> 5.0', '~> 5.1', '~> 5.2' then Haml::Parser.new(options).call(@source)
                  else raise "Cannot handle Haml version: #{version}"
                  end

  @tree = process_tree(original_tree)
end

Private Instance Methods

convert_tree(haml_node, parent = nil) click to toggle source
# File lib/rails-route-checker/parsers/haml_parser/document.rb, line 32
def convert_tree(haml_node, parent = nil)
  node_class_name = "#{haml_node.type.to_s.split(/[-_ ]/).collect(&:capitalize).join}Node"
  node_class_name = 'Node' unless RailsRouteChecker::Parsers::HamlParser::Tree.const_defined?(node_class_name)

  new_node = RailsRouteChecker::Parsers::HamlParser::Tree.const_get(node_class_name).new(self, haml_node)
  new_node.parent = parent

  new_node.children = haml_node.children.map do |child|
    convert_tree(child, new_node)
  end

  new_node
end
process_tree(original_tree) click to toggle source
# File lib/rails-route-checker/parsers/haml_parser/document.rb, line 26
def process_tree(original_tree)
  original_tree.children.pop if Gem::Requirement.new('~> 4.0.0').satisfied_by?(Gem.loaded_specs['haml'].version)

  convert_tree(original_tree)
end