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: Rebased on top of Wladimir's changes for link resolving. Created April 21, 2015, 4:19 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 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
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
Wladimir Palant 2015/04/22 13:52:24 Yes, this line isn't very readable now. How about
kzar 2015/04/22 14:46:06 Nice, that looks much better and still seems to wo
183 r"\}\}",
173 lookup_string, 184 lookup_string,
174 text, 185 text,
175 flags=re.S 186 flags=re.S
176 ) 187 )
177 188
178 def process_links(self, text): 189 def process_links(self, text):
179 def process_link(match): 190 def process_link(match):
180 pre, attr, url, post = match.groups() 191 pre, attr, url, post = match.groups()
181 url = jinja2.Markup(url).unescape() 192 url = jinja2.Markup(url).unescape()
182 193
(...skipping 199 matching lines...) Expand 10 before | Expand all | Expand 10 after
382 stack.pop() 393 stack.pop()
383 stack[-1]["subitems"].append(item) 394 stack[-1]["subitems"].append(item)
384 stack.append(item) 395 stack.append(item)
385 return structured 396 return structured
386 397
387 converters = { 398 converters = {
388 "html": RawConverter, 399 "html": RawConverter,
389 "md": MarkdownConverter, 400 "md": MarkdownConverter,
390 "tmpl": TemplateConverter, 401 "tmpl": TemplateConverter,
391 } 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