Class: I18nliner::PreProcessors::ErbPreProcessor::TBlock
- Inherits:
-
Context
- Object
- Context
- I18nliner::PreProcessors::ErbPreProcessor::TBlock
show all
- Includes:
- CallHelpers
- Defined in:
- lib/i18nliner/pre_processors/erb_pre_processor.rb
Constant Summary
collapse
- TEMP_PLACEHOLDER =
/(?<content>__I18NLINER_\d+__)/
Constants included
from CallHelpers
CallHelpers::ALLOWED_PLURALIZATION_KEYS, CallHelpers::REQUIRED_PLURALIZATION_KEYS
Instance Attribute Summary
Attributes inherited from Context
#buffer, #parent
Instance Method Summary
collapse
-
#<<(string) ⇒ Object
-
#extract_helper_wrappers!(source, wrappers, placeholder_map) ⇒ Object
-
#extract_html_wrappers!(source, wrappers, placeholder_map) ⇒ Object
incidentally this converts entities to their corresponding values.
-
#extract_placeholders!(buffer = @buffer, pattern = ERB_EXPRESSION, wrap_placeholder = true) ⇒ Object
-
#extract_temp_placeholders! ⇒ Object
-
#extract_text(root_node) ⇒ Object
-
#extract_wrappers!(source, wrappers, placeholder_map) ⇒ Object
-
#find_or_add_wrapper(wrapper, wrappers) ⇒ Object
-
#infer_interpolation_key(string, others) ⇒ Object
get a unique and reasonable looking key for a given erb expression.
-
#infer_wrappers(source) ⇒ Object
-
#initialize(parent, content) ⇒ TBlock
constructor
A new instance of TBlock.
-
#normalize_call ⇒ Object
-
#options_to_ruby(options) ⇒ Object
-
#prepare_wrapper(content, placeholder_map) ⇒ Object
-
#result ⇒ Object
-
#wrap(text, index) ⇒ Object
-
#wrappers_to_ruby(wrappers) ⇒ Object
#infer_arguments, #infer_key, #infer_pluralization_hash, #key_provided?, #keyify, #keyify_underscored, #keyify_underscored_crc32, #normalize_default, #normalize_key, #normalize_whitespace, #pluralization_hash?
Constructor Details
#initialize(parent, content) ⇒ TBlock
Returns a new instance of TBlock.
88
89
90
91
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 88
def initialize(parent, content)
super(parent)
@lines = content.count("\n")
end
|
Instance Method Details
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 179
def (source, wrappers, placeholder_map)
source.gsub(TEMP_PLACEHOLDER) do |string|
if (helper = Helper.match_for(placeholder_map[string])) && helper.wrappable?
placeholder_map.delete(string)
if helper.placeholder helper.content = "__I18NLINER_#{placeholder_map.size}__"
placeholder_map[helper.content] = helper.placeholder
end
pos = find_or_add_wrapper(helper.wrapper, wrappers)
wrap(helper.content, pos + 1)
else
string
end
end
end
|
incidentally this converts entities to their corresponding values
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 159
def (source, wrappers, placeholder_map)
default = ''
nodes = Nokogiri::HTML.fragment(source).children
nodes.each do |node|
if node.is_a?(Nokogiri::XML::Text)
default << node.content
elsif text = (node)
wrapper = node.to_s.sub(text, "\\\\1")
wrapper = prepare_wrapper(wrapper, placeholder_map)
pos = find_or_add_wrapper(wrapper, wrappers)
default << wrap(text, pos + 1)
else key = "__I18NLINER_#{placeholder_map.size}__"
placeholder_map[key] = node.to_s.inspect << ".html_safe"
default << key
end
end
default
end
|
209
210
211
212
213
214
215
216
217
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 209
def (buffer = @buffer, pattern = ERB_EXPRESSION, wrap_placeholder = true)
map = {}
buffer.gsub!(pattern) do |str|
key, str = yield($~[:content], map)
map[key] = str
wrap_placeholder ? "%{#{key}}" : key
end
map
end
|
203
204
205
206
207
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 203
def
(@buffer, ERB_EXPRESSION, false) do |str, map|
["__I18NLINER_#{map.size}__", str]
end
end
|
251
252
253
254
255
256
257
258
259
260
261
262
263
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 251
def (root_node)
text = nil
nodes = root_node.children.to_a
while node = nodes.shift
if node.is_a?(Nokogiri::XML::Text) && !node.content.strip.empty?
raise UnwrappableContentError.new "multiple text nodes in html markup" if text
text = node.content
else
nodes.concat node.children
end
end
text
end
|
144
145
146
147
148
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 144
def (source, wrappers, placeholder_map)
source = (source, wrappers, placeholder_map)
source = (source, wrappers, placeholder_map)
source
end
|
#find_or_add_wrapper(wrapper, wrappers) ⇒ Object
150
151
152
153
154
155
156
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 150
def find_or_add_wrapper(wrapper, wrappers)
unless pos = wrappers.index(wrapper)
pos = wrappers.size
wrappers << wrapper
end
pos
end
|
#infer_interpolation_key(string, others) ⇒ Object
get a unique and reasonable looking key for a given erb expression
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 128
def infer_interpolation_key(string, others)
key = string.downcase
key.sub!(/\.html_safe\z/, '')
key.gsub!(/[^a-z0-9]/, ' ')
key.strip!
key.gsub!(/ +/, '_')
key.slice!(20)
i = 0
base_key = key
while others.key?(key) && others[key] != string
key = "#{base_key}_#{i}"
i += 1
end
key
end
|
#infer_wrappers(source) ⇒ Object
270
271
272
273
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 270
def infer_wrappers(source)
wrappers = []
[source, wrappers]
end
|
#normalize_call ⇒ Object
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 220
def normalize_call
wrappers = []
temp_map =
default = (@buffer, wrappers, temp_map)
options = (default, TEMP_PLACEHOLDER) do |str, map|
[infer_interpolation_key(temp_map[str], map), temp_map[str]]
end
default.strip!
default.gsub!(/\s+/, ' ')
key = infer_key(default, options)
default = default.inspect
options = options_to_ruby(options)
wrappers = wrappers_to_ruby(wrappers)
[key, default, options, wrappers]
end
|
#options_to_ruby(options) ⇒ Object
239
240
241
242
243
244
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 239
def options_to_ruby(options)
options["i18nliner_inferred_key"] = "true"
options.map do |key, value|
":" << key << " => (" << value << ")"
end.join(", ")
end
|
#prepare_wrapper(content, placeholder_map) ⇒ Object
195
196
197
198
199
200
201
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 195
def prepare_wrapper(content, placeholder_map)
content = content.inspect
content.gsub!(TEMP_PLACEHOLDER) do |key|
"\#{#{placeholder_map[key]}}"
end
content
end
|
#result ⇒ Object
116
117
118
119
120
121
122
123
124
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 116
def result
@lines += @buffer.count("\n")
key, default, options, wrappers = normalize_call
result = "<%= t :#{key}, #{default}"
result << ", " << options if options
result << ", " << wrappers if wrappers
result << (@lines > 0 ? "\n" * @lines : " ")
result << "%>"
end
|
#wrap(text, index) ⇒ Object
265
266
267
268
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 265
def wrap(text, index)
delimiter = "*" * index
"" << delimiter << text << delimiter
end
|
#wrappers_to_ruby(wrappers) ⇒ Object
246
247
248
249
|
# File 'lib/i18nliner/pre_processors/erb_pre_processor.rb', line 246
def wrappers_to_ruby(wrappers)
return if wrappers.size == 0
":wrappers => [" << wrappers.join(", ") << "]"
end
|