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

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

Issue 5352791199449088: Added support for adding addional HTML attributes to the "linkify" template filter (Closed)
Patch Set: Created Feb. 4, 2014, 1:55 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
« no previous file with comments | « no previous file | no next file » | 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,
(...skipping 221 matching lines...) Expand 10 before | Expand all | Expand 10 after
232 template = self._env.from_string(source) 232 template = self._env.from_string(source)
233 return template.render(self._params) 233 return template.render(self._params)
234 234
235 def translate(self, name, page=None, links=[]): 235 def translate(self, name, page=None, links=[]):
236 if page == None: 236 if page == None:
237 localedata = self._params["localedata"] 237 localedata = self._params["localedata"]
238 else: 238 else:
239 localedata = self._params["source"].read_locale(self._params["locale"], pa ge) 239 localedata = self._params["source"].read_locale(self._params["locale"], pa ge)
240 return jinja2.Markup(self.localize_string(name, localedata, html_escapes, li nks=links)) 240 return jinja2.Markup(self.localize_string(name, localedata, html_escapes, li nks=links))
241 241
242 def linkify(self, page, locale=None): 242 def linkify(self, page, locale=None, **attrs):
243 if locale == None: 243 if locale == None:
244 locale = self._params["locale"] 244 locale = self._params["locale"]
245 245
246 locale, url = self._params["source"].resolve_link(page, locale) 246 locale, url = self._params["source"].resolve_link(page, locale)
247 return jinja2.Markup('<a href="%s" hreflang="%s">' % ( 247 return jinja2.Markup('<a%s>' % ''.join(
248 jinja2.Markup.escape(url), 248 ' %s="%s"' % (name, jinja2.escape(value)) for name, value in [
249 jinja2.Markup.escape(locale) 249 ('href', url),
250 ('hreflang', locale)
251 ] + attrs.items()
250 )) 252 ))
251 253
252 def toclist(self, content): 254 def toclist(self, content):
253 flat = [] 255 flat = []
254 for match in re.finditer(r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>' , content, re.S): 256 for match in re.finditer(r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>' , content, re.S):
255 flat.append({ 257 flat.append({
256 "level": int(match.group(1)), 258 "level": int(match.group(1)),
257 "anchor": jinja2.Markup(match.group(2)).unescape(), 259 "anchor": jinja2.Markup(match.group(2)).unescape(),
258 "title": jinja2.Markup(match.group(3)).unescape(), 260 "title": jinja2.Markup(match.group(3)).unescape(),
259 "subitems": [], 261 "subitems": [],
260 }) 262 })
261 263
262 structured = [] 264 structured = []
263 stack = [{"level": 0, "subitems": structured}] 265 stack = [{"level": 0, "subitems": structured}]
264 for item in flat: 266 for item in flat:
265 while stack[-1]["level"] >= item["level"]: 267 while stack[-1]["level"] >= item["level"]:
266 stack.pop() 268 stack.pop()
267 stack[-1]["subitems"].append(item) 269 stack[-1]["subitems"].append(item)
268 stack.append(item) 270 stack.append(item)
269 return structured 271 return structured
270 272
271 converters = { 273 converters = {
272 "raw": RawConverter, 274 "raw": RawConverter,
273 "md": MarkdownConverter, 275 "md": MarkdownConverter,
274 "tmpl": TemplateConverter, 276 "tmpl": TemplateConverter,
275 } 277 }
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld