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

Side by Side Diff: cms/converters.py

Issue 4802066526502912: Issue 2323 - Make sure to always resolve links in translatable strings (Closed)
Patch Set: Added hreflang attribute Created April 15, 2015, 7:23 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 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
125 return re.sub(r".", 125 return re.sub(r".",
126 lambda match: escapes.get(match.group(0), match.group(0)), 126 lambda match: escapes.get(match.group(0), match.group(0)),
127 s, flags=re.S) 127 s, flags=re.S)
128 def re_escape(s): 128 def re_escape(s):
129 return re.escape(escape(s)) 129 return re.escape(escape(s))
130 130
131 # Extract tag attributes from default string 131 # Extract tag attributes from default string
132 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"]) 132 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"])
133 133
134 # Get translation 134 # Get translation
135 if self._params["locale"] != self._params["defaultlocale"] and name in local edata: 135 locale = self._params["locale"]
136 if locale != self._params["defaultlocale"] and name in localedata:
136 result = localedata[name].strip() 137 result = localedata[name].strip()
137 else: 138 else:
138 result = default 139 result = default
139 140
140 # Insert fixed strings 141 # Insert fixed strings
141 for i in range(len(fixed_strings)): 142 for i in range(len(fixed_strings)):
142 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)
143 144
144 # Insert attributes 145 # Insert attributes
145 result = escape(result) 146 result = escape(result)
147 def stringify_attribute((name, value)):
148 if name == "href":
149 link_locale, link = self._params["source"].resolve_link(value, locale)
150 if link:
151 return 'href="%s" hreflang="%s"' % (escape(link), escape(link_locale))
152 return '%s="%s"' % (escape(name), escape(value))
153
146 for tag in self.whitelist: 154 for tag in self.whitelist:
147 saved = saved_attributes.get(tag, []) 155 saved = saved_attributes.get(tag, [])
148 for attrs in saved: 156 for attrs in saved:
149 attrs = map(lambda (name, value): '%s="%s"' % (escape(name), escape(valu e)), attrs) 157 attrs = map(stringify_attribute, attrs)
150 result = re.sub( 158 result = re.sub(
151 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)) , 159 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)) ,
152 r'<%s %s>\1</%s>' % (tag, " ".join(attrs), tag), 160 r'<%s %s>\1</%s>' % (tag, " ".join(attrs), tag),
153 result, 1, flags=re.S 161 result, 1, flags=re.S
154 ) 162 )
155 result = re.sub( 163 result = re.sub(
156 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)), 164 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)),
157 r"<%s>\1</%s>" % (tag, tag), 165 r"<%s>\1</%s>" % (tag, tag),
158 result, flags=re.S 166 result, flags=re.S
159 ) 167 )
(...skipping 219 matching lines...) Expand 10 before | Expand all | Expand 10 after
379 stack.pop() 387 stack.pop()
380 stack[-1]["subitems"].append(item) 388 stack[-1]["subitems"].append(item)
381 stack.append(item) 389 stack.append(item)
382 return structured 390 return structured
383 391
384 converters = { 392 converters = {
385 "html": RawConverter, 393 "html": RawConverter,
386 "md": MarkdownConverter, 394 "md": MarkdownConverter,
387 "tmpl": TemplateConverter, 395 "tmpl": TemplateConverter,
388 } 396 }
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