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 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
100 self._append_text(data) | 100 self._append_text(data) |
101 | 101 |
102 def handle_entityref(self, name): | 102 def handle_entityref(self, name): |
103 self._append_text(self.unescape("&%s;" % name)) | 103 self._append_text(self.unescape("&%s;" % name)) |
104 | 104 |
105 def handle_charref(self, name): | 105 def handle_charref(self, name): |
106 self._append_text(self.unescape("&#%s;" % name)) | 106 self._append_text(self.unescape("&#%s;" % name)) |
107 | 107 |
108 class Converter: | 108 class Converter: |
109 whitelist = set(["a", "em", "strong"]) | 109 whitelist = set(["a", "em", "strong"]) |
| 110 missing_translations = 0 |
| 111 total_translations = 0 |
110 | 112 |
111 def __init__(self, params, key="pagedata"): | 113 def __init__(self, params, key="pagedata"): |
112 self._params = params | 114 self._params = params |
113 self._key = key | 115 self._key = key |
114 self._attribute_parser = AttributeParser(self.whitelist) | 116 self._attribute_parser = AttributeParser(self.whitelist) |
115 | 117 |
116 # Read in any parameters specified at the beginning of the file | 118 # Read in any parameters specified at the beginning of the file |
117 lines = params[key].splitlines(True) | 119 lines = params[key].splitlines(True) |
118 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): | 120 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): |
119 name, value = lines.pop(0).split("=", 1) | 121 name, value = lines.pop(0).split("=", 1) |
120 params[name.strip()] = value.strip() | 122 params[name.strip()] = value.strip() |
121 params[key] = "".join(lines) | 123 params[key] = "".join(lines) |
122 | 124 |
123 def localize_string(self, name, default, localedata, escapes): | 125 def localize_string(self, name, default, localedata, escapes): |
124 def escape(s): | 126 def escape(s): |
125 return re.sub(r".", | 127 return re.sub(r".", |
126 lambda match: escapes.get(match.group(0), match.group(0)), | 128 lambda match: escapes.get(match.group(0), match.group(0)), |
127 s, flags=re.S) | 129 s, flags=re.S) |
128 def re_escape(s): | 130 def re_escape(s): |
129 return re.escape(escape(s)) | 131 return re.escape(escape(s)) |
130 | 132 |
131 # Extract tag attributes from default string | 133 # Extract tag attributes from default string |
132 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa
ult, self._params["page"]) | 134 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa
ult, self._params["page"]) |
133 | 135 |
134 # Get translation | 136 # Get translation |
135 locale = self._params["locale"] | 137 locale = self._params["locale"] |
136 if locale != self._params["defaultlocale"] and name in localedata: | 138 if locale == self._params["defaultlocale"]: |
| 139 result = default |
| 140 elif name in localedata: |
137 result = localedata[name].strip() | 141 result = localedata[name].strip() |
138 else: | 142 else: |
139 result = default | 143 result = default |
| 144 self.missing_translations += 1 |
| 145 self.total_translations += 1 |
140 | 146 |
141 # Insert fixed strings | 147 # Insert fixed strings |
142 for i in range(len(fixed_strings)): | 148 for i in range(len(fixed_strings)): |
143 result = re.sub(r"\{%d\}" % (i + 1), fixed_strings[i], result, 1) | 149 result = re.sub(r"\{%d\}" % (i + 1), fixed_strings[i], result, 1) |
144 | 150 |
145 # Insert attributes | 151 # Insert attributes |
146 result = escape(result) | 152 result = escape(result) |
147 def stringify_attribute((name, value)): | 153 def stringify_attribute((name, value)): |
148 if name == "href": | 154 if name == "href": |
149 link_locale, link = self._params["source"].resolve_link(value, locale) | 155 link_locale, link = self._params["source"].resolve_link(value, locale) |
(...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
204 include_end_regex = '>' | 210 include_end_regex = '>' |
205 | 211 |
206 def resolve_includes(self, text): | 212 def resolve_includes(self, text): |
207 def resolve_include(match): | 213 def resolve_include(match): |
208 global converters | 214 global converters |
209 name = match.group(1) | 215 name = match.group(1) |
210 for format, converter_class in converters.iteritems(): | 216 for format, converter_class in converters.iteritems(): |
211 if self._params["source"].has_include(name, format): | 217 if self._params["source"].has_include(name, format): |
212 self._params["includedata"] = self._params["source"].read_include(name
, format) | 218 self._params["includedata"] = self._params["source"].read_include(name
, format) |
213 converter = converter_class(self._params, key="includedata") | 219 converter = converter_class(self._params, key="includedata") |
214 return converter() | 220 result = converter() |
| 221 self.missing_translations += converter.missing_translations |
| 222 self.total_translations += converter.total_translations |
| 223 return result |
215 raise Exception("Failed to resolve include %s on page %s" % (name, self._p
arams["page"])) | 224 raise Exception("Failed to resolve include %s on page %s" % (name, self._p
arams["page"])) |
216 | 225 |
217 return re.sub( | 226 return re.sub( |
218 r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % ( | 227 r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % ( |
219 self.include_start_regex, | 228 self.include_start_regex, |
220 self.include_end_regex | 229 self.include_end_regex |
221 ), | 230 ), |
222 resolve_include, | 231 resolve_include, |
223 text | 232 text |
224 ) | 233 ) |
(...skipping 162 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
387 stack.pop() | 396 stack.pop() |
388 stack[-1]["subitems"].append(item) | 397 stack[-1]["subitems"].append(item) |
389 stack.append(item) | 398 stack.append(item) |
390 return structured | 399 return structured |
391 | 400 |
392 converters = { | 401 converters = { |
393 "html": RawConverter, | 402 "html": RawConverter, |
394 "md": MarkdownConverter, | 403 "md": MarkdownConverter, |
395 "tmpl": TemplateConverter, | 404 "tmpl": TemplateConverter, |
396 } | 405 } |
OLD | NEW |