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

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

Issue 17817001: Simple CMS as Anwiki replacement (Closed)
Patch Set: Various improvements Created Oct. 24, 2013, 11:42 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/web/bin/test_server.py ('k') | sitescripts/web/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
(Empty)
1 # coding: utf-8
2
3 # This file is part of the Adblock Plus web scripts,
4 # Copyright (C) 2006-2013 Eyeo GmbH
5 #
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
8 # published by the Free Software Foundation.
9 #
10 # Adblock Plus is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
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/>.
17
18 import re, jinja2, markdown
19 from ..utils import get_custom_template_environment
20
21 # Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs are
22 # inserted into the <head> tag
23 orig_isBlockLevel = markdown.util.isBlockLevel
24 def isBlockLevel(tag):
25 if tag == "head":
26 return True
27 else:
28 return orig_isBlockLevel(tag)
29 markdown.util.isBlockLevel = isBlockLevel
30
31 html_escapes = {
32 "<": "&lt;",
33 ">": "&gt;",
34 "&": "&amp;",
35 "\"": "&quot;",
36 "'": "&#39;",
37 }
38
39 class Converter:
40 def __init__(self, params, key="pagedata"):
41 self._params = params
42 self._key = key
43
44 # Read in any parameters specified at the beginning of the file
45 lines = params[key].splitlines(True)
46 while len(lines) and re.search(r"^\s*[\w\-]+\s*=", lines[0]):
47 name, value = lines.pop(0).split("=", 1)
48 params[name.strip()] = value.strip()
49 params[key] = "".join(lines)
50
51 def insert_localized_strings(self, text, escapes):
52 def escape(s):
53 return re.sub(r".",
54 lambda match: escapes.get(match.group(0), match.group(0)),
55 s, flags=re.S)
56
57 def lookup_string(match):
58 name, links = match.groups()
59 try:
60 result = self._params["localedata"][name].strip()
61 except KeyError:
62 raise Exception("Lookup failed for string %s used on page %s" % (name, s elf._params["page"]))
63
64 result = escape(result)
65 if links:
66 links = map(unicode.strip, links.strip("()").split(","))
67 while len(links):
68 result = re.sub(
69 r"%s(.*?)%s" % (escape("<a>"), escape("</a>")),
70 r'<a href="%s">\1</a>' % links.pop(0),
71 result, 1, flags=re.S
72 )
73 return result
74
75 return re.sub(
76 r"\$([\w\-]+)(\([^()$]+\))?\$",
77 lookup_string,
78 text
79 )
80
81 def process_links(self, text):
82 def process_link(match):
83 pre, attr, url, post = match.groups()
84 url = jinja2.Markup(url).unescape()
85
86 locale, new_url = self._params["source"].resolve_link(url, self._params["l ocale"])
87 if new_url != None:
88 url = new_url
89 if attr == "href":
90 post += ' hreflang="%s"' % jinja2.Markup.escape(locale)
91
92 return "".join((pre, jinja2.Markup.escape(url), post))
93
94 text = re.sub(r"(<a [^<>]*\b(href)=\")([^<>\"]+)(\")", process_link, text)
95 text = re.sub(r"(<img [^<>]*\b(src)=\")([^<>\"]+)(\")", process_link, text)
96 return text
97
98 def __call__(self):
99 result = self.get_html(self._params[self._key])
100 if self._key == "pagedata":
101 head = []
102 def add_to_head(match):
103 head.append(match.group(1))
104 return ""
105 body = re.sub(r"<head>(.*?)</head>", add_to_head, result, flags=re.S)
106 return "".join(head), body
107 else:
108 return result
109
110 class RawConverter(Converter):
111 def get_html(self, source):
112 result = self.insert_localized_strings(source, html_escapes)
113 result = self.process_links(result)
114 return result
115
116 class MarkdownConverter(Converter):
117 def get_html(self, source):
118 def remove_unnecessary_entities(match):
119 char = chr(int(match.group(1)))
120 if char in html_escapes:
121 return match.group(0)
122 else:
123 return char
124
125 escapes = {}
126 for char in markdown.Markdown.ESCAPED_CHARS:
127 escapes[char] = "&#" + str(ord(char)) + ";"
128 for key, value in html_escapes.iteritems():
129 escapes[key] = value
130
131 result = self.insert_localized_strings(source, escapes)
132 result = markdown.Markdown(output="html5", extensions=["attr_list"]).convert (result)
133 result = re.sub(r"&#(\d+);", remove_unnecessary_entities, result)
134 result = self.process_links(result)
135 return result
136
137 class TemplateConverter(Converter):
138 def __init__(self, *args, **kwargs):
139 Converter.__init__(self, *args, **kwargs)
140
141 filters = {
142 "translate": self.translate,
143 "linkify": self.linkify,
144 "toclist": self.toclist,
145 }
146 self._env = get_custom_template_environment(filters)
147
148 def get_html(self, source):
149 template = self._env.from_string(source)
150 return template.render(self._params)
151
152 def translate(self, name, page=None):
153 if page == None:
154 localedata = self._params["localedata"]
155 else:
156 localedata = self._params["source"].read_locale(self._params["locale"], pa ge)
157
158 try:
159 return localedata[name]
160 except KeyError:
161 raise Exception("Lookup failed for string %s used on page %s" % (name, sel f._params["page"]))
162
163 def linkify(self, page, locale=None):
164 if locale == None:
165 locale = self._params["locale"]
166
167 locale, url = self._params["source"].resolve_link(page, locale)
168 return jinja2.Markup('<a href="%s" hreflang="%s">' % (
169 jinja2.Markup.escape(url),
170 jinja2.Markup.escape(locale)
171 ))
172
173 def toclist(self, content):
174 flat = []
175 for match in re.finditer(r'<h(\d) [^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>', content, re.S):
176 flat.append({
177 "level": int(match.group(1)),
178 "anchor": jinja2.Markup(match.group(2)).unescape(),
179 "title": jinja2.Markup(match.group(3)).unescape(),
180 "subitems": [],
181 })
182
183 structured = []
184 stack = [{"level": 0, "subitems": structured}]
185 for item in flat:
186 while stack[-1]["level"] >= item["level"]:
187 stack.pop()
188 stack[-1]["subitems"].append(item)
189 stack.append(item)
190 return structured
191
192 converters = {
193 "raw": RawConverter,
194 "md": MarkdownConverter,
195 "tmpl": TemplateConverter,
196 }
OLDNEW
« no previous file with comments | « sitescripts/web/bin/test_server.py ('k') | sitescripts/web/sources.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld