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

Unified Diff: sitescripts/cms/converters.py

Issue 5567002995326976: Multiple CMS improvements (Closed)
Patch Set: Created Dec. 11, 2013, 10:08 a.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 | « sitescripts/cms/bin/test_server.py ('k') | sitescripts/cms/sources.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: sitescripts/cms/converters.py
===================================================================
--- a/sitescripts/cms/converters.py
+++ b/sitescripts/cms/converters.py
@@ -10,17 +10,17 @@
# Adblock Plus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
-import re, jinja2, markdown
+import os, imp, re, jinja2, markdown
from ..utils import get_custom_template_environment
# Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs are
# inserted into the <head> tag
orig_isBlockLevel = markdown.util.isBlockLevel
def isBlockLevel(tag):
if tag == "head":
return True
@@ -145,43 +145,63 @@ class RawConverter(Converter):
def get_html(self, source):
result = self.insert_localized_strings(source, html_escapes)
result = self.process_links(result)
return result
class MarkdownConverter(Converter):
def get_html(self, source):
def remove_unnecessary_entities(match):
- char = chr(int(match.group(1)))
+ char = unichr(int(match.group(1)))
if char in html_escapes:
return match.group(0)
else:
return char
escapes = {}
for char in markdown.Markdown.ESCAPED_CHARS:
escapes[char] = "&#" + str(ord(char)) + ";"
for key, value in html_escapes.iteritems():
escapes[key] = value
+ md = markdown.Markdown(output="html5", extensions=["attr_list"])
+ md.preprocessors["html_block"].markdown_in_raw = True
+
result = self.insert_localized_strings(source, escapes)
- result = markdown.Markdown(output="html5", extensions=["attr_list"]).convert(result)
+ result = md.convert(result)
result = re.sub(r"&#(\d+);", remove_unnecessary_entities, result)
result = self.process_links(result)
return result
class TemplateConverter(Converter):
def __init__(self, *args, **kwargs):
Converter.__init__(self, *args, **kwargs)
filters = {
"translate": self.translate,
"linkify": self.linkify,
"toclist": self.toclist,
}
+
+ for filename in self._params["source"].list_files("filters"):
+ root, ext = os.path.splitext(filename)
+ 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
+ continue
+
+ path = "%s/%s" % ("filters", filename)
+ code = self._params["source"].read_file(path)
+ module = imp.new_module(root.replace("/", "."))
+ exec code in module.__dict__
+
+ func = os.path.basename(root)
+ if func not in module.__dict__:
Sebastian Noack 2013/12/11 11:38:29 You should use the getattr/setattr/hasattr/delattr
+ raise Exception("Expected function %s not found in filter file %s" % (func, filename))
+ filters[func] = module.__dict__[func]
Sebastian Noack 2013/12/11 11:38:29 See above: getattr(module, func)
+ 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
+
self._env = get_custom_template_environment(filters)
def get_html(self, source):
template = self._env.from_string(source)
return template.render(self._params)
def translate(self, name, page=None, links=[]):
if page == None:
« 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