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

Side by Side Diff: cms/converters.py

Issue 29495555: Fixes 5343 - add global function: get_canonical_url (Closed)
Patch Set: Created July 23, 2017, 4:35 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 # This file is part of the Adblock Plus web scripts, 1 # This file is part of the Adblock Plus web scripts,
2 # Copyright (C) 2006-2017 eyeo GmbH 2 # Copyright (C) 2006-2017 eyeo GmbH
3 # 3 #
4 # Adblock Plus is free software: you can redistribute it and/or modify 4 # Adblock Plus is free software: you can redistribute it and/or modify
5 # it under the terms of the GNU General Public License version 3 as 5 # it under the terms of the GNU General Public License version 3 as
6 # published by the Free Software Foundation. 6 # published by the Free Software Foundation.
7 # 7 #
8 # Adblock Plus is distributed in the hope that it will be useful, 8 # Adblock Plus is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of 9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details. 11 # GNU General Public License for more details.
12 # 12 #
13 # You should have received a copy of the GNU General Public License 13 # You should have received a copy of the GNU General Public License
14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>. 14 # along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
15 15
16 from __future__ import unicode_literals 16 from __future__ import unicode_literals
17 17
18 import os 18 import os
19 import HTMLParser 19 import HTMLParser
20 import re 20 import re
21 import urlparse
21 22
22 import jinja2 23 import jinja2
23 import markdown 24 import markdown
24 25
25 26
26 # Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs 27 # Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs
27 # are inserted into the <head> tag 28 # are inserted into the <head> tag
28 orig_isBlockLevel = markdown.util.isBlockLevel 29 orig_isBlockLevel = markdown.util.isBlockLevel
29 30
30 31
(...skipping 358 matching lines...) Expand 10 before | Expand all | Expand 10 after
389 'translate': self.translate, 390 'translate': self.translate,
390 'linkify': self.linkify, 391 'linkify': self.linkify,
391 'toclist': self.toclist, 392 'toclist': self.toclist,
392 } 393 }
393 394
394 globals = { 395 globals = {
395 'get_string': self.get_string, 396 'get_string': self.get_string,
396 'has_string': self.has_string, 397 'has_string': self.has_string,
397 'get_page_content': self.get_page_content, 398 'get_page_content': self.get_page_content,
398 'get_pages_metadata': self.get_pages_metadata, 399 'get_pages_metadata': self.get_pages_metadata,
400 'get_page_url': self.get_page_url,
399 } 401 }
400 402
401 for dirname, dictionary in [('filters', filters), 403 for dirname, dictionary in [('filters', filters),
402 ('globals', globals)]: 404 ('globals', globals)]:
403 for filename in self._params['source'].list_files(dirname): 405 for filename in self._params['source'].list_files(dirname):
404 root, ext = os.path.splitext(filename) 406 root, ext = os.path.splitext(filename)
405 if ext.lower() != '.py': 407 if ext.lower() != '.py':
406 continue 408 continue
407 409
408 path = os.path.join(dirname, filename) 410 path = os.path.join(dirname, filename)
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
510 if isinstance(metadata[filter_name], list): 512 if isinstance(metadata[filter_name], list):
511 if isinstance(filter_value, basestring): 513 if isinstance(filter_value, basestring):
512 filter_value = [filter_value] 514 filter_value = [filter_value]
513 for option in filter_value: 515 for option in filter_value:
514 if str(option) not in metadata[filter_name]: 516 if str(option) not in metadata[filter_name]:
515 return False 517 return False
516 elif filter_value != metadata[filter_name]: 518 elif filter_value != metadata[filter_name]:
517 return False 519 return False
518 return True 520 return True
519 521
522 def get_page_url(self, page, locale='default'):
523 """Return URL of `page` in given `locale`
524
525 By default current locale will be used. When `locale` is set to `None`,
526 the canonical URL for the page (with no locale component) will be
527 returned.
528 """
529 try:
530 base_url = self._params['site_url']
531 except KeyError:
532 raise Exception('You must configure siteurl to use `get_page_url`')
533
534 if locale == 'default':
535 locale = self._params['locale']
536 if locale is not None:
537 base_url = urlparse.urljoin(base_url, locale + '/')
538
539 return urlparse.urljoin(base_url, page)
540
520 def toclist(self, content): 541 def toclist(self, content):
521 toc_re = r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>' 542 toc_re = r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>'
522 flat = [] 543 flat = []
523 for match in re.finditer(toc_re, content, re.S): 544 for match in re.finditer(toc_re, content, re.S):
524 flat.append({ 545 flat.append({
525 'level': int(match.group(1)), 546 'level': int(match.group(1)),
526 'anchor': jinja2.Markup(match.group(2)).unescape(), 547 'anchor': jinja2.Markup(match.group(2)).unescape(),
527 'title': jinja2.Markup(match.group(3)).unescape(), 548 'title': jinja2.Markup(match.group(3)).unescape(),
528 'subitems': [], 549 'subitems': [],
529 }) 550 })
530 551
531 structured = [] 552 structured = []
532 stack = [{'level': 0, 'subitems': structured}] 553 stack = [{'level': 0, 'subitems': structured}]
533 for item in flat: 554 for item in flat:
534 while stack[-1]['level'] >= item['level']: 555 while stack[-1]['level'] >= item['level']:
535 stack.pop() 556 stack.pop()
536 stack[-1]['subitems'].append(item) 557 stack[-1]['subitems'].append(item)
537 stack.append(item) 558 stack.append(item)
538 return structured 559 return structured
539 560
540 converters = { 561 converters = {
541 'html': RawConverter, 562 'html': RawConverter,
542 'md': MarkdownConverter, 563 'md': MarkdownConverter,
543 'tmpl': TemplateConverter, 564 'tmpl': TemplateConverter,
544 } 565 }
OLDNEW
« no previous file with comments | « no previous file | tests/expected_output/get_page_url@dynamic » ('j') | tests/expected_output/sitemap » ('J')

Powered by Google App Engine
This is Rietveld