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: Translate rel_path Created Nov. 8, 2018, 2:48 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
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 141 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 Exception 174 Exception
174 If the default is required but cannot be obtained. 175 If the default is required but cannot be obtained.
175 176
176 """ 177 """
177 def escape(s): 178 def escape(s):
178 return ''.join(escapes.get(c, c) for c in s) 179 return ''.join(escapes.get(c, c) for c in s)
179 180
180 def re_escape(s): 181 def re_escape(s):
181 return re.escape(escape(s)) 182 return re.escape(escape(s))
182 183
184 # import ipdb; ipdb.set_trace()
Vasily Kuznetsov 2018/11/08 14:36:14 Oops
rhowell 2018/11/08 20:05:18 Done.
183 locale = self._params['locale'] 185 locale = self._params['locale']
184 localedata = self._get_locale_data(page, locale) 186 localedata = self._get_locale_data(page, locale)
185 defaultlocale = self._params['defaultlocale'] 187 defaultlocale = self._params['defaultlocale']
186 default_localedata = self._get_locale_data(page, defaultlocale) 188 default_localedata = self._get_locale_data(page, defaultlocale)
187 189
188 if default: 190 if default:
189 # The default is provided in the page: remember it, in case the 191 # The default is provided in the page: remember it, in case the
190 # same string name is used again in this page. 192 # same string name is used again in this page.
191 self._seen_defaults[(page, name)] = (default, comment) 193 self._seen_defaults[(page, name)] = (default, comment)
192 elif (page, name) in self._seen_defaults: 194 elif (page, name) in self._seen_defaults:
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
291 r'{{(?:(?!}}).)*}}' # Nested translation 293 r'{{(?:(?!}}).)*}}' # Nested translation
292 r')*?)' 294 r')*?)'
293 r')?' 295 r')?'
294 r'}}', 296 r'}}',
295 lookup_string, 297 lookup_string,
296 text, 298 text,
297 flags=re.S, 299 flags=re.S,
298 ) 300 )
299 301
300 def process_links(self, text): 302 def process_links(self, text):
303 def make_relative(base_url, target):
304 if not target.startswith('/'):
305 # Links to an external resource
306 return target
307 return relpath(target, base_url.rsplit('/', 1)[0])
308
301 def process_link(match): 309 def process_link(match):
310 # if 'rel_path' in match.groups():
311 # import ipdb; ipdb.set_trace()
302 pre, attr, url, post = match.groups() 312 pre, attr, url, post = match.groups()
303 url = jinja2.Markup(url).unescape() 313 url = jinja2.Markup(url).unescape()
304 314
305 locale, new_url = ( 315 locale, new_url = (
306 self._params['source'] 316 self._params['source']
307 .resolve_link(url, self._params['locale'])) 317 .resolve_link(url, self._params['locale']))
308 318
309 if new_url is not None: 319 if new_url is not None:
310 url = new_url 320 url = new_url
311 if attr == 'href': 321 if attr == 'href':
312 post += ' hreflang="{}"'\ 322 post += ' hreflang="{}"'\
313 .format(jinja2.Markup.escape(locale)) 323 .format(jinja2.Markup.escape(locale))
314 324
325 if self._params['relative']:
326 current_page = '/{}/{}'.format(self._params['locale'],
327 self._params['page'])
328 url = make_relative(current_page, url)
329
315 return ''.join((pre, jinja2.Markup.escape(url), post)) 330 return ''.join((pre, jinja2.Markup.escape(url), post))
316 331
317 text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")', 332 text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")',
318 process_link, text) 333 process_link, text)
319 text = re.sub(r'(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")', 334 text = re.sub(r'(<img\s[^<>]*\b(src)=\")([^<>\"]+)(\")',
320 process_link, text) 335 process_link, text)
321 return text 336 return text
322 337
323 include_start_regex = '<' 338 include_start_regex = '<'
324 include_end_regex = '>' 339 include_end_regex = '>'
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after
425 'linkify': self.linkify, 440 'linkify': self.linkify,
426 'toclist': self.toclist, 441 'toclist': self.toclist,
427 } 442 }
428 443
429 globals = { 444 globals = {
430 'get_string': self.get_string, 445 'get_string': self.get_string,
431 'has_string': self.has_string, 446 'has_string': self.has_string,
432 'get_page_content': self.get_page_content, 447 'get_page_content': self.get_page_content,
433 'get_pages_metadata': self.get_pages_metadata, 448 'get_pages_metadata': self.get_pages_metadata,
434 'get_canonical_url': self.get_canonical_url, 449 'get_canonical_url': self.get_canonical_url,
450 'relative': self._params['relative'],
435 } 451 }
436 452
437 for dirname, dictionary in [('filters', filters), 453 for dirname, dictionary in [('filters', filters),
438 ('globals', globals)]: 454 ('globals', globals)]:
439 for filename in self._params['source'].list_files(dirname): 455 for filename in self._params['source'].list_files(dirname):
440 root, ext = os.path.splitext(filename) 456 root, ext = os.path.splitext(filename)
441 if ext.lower() != '.py': 457 if ext.lower() != '.py':
442 continue 458 continue
443 459
444 path = os.path.join(dirname, filename) 460 path = os.path.join(dirname, filename)
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
577 stack[-1]['subitems'].append(item) 593 stack[-1]['subitems'].append(item)
578 stack.append(item) 594 stack.append(item)
579 return structured 595 return structured
580 596
581 597
582 converters = { 598 converters = {
583 'html': RawConverter, 599 'html': RawConverter,
584 'md': MarkdownConverter, 600 'md': MarkdownConverter,
585 'tmpl': TemplateConverter, 601 'tmpl': TemplateConverter,
586 } 602 }
OLDNEW

Powered by Google App Engine
This is Rietveld