| 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 def lookup_string(match): | 169 def lookup_string(match): |
| 173 name, comment, default = match.groups() | 170 name, comment, default = match.groups() |
| 174 default = to_html(default).strip() | 171 default = to_html(default).strip() |
| 175 | 172 |
| 176 # Note: We currently ignore the comment, it is only relevant when | 173 # Note: We currently ignore the comment, it is only relevant when |
| 177 # generating the master translation. | 174 # generating the master translation. |
| 178 return self.localize_string(name, default, self._params["localedata"], esc
apes) | 175 return self.localize_string(name, default, self._params["localedata"], esc
apes) |
| 179 | 176 |
| 180 return re.sub( | 177 return re.sub( |
| 181 r"\{\{\s*" | 178 r"{{\s*" |
| 182 r"([\w\-]+)" # String ID | 179 r"([\w\-]+)" # String ID |
| 183 r"(?:\[(.*?)\])?" # Optional comment | 180 r"(?:\[(.*?)\])?" # Optional comment |
| 184 r"\s+" | 181 r"\s+" |
| 185 r"((?:[^\{]|\{(?!\{)|\{\{(?:[^\}]|\}(?!\}))*?\}\})*?)" # Translatable text | 182 r"((?:(?!{{).|" # Translatable text |
| 186 r"\}\}", | 183 r"{{(?:(?!}}).)*}}" # Nested translation |
| 184 r")*?)" |
| 185 r"}}", |
| 187 lookup_string, | 186 lookup_string, |
| 188 text, | 187 text, |
| 189 flags=re.S | 188 flags=re.S |
| 190 ) | 189 ) |
| 191 | 190 |
| 192 def process_links(self, text): | 191 def process_links(self, text): |
| 193 def process_link(match): | 192 def process_link(match): |
| 194 pre, attr, url, post = match.groups() | 193 pre, attr, url, post = match.groups() |
| 195 url = jinja2.Markup(url).unescape() | 194 url = jinja2.Markup(url).unescape() |
| 196 | 195 |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 337 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) |
| 338 self._env.filters.update(filters) | 337 self._env.filters.update(filters) |
| 339 self._env.globals.update(globals) | 338 self._env.globals.update(globals) |
| 340 | 339 |
| 341 def get_html(self, source): | 340 def get_html(self, source): |
| 342 template = self._env.from_string(source) | 341 template = self._env.from_string(source) |
| 343 module = template.make_module(self._params) | 342 module = template.make_module(self._params) |
| 344 for key, value in module.__dict__.iteritems(): | 343 for key, value in module.__dict__.iteritems(): |
| 345 if not key.startswith("_"): | 344 if not key.startswith("_"): |
| 346 self._params[key] = value | 345 self._params[key] = value |
| 347 return unicode(module) | 346 |
| 347 result = unicode(module) |
| 348 result = self.process_links(result) |
| 349 return result |
| 348 | 350 |
| 349 def translate(self, default, name, comment=None): | 351 def translate(self, default, name, comment=None): |
| 350 # Note: We currently ignore the comment, it is only relevant when | 352 # Note: We currently ignore the comment, it is only relevant when |
| 351 # generating the master translation. | 353 # generating the master translation. |
| 352 localedata = self._params["localedata"] | 354 localedata = self._params["localedata"] |
| 353 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)) |
| 354 | 356 |
| 355 def get_string(self, name, page): | 357 def get_string(self, name, page): |
| 356 localedata = self._params["source"].read_locale(self._params["locale"], page
) | 358 localedata = self._params["source"].read_locale(self._params["locale"], page
) |
| 357 default = localedata[name] | 359 default = localedata[name] |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 393 stack.pop() | 395 stack.pop() |
| 394 stack[-1]["subitems"].append(item) | 396 stack[-1]["subitems"].append(item) |
| 395 stack.append(item) | 397 stack.append(item) |
| 396 return structured | 398 return structured |
| 397 | 399 |
| 398 converters = { | 400 converters = { |
| 399 "html": RawConverter, | 401 "html": RawConverter, |
| 400 "md": MarkdownConverter, | 402 "md": MarkdownConverter, |
| 401 "tmpl": TemplateConverter, | 403 "tmpl": TemplateConverter, |
| 402 } | 404 } |
| LEFT | RIGHT |