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 |
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 import urlparse |
| 22 from posixpath import relpath |
22 | 23 |
23 import jinja2 | 24 import jinja2 |
24 import markdown | 25 import markdown |
25 | 26 |
26 from cms import utils | 27 from cms import utils |
27 | 28 |
28 # Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs | 29 # Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs |
29 # are inserted into the <head> tag | 30 # are inserted into the <head> tag |
30 orig_isBlockLevel = markdown.util.isBlockLevel | 31 orig_isBlockLevel = markdown.util.isBlockLevel |
31 | 32 |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
291 r'{{(?:(?!}}).)*}}' # Nested translation | 292 r'{{(?:(?!}}).)*}}' # Nested translation |
292 r')*?)' | 293 r')*?)' |
293 r')?' | 294 r')?' |
294 r'}}', | 295 r'}}', |
295 lookup_string, | 296 lookup_string, |
296 text, | 297 text, |
297 flags=re.S, | 298 flags=re.S, |
298 ) | 299 ) |
299 | 300 |
300 def process_links(self, text): | 301 def process_links(self, text): |
| 302 def make_relative(base_url, target): |
| 303 if not target.startswith('/'): |
| 304 # Links to an external resource |
| 305 return target |
| 306 return relpath(target, base_url.rsplit('/', 1)[0]) |
| 307 |
301 def process_link(match): | 308 def process_link(match): |
302 pre, attr, url, post = match.groups() | 309 pre, attr, url, post = match.groups() |
303 url = jinja2.Markup(url).unescape() | 310 url = jinja2.Markup(url).unescape() |
304 | 311 |
305 locale, new_url = ( | 312 locale, new_url = ( |
306 self._params['source'] | 313 self._params['source'] |
307 .resolve_link(url, self._params['locale'])) | 314 .resolve_link(url, self._params['locale'])) |
308 | 315 |
309 if new_url is not None: | 316 if new_url is not None: |
310 url = new_url | 317 url = new_url |
311 if attr == 'href': | 318 if attr == 'href': |
312 post += ' hreflang="{}"'\ | 319 post += ' hreflang="{}"'\ |
313 .format(jinja2.Markup.escape(locale)) | 320 .format(jinja2.Markup.escape(locale)) |
314 | 321 |
| 322 if self._params['relative']: |
| 323 current_page = '/{}/{}'.format(self._params['locale'], |
| 324 self._params['page']) |
| 325 url = make_relative(current_page, url) |
| 326 |
315 return ''.join((pre, jinja2.Markup.escape(url), post)) | 327 return ''.join((pre, jinja2.Markup.escape(url), post)) |
316 | 328 |
317 text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")', | 329 text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")', |
318 process_link, text) | 330 process_link, text) |
319 text = re.sub(r'(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")', | 331 text = re.sub(r'(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")', |
320 process_link, text) | 332 process_link, text) |
321 return text | 333 return text |
322 | 334 |
323 include_start_regex = '<' | 335 include_start_regex = '<' |
324 include_end_regex = '>' | 336 include_end_regex = '>' |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
425 'linkify': self.linkify, | 437 'linkify': self.linkify, |
426 'toclist': self.toclist, | 438 'toclist': self.toclist, |
427 } | 439 } |
428 | 440 |
429 globals = { | 441 globals = { |
430 'get_string': self.get_string, | 442 'get_string': self.get_string, |
431 'has_string': self.has_string, | 443 'has_string': self.has_string, |
432 'get_page_content': self.get_page_content, | 444 'get_page_content': self.get_page_content, |
433 'get_pages_metadata': self.get_pages_metadata, | 445 'get_pages_metadata': self.get_pages_metadata, |
434 'get_canonical_url': self.get_canonical_url, | 446 'get_canonical_url': self.get_canonical_url, |
| 447 'relative': self._params['relative'], |
435 } | 448 } |
436 | 449 |
437 for dirname, dictionary in [('filters', filters), | 450 for dirname, dictionary in [('filters', filters), |
438 ('globals', globals)]: | 451 ('globals', globals)]: |
439 for filename in self._params['source'].list_files(dirname): | 452 for filename in self._params['source'].list_files(dirname): |
440 root, ext = os.path.splitext(filename) | 453 root, ext = os.path.splitext(filename) |
441 if ext.lower() != '.py': | 454 if ext.lower() != '.py': |
442 continue | 455 continue |
443 | 456 |
444 path = os.path.join(dirname, filename) | 457 path = os.path.join(dirname, filename) |
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
577 stack[-1]['subitems'].append(item) | 590 stack[-1]['subitems'].append(item) |
578 stack.append(item) | 591 stack.append(item) |
579 return structured | 592 return structured |
580 | 593 |
581 | 594 |
582 converters = { | 595 converters = { |
583 'html': RawConverter, | 596 'html': RawConverter, |
584 'md': MarkdownConverter, | 597 'md': MarkdownConverter, |
585 'tmpl': TemplateConverter, | 598 'tmpl': TemplateConverter, |
586 } | 599 } |
OLD | NEW |