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

Delta Between Two Patch Sets: cms/converters.py

Issue 29495555: Fixes 5343 - add global function: get_canonical_url (Closed)
Left Patch Set: Created July 23, 2017, 4:35 p.m.
Right 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:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « 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')
LEFTRIGHT
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
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after
390 'translate': self.translate, 390 'translate': self.translate,
391 'linkify': self.linkify, 391 'linkify': self.linkify,
392 'toclist': self.toclist, 392 'toclist': self.toclist,
393 } 393 }
394 394
395 globals = { 395 globals = {
396 'get_string': self.get_string, 396 'get_string': self.get_string,
397 'has_string': self.has_string, 397 'has_string': self.has_string,
398 'get_page_content': self.get_page_content, 398 'get_page_content': self.get_page_content,
399 'get_pages_metadata': self.get_pages_metadata, 399 'get_pages_metadata': self.get_pages_metadata,
400 'get_page_url': self.get_page_url, 400 'get_canonical_url': self.get_canonical_url,
401 } 401 }
402 402
403 for dirname, dictionary in [('filters', filters), 403 for dirname, dictionary in [('filters', filters),
404 ('globals', globals)]: 404 ('globals', globals)]:
405 for filename in self._params['source'].list_files(dirname): 405 for filename in self._params['source'].list_files(dirname):
406 root, ext = os.path.splitext(filename) 406 root, ext = os.path.splitext(filename)
407 if ext.lower() != '.py': 407 if ext.lower() != '.py':
408 continue 408 continue
409 409
410 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
512 if isinstance(metadata[filter_name], list): 512 if isinstance(metadata[filter_name], list):
513 if isinstance(filter_value, basestring): 513 if isinstance(filter_value, basestring):
514 filter_value = [filter_value] 514 filter_value = [filter_value]
515 for option in filter_value: 515 for option in filter_value:
516 if str(option) not in metadata[filter_name]: 516 if str(option) not in metadata[filter_name]:
517 return False 517 return False
518 elif filter_value != metadata[filter_name]: 518 elif filter_value != metadata[filter_name]:
519 return False 519 return False
520 return True 520 return True
521 521
522 def get_page_url(self, page, locale='default'): 522 def get_canonical_url(self, page):
523 """Return URL of `page` in given `locale` 523 """Return canonical URL for the page (without locale code)"""
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: 524 try:
530 base_url = self._params['site_url'] 525 base_url = self._params['site_url']
531 except KeyError: 526 except KeyError:
532 raise Exception('You must configure siteurl to use `get_page_url`') 527 raise Exception('You must configure `siteurl` to use'
533 528 '`get_canonical_url()`')
534 if locale == 'default': 529
535 locale = self._params['locale'] 530 locale, page_url = self._params['source'].resolve_link(
536 if locale is not None: 531 page, self._params['locale']
537 base_url = urlparse.urljoin(base_url, locale + '/') 532 )
538 533 # Remove the locale component that `resolve_link` adds at the
539 return urlparse.urljoin(base_url, page) 534 # beginning.
535 page_url = page_url[len(locale) + 1:]
536 return urlparse.urljoin(base_url, page_url)
540 537
541 def toclist(self, content): 538 def toclist(self, content):
542 toc_re = r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>' 539 toc_re = r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>'
543 flat = [] 540 flat = []
544 for match in re.finditer(toc_re, content, re.S): 541 for match in re.finditer(toc_re, content, re.S):
545 flat.append({ 542 flat.append({
546 'level': int(match.group(1)), 543 'level': int(match.group(1)),
547 'anchor': jinja2.Markup(match.group(2)).unescape(), 544 'anchor': jinja2.Markup(match.group(2)).unescape(),
548 'title': jinja2.Markup(match.group(3)).unescape(), 545 'title': jinja2.Markup(match.group(3)).unescape(),
549 'subitems': [], 546 'subitems': [],
550 }) 547 })
551 548
552 structured = [] 549 structured = []
553 stack = [{'level': 0, 'subitems': structured}] 550 stack = [{'level': 0, 'subitems': structured}]
554 for item in flat: 551 for item in flat:
555 while stack[-1]['level'] >= item['level']: 552 while stack[-1]['level'] >= item['level']:
556 stack.pop() 553 stack.pop()
557 stack[-1]['subitems'].append(item) 554 stack[-1]['subitems'].append(item)
558 stack.append(item) 555 stack.append(item)
559 return structured 556 return structured
560 557
561 converters = { 558 converters = {
562 'html': RawConverter, 559 'html': RawConverter,
563 'md': MarkdownConverter, 560 'md': MarkdownConverter,
564 'tmpl': TemplateConverter, 561 'tmpl': TemplateConverter,
565 } 562 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld