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 107 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
118 self._key = key | 118 self._key = key |
119 self._attribute_parser = AttributeParser(self.whitelist) | 119 self._attribute_parser = AttributeParser(self.whitelist) |
120 | 120 |
121 # Read in any parameters specified at the beginning of the file | 121 # Read in any parameters specified at the beginning of the file |
122 lines = params[key].splitlines(True) | 122 lines = params[key].splitlines(True) |
123 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): | 123 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): |
124 name, value = lines.pop(0).split("=", 1) | 124 name, value = lines.pop(0).split("=", 1) |
125 params[name.strip()] = value.strip() | 125 params[name.strip()] = value.strip() |
126 params[key] = "".join(lines) | 126 params[key] = "".join(lines) |
127 | 127 |
128 def localize_string(self, name, default, 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 # Extract tag attributes from default string | 136 # Extract tag attributes from default string |
137 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa
ult, self._params["page"]) | 137 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa
ult, self._params["page"]) |
138 | 138 |
139 # Get translation | 139 # Get translation |
140 locale = self._params["locale"] | 140 locale = self._params["locale"] |
141 if locale == self._params["defaultlocale"]: | 141 if locale == self._params["defaultlocale"]: |
142 result = default | 142 result = default |
143 elif name in localedata: | 143 elif name in localedata: |
144 result = localedata[name].strip() | 144 result = localedata[name].strip() |
145 else: | 145 else: |
146 result = default | 146 result = default |
147 self.missing_translations += 1 | 147 self.missing_translations += 1 |
148 self.total_translations += 1 | 148 self.total_translations += 1 |
149 | 149 |
| 150 # Perform callback with the string if required, e.g. for the translations sc
ript |
| 151 callback = self._params["localized_string_callback"] |
| 152 if callback: |
| 153 callback(page, locale, name, result, comment, fixed_strings) |
| 154 |
| 155 |
150 # Insert fixed strings | 156 # Insert fixed strings |
151 for i, fixed_string in enumerate(fixed_strings, 1): | 157 for i, fixed_string in enumerate(fixed_strings, 1): |
152 result = result.replace("{%d}" % i, fixed_string) | 158 result = result.replace("{%d}" % i, fixed_string) |
153 | 159 |
154 # Insert attributes | 160 # Insert attributes |
155 result = escape(result) | 161 result = escape(result) |
156 def stringify_attribute((name, value)): | 162 def stringify_attribute((name, value)): |
157 return '%s="%s"' % ( | 163 return '%s="%s"' % ( |
158 escape(name), | 164 escape(name), |
159 escape(self.insert_localized_strings(value, {})) | 165 escape(self.insert_localized_strings(value, {})) |
(...skipping 18 matching lines...) Expand all Loading... |
178 r"<%s>\1</%s>" % (tag, tag), | 184 r"<%s>\1</%s>" % (tag, tag), |
179 result, flags=re.S | 185 result, flags=re.S |
180 ) | 186 ) |
181 return result | 187 return result |
182 | 188 |
183 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): | 189 def insert_localized_strings(self, text, escapes, to_html=lambda s: s): |
184 def lookup_string(match): | 190 def lookup_string(match): |
185 name, comment, default = match.groups() | 191 name, comment, default = match.groups() |
186 default = to_html(default).strip() | 192 default = to_html(default).strip() |
187 | 193 |
188 # Note: We currently ignore the comment, it is only relevant when | 194 return self.localize_string(self._params["page"], name, default, |
189 # generating the master translation. | 195 comment, self._params["localedata"], escapes) |
190 return self.localize_string(name, default, self._params["localedata"], esc
apes) | |
191 | 196 |
192 return re.sub( | 197 return re.sub( |
193 r"{{\s*" | 198 r"{{\s*" |
194 r"([\w\-]+)" # String ID | 199 r"([\w\-]+)" # String ID |
195 r"(?:\[(.*?)\])?" # Optional comment | 200 r"(?:\[(.*?)\])?" # Optional comment |
196 r"\s+" | 201 r"\s+" |
197 r"((?:(?!{{).|" # Translatable text | 202 r"((?:(?!{{).|" # Translatable text |
198 r"{{(?:(?!}}).)*}}" # Nested translation | 203 r"{{(?:(?!}}).)*}}" # Nested translation |
199 r")*?)" | 204 r")*?)" |
200 r"}}", | 205 r"}}", |
(...skipping 144 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
345 module = template.make_module(self._params) | 350 module = template.make_module(self._params) |
346 for key, value in module.__dict__.iteritems(): | 351 for key, value in module.__dict__.iteritems(): |
347 if not key.startswith("_"): | 352 if not key.startswith("_"): |
348 self._params[key] = value | 353 self._params[key] = value |
349 | 354 |
350 result = unicode(module) | 355 result = unicode(module) |
351 result = self.process_links(result) | 356 result = self.process_links(result) |
352 return result | 357 return result |
353 | 358 |
354 def translate(self, default, name, comment=None): | 359 def translate(self, default, name, comment=None): |
355 # Note: We currently ignore the comment, it is only relevant when | 360 return jinja2.Markup(self.localize_string( |
356 # generating the master translation. | 361 self._params["page"], name, default, comment, |
357 localedata = self._params["localedata"] | 362 self._params["localedata"], html_escapes |
358 return jinja2.Markup(self.localize_string(name, default, localedata, html_es
capes)) | 363 )) |
359 | 364 |
360 def get_string(self, name, page): | 365 def get_string(self, name, page): |
361 localedata = self._params["source"].read_locale(self._params["locale"], page
) | 366 localedata = self._params["source"].read_locale(self._params["locale"], page
) |
362 default = localedata[name] | 367 default = localedata[name] |
363 return jinja2.Markup(self.localize_string(name, default, localedata, html_es
capes)) | 368 return jinja2.Markup(self.localize_string( |
| 369 page, name, default, "", localedata, html_escapes |
| 370 )) |
364 | 371 |
365 def get_page_content(self, page, locale=None): | 372 def get_page_content(self, page, locale=None): |
366 from cms.utils import get_page_params | 373 from cms.utils import get_page_params |
367 | 374 |
368 if locale is None: | 375 if locale is None: |
369 locale = self._params["locale"] | 376 locale = self._params["locale"] |
370 return get_page_params(self._params["source"], locale, page) | 377 return get_page_params(self._params["source"], locale, page) |
371 | 378 |
372 def linkify(self, page, locale=None, **attrs): | 379 def linkify(self, page, locale=None, **attrs): |
373 if locale is None: | 380 if locale is None: |
(...skipping 24 matching lines...) Expand all Loading... |
398 stack.pop() | 405 stack.pop() |
399 stack[-1]["subitems"].append(item) | 406 stack[-1]["subitems"].append(item) |
400 stack.append(item) | 407 stack.append(item) |
401 return structured | 408 return structured |
402 | 409 |
403 converters = { | 410 converters = { |
404 "html": RawConverter, | 411 "html": RawConverter, |
405 "md": MarkdownConverter, | 412 "md": MarkdownConverter, |
406 "tmpl": TemplateConverter, | 413 "tmpl": TemplateConverter, |
407 } | 414 } |
OLD | NEW |