| 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 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 126 params[key] = "".join(lines) | 126 params[key] = "".join(lines) | 
| 127 | 127 | 
| 128 def localize_string(self, page, name, default, comment, localedata, escapes): | 128 def localize_string(self, page, name, default, comment, localedata, escapes): | 
| 129 def escape(s): | 129 def escape(s): | 
| 130 return re.sub(r".", | 130 return re.sub(r".", | 
| 131 lambda match: escapes.get(match.group(0), match.group(0)), | 131 lambda match: escapes.get(match.group(0), match.group(0)), | 
| 132 s, flags=re.S) | 132 s, flags=re.S) | 
| 133 def re_escape(s): | 133 def re_escape(s): | 
| 134 return re.escape(escape(s)) | 134 return re.escape(escape(s)) | 
| 135 | 135 | 
| 136 locale = self._params["locale"] | |
| 137 is_default_locale = locale == self._params["defaultlocale"] | |
| 138 | |
| 139 # Handle duplicated strings | |
| 140 if not default: | |
| 141 try: | |
| 142 default = self._params["localedata"][name] | |
| 
 
Wladimir Palant
2015/08/24 11:07:17
This logic is wrong, the default value should neve
 
kzar
2015/08/24 14:29:45
Acknowledged.
 
 | |
| 143 except KeyError: | |
| 144 raise Exception("Text not yet defined for string %s on page %s" % | |
| 145 (name, self._params["page"])) | |
| 146 elif is_default_locale: | |
| 147 self._params["localedata"][name] = default | |
| 
 
Wladimir Palant
2015/08/24 11:07:17
1) This should be using localedata parameter inste
 
kzar
2015/08/24 14:29:46
Done.
 
 | |
| 148 | |
| 136 # Extract tag attributes from default string | 149 # Extract tag attributes from default string | 
| 137 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"]) | 150 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"]) | 
| 138 | 151 | 
| 139 # Get translation | 152 # Get translation | 
| 140 locale = self._params["locale"] | 153 if is_default_locale: | 
| 141 if locale == self._params["defaultlocale"]: | |
| 142 result = default | 154 result = default | 
| 143 elif name in localedata: | 155 elif name in localedata: | 
| 144 result = localedata[name].strip() | 156 result = localedata[name].strip() | 
| 145 else: | 157 else: | 
| 146 result = default | 158 result = default | 
| 147 self.missing_translations += 1 | 159 self.missing_translations += 1 | 
| 148 self.total_translations += 1 | 160 self.total_translations += 1 | 
| 149 | 161 | 
| 150 # Perform callback with the string if required, e.g. for the translations sc ript | 162 if default: | 
| 
 
Wladimir Palant
2015/08/24 11:07:17
Why add this check? The string is still there, reg
 
kzar
2015/08/24 14:29:45
Well as a consumer of this callback I would be sur
 
Wladimir Palant
2015/08/24 15:00:21
That's something one has to count on anyway.
 
 | |
| 151 callback = self._params["localized_string_callback"] | 163 # Perform callback with the string if required, | 
| 152 if callback: | 164 # e.g. for the translations script | 
| 153 callback(page, locale, name, result, comment, fixed_strings) | 165 callback = self._params["localized_string_callback"] | 
| 154 | 166 if callback: | 
| 167 callback(page, locale, name, result, comment, fixed_strings) | |
| 155 | 168 | 
| 156 # Insert fixed strings | 169 # Insert fixed strings | 
| 157 for i, fixed_string in enumerate(fixed_strings, 1): | 170 for i, fixed_string in enumerate(fixed_strings, 1): | 
| 158 result = result.replace("{%d}" % i, fixed_string) | 171 result = result.replace("{%d}" % i, fixed_string) | 
| 159 | 172 | 
| 160 # Insert attributes | 173 # Insert attributes | 
| 161 result = escape(result) | 174 result = escape(result) | 
| 162 def stringify_attribute((name, value)): | 175 def stringify_attribute((name, value)): | 
| 163 return '%s="%s"' % ( | 176 return '%s="%s"' % ( | 
| 164 escape(name), | 177 escape(name), | 
| (...skipping 17 matching lines...) Expand all Loading... | |
| 182 result = re.sub( | 195 result = re.sub( | 
| 183 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), | 196 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), | 
| 184 r"<%s>\1</%s>" % (tag, tag), | 197 r"<%s>\1</%s>" % (tag, tag), | 
| 185 result, flags=re.S | 198 result, flags=re.S | 
| 186 ) | 199 ) | 
| 187 return result | 200 return result | 
| 188 | 201 | 
| 189 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): | 202 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): | 
| 190 def lookup_string(match): | 203 def lookup_string(match): | 
| 191 name, comment, default = match.groups() | 204 name, comment, default = match.groups() | 
| 192 default = to_html(default).strip() | 205 if default: | 
| 193 | 206 default = to_html(default).strip() | 
| 194 return self.localize_string(self._params["page"], name, default, | 207 return self.localize_string(self._params["page"], name, default, | 
| 195 comment, self._params["localedata"], escapes) | 208 comment, self._params["localedata"], escapes) | 
| 196 | 209 | 
| 197 return re.sub( | 210 return re.sub( | 
| 198 r"{{\s*" | 211 r"{{\s*" | 
| 199 r"([\w\-]+)" # String ID | 212 r"([\w\-]+)" # String ID | 
| 200 r"(?:\[(.*?)\])?" # Optional comment | 213 r"(?:(?:\[(.*?)\])?" # Optional comment | 
| 201 r"\s+" | 214 r"\s+" | 
| 202 r"((?:(?!{{).|" # Translatable text | 215 r"((?:(?!{{).|" # Translatable text | 
| 203 r"{{(?:(?!}}).)*}}" # Nested translation | 216 r"{{(?:(?!}}).)*}}" # Nested translation | 
| 204 r")*?)" | 217 r")*?)" | 
| 218 r")?" | |
| 205 r"}}", | 219 r"}}", | 
| 206 lookup_string, | 220 lookup_string, | 
| 207 text, | 221 text, | 
| 208 flags=re.S | 222 flags=re.S | 
| 209 ) | 223 ) | 
| 210 | 224 | 
| 211 def process_links(self, text): | 225 def process_links(self, text): | 
| 212 def process_link(match): | 226 def process_link(match): | 
| 213 pre, attr, url, post = match.groups() | 227 pre, attr, url, post = match.groups() | 
| 214 url = jinja2.Markup(url).unescape() | 228 url = jinja2.Markup(url).unescape() | 
| (...skipping 190 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 405 stack.pop() | 419 stack.pop() | 
| 406 stack[-1]["subitems"].append(item) | 420 stack[-1]["subitems"].append(item) | 
| 407 stack.append(item) | 421 stack.append(item) | 
| 408 return structured | 422 return structured | 
| 409 | 423 | 
| 410 converters = { | 424 converters = { | 
| 411 "html": RawConverter, | 425 "html": RawConverter, | 
| 412 "md": MarkdownConverter, | 426 "md": MarkdownConverter, | 
| 413 "tmpl": TemplateConverter, | 427 "tmpl": TemplateConverter, | 
| 414 } | 428 } | 
| OLD | NEW |