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

Side by Side Diff: cms/converters.py

Issue 5242593268989952: Issue 2340 - Don`t generate pages if less than 30% have been translated (Closed)
Patch Set: Don`t link to pages that don`t exist Created May 6, 2015, 3: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
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 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 self._append_text(data) 104 self._append_text(data)
105 105
106 def handle_entityref(self, name): 106 def handle_entityref(self, name):
107 self._append_text(self.unescape("&%s;" % name)) 107 self._append_text(self.unescape("&%s;" % name))
108 108
109 def handle_charref(self, name): 109 def handle_charref(self, name):
110 self._append_text(self.unescape("&#%s;" % name)) 110 self._append_text(self.unescape("&#%s;" % name))
111 111
112 class Converter: 112 class Converter:
113 whitelist = {"a", "em", "strong", "code", "span"} 113 whitelist = {"a", "em", "strong", "code", "span"}
114 missing_translations = 0
115 total_translations = 0
114 116
115 def __init__(self, params, key="pagedata"): 117 def __init__(self, params, key="pagedata"):
116 self._params = params 118 self._params = params
117 self._key = key 119 self._key = key
118 self._attribute_parser = AttributeParser(self.whitelist) 120 self._attribute_parser = AttributeParser(self.whitelist)
119 121
120 # Read in any parameters specified at the beginning of the file 122 # Read in any parameters specified at the beginning of the file
121 lines = params[key].splitlines(True) 123 lines = params[key].splitlines(True)
122 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]): 124 while lines and re.search(r"^\s*[\w\-]+\s*=", lines[0]):
123 name, value = lines.pop(0).split("=", 1) 125 name, value = lines.pop(0).split("=", 1)
124 params[name.strip()] = value.strip() 126 params[name.strip()] = value.strip()
125 params[key] = "".join(lines) 127 params[key] = "".join(lines)
126 128
127 def localize_string(self, name, default, localedata, escapes): 129 def localize_string(self, name, default, localedata, escapes):
128 def escape(s): 130 def escape(s):
129 return re.sub(r".", 131 return re.sub(r".",
130 lambda match: escapes.get(match.group(0), match.group(0)), 132 lambda match: escapes.get(match.group(0), match.group(0)),
131 s, flags=re.S) 133 s, flags=re.S)
132 def re_escape(s): 134 def re_escape(s):
133 return re.escape(escape(s)) 135 return re.escape(escape(s))
134 136
135 # Extract tag attributes from default string 137 # Extract tag attributes from default string
136 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"]) 138 default, saved_attributes, fixed_strings = self._attribute_parser.parse(defa ult, self._params["page"])
137 139
138 # Get translation 140 # Get translation
139 if self._params["locale"] != self._params["defaultlocale"] and name in local edata: 141 locale = self._params["locale"]
142 if locale == self._params["defaultlocale"]:
143 result = default
144 elif name in localedata:
140 result = localedata[name].strip() 145 result = localedata[name].strip()
141 else: 146 else:
142 result = default 147 result = default
148 self.missing_translations += 1
149 self.total_translations += 1
143 150
144 # Insert fixed strings 151 # Insert fixed strings
145 for i in range(len(fixed_strings)): 152 for i in range(len(fixed_strings)):
146 result = re.sub(r"\{%d\}" % (i + 1), fixed_strings[i], result, 1) 153 result = re.sub(r"\{%d\}" % (i + 1), fixed_strings[i], result, 1)
147 154
148 # Insert attributes 155 # Insert attributes
149 result = escape(result) 156 result = escape(result)
150 def stringify_attribute((name, value)): 157 def stringify_attribute((name, value)):
151 return '%s="%s"' % ( 158 return '%s="%s"' % (
152 escape(name), 159 escape(name),
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
213 include_end_regex = '>' 220 include_end_regex = '>'
214 221
215 def resolve_includes(self, text): 222 def resolve_includes(self, text):
216 def resolve_include(match): 223 def resolve_include(match):
217 global converters 224 global converters
218 name = match.group(1) 225 name = match.group(1)
219 for format, converter_class in converters.iteritems(): 226 for format, converter_class in converters.iteritems():
220 if self._params["source"].has_include(name, format): 227 if self._params["source"].has_include(name, format):
221 self._params["includedata"] = self._params["source"].read_include(name , format) 228 self._params["includedata"] = self._params["source"].read_include(name , format)
222 converter = converter_class(self._params, key="includedata") 229 converter = converter_class(self._params, key="includedata")
223 return converter() 230 result = converter()
231 self.missing_translations += converter.missing_translations
232 self.total_translations += converter.total_translations
233 return result
224 raise Exception("Failed to resolve include %s on page %s" % (name, self._p arams["page"])) 234 raise Exception("Failed to resolve include %s on page %s" % (name, self._p arams["page"]))
225 235
226 return re.sub( 236 return re.sub(
227 r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % ( 237 r'%s\?\s*include\s+([^\s<>"]+)\s*\?%s' % (
228 self.include_start_regex, 238 self.include_start_regex,
229 self.include_end_regex 239 self.include_end_regex
230 ), 240 ),
231 resolve_include, 241 resolve_include,
232 text 242 text
233 ) 243 )
(...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after
397 stack.pop() 407 stack.pop()
398 stack[-1]["subitems"].append(item) 408 stack[-1]["subitems"].append(item)
399 stack.append(item) 409 stack.append(item)
400 return structured 410 return structured
401 411
402 converters = { 412 converters = {
403 "html": RawConverter, 413 "html": RawConverter,
404 "md": MarkdownConverter, 414 "md": MarkdownConverter,
405 "tmpl": TemplateConverter, 415 "tmpl": TemplateConverter,
406 } 416 }
OLDNEW

Powered by Google App Engine
This is Rietveld