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

Side by Side Diff: sitescripts/cms/converters.py

Issue 5567002995326976: Multiple CMS improvements (Closed)
Patch Set: Created Dec. 11, 2013, 10:08 a.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 | « sitescripts/cms/bin/test_server.py ('k') | sitescripts/cms/sources.py » ('j') | 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-2013 Eyeo GmbH 4 # Copyright (C) 2006-2013 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,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of 11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details. 13 # GNU General Public License for more details.
14 # 14 #
15 # You should have received a copy of the GNU General Public License 15 # You should have received a copy of the GNU General Public License
16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 16 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
17 17
18 import re, jinja2, markdown 18 import os, imp, re, jinja2, markdown
19 from ..utils import get_custom_template_environment 19 from ..utils import get_custom_template_environment
20 20
21 # Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs are 21 # Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs are
22 # inserted into the <head> tag 22 # inserted into the <head> tag
23 orig_isBlockLevel = markdown.util.isBlockLevel 23 orig_isBlockLevel = markdown.util.isBlockLevel
24 def isBlockLevel(tag): 24 def isBlockLevel(tag):
25 if tag == "head": 25 if tag == "head":
26 return True 26 return True
27 else: 27 else:
28 return orig_isBlockLevel(tag) 28 return orig_isBlockLevel(tag)
(...skipping 114 matching lines...) Expand 10 before | Expand all | Expand 10 after
143 143
144 class RawConverter(Converter): 144 class RawConverter(Converter):
145 def get_html(self, source): 145 def get_html(self, source):
146 result = self.insert_localized_strings(source, html_escapes) 146 result = self.insert_localized_strings(source, html_escapes)
147 result = self.process_links(result) 147 result = self.process_links(result)
148 return result 148 return result
149 149
150 class MarkdownConverter(Converter): 150 class MarkdownConverter(Converter):
151 def get_html(self, source): 151 def get_html(self, source):
152 def remove_unnecessary_entities(match): 152 def remove_unnecessary_entities(match):
153 char = chr(int(match.group(1))) 153 char = unichr(int(match.group(1)))
154 if char in html_escapes: 154 if char in html_escapes:
155 return match.group(0) 155 return match.group(0)
156 else: 156 else:
157 return char 157 return char
158 158
159 escapes = {} 159 escapes = {}
160 for char in markdown.Markdown.ESCAPED_CHARS: 160 for char in markdown.Markdown.ESCAPED_CHARS:
161 escapes[char] = "&#" + str(ord(char)) + ";" 161 escapes[char] = "&#" + str(ord(char)) + ";"
162 for key, value in html_escapes.iteritems(): 162 for key, value in html_escapes.iteritems():
163 escapes[key] = value 163 escapes[key] = value
164 164
165 md = markdown.Markdown(output="html5", extensions=["attr_list"])
166 md.preprocessors["html_block"].markdown_in_raw = True
167
165 result = self.insert_localized_strings(source, escapes) 168 result = self.insert_localized_strings(source, escapes)
166 result = markdown.Markdown(output="html5", extensions=["attr_list"]).convert (result) 169 result = md.convert(result)
167 result = re.sub(r"&#(\d+);", remove_unnecessary_entities, result) 170 result = re.sub(r"&#(\d+);", remove_unnecessary_entities, result)
168 result = self.process_links(result) 171 result = self.process_links(result)
169 return result 172 return result
170 173
171 class TemplateConverter(Converter): 174 class TemplateConverter(Converter):
172 def __init__(self, *args, **kwargs): 175 def __init__(self, *args, **kwargs):
173 Converter.__init__(self, *args, **kwargs) 176 Converter.__init__(self, *args, **kwargs)
174 177
175 filters = { 178 filters = {
176 "translate": self.translate, 179 "translate": self.translate,
177 "linkify": self.linkify, 180 "linkify": self.linkify,
178 "toclist": self.toclist, 181 "toclist": self.toclist,
179 } 182 }
183
184 for filename in self._params["source"].list_files("filters"):
185 root, ext = os.path.splitext(filename)
186 if ext != ".py":
Sebastian Noack 2013/12/11 11:38:29 You might want to compare the file extension case-
Wladimir Palant 2013/12/11 12:05:19 Then we should do the same everywhere else where w
187 continue
188
189 path = "%s/%s" % ("filters", filename)
190 code = self._params["source"].read_file(path)
191 module = imp.new_module(root.replace("/", "."))
192 exec code in module.__dict__
193
194 func = os.path.basename(root)
195 if func not in module.__dict__:
Sebastian Noack 2013/12/11 11:38:29 You should use the getattr/setattr/hasattr/delattr
196 raise Exception("Expected function %s not found in filter file %s" % (fu nc, filename))
197 filters[func] = module.__dict__[func]
Sebastian Noack 2013/12/11 11:38:29 See above: getattr(module, func)
198 filters[func].__module__ = module # Prevent garbage collection
Sebastian Noack 2013/12/11 11:38:29 The __module__ attribute of objects is supposed to
Wladimir Palant 2013/12/11 12:05:19 Ok, clashing with a predefined property was uninte
199
180 self._env = get_custom_template_environment(filters) 200 self._env = get_custom_template_environment(filters)
181 201
182 def get_html(self, source): 202 def get_html(self, source):
183 template = self._env.from_string(source) 203 template = self._env.from_string(source)
184 return template.render(self._params) 204 return template.render(self._params)
185 205
186 def translate(self, name, page=None, links=[]): 206 def translate(self, name, page=None, links=[]):
187 if page == None: 207 if page == None:
188 localedata = self._params["localedata"] 208 localedata = self._params["localedata"]
189 else: 209 else:
(...skipping 27 matching lines...) Expand all
217 stack.pop() 237 stack.pop()
218 stack[-1]["subitems"].append(item) 238 stack[-1]["subitems"].append(item)
219 stack.append(item) 239 stack.append(item)
220 return structured 240 return structured
221 241
222 converters = { 242 converters = {
223 "raw": RawConverter, 243 "raw": RawConverter,
224 "md": MarkdownConverter, 244 "md": MarkdownConverter,
225 "tmpl": TemplateConverter, 245 "tmpl": TemplateConverter,
226 } 246 }
OLDNEW
« no previous file with comments | « sitescripts/cms/bin/test_server.py ('k') | sitescripts/cms/sources.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld