Module: Haml::MagicTranslations::RGetText::HamlParser::RGetTextEngine

Defined in:
lib/haml/magic_translations/rgettext/haml_parser.rb

Overview

:nodoc:all

Instance Method Summary collapse

Instance Method Details

#add_target(text, lineno = @node.line) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/haml/magic_translations/rgettext/haml_parser.rb', line 49

def add_target(text, lineno = @node.line)
  @targets = {} if @targets.nil?
  unless text.empty?
    @targets[text] = [] unless @targets[text]
    @targets[text].push("#{options[:filename]}:#{lineno}")
  end
end

#compile_doctypeObject



99
100
101
# File 'lib/haml/magic_translations/rgettext/haml_parser.rb', line 99

def compile_doctype
  return super unless self.options[:rgettext]
end

#compile_filterObject



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/haml/magic_translations/rgettext/haml_parser.rb', line 128

def compile_filter
  return super unless self.options[:rgettext]

  case @node.value[:name]
  when 'markdown', 'maruku'
    add_target(@node.value[:text].rstrip.gsub(/\n/, '\n'))
  when 'javascript'
    lineno = 0
    @node.value[:text].split(/\r\n|\r|\n/).each do |line|
      lineno += 1
      line.gsub(/_\('(([^']|\\')+)'\)/) do |m|
        parsed_string = JSON.parse("[\"#{$1}\"]")[0]
        add_target(parsed_string, @node.line + lineno)
      end
    end
  end
end

#compile_plainObject



93
94
95
96
97
# File 'lib/haml/magic_translations/rgettext/haml_parser.rb', line 93

def compile_plain
  return super unless self.options[:rgettext]

  add_target(@node.value[:text])
end

#compile_scriptObject



103
104
105
106
107
# File 'lib/haml/magic_translations/rgettext/haml_parser.rb', line 103

def compile_script
  return super unless self.options[:rgettext]

  yield if block_given?
end

#compile_silent_scriptObject



109
110
111
112
113
# File 'lib/haml/magic_translations/rgettext/haml_parser.rb', line 109

def compile_silent_script
  return super unless self.options[:rgettext]

  yield if block_given?
end

#compile_tagObject



115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/haml/magic_translations/rgettext/haml_parser.rb', line 115

def compile_tag
  return super unless self.options[:rgettext]

  # handle explicit translations in attributes
  @node.value[:attributes_hashes].each do |hash_string|
    hash_string.gsub(/_\('(([^']|\\')+)'\)/) do |m|
      add_target($1)
    end
  end
  # targets have been already handled in parse_tag
  yield if @node.value[:value].nil? && block_given?
end

#parse_tag(line) ⇒ Object

We can’t piggyback ‘compile_tag` because the following gets no distinction at all:

%p= "Hello"

and:

%p #{"Hello"}

So let’s hook in the parser, just for this specific case.



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/haml/magic_translations/rgettext/haml_parser.rb', line 73

def parse_tag(line)
  return super unless self.options[:rgettext]

  tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
    nuke_inner_whitespace, action, value, last_line = super(line)
  if action && (action == '=' || (action == '!' && value[0] == ?=) ||
                                 (action == '&' && value[0] == ?=))
    # Search for explicitely translated strings
    value.gsub(/_\('(([^']|\\')+)'\)/) do |m|
      parsed_string = "#{$1}"
      add_target(parsed_string, last_line)
    end
  else
    interpolated = Haml::MagicTranslations.prepare_i18n_interpolation(value)
    add_target(interpolated[0], last_line)
  end
  [tag_name, attributes, attributes_hashes, object_ref, nuke_outer_whitespace,
      nuke_inner_whitespace, action, value, last_line]
end

#targetsObject



57
58
59
60
61
# File 'lib/haml/magic_translations/rgettext/haml_parser.rb', line 57

def targets
  (@targets || {}).keys.sort.collect do |k|
    [k] + @targets[k]
  end
end