OLD | NEW |
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 291 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 return relpath(target, base_url.rsplit('/', 1)[0]) | 306 return relpath(target, base_url.rsplit('/', 1)[0]) |
307 | 307 |
308 def process_link(match): | 308 def process_link(match): |
309 pre, attr, url, post = match.groups() | 309 pre, attr, url, post = match.groups() |
310 url = jinja2.Markup(url).unescape() | 310 url = jinja2.Markup(url).unescape() |
311 | 311 |
312 locale, new_url = ( | 312 locale, new_url = self._params['source'].resolve_link( |
313 self._params['source'] | 313 url, self._params['locale'], self._params['page'], |
314 .resolve_link(url, self._params['locale'])) | 314 ) |
315 | 315 |
316 if new_url is not None: | 316 if new_url is not None: |
317 url = new_url | 317 url = new_url |
318 if attr == 'href': | 318 if attr == 'href': |
319 post += ' hreflang="{}"'\ | 319 post += ' hreflang="{}"'\ |
320 .format(jinja2.Markup.escape(locale)) | 320 .format(jinja2.Markup.escape(locale)) |
321 | 321 |
322 if self._params['relative']: | 322 if self._params['relative']: |
323 current_page = '/{}/{}'.format(self._params['locale'], | 323 current_page = '/{}/{}'.format(self._params['locale'], |
324 self._params['page']) | 324 self._params['page']) |
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
509 | 509 |
510 def get_page_content(self, page, locale=None): | 510 def get_page_content(self, page, locale=None): |
511 if locale is None: | 511 if locale is None: |
512 locale = self._params['locale'] | 512 locale = self._params['locale'] |
513 return utils.get_page_params(self._params['source'], locale, page) | 513 return utils.get_page_params(self._params['source'], locale, page) |
514 | 514 |
515 def linkify(self, page, locale=None, **attrs): | 515 def linkify(self, page, locale=None, **attrs): |
516 if locale is None: | 516 if locale is None: |
517 locale = self._params['locale'] | 517 locale = self._params['locale'] |
518 | 518 |
519 locale, url = self._params['source'].resolve_link(page, locale) | 519 locale, url = self._params['source'].resolve_link(page, locale, |
| 520 self._params['page']) |
520 return jinja2.Markup('<a{}>'.format(''.join( | 521 return jinja2.Markup('<a{}>'.format(''.join( |
521 ' {}="{}"'.format(name, jinja2.escape(value)) for name, value in [ | 522 ' {}="{}"'.format(name, jinja2.escape(value)) for name, value in [ |
522 ('href', url), | 523 ('href', url), |
523 ('hreflang', locale), | 524 ('hreflang', locale), |
524 ] + attrs.items() | 525 ] + attrs.items() |
525 ))) | 526 ))) |
526 | 527 |
527 def get_pages_metadata(self, filters=None): | 528 def get_pages_metadata(self, filters=None): |
528 if filters is not None and not isinstance(filters, dict): | 529 if filters is not None and not isinstance(filters, dict): |
529 raise TypeError('Filters are not a dictionary') | 530 raise TypeError('Filters are not a dictionary') |
(...skipping 26 matching lines...) Expand all Loading... |
556 | 557 |
557 def get_canonical_url(self, page): | 558 def get_canonical_url(self, page): |
558 """Return canonical URL for the page (without locale code)""" | 559 """Return canonical URL for the page (without locale code)""" |
559 try: | 560 try: |
560 base_url = self._params['site_url'] | 561 base_url = self._params['site_url'] |
561 except KeyError: | 562 except KeyError: |
562 raise Exception('You must configure `siteurl` to use' | 563 raise Exception('You must configure `siteurl` to use' |
563 '`get_canonical_url()`') | 564 '`get_canonical_url()`') |
564 | 565 |
565 locale, page_url = self._params['source'].resolve_link( | 566 locale, page_url = self._params['source'].resolve_link( |
566 page, self._params['locale'], | 567 page, self._params['locale'], self._params['page'], |
567 ) | 568 ) |
568 # Remove the locale component that `resolve_link` adds at the | 569 # Remove the locale component that `resolve_link` adds at the |
569 # beginning. | 570 # beginning. |
570 page_url = page_url[len(locale) + 1:] | 571 page_url = page_url[len(locale) + 1:] |
571 return urlparse.urljoin(base_url, page_url) | 572 return urlparse.urljoin(base_url, page_url) |
572 | 573 |
573 def toclist(self, content): | 574 def toclist(self, content): |
574 toc_re = r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>' | 575 toc_re = r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>' |
575 flat = [] | 576 flat = [] |
576 for match in re.finditer(toc_re, content, re.S): | 577 for match in re.finditer(toc_re, content, re.S): |
(...skipping 13 matching lines...) Expand all Loading... |
590 stack.append(item) | 591 stack.append(item) |
591 return structured | 592 return structured |
592 | 593 |
593 def page_has_locale(self, page, locale): | 594 def page_has_locale(self, page, locale): |
594 return self._params['source'].has_locale(locale, page) | 595 return self._params['source'].has_locale(locale, page) |
595 | 596 |
596 def get_page_url(self, page, locale=None, redirect=False): | 597 def get_page_url(self, page, locale=None, redirect=False): |
597 if not locale: | 598 if not locale: |
598 locale = self._params['locale'] | 599 locale = self._params['locale'] |
599 if self.page_has_locale(page, locale) or redirect: | 600 if self.page_has_locale(page, locale) or redirect: |
600 return self._params['source'].resolve_link(page, locale)[1] | 601 return self._params['source'].resolve_link(page, locale, |
| 602 self._params['page'])[1] |
601 raise Exception('{} does not exist in {}'.format(page, locale)) | 603 raise Exception('{} does not exist in {}'.format(page, locale)) |
602 | 604 |
603 | 605 |
604 converters = { | 606 converters = { |
605 'html': RawConverter, | 607 'html': RawConverter, |
606 'md': MarkdownConverter, | 608 'md': MarkdownConverter, |
607 'tmpl': TemplateConverter, | 609 'tmpl': TemplateConverter, |
608 } | 610 } |
OLD | NEW |