| Left: | ||
| Right: |
| OLD | NEW |
|---|---|
| 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 127 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 138 else: | 138 else: |
| 139 result = default | 139 result = default |
| 140 | 140 |
| 141 # Insert fixed strings | 141 # Insert fixed strings |
| 142 for i in range(len(fixed_strings)): | 142 for i in range(len(fixed_strings)): |
| 143 result = re.sub(r"\{%d\}" % (i + 1), fixed_strings[i], result, 1) | 143 result = re.sub(r"\{%d\}" % (i + 1), fixed_strings[i], result, 1) |
| 144 | 144 |
| 145 # Insert attributes | 145 # Insert attributes |
| 146 result = escape(result) | 146 result = escape(result) |
| 147 def stringify_attribute((name, value)): | 147 def stringify_attribute((name, value)): |
| 148 value = self.insert_localized_strings(value, escapes) | |
| 148 if name == "href": | 149 if name == "href": |
| 149 link_locale, link = self._params["source"].resolve_link(value, locale) | 150 link_locale, link = self._params["source"].resolve_link(value, locale) |
| 150 if link: | 151 if link: |
| 151 return 'href="%s" hreflang="%s"' % (escape(link), escape(link_locale)) | 152 return 'href="%s" hreflang="%s"' % (escape(link), escape(link_locale)) |
| 152 return '%s="%s"' % (escape(name), escape(value)) | 153 return '%s="%s"' % (escape(name), escape(value)) |
| 153 | 154 |
| 154 for tag in self.whitelist: | 155 for tag in self.whitelist: |
| 155 saved = saved_attributes.get(tag, []) | 156 saved = saved_attributes.get(tag, []) |
| 156 for attrs in saved: | 157 for attrs in saved: |
| 157 attrs = map(stringify_attribute, attrs) | 158 attrs = map(stringify_attribute, attrs) |
| 158 result = re.sub( | 159 result = re.sub( |
| 159 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)) , | 160 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)) , |
| 160 r'<%s %s>\1</%s>' % (tag, " ".join(attrs), tag), | 161 r'<%s %s>\1</%s>' % (tag, " ".join(attrs), tag), |
| 161 result, 1, flags=re.S | 162 result, 1, flags=re.S |
| 162 ) | 163 ) |
| 163 result = re.sub( | 164 result = re.sub( |
| 164 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), | 165 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), |
| 165 r"<%s>\1</%s>" % (tag, tag), | 166 r"<%s>\1</%s>" % (tag, tag), |
| 166 result, flags=re.S | 167 result, flags=re.S |
| 167 ) | 168 ) |
| 168 return result | 169 return result |
| 169 | 170 |
| 170 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): | 171 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): |
| 171 def lookup_string(match): | 172 # Find the positions for the top-level translatable strings |
| 172 name, comment, default = match.groups() | 173 level = 0 |
| 173 default = to_html(default).strip() | 174 start = None |
| 175 positions = [] | |
| 176 for m in re.finditer(r"(\{\{|\}\})", text): | |
| 177 if m.group(0) == "{{": | |
| 178 if level == 0: | |
| 179 start = m.start() | |
| 180 level += 1 | |
| 181 else: | |
| 182 level -= 1 | |
| 183 if level == 0: | |
| 184 positions.append((start, m.end())) | |
| 174 | 185 |
| 175 # Note: We currently ignore the comment, it is only relevant when | 186 # Replace each string with it's translation |
| 176 # generating the master translation. | 187 parts = [] |
| 177 return self.localize_string(name, default, self._params["localedata"], esc apes) | 188 last_end = 0 |
| 178 | 189 for start, end in positions: |
| 179 return re.sub( | 190 # Append any text before this tag since the last one |
| 180 r"\{\{\s*([\w\-]+)(?:\[(.*?)\])?\s+(.*?)\}\}", | 191 parts.append(text[last_end:start]) |
| 181 lookup_string, | 192 # Append our localized string |
| 182 text, | 193 name, comment, default = re.match( |
| 183 flags=re.S | 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
| |
| 184 ) | 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:] | |
| 185 | 206 |
| 186 def process_links(self, text): | 207 def process_links(self, text): |
| 187 def process_link(match): | 208 def process_link(match): |
| 188 pre, attr, url, post = match.groups() | 209 pre, attr, url, post = match.groups() |
| 189 url = jinja2.Markup(url).unescape() | 210 url = jinja2.Markup(url).unescape() |
| 190 | 211 |
| 191 locale, new_url = self._params["source"].resolve_link(url, self._params["l ocale"]) | 212 locale, new_url = self._params["source"].resolve_link(url, self._params["l ocale"]) |
| 192 if new_url != None: | 213 if new_url != None: |
| 193 url = new_url | 214 url = new_url |
| 194 if attr == "href": | 215 if attr == "href": |
| (...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 387 stack.pop() | 408 stack.pop() |
| 388 stack[-1]["subitems"].append(item) | 409 stack[-1]["subitems"].append(item) |
| 389 stack.append(item) | 410 stack.append(item) |
| 390 return structured | 411 return structured |
| 391 | 412 |
| 392 converters = { | 413 converters = { |
| 393 "html": RawConverter, | 414 "html": RawConverter, |
| 394 "md": MarkdownConverter, | 415 "md": MarkdownConverter, |
| 395 "tmpl": TemplateConverter, | 416 "tmpl": TemplateConverter, |
| 396 } | 417 } |
| OLD | NEW |