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

Unified Diff: cms/converters.py

Issue 5144941147717632: Issue 2112 - Support fixed text in strings. (Closed)
Patch Set: Created March 26, 2015, 4:54 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cms/converters.py
diff --git a/cms/converters.py b/cms/converters.py
index 4c50beb658a8a6dcc7aff70aa90b563bf5ca94b4..a2d44d92851d22d81b56d0827efd370b7d05518d 100644
--- a/cms/converters.py
+++ b/cms/converters.py
@@ -44,6 +44,8 @@ html_escapes = {
class AttributeParser(HTMLParser.HTMLParser):
_string = None
+ _inside_fixed = False
+ _fixed_strings = None
_attrs = None
def __init__(self, whitelist):
@@ -52,39 +54,57 @@ class AttributeParser(HTMLParser.HTMLParser):
def parse(self, text, pagename):
self.reset()
self._string = []
+ self._fixed_strings = []
+ self._inside_fixed = False
self._attrs = {}
self._pagename = pagename
try:
self.feed(text)
- return "".join(self._string), self._attrs
+ return "".join(self._string), self._attrs, map(lambda s: "".join(s), self._fixed_strings)
Wladimir Palant 2015/03/26 20:48:56 Nit: ["".join(s) for s in self._fixed_strings] is
kzar 2015/03/26 22:11:32 Done.
finally:
self._string = None
self._attrs = None
self._pagename = None
+ self._inside_fixed = False
+ self._fixed_strings = None
def handle_starttag(self, tag, attrs):
if tag not in self._whitelist:
Wladimir Palant 2015/03/26 20:48:56 No tags should be allowed inside <fix> - meaning |
kzar 2015/03/26 22:11:32 Done.
raise Exception("Unexpected HTML tag '%s' in localizable string on page %s" % (tag, self._pagename))
- self._attrs.setdefault(tag, []).append(attrs)
- self._string.append("<%s>" % tag)
+ if tag == "fix":
+ self._inside_fixed = True
+ self._fixed_strings.append([])
+ else:
+ self._attrs.setdefault(tag, []).append(attrs)
+ self._string.append("<%s>" % tag)
def handle_endtag(self, tag):
- self._string.append("</%s>" % tag)
+ if tag == "fix":
+ self._string.append("{%d}" % len(self._fixed_strings))
+ self._inside_fixed = False
+ else:
+ self._string.append("</%s>" % tag)
+
+ def _append_string(self, s):
Wladimir Palant 2015/03/26 20:48:56 Nit: we are adding to self._string a lot, this hel
kzar 2015/03/26 22:11:32 Done.
+ if self._inside_fixed:
+ self._fixed_strings[-1].append(s)
+ else:
+ self._string.append(s)
def handle_data(self, data):
# Note: lack of escaping here is intentional. The result is a locale string,
# HTML escaping is applied when this string is inserted into the document.
- self._string.append(data)
+ self._append_string(data)
def handle_entityref(self, name):
- self._string.append(self.unescape("&%s;" % name))
+ self._append_string(self.unescape("&%s;" % name))
def handle_charref(self, name):
- self._string.append(self.unescape("&#%s;" % name))
+ self._append_string(self.unescape("&#%s;" % name))
class Converter:
- whitelist = set(["a", "em", "strong"])
+ whitelist = set(["a", "em", "fix", "strong"])
Wladimir Palant 2015/03/26 20:48:56 It makes no sense to change this set - <fix> isn't
kzar 2015/03/26 22:11:32 Done.
def __init__(self, params, key="pagedata"):
self._params = params
@@ -107,7 +127,7 @@ class Converter:
return re.escape(escape(s))
# Extract tag attributes from default string
- default, saved_attributes = self._attribute_parser.parse(default, self._params["page"])
+ default, saved_attributes, fixed_strings = self._attribute_parser.parse(default, self._params["page"])
# Get translation
if self._params["locale"] != self._params["defaultlocale"] and name in localedata:
@@ -115,6 +135,10 @@ class Converter:
else:
result = default
+ # Insert fixed strings
+ for i in range(len(fixed_strings)):
+ result = re.sub(re_escape("{%d}" % (i + 1)), fixed_strings[i], result, 1)
Wladimir Palant 2015/03/26 20:48:56 Escape the string being inserted? It looks like cu
kzar 2015/03/26 22:11:32 I originally did escape fixed_strings[i] here but
Wladimir Palant 2015/03/27 07:29:48 I see, you are inserting them before the escaping
+
# Insert attributes
result = escape(result)
for tag in self.whitelist:
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld