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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
136 result = localedata[name].strip() | 136 result = localedata[name].strip() |
137 else: | 137 else: |
138 result = default | 138 result = default |
139 | 139 |
140 # Insert fixed strings | 140 # Insert fixed strings |
141 for i in range(len(fixed_strings)): | 141 for i in range(len(fixed_strings)): |
142 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) |
143 | 143 |
144 # Insert attributes | 144 # Insert attributes |
145 result = escape(result) | 145 result = escape(result) |
| 146 def stringify_attribute((name, value)): |
| 147 return '%s="%s"' % ( |
| 148 escape(name), |
| 149 escape(self.insert_localized_strings(value, escapes)) |
| 150 ) |
| 151 |
146 for tag in self.whitelist: | 152 for tag in self.whitelist: |
147 saved = saved_attributes.get(tag, []) | 153 saved = saved_attributes.get(tag, []) |
148 for attrs in saved: | 154 for attrs in saved: |
149 attrs = map(lambda (name, value): '%s="%s"' % (escape(name), escape(valu
e)), attrs) | 155 attrs = map(stringify_attribute, attrs) |
150 result = re.sub( | 156 result = re.sub( |
151 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag))
, | 157 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag))
, |
152 r'<%s%s>\1</%s>' % (tag, " " + " ".join(attrs) if attrs else "", tag), | 158 r'<%s%s>\1</%s>' % (tag, " " + " ".join(attrs) if attrs else "", tag), |
153 result, 1, flags=re.S | 159 result, 1, flags=re.S |
154 ) | 160 ) |
155 result = re.sub( | 161 result = re.sub( |
156 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), | 162 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), |
157 r"<%s>\1</%s>" % (tag, tag), | 163 r"<%s>\1</%s>" % (tag, tag), |
158 result, flags=re.S | 164 result, flags=re.S |
159 ) | 165 ) |
160 return result | 166 return result |
161 | 167 |
162 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): |
163 def lookup_string(match): | 169 def lookup_string(match): |
164 name, comment, default = match.groups() | 170 name, comment, default = match.groups() |
165 default = to_html(default).strip() | 171 default = to_html(default).strip() |
166 | 172 |
167 # Note: We currently ignore the comment, it is only relevant when | 173 # Note: We currently ignore the comment, it is only relevant when |
168 # generating the master translation. | 174 # generating the master translation. |
169 return self.localize_string(name, default, self._params["localedata"], esc
apes) | 175 return self.localize_string(name, default, self._params["localedata"], esc
apes) |
170 | 176 |
171 return re.sub( | 177 return re.sub( |
172 r"\{\{\s*([\w\-]+)(?:\[(.*?)\])?\s+(.*?)\}\}", | 178 r"{{\s*" |
| 179 r"([\w\-]+)" # String ID |
| 180 r"(?:\[(.*?)\])?" # Optional comment |
| 181 r"\s+" |
| 182 r"((?:(?!{{).|" # Translatable text |
| 183 r"{{(?:(?!}}).)*}}" # Nested translation |
| 184 r")*?)" |
| 185 r"}}", |
173 lookup_string, | 186 lookup_string, |
174 text, | 187 text, |
175 flags=re.S | 188 flags=re.S |
176 ) | 189 ) |
177 | 190 |
178 def process_links(self, text): | 191 def process_links(self, text): |
179 def process_link(match): | 192 def process_link(match): |
180 pre, attr, url, post = match.groups() | 193 pre, attr, url, post = match.groups() |
181 url = jinja2.Markup(url).unescape() | 194 url = jinja2.Markup(url).unescape() |
182 | 195 |
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
382 stack.pop() | 395 stack.pop() |
383 stack[-1]["subitems"].append(item) | 396 stack[-1]["subitems"].append(item) |
384 stack.append(item) | 397 stack.append(item) |
385 return structured | 398 return structured |
386 | 399 |
387 converters = { | 400 converters = { |
388 "html": RawConverter, | 401 "html": RawConverter, |
389 "md": MarkdownConverter, | 402 "md": MarkdownConverter, |
390 "tmpl": TemplateConverter, | 403 "tmpl": TemplateConverter, |
391 } | 404 } |
OLD | NEW |