class I18nChecker::Locale::Collector::Haml

Attributes

file_caches[R]

Public Class Methods

new(file_caches = I18nChecker::Cache::Files.new) click to toggle source
# File lib/i18n_checker/locale/collector/haml.rb, line 16
def initialize(file_caches = I18nChecker::Cache::Files.new)
  @file_caches = file_caches
end

Public Instance Methods

collect(template_file) click to toggle source
# File lib/i18n_checker/locale/collector/haml.rb, line 20
def collect(template_file)
  template = read_template_file(template_file)
  parser = HamlParser::Parser.new(filename: template_file)
  I18nChecker::Locale::Texts.new(collect_locale_texts(parser.call(template)))
end

Private Instance Methods

collect_locale_text(ast) click to toggle source
# File lib/i18n_checker/locale/collector/haml.rb, line 40
def collect_locale_text(ast)
  return locale_text_from_text(ast) if ast.is_a?(HamlParser::Ast::Text)
  return locale_text_from_script(ast) if script_node?(ast)
  return unless ast.respond_to?(:oneline_child)
  if ast.oneline_child.is_a?(HamlParser::Ast::Text)
    locale_text_from_text(ast.oneline_child)
  elsif script_node?(ast.oneline_child)
    locale_text_from_script(ast.oneline_child)
  end
end
collect_locale_texts(ast) click to toggle source
# File lib/i18n_checker/locale/collector/haml.rb, line 32
def collect_locale_texts(ast)
  locale_texts = []
  locale_texts << collect_locale_text(ast)
  return unless ast.respond_to?(:children)
  locale_texts << ast.children.map { |child| collect_locale_texts(child) }
  locale_texts.flatten.compact
end
create_node_result(ast_node, translate_scripts) click to toggle source
# File lib/i18n_checker/locale/collector/haml.rb, line 65
def create_node_result(ast_node, translate_scripts)
  translate_scripts.map do |script_params|
    (text_key, line, column) = script_params

    locale_text = if text_key =~ /^\.(.+)/
                    action_view = action_view_name_of(ast_node.filename.dup)
                    "#{action_view}#{text_key}"
                  else
                    text_key
                  end

    I18nChecker::Locale::Text.new(
      file: ast_node.filename,
      line: line,
      column: column,
      text: locale_text,
    )
  end
end
locale_text_from_script(script_node) click to toggle source
# File lib/i18n_checker/locale/collector/haml.rb, line 58
def locale_text_from_script(script_node)
  return if script_node.script == '""'
  translate_scripts = translate_scripts_from_node(script_node)
  return if translate_scripts.empty?
  create_node_result(script_node, translate_scripts)
end
locale_text_from_text(text_node) click to toggle source
# File lib/i18n_checker/locale/collector/haml.rb, line 51
def locale_text_from_text(text_node)
  return if text_node.text == '""'
  translate_scripts = translate_scripts_from_node(text_node)
  return if translate_scripts.empty?
  create_node_result(text_node, translate_scripts)
end
read_template_file(template_file) click to toggle source
# File lib/i18n_checker/locale/collector/haml.rb, line 28
def read_template_file(template_file)
  file_caches.read(template_file).to_s
end
script_node?(ast_node) click to toggle source
# File lib/i18n_checker/locale/collector/haml.rb, line 114
def script_node?(ast_node)
  ast_node.is_a?(HamlParser::Ast::Script) || ast_node.is_a?(HamlParser::Ast::SilentScript)
end
translate_scripts_from_node(ast_node) click to toggle source
# File lib/i18n_checker/locale/collector/haml.rb, line 85
def translate_scripts_from_node(ast_node)
  results = []
  file_cache = file_caches.read(ast_node.filename)

  node_lines = if ast_node.is_a?(HamlParser::Ast::Text)
                 ast_node.text.split("\n")
               elsif script_node?(ast_node)
                 ast_node.script.split("\n")
  end

  node_lines.each_with_index do |node_line, i|
    offset_at = 0
    translate_scripts = node_line.scan(/t\s?\(?('[^']+'|"[^"]+")\)?/).map(&:first)
    map_results = translate_scripts.map do |script|
      line = ast_node.lineno + i
      text_key = script.gsub!(/'|"/, '')
      column = file_cache[line].start_of(text_key, offset_at)
      offset_at = column + 1
      [
        text_key,
        line,
        column,
      ]
    end
    results.concat(map_results)
  end
  results
end