diff --git a/hyde/ext/templates/jinja.py b/hyde/ext/templates/jinja.py index a6717a8..86f5767 100644 --- a/hyde/ext/templates/jinja.py +++ b/hyde/ext/templates/jinja.py @@ -7,6 +7,7 @@ from datetime import datetime, date import os import re import itertools +from urllib import quote, unquote from hyde.fs import File, Folder from hyde.model import Expando @@ -55,6 +56,13 @@ def full_url(context, path): """ return context['site'].full_url(path) +@contextfilter +def urlencode(ctx, url): + return quote(url.encode('utf8')) + +@contextfilter +def urldecode(ctx, url): + return unquote(url).decode('utf8') @contextfilter def date_format(ctx, dt, fmt=None): @@ -654,6 +662,8 @@ class Jinja2Template(Template): self.env.globals['full_url'] = full_url self.env.globals['engine'] = engine self.env.globals['deps'] = {} + self.env.filters['urlencode'] = urlencode + self.env.filters['urldecode'] = urldecode self.env.filters['asciidoc'] = asciidoc self.env.filters['markdown'] = markdown self.env.filters['restructuredtext'] = restructuredtext diff --git a/hyde/tests/test_jinja2template.py b/hyde/tests/test_jinja2template.py index 881258e..40c39bc 100644 --- a/hyde/tests/test_jinja2template.py +++ b/hyde/tests/test_jinja2template.py @@ -689,5 +689,30 @@ item_list: html = t.render(text, {}).strip() assert html.strip() == expected.strip() + def test_urlencode_filter(self): + text= u""" +фотография +quoted +""" + expected = u""" +фотография +quoted +""" + t = Jinja2Template(JINJA2.path) + t.configure(None) + html = t.render(text, {}).strip() + assert html.strip() == expected.strip() + def test_urldecode_filter(self): + text= u""" +{{ "%D1%84%D0%BE%D1%82%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D1%8F.jpg"|urldecode }} +""" + expected = u""" +фотография.jpg +""" + t = Jinja2Template(JINJA2.path) + t.configure(None) + html = t.render(text, {}).strip() + print html + assert html.strip() == expected.strip()