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: Add comment about test-type-specific expected output handling Created Aug. 3, 2017, 9:10 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 | « no previous file | tests/expected_output/get_page_url@dynamic » ('j') | tests/test_page_outputs.py » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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_canonical_url': self.get_canonical_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_canonical_url(self, page):
523 """Return canonical URL for the page (without locale code)"""
524 try:
525 base_url = self._params['site_url']
526 except KeyError:
527 raise Exception('You must configure `siteurl` to use'
528 '`get_canonical_url()`')
529
530 locale, page_url = self._params['source'].resolve_link(
531 page, self._params['locale']
532 )
533 # Remove the locale component that `resolve_link` adds at the
534 # beginning.
535 page_url = page_url[len(locale) + 1:]
536 return urlparse.urljoin(base_url, page_url)
537
520 def toclist(self, content): 538 def toclist(self, content):
521 toc_re = r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>' 539 toc_re = r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>'
522 flat = [] 540 flat = []
523 for match in re.finditer(toc_re, content, re.S): 541 for match in re.finditer(toc_re, content, re.S):
524 flat.append({ 542 flat.append({
525 'level': int(match.group(1)), 543 'level': int(match.group(1)),
526 'anchor': jinja2.Markup(match.group(2)).unescape(), 544 'anchor': jinja2.Markup(match.group(2)).unescape(),
527 'title': jinja2.Markup(match.group(3)).unescape(), 545 'title': jinja2.Markup(match.group(3)).unescape(),
528 'subitems': [], 546 'subitems': [],
529 }) 547 })
530 548
531 structured = [] 549 structured = []
532 stack = [{'level': 0, 'subitems': structured}] 550 stack = [{'level': 0, 'subitems': structured}]
533 for item in flat: 551 for item in flat:
534 while stack[-1]['level'] >= item['level']: 552 while stack[-1]['level'] >= item['level']:
535 stack.pop() 553 stack.pop()
536 stack[-1]['subitems'].append(item) 554 stack[-1]['subitems'].append(item)
537 stack.append(item) 555 stack.append(item)
538 return structured 556 return structured
539 557
540 converters = { 558 converters = {
541 'html': RawConverter, 559 'html': RawConverter,
542 'md': MarkdownConverter, 560 'md': MarkdownConverter,
543 'tmpl': TemplateConverter, 561 'tmpl': TemplateConverter,
544 } 562 }
OLDNEW
« no previous file with comments | « no previous file | tests/expected_output/get_page_url@dynamic » ('j') | tests/test_page_outputs.py » ('J')

Powered by Google App Engine
This is Rietveld