class ExtractI18n::Slimkeyfy::SlimTransformer

Constants

AFTER
BEFORE
EQUALS
HTML_ARGUMENTS
HTML_TAGS
STRING
STRING_WITHOUT_QUOTES
TRANSLATED
TRANSLATION

Public Class Methods

new(word, file_key) click to toggle source
# File lib/extract_i18n/slimkeyfy/slim_transformer.rb, line 47
def initialize(word, file_key)
  @word = word
  @file_key = file_key
end

Public Instance Methods

transform() { |nil| ... } click to toggle source
# File lib/extract_i18n/slimkeyfy/slim_transformer.rb, line 34
def transform(&block)
  return yield(nil) if should_not_be_processed?(@word.as_list)
  unindented_line = @word.unindented_line

  if unindented_line.match(EQUALS)
    parse_html_arguments(unindented_line, &block)
  elsif @word.head.match(HTML_TAGS)
    parse_html(&block)
  else
    yield(nil)
  end
end

Private Instance Methods

extract_arguments(translation) click to toggle source
# File lib/extract_i18n/slimkeyfy/slim_transformer.rb, line 143
def extract_arguments(translation)
  args = {}
  translation.scan(/\#{[^}]*}/).each_with_index do |arg, index|
    stripped_arg = arg[2..-2]
    key = ExtractI18n.key(arg)
    key += index.to_s if index > 0
    translation = translation.gsub(arg, "%{#{key}}")
    args[key] = stripped_arg
  end
  [args, translation]
end
match_string(translation) click to toggle source
# File lib/extract_i18n/slimkeyfy/slim_transformer.rb, line 129
def match_string(translation)
  m = translation.match(STRING_WITHOUT_QUOTES)
  return translation if m.nil?
  if m[:double_quot] != nil
    m[:double_quot]
  else
    (m[:single_quot] != nil ? m[:single_quot] : translation)
  end
end
matches_string?(translation) click to toggle source
# File lib/extract_i18n/slimkeyfy/slim_transformer.rb, line 123
def matches_string?(translation)
  m = translation.match(STRING_WITHOUT_QUOTES)
  return false if m.nil?
  (m[:double_quot] != nil or m[:single_quot] != nil)
end
parse_html() { |change| ... } click to toggle source
# File lib/extract_i18n/slimkeyfy/slim_transformer.rb, line 54
def parse_html(&_block)
  return @word.line if @word.line.match(TRANSLATED)

  tagged_with_equals = Slimkeyfy::Whitespacer.convert_slim(@word.head)
  body = @word.tail.join(" ")
  body, tagged_with_equals = Slimkeyfy::Whitespacer.convert_nbsp(body, tagged_with_equals)

  if body.match(LINK_TO) != nil
    body = link_tos(body)
  end

  interpolate_arguments, body = extract_arguments(body)
  change = ExtractI18n::SourceChange.new(
    source_line: @word.line,
    remove: body,
    interpolate_arguments: interpolate_arguments,
    i18n_key: "#{@file_key}.#{ExtractI18n.key(body)}",
    i18n_string: body,
    t_template: "#{@word.indentation}#{tagged_with_equals} t('%s'%s)"
  )
  yield(change)
end
parse_html_arguments(line, token_skipped_before = nil) { |change| ... } click to toggle source
# File lib/extract_i18n/slimkeyfy/slim_transformer.rb, line 77
def parse_html_arguments(line, token_skipped_before = nil, &block)
  final_line = line
  regex_list.each do |regex|
    line.scan(regex) do |m_data|
      next if m_data == token_skipped_before
      before = m_data[0]
      html_tag = m_data[1]
      translation = match_string(m_data[2])
      after = m_data[3]
      interpolate_arguments, translation = extract_arguments(translation)
      change = ExtractI18n::SourceChange.new(
        source_line: @word.indentation + final_line,
        i18n_string: translation,
        i18n_key: "#{@file_key}.#{ExtractI18n.key(translation)}",
        remove: m_data[2],
        interpolate_arguments: interpolate_arguments,
        t_template: "#{before}#{html_tag}t('%s'%s)#{after}"
      )
      final_line = yield(change)
      return parse_html_arguments(final_line, &block)
    end
  end
  if final_line == line
    @word.indentation + final_line
  else
    final_line
  end
end
regex_list() click to toggle source
# File lib/extract_i18n/slimkeyfy/slim_transformer.rb, line 139
def regex_list
  HTML_ARGUMENTS.map { |_, regex| /#{BEFORE}#{regex}#{TRANSLATION}#{AFTER}/ }
end
should_not_be_processed?(tokens) click to toggle source
# File lib/extract_i18n/slimkeyfy/slim_transformer.rb, line 119
def should_not_be_processed?(tokens)
  (tokens.nil? or tokens.size < 2)
end