Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Side by Side Diff: cms/converters.py

Issue 6440550915899392: Issue 2139 - Allow nested translations for tag attributes. (Closed)
Patch Set: Use complex regexp instead of keeping track of nested levels ourselves. Created April 21, 2015, 2:43 p.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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
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)
(...skipping 12 matching lines...) Expand all
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 def lookup_string(match):
172 name, comment, default = match.groups() 173 name, comment, default = match.groups()
173 default = to_html(default).strip() 174 default = to_html(default).strip()
174 175
175 # Note: We currently ignore the comment, it is only relevant when 176 # Note: We currently ignore the comment, it is only relevant when
176 # generating the master translation. 177 # generating the master translation.
177 return self.localize_string(name, default, self._params["localedata"], esc apes) 178 return self.localize_string(name, default, self._params["localedata"], esc apes)
178 179
179 return re.sub( 180 return re.sub(
180 r"\{\{\s*([\w\-]+)(?:\[(.*?)\])?\s+(.*?)\}\}", 181 r"\{\{\s*"
182 r"([\w\-]+)" # String ID
183 r"(?:\[(.*?)\])?" # Optional comment
184 r"\s+"
185 r"((?:[^\{]|\{(?!\{)|\{\{(?:[^\}]|\}(?!\}))*?\}\})*?)" # Translatable text
186 r"\}\}",
181 lookup_string, 187 lookup_string,
182 text, 188 text,
183 flags=re.S 189 flags=re.S
184 ) 190 )
185 191
186 def process_links(self, text): 192 def process_links(self, text):
187 def process_link(match): 193 def process_link(match):
188 pre, attr, url, post = match.groups() 194 pre, attr, url, post = match.groups()
189 url = jinja2.Markup(url).unescape() 195 url = jinja2.Markup(url).unescape()
190 196
(...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
387 stack.pop() 393 stack.pop()
388 stack[-1]["subitems"].append(item) 394 stack[-1]["subitems"].append(item)
389 stack.append(item) 395 stack.append(item)
390 return structured 396 return structured
391 397
392 converters = { 398 converters = {
393 "html": RawConverter, 399 "html": RawConverter,
394 "md": MarkdownConverter, 400 "md": MarkdownConverter,
395 "tmpl": TemplateConverter, 401 "tmpl": TemplateConverter,
396 } 402 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld