Left: | ||
Right: |
LEFT | RIGHT |
---|---|
1 # coding: utf-8 | 1 # coding: utf-8 |
2 | 2 |
3 # This file is part of the Adblock Plus web scripts, | 3 # This file is part of the Adblock Plus web scripts, |
4 # Copyright (C) 2006-2015 Eyeo GmbH | 4 # Copyright (C) 2006-2015 Eyeo GmbH |
5 # | 5 # |
6 # Adblock Plus is free software: you can redistribute it and/or modify | 6 # Adblock Plus is free software: you can redistribute it and/or modify |
7 # it under the terms of the GNU General Public License version 3 as | 7 # it under the terms of the GNU General Public License version 3 as |
8 # published by the Free Software Foundation. | 8 # published by the Free Software Foundation. |
9 # | 9 # |
10 # Adblock Plus is distributed in the hope that it will be useful, | 10 # Adblock Plus is distributed in the hope that it will be useful, |
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
125 return re.sub(r".", | 125 return re.sub(r".", |
126 lambda match: escapes.get(match.group(0), match.group(0)), | 126 lambda match: escapes.get(match.group(0), match.group(0)), |
127 s, flags=re.S) | 127 s, flags=re.S) |
128 def re_escape(s): | 128 def re_escape(s): |
129 return re.escape(escape(s)) | 129 return re.escape(escape(s)) |
130 | 130 |
131 # Extract tag attributes from default string | 131 # Extract tag attributes from default string |
132 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"]) | 132 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"]) |
133 | 133 |
134 # Get translation | 134 # Get translation |
135 locale = self._params["locale"] | 135 if self._params["locale"] != self._params["defaultlocale"] and name in local edata: |
136 if locale != self._params["defaultlocale"] and name in localedata: | |
137 result = localedata[name].strip() | 136 result = localedata[name].strip() |
138 else: | 137 else: |
139 result = default | 138 result = default |
140 | 139 |
141 # Insert fixed strings | 140 # Insert fixed strings |
142 for i in range(len(fixed_strings)): | 141 for i in range(len(fixed_strings)): |
143 result = re.sub(r"\{%d\}" % (i + 1), fixed_strings[i], result, 1) | 142 result = re.sub(r"\{%d\}" % (i + 1), fixed_strings[i], result, 1) |
144 | 143 |
145 # Insert attributes | 144 # Insert attributes |
146 result = escape(result) | 145 result = escape(result) |
147 def stringify_attribute((name, value)): | 146 def stringify_attribute((name, value)): |
148 value = self.insert_localized_strings(value, escapes) | 147 return '%s="%s"' % ( |
149 if name == "href": | 148 escape(name), |
150 link_locale, link = self._params["source"].resolve_link(value, locale) | 149 escape(self.insert_localized_strings(value, escapes)) |
151 if link: | 150 ) |
152 return 'href="%s" hreflang="%s"' % (escape(link), escape(link_locale)) | |
153 return '%s="%s"' % (escape(name), escape(value)) | |
154 | 151 |
155 for tag in self.whitelist: | 152 for tag in self.whitelist: |
156 saved = saved_attributes.get(tag, []) | 153 saved = saved_attributes.get(tag, []) |
157 for attrs in saved: | 154 for attrs in saved: |
158 attrs = map(stringify_attribute, attrs) | 155 attrs = map(stringify_attribute, attrs) |
159 result = re.sub( | 156 result = re.sub( |
160 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)) , | 157 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)) , |
161 r'<%s %s>\1</%s>' % (tag, " ".join(attrs), tag), | 158 r'<%s%s>\1</%s>' % (tag, " " + " ".join(attrs) if attrs else "", tag), |
162 result, 1, flags=re.S | 159 result, 1, flags=re.S |
163 ) | 160 ) |
164 result = re.sub( | 161 result = re.sub( |
165 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), | 162 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), |
166 r"<%s>\1</%s>" % (tag, tag), | 163 r"<%s>\1</%s>" % (tag, tag), |
167 result, flags=re.S | 164 result, flags=re.S |
168 ) | 165 ) |
169 return result | 166 return result |
170 | 167 |
171 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): | 168 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): |
172 # Find the positions for the top-level translatable strings | 169 def lookup_string(match): |
173 level = 0 | 170 name, comment, default = match.groups() |
174 start = None | 171 default = to_html(default).strip() |
175 positions = [] | 172 |
176 for m in re.finditer(r"(\{\{|\}\})", text): | 173 # Note: We currently ignore the comment, it is only relevant when |
177 if m.group(0) == "{{": | 174 # generating the master translation. |
178 if level == 0: | 175 return self.localize_string(name, default, self._params["localedata"], esc apes) |
179 start = m.start() | 176 |
180 level += 1 | 177 return re.sub( |
181 else: | 178 r"{{\s*" |
182 level -= 1 | 179 r"([\w\-]+)" # String ID |
183 if level == 0: | 180 r"(?:\[(.*?)\])?" # Optional comment |
184 positions.append((start, m.end())) | 181 r"\s+" |
185 | 182 r"((?:(?!{{).|" # Translatable text |
186 # Replace each string with it's translation | 183 r"{{(?:(?!}}).)*}}" # Nested translation |
187 parts = [] | 184 r")*?)" |
188 last_end = 0 | 185 r"}}", |
189 for start, end in positions: | 186 lookup_string, |
190 # Append any text before this tag since the last one | 187 text, |
191 parts.append(text[last_end:start]) | 188 flags=re.S |
192 # Append our localized string | 189 ) |
193 name, comment, default = re.match( | |
194 r"\{\{\s*([\w\-]+)(?:\[(.*?)\])?\s+(.*)\}\}", | |
Wladimir Palant
2015/04/16 21:01:13
Only one nesting level is actually allowed here. S
kzar
2015/04/21 11:28:57
With a couple of tweaks I managed to get your rege
kzar
2015/04/21 14:44:53
OK I made another change to the regexp to hopefull
| |
195 text[start:end], | |
196 flags=re.S | |
197 ).groups() | |
198 parts.append(self.localize_string( | |
199 name, | |
200 to_html(default).strip(), | |
201 self._params["localedata"], | |
202 escapes | |
203 )) | |
204 last_end = end | |
205 return "".join(parts) + text[last_end:] | |
206 | 190 |
207 def process_links(self, text): | 191 def process_links(self, text): |
208 def process_link(match): | 192 def process_link(match): |
209 pre, attr, url, post = match.groups() | 193 pre, attr, url, post = match.groups() |
210 url = jinja2.Markup(url).unescape() | 194 url = jinja2.Markup(url).unescape() |
211 | 195 |
212 locale, new_url = self._params["source"].resolve_link(url, self._params["l ocale"]) | 196 locale, new_url = self._params["source"].resolve_link(url, self._params["l ocale"]) |
213 if new_url != None: | 197 if new_url != None: |
214 url = new_url | 198 url = new_url |
215 if attr == "href": | 199 if attr == "href": |
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
352 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True) | 336 self._env = jinja2.Environment(loader=self._SourceLoader(self._params["sourc e"]), autoescape=True) |
353 self._env.filters.update(filters) | 337 self._env.filters.update(filters) |
354 self._env.globals.update(globals) | 338 self._env.globals.update(globals) |
355 | 339 |
356 def get_html(self, source): | 340 def get_html(self, source): |
357 template = self._env.from_string(source) | 341 template = self._env.from_string(source) |
358 module = template.make_module(self._params) | 342 module = template.make_module(self._params) |
359 for key, value in module.__dict__.iteritems(): | 343 for key, value in module.__dict__.iteritems(): |
360 if not key.startswith("_"): | 344 if not key.startswith("_"): |
361 self._params[key] = value | 345 self._params[key] = value |
362 return unicode(module) | 346 |
347 result = unicode(module) | |
348 result = self.process_links(result) | |
349 return result | |
363 | 350 |
364 def translate(self, default, name, comment=None): | 351 def translate(self, default, name, comment=None): |
365 # Note: We currently ignore the comment, it is only relevant when | 352 # Note: We currently ignore the comment, it is only relevant when |
366 # generating the master translation. | 353 # generating the master translation. |
367 localedata = self._params["localedata"] | 354 localedata = self._params["localedata"] |
368 return jinja2.Markup(self.localize_string(name, default, localedata, html_es capes)) | 355 return jinja2.Markup(self.localize_string(name, default, localedata, html_es capes)) |
369 | 356 |
370 def get_string(self, name, page): | 357 def get_string(self, name, page): |
371 localedata = self._params["source"].read_locale(self._params["locale"], page ) | 358 localedata = self._params["source"].read_locale(self._params["locale"], page ) |
372 default = localedata[name] | 359 default = localedata[name] |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
408 stack.pop() | 395 stack.pop() |
409 stack[-1]["subitems"].append(item) | 396 stack[-1]["subitems"].append(item) |
410 stack.append(item) | 397 stack.append(item) |
411 return structured | 398 return structured |
412 | 399 |
413 converters = { | 400 converters = { |
414 "html": RawConverter, | 401 "html": RawConverter, |
415 "md": MarkdownConverter, | 402 "md": MarkdownConverter, |
416 "tmpl": TemplateConverter, | 403 "tmpl": TemplateConverter, |
417 } | 404 } |
LEFT | RIGHT |