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 |
@@ -180,6 +181,7 @@ |
def re_escape(s): |
return re.escape(escape(s)) |
+ # import ipdb; ipdb.set_trace() |
Vasily Kuznetsov
2018/11/08 14:36:14
Oops
rhowell
2018/11/08 20:05:18
Done.
|
locale = self._params['locale'] |
localedata = self._get_locale_data(page, locale) |
defaultlocale = self._params['defaultlocale'] |
@@ -298,7 +300,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(): |
+ # import ipdb; ipdb.set_trace() |
pre, attr, url, post = match.groups() |
url = jinja2.Markup(url).unescape() |
@@ -312,6 +322,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 +447,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), |