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

Side by Side Diff: cms/converters.py

Issue 29933596: Issue 5333 - Allow cms to generate relative pages (Closed) Base URL: https://hg.adblockplus.org/cms/
Patch Set: Allow cms to generate relative pages Created Nov. 2, 2018, 2:59 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
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-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 280 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 r'{{(?:(?!}}).)*}}' # Nested translation 291 r'{{(?:(?!}}).)*}}' # Nested translation
292 r')*?)' 292 r')*?)'
293 r')?' 293 r')?'
294 r'}}', 294 r'}}',
295 lookup_string, 295 lookup_string,
296 text, 296 text,
297 flags=re.S, 297 flags=re.S,
298 ) 298 )
299 299
300 def process_links(self, text): 300 def process_links(self, text):
301 def make_relative(source, target):
Vasily Kuznetsov 2018/11/02 10:45:35 It would probably be good to rename `source` to `b
rhowell 2018/11/06 03:34:41 Done.
302 if not target.startswith('/'):
303 # Links to an external resource
304 return target
305 if source == target:
306 target = re.split('/', target)
Vasily Kuznetsov 2018/11/02 10:45:35 If this better than target.split('/')?
rhowell 2018/11/06 03:34:41 Done.
307 return target[-1]
308 return os.path.relpath(target, source)
Vasily Kuznetsov 2018/11/02 10:45:35 Here we need to take the part of `source` (or `bas
rhowell 2018/11/06 03:34:41 Or, maybe it's cleaner to just pass the base path
Vasily Kuznetsov 2018/11/06 12:45:16 This is not quite right. First, "base URL" has spe
rhowell 2018/11/07 16:16:58 Ok, I think I get it now. I've tried a different w
309
301 def process_link(match): 310 def process_link(match):
302 pre, attr, url, post = match.groups() 311 pre, attr, url, post = match.groups()
303 url = jinja2.Markup(url).unescape() 312 url = jinja2.Markup(url).unescape()
304 313
305 locale, new_url = ( 314 locale, new_url = (
306 self._params['source'] 315 self._params['source']
307 .resolve_link(url, self._params['locale'])) 316 .resolve_link(url, self._params['locale']))
308 317
309 if new_url is not None: 318 if new_url is not None:
310 url = new_url 319 url = new_url
311 if attr == 'href': 320 if attr == 'href':
312 post += ' hreflang="{}"'\ 321 post += ' hreflang="{}"'\
313 .format(jinja2.Markup.escape(locale)) 322 .format(jinja2.Markup.escape(locale))
314 323
324 if self._params['relative']:
325 current_page = '/{}/{}'.format(self._params['locale'],
326 self._params['page'])
327 url = make_relative(current_page, url)
328
315 return ''.join((pre, jinja2.Markup.escape(url), post)) 329 return ''.join((pre, jinja2.Markup.escape(url), post))
316 330
317 text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")', 331 text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")',
318 process_link, text) 332 process_link, text)
319 text = re.sub(r'(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")', 333 text = re.sub(r'(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")',
320 process_link, text) 334 process_link, text)
321 return text 335 return text
322 336
323 include_start_regex = '<' 337 include_start_regex = '<'
324 include_end_regex = '>' 338 include_end_regex = '>'
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 'linkify': self.linkify, 439 'linkify': self.linkify,
426 'toclist': self.toclist, 440 'toclist': self.toclist,
427 } 441 }
428 442
429 globals = { 443 globals = {
430 'get_string': self.get_string, 444 'get_string': self.get_string,
431 'has_string': self.has_string, 445 'has_string': self.has_string,
432 'get_page_content': self.get_page_content, 446 'get_page_content': self.get_page_content,
433 'get_pages_metadata': self.get_pages_metadata, 447 'get_pages_metadata': self.get_pages_metadata,
434 'get_canonical_url': self.get_canonical_url, 448 'get_canonical_url': self.get_canonical_url,
449 'relative': self._params['relative'],
435 } 450 }
436 451
437 for dirname, dictionary in [('filters', filters), 452 for dirname, dictionary in [('filters', filters),
438 ('globals', globals)]: 453 ('globals', globals)]:
439 for filename in self._params['source'].list_files(dirname): 454 for filename in self._params['source'].list_files(dirname):
440 root, ext = os.path.splitext(filename) 455 root, ext = os.path.splitext(filename)
441 if ext.lower() != '.py': 456 if ext.lower() != '.py':
442 continue 457 continue
443 458
444 path = os.path.join(dirname, filename) 459 path = os.path.join(dirname, filename)
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 stack[-1]['subitems'].append(item) 592 stack[-1]['subitems'].append(item)
578 stack.append(item) 593 stack.append(item)
579 return structured 594 return structured
580 595
581 596
582 converters = { 597 converters = {
583 'html': RawConverter, 598 'html': RawConverter,
584 'md': MarkdownConverter, 599 'md': MarkdownConverter,
585 'tmpl': TemplateConverter, 600 'tmpl': TemplateConverter,
586 } 601 }
OLDNEW
« no previous file with comments | « cms/bin/generate_static_pages.py ('k') | cms/utils.py » ('j') | tests/rel_path.html » ('J')

Powered by Google App Engine
This is Rietveld