Browse Source

Issue #102: Added urlencode and urldecode filter.

main
Lakshmi Vyasarajan 13 years ago
parent
commit
97a1b01203
2 changed files with 35 additions and 0 deletions
  1. +10
    -0
      hyde/ext/templates/jinja.py
  2. +25
    -0
      hyde/tests/test_jinja2template.py

+ 10
- 0
hyde/ext/templates/jinja.py View File

@@ -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


+ 25
- 0
hyde/tests/test_jinja2template.py View File

@@ -689,5 +689,30 @@ item_list:
html = t.render(text, {}).strip()
assert html.strip() == expected.strip()

def test_urlencode_filter(self):
text= u"""
<a href="{{ 'фотография.jpg'|urlencode }}">фотография</a>
<a href="{{ 'http://localhost:8080/"abc.jpg'|urlencode }}">quoted</a>
"""
expected = u"""
<a href="%D1%84%D0%BE%D1%82%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D1%8F.jpg">фотография</a>
<a href="http%3A//localhost%3A8080/%22abc.jpg">quoted</a>
"""
t = Jinja2Template(JINJA2.path)
t.configure(None)
html = t.render(text, {}).strip()
assert html.strip() == expected.strip()

def test_urldecode_filter(self):
text= u"""
<a href="{{ 'фотография.jpg'|urlencode }}">{{ "%D1%84%D0%BE%D1%82%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D1%8F.jpg"|urldecode }}</a>
"""
expected = u"""
<a href="%D1%84%D0%BE%D1%82%D0%BE%D0%B3%D1%80%D0%B0%D1%84%D0%B8%D1%8F.jpg">фотография.jpg</a>
"""
t = Jinja2Template(JINJA2.path)
t.configure(None)
html = t.render(text, {}).strip()
print html
assert html.strip() == expected.strip()


Loading…
Cancel
Save