Class: Haml::MagicTranslations::XGetText::HamlParser::HamlEngineCompiler

Inherits:
Compiler
  • Object
show all
Defined in:
lib/haml/magic_translations/xgettext/haml_parser.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.filenameObject

Returns the value of attribute filename.



70
71
72
# File 'lib/haml/magic_translations/xgettext/haml_parser.rb', line 70

def filename
  @filename
end

Class Method Details

.add_target(text, lineno) ⇒ Object



72
73
74
75
76
77
78
# File 'lib/haml/magic_translations/xgettext/haml_parser.rb', line 72

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

.reset_targetsObject



80
81
82
# File 'lib/haml/magic_translations/xgettext/haml_parser.rb', line 80

def reset_targets
  @targets = {}
end

.targetsObject



84
85
86
87
88
# File 'lib/haml/magic_translations/xgettext/haml_parser.rb', line 84

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

Instance Method Details

#compile(node) ⇒ Object



91
92
93
# File 'lib/haml/magic_translations/xgettext/haml_parser.rb', line 91

def compile(node)
  super(node)
end

#compile_doctypeObject



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

def compile_doctype
  # do nothing
end

#compile_filterObject



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/haml/magic_translations/xgettext/haml_parser.rb', line 141

def compile_filter
  case @node.value[:name]
  when 'markdown', 'maruku'
    HamlEngineCompiler.add_target(@node.value[:text].rstrip, @node.line)
  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]
        HamlEngineCompiler.add_target(parsed_string, @node.line + lineno)
      end
    end
  end
end

#compile_plainObject



95
96
97
# File 'lib/haml/magic_translations/xgettext/haml_parser.rb', line 95

def compile_plain
  HamlEngineCompiler.add_target(@node.value[:text], @node.line)
end

#compile_scriptObject



103
104
105
# File 'lib/haml/magic_translations/xgettext/haml_parser.rb', line 103

def compile_script
  yield if block_given?
end

#compile_silent_scriptObject



107
108
109
110
111
112
113
114
# File 'lib/haml/magic_translations/xgettext/haml_parser.rb', line 107

def compile_silent_script
  # Search for explicitely translated strings
  @node.value[:text].gsub(/_\('(([^']|\\')+)'\)/) do |m|
    parsed_string = "#{$1}"
    HamlEngineCompiler.add_target(parsed_string, @node.line)
  end
  yield if block_given?
end

#compile_tagObject



116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/haml/magic_translations/xgettext/haml_parser.rb', line 116

def compile_tag
  if @node.value[:parsed_tag]
    # Search for explicitely translated strings
    @node.value[:value].gsub(/_\('(([^']|\\')+)'\)/) do |m|
      parsed_string = "#{$1}"
      HamlEngineCompiler.add_target(parsed_string, @node.line)
    end
  else
    value = @node.value[:value]
    if value
      # strip quotes if needed
      value = value[1..-2] if @node.value[:parse]
      value, args = Haml::MagicTranslations.prepare_i18n_interpolation(value)
      HamlEngineCompiler.add_target(value, @node.line)
    end
  end
  # handle explicit translations in attributes
  @node.value[:attributes_hashes].each do |hash_string|
    hash_string.gsub(/_\('(([^']|\\')+)'\)/) do |m|
      HamlEngineCompiler.add_target($1, @node.line)
    end
  end
  yield if @node.value[:value].nil? && block_given?
end