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

Delta Between Two Patch Sets: cms/converters.py

Issue 29933596: Issue 5333 - Allow cms to generate relative pages (Closed) Base URL: https://hg.adblockplus.org/cms/
Left Patch Set: Address comments on PS1, rearrange test files Created Nov. 6, 2018, 3:28 a.m.
Right Patch Set: Add documentation, remove extraneous global Created Nov. 9, 2018, 8:16 p.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 | « cms/bin/generate_static_pages.py ('k') | cms/utils.py » ('j') | no next file with change/comment »
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-present eyeo GmbH 2 # Copyright (C) 2006-present 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 285 matching lines...) Expand 10 before | Expand all | Expand 10 after
296 lookup_string, 296 lookup_string,
297 text, 297 text,
298 flags=re.S, 298 flags=re.S,
299 ) 299 )
300 300
301 def process_links(self, text): 301 def process_links(self, text):
302 def make_relative(base_url, target): 302 def make_relative(base_url, target):
303 if not target.startswith('/'): 303 if not target.startswith('/'):
304 # Links to an external resource 304 # Links to an external resource
305 return target 305 return target
306 if base_url == target: 306 return relpath(target, base_url.rsplit('/', 1)[0])
Vasily Kuznetsov 2018/11/06 12:45:17 This special case should not be necessary if the a
rhowell 2018/11/07 16:16:58 Done.
307 target = target.split('/')
308 return target[-1]
309 return relpath(target, base_url)
310 307
311 def process_link(match): 308 def process_link(match):
312 pre, attr, url, post = match.groups() 309 pre, attr, url, post = match.groups()
313 url = jinja2.Markup(url).unescape() 310 url = jinja2.Markup(url).unescape()
314 311
315 locale, new_url = ( 312 locale, new_url = (
316 self._params['source'] 313 self._params['source']
317 .resolve_link(url, self._params['locale'])) 314 .resolve_link(url, self._params['locale']))
318 315
319 if new_url is not None: 316 if new_url is not None:
320 url = new_url 317 url = new_url
321 if attr == 'href': 318 if attr == 'href':
322 post += ' hreflang="{}"'\ 319 post += ' hreflang="{}"'\
323 .format(jinja2.Markup.escape(locale)) 320 .format(jinja2.Markup.escape(locale))
324 321
325 if self._params['relative']: 322 if self._params['relative']:
326 current_path = '/' + self._params['locale'] 323 current_page = '/{}/{}'.format(self._params['locale'],
327 url = make_relative(current_path, url) 324 self._params['page'])
325 url = make_relative(current_page, url)
328 326
329 return ''.join((pre, jinja2.Markup.escape(url), post)) 327 return ''.join((pre, jinja2.Markup.escape(url), post))
330 328
331 text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")', 329 text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")',
332 process_link, text) 330 process_link, text)
333 text = re.sub(r'(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")', 331 text = re.sub(r'(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")',
334 process_link, text) 332 process_link, text)
335 return text 333 return text
336 334
337 include_start_regex = '<' 335 include_start_regex = '<'
(...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after
439 'linkify': self.linkify, 437 'linkify': self.linkify,
440 'toclist': self.toclist, 438 'toclist': self.toclist,
441 } 439 }
442 440
443 globals = { 441 globals = {
444 'get_string': self.get_string, 442 'get_string': self.get_string,
445 'has_string': self.has_string, 443 'has_string': self.has_string,
446 'get_page_content': self.get_page_content, 444 'get_page_content': self.get_page_content,
447 'get_pages_metadata': self.get_pages_metadata, 445 'get_pages_metadata': self.get_pages_metadata,
448 'get_canonical_url': self.get_canonical_url, 446 'get_canonical_url': self.get_canonical_url,
449 'relative': self._params['relative'],
450 } 447 }
451 448
452 for dirname, dictionary in [('filters', filters), 449 for dirname, dictionary in [('filters', filters),
453 ('globals', globals)]: 450 ('globals', globals)]:
454 for filename in self._params['source'].list_files(dirname): 451 for filename in self._params['source'].list_files(dirname):
455 root, ext = os.path.splitext(filename) 452 root, ext = os.path.splitext(filename)
456 if ext.lower() != '.py': 453 if ext.lower() != '.py':
457 continue 454 continue
458 455
459 path = os.path.join(dirname, filename) 456 path = os.path.join(dirname, filename)
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
592 stack[-1]['subitems'].append(item) 589 stack[-1]['subitems'].append(item)
593 stack.append(item) 590 stack.append(item)
594 return structured 591 return structured
595 592
596 593
597 converters = { 594 converters = {
598 'html': RawConverter, 595 'html': RawConverter,
599 'md': MarkdownConverter, 596 'md': MarkdownConverter,
600 'tmpl': TemplateConverter, 597 'tmpl': TemplateConverter,
601 } 598 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld