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: Allow cms to generate relative pages Created Nov. 2, 2018, 2:59 a.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
Index: cms/converters.py
===================================================================
--- a/cms/converters.py
+++ b/cms/converters.py
@@ -298,6 +298,15 @@
)
def process_links(self, text):
+ 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.
+ if not target.startswith('/'):
+ # Links to an external resource
+ return target
+ if source == target:
+ 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.
+ return target[-1]
+ 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
+
def process_link(match):
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') | tests/rel_path.html » ('J')

Powered by Google App Engine
This is Rietveld