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

Unified Diff: cms/converters.py

Issue 29495555: Fixes 5343 - add global function: get_canonical_url (Closed)
Patch Set: Created July 23, 2017, 4:35 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 | « no previous file | tests/expected_output/get_page_url@dynamic » ('j') | tests/expected_output/sitemap » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: cms/converters.py
===================================================================
--- a/cms/converters.py
+++ b/cms/converters.py
@@ -13,16 +13,17 @@
# You should have received a copy of the GNU General Public License
# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
from __future__ import unicode_literals
import os
import HTMLParser
import re
+import urlparse
import jinja2
import markdown
# Monkey-patch Markdown's isBlockLevel function to ensure that no paragraphs
# are inserted into the <head> tag
orig_isBlockLevel = markdown.util.isBlockLevel
@@ -391,16 +392,17 @@
'toclist': self.toclist,
}
globals = {
'get_string': self.get_string,
'has_string': self.has_string,
'get_page_content': self.get_page_content,
'get_pages_metadata': self.get_pages_metadata,
+ 'get_page_url': self.get_page_url,
}
for dirname, dictionary in [('filters', filters),
('globals', globals)]:
for filename in self._params['source'].list_files(dirname):
root, ext = os.path.splitext(filename)
if ext.lower() != '.py':
continue
@@ -512,16 +514,35 @@
filter_value = [filter_value]
for option in filter_value:
if str(option) not in metadata[filter_name]:
return False
elif filter_value != metadata[filter_name]:
return False
return True
+ def get_page_url(self, page, locale='default'):
+ """Return URL of `page` in given `locale`
+
+ By default current locale will be used. When `locale` is set to `None`,
+ the canonical URL for the page (with no locale component) will be
+ returned.
+ """
+ try:
+ base_url = self._params['site_url']
+ except KeyError:
+ raise Exception('You must configure siteurl to use `get_page_url`')
+
+ if locale == 'default':
+ locale = self._params['locale']
+ if locale is not None:
+ base_url = urlparse.urljoin(base_url, locale + '/')
+
+ return urlparse.urljoin(base_url, page)
+
def toclist(self, content):
toc_re = r'<h(\d)\s[^<>]*\bid="([^<>"]+)"[^<>]*>(.*?)</h\1>'
flat = []
for match in re.finditer(toc_re, content, re.S):
flat.append({
'level': int(match.group(1)),
'anchor': jinja2.Markup(match.group(2)).unescape(),
'title': jinja2.Markup(match.group(3)).unescape(),
« no previous file with comments | « no previous file | tests/expected_output/get_page_url@dynamic » ('j') | tests/expected_output/sitemap » ('J')

Powered by Google App Engine
This is Rietveld