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

Unified Diff: cms/converters.py

Issue 29933596: Issue 5333 - Allow cms to generate relative pages (Closed) Base URL: https://hg.adblockplus.org/cms/
Patch Set: Address comments on PS5 Created Nov. 8, 2018, 8:04 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « cms/bin/generate_static_pages.py ('k') | cms/utils.py » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cms/converters.py
===================================================================
--- a/cms/converters.py
+++ b/cms/converters.py
@@ -19,6 +19,7 @@
import HTMLParser
import re
import urlparse
+from posixpath import relpath
import jinja2
import markdown
@@ -298,7 +299,15 @@
)
def process_links(self, text):
+ def make_relative(base_url, target):
+ if not target.startswith('/'):
+ # Links to an external resource
+ return target
+ return relpath(target, base_url.rsplit('/', 1)[0])
+
def process_link(match):
+ # if 'rel_path' in match.groups():
Vasily Kuznetsov 2018/11/09 10:53:27 You also forgot this one. Sorry, I haven't marked
rhowell 2018/11/09 17:18:36 Oops! I've added `grep -R ipdb` to my checklist be
+ # import ipdb; ipdb.set_trace()
pre, attr, url, post = match.groups()
url = jinja2.Markup(url).unescape()
@@ -312,6 +321,11 @@
post += ' hreflang="{}"'\
.format(jinja2.Markup.escape(locale))
+ if self._params['relative']:
+ current_page = '/{}/{}'.format(self._params['locale'],
+ self._params['page'])
+ url = make_relative(current_page, url)
+
return ''.join((pre, jinja2.Markup.escape(url), post))
text = re.sub(r'(<a\s[^<>]*\b(href)=\")([^<>\"]+)(\")',
@@ -432,6 +446,7 @@
'get_page_content': self.get_page_content,
'get_pages_metadata': self.get_pages_metadata,
'get_canonical_url': self.get_canonical_url,
+ 'relative': self._params['relative'],
}
for dirname, dictionary in [('filters', filters),
« no previous file with comments | « cms/bin/generate_static_pages.py ('k') | cms/utils.py » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld