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

Delta Between Two Patch Sets: cms/converters.py

Issue 5144941147717632: Issue 2112 - Support fixed text in strings. (Closed)
Left Patch Set: Created March 26, 2015, 4:54 p.m.
Right Patch Set: Avoid escaping search string for fixed strings. Created March 27, 2015, 9:49 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « no previous file | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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 43 matching lines...) Expand 10 before | Expand all | Expand 10 after
54 def parse(self, text, pagename): 54 def parse(self, text, pagename):
55 self.reset() 55 self.reset()
56 self._string = [] 56 self._string = []
57 self._fixed_strings = [] 57 self._fixed_strings = []
58 self._inside_fixed = False 58 self._inside_fixed = False
59 self._attrs = {} 59 self._attrs = {}
60 self._pagename = pagename 60 self._pagename = pagename
61 61
62 try: 62 try:
63 self.feed(text) 63 self.feed(text)
64 return "".join(self._string), self._attrs, map(lambda s: "".join(s), self. _fixed_strings) 64 return "".join(self._string), self._attrs, ["".join(s) for s in self._fixe d_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.
65 finally: 65 finally:
66 self._string = None 66 self._string = None
67 self._attrs = None 67 self._attrs = None
68 self._pagename = None 68 self._pagename = None
69 self._inside_fixed = False 69 self._inside_fixed = False
70 self._fixed_strings = None 70 self._fixed_strings = None
71 71
72 def handle_starttag(self, tag, attrs): 72 def handle_starttag(self, tag, attrs):
73 if tag not in self._whitelist: 73 if self._inside_fixed:
Wladimir Palant 2015/03/26 20:48:56 No tags should be allowed inside <fix> - meaning |
kzar 2015/03/26 22:11:32 Done.
74 raise Exception("Unexpected HTML tag '%s' in localizable string on page %s " % (tag, self._pagename)) 74 raise Exception("Unexpected HTML tag '%s' inside a fixed string on page %s " % (tag, self._pagename))
75 if tag == "fix": 75 elif tag == "fix":
76 self._inside_fixed = True 76 self._inside_fixed = True
77 self._fixed_strings.append([]) 77 self._fixed_strings.append([])
78 else: 78 elif tag in self._whitelist:
79 self._attrs.setdefault(tag, []).append(attrs) 79 self._attrs.setdefault(tag, []).append(attrs)
80 self._string.append("<%s>" % tag) 80 self._string.append("<%s>" % tag)
81 else:
82 raise Exception("Unexpected HTML tag '%s' in localizable string on page %s " % (tag, self._pagename))
81 83
82 def handle_endtag(self, tag): 84 def handle_endtag(self, tag):
83 if tag == "fix": 85 if tag == "fix":
84 self._string.append("{%d}" % len(self._fixed_strings)) 86 self._string.append("{%d}" % len(self._fixed_strings))
85 self._inside_fixed = False 87 self._inside_fixed = False
86 else: 88 else:
87 self._string.append("</%s>" % tag) 89 self._string.append("</%s>" % tag)
88 90
89 def _append_string(self, s): 91 def _append_text(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.
90 if self._inside_fixed: 92 if self._inside_fixed:
91 self._fixed_strings[-1].append(s) 93 self._fixed_strings[-1].append(s)
92 else: 94 else:
93 self._string.append(s) 95 self._string.append(s)
94 96
95 def handle_data(self, data): 97 def handle_data(self, data):
96 # Note: lack of escaping here is intentional. The result is a locale string, 98 # Note: lack of escaping here is intentional. The result is a locale string,
97 # HTML escaping is applied when this string is inserted into the document. 99 # HTML escaping is applied when this string is inserted into the document.
98 self._append_string(data) 100 self._append_text(data)
99 101
100 def handle_entityref(self, name): 102 def handle_entityref(self, name):
101 self._append_string(self.unescape("&%s;" % name)) 103 self._append_text(self.unescape("&%s;" % name))
102 104
103 def handle_charref(self, name): 105 def handle_charref(self, name):
104 self._append_string(self.unescape("&#%s;" % name)) 106 self._append_text(self.unescape("&#%s;" % name))
105 107
106 class Converter: 108 class Converter:
107 whitelist = set(["a", "em", "fix", "strong"]) 109 whitelist = set(["a", "em", "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.
108 110
109 def __init__(self, params, key="pagedata"): 111 def __init__(self, params, key="pagedata"):
110 self._params = params 112 self._params = params
111 self._key = key 113 self._key = key
112 self._attribute_parser = AttributeParser(self.whitelist) 114 self._attribute_parser = AttributeParser(self.whitelist)
113 115
114 # Read in any parameters specified at the beginning of the file 116 # Read in any parameters specified at the beginning of the file
115 lines = params[key].splitlines(True) 117 lines = params[key].splitlines(True)
116 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): 118 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]):
117 name, value = lines.pop(0).split("=", 1) 119 name, value = lines.pop(0).split("=", 1)
(...skipping 12 matching lines...) Expand all
130 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"])
131 133
132 # Get translation 134 # Get translation
133 if self._params["locale"] != self._params["defaultlocale"] and name in local edata: 135 if self._params["locale"] != self._params["defaultlocale"] and name in local edata:
134 result = localedata[name].strip() 136 result = localedata[name].strip()
135 else: 137 else:
136 result = default 138 result = default
137 139
138 # Insert fixed strings 140 # Insert fixed strings
139 for i in range(len(fixed_strings)): 141 for i in range(len(fixed_strings)):
140 result = re.sub(re_escape("{%d}" % (i + 1)), fixed_strings[i], result, 1) 142 result = re.sub(r"\{%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
141 143
142 # Insert attributes 144 # Insert attributes
143 result = escape(result) 145 result = escape(result)
144 for tag in self.whitelist: 146 for tag in self.whitelist:
145 saved = saved_attributes.get(tag, []) 147 saved = saved_attributes.get(tag, [])
146 for attrs in saved: 148 for attrs in saved:
147 attrs = map(lambda (name, value): '%s="%s"' % (escape(name), escape(valu e)), attrs) 149 attrs = map(lambda (name, value): '%s="%s"' % (escape(name), escape(valu e)), attrs)
148 result = re.sub( 150 result = re.sub(
149 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)) , 151 r"%s([^<>]*?)%s" % (re_escape("<%s>" % tag), re_escape("</%s>" % tag)) ,
150 r'<%s %s>\1</%s>' % (tag, " ".join(attrs), tag), 152 r'<%s %s>\1</%s>' % (tag, " ".join(attrs), tag),
(...skipping 218 matching lines...) Expand 10 before | Expand all | Expand 10 after
369 stack.pop() 371 stack.pop()
370 stack[-1]["subitems"].append(item) 372 stack[-1]["subitems"].append(item)
371 stack.append(item) 373 stack.append(item)
372 return structured 374 return structured
373 375
374 converters = { 376 converters = {
375 "html": RawConverter, 377 "html": RawConverter,
376 "md": MarkdownConverter, 378 "md": MarkdownConverter,
377 "tmpl": TemplateConverter, 379 "tmpl": TemplateConverter,
378 } 380 }
LEFTRIGHT
« no previous file | no next file » | Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Toggle Comments ('s')

Powered by Google App Engine
This is Rietveld