Skip to content
Snippets Groups Projects
Commit 7525e89e authored by Mike Bayer's avatar Mike Bayer
Browse files

- added "encoding()" filter; this allows a filter expression to specify the encoding and error

handling for the given expression.
usage is like:  ${data | encoding('utf-8', errors='strict')}
parent 34cf1313
No related branches found
No related tags found
No related merge requests found
0.1.1
- AST parsing fixes: fixed TryExcept identifier parsing
- added "encoding()" filter; this allows a filter expression to specify the encoding and error
handling for the given expression.
usage is like: ${data | encoding('utf-8', errors='strict')}
0.1.0
......
......@@ -377,7 +377,14 @@ class _GenerateRenderMethod(object):
if self.compiler.pagetag:
args += self.compiler.pagetag.filter_args.args
for e in args:
e = d.get(e, e)
# if filter given as a function, get just the identifier portion
m = re.match(r'(.+?)(\(.*\))', e)
if m:
(ident, fargs) = m.group(1,2)
f = d.get(ident, ident)
e = f + fargs
else:
e = d.get(e, e)
target = "%s(%s)" % (e, target)
return target
......
......@@ -38,7 +38,9 @@ def url_unescape(string):
def trim(string):
return string.strip()
def encoding(encoding, errors='strict'):
return lambda x:unicode(x, encoding=encoding, errors=errors)
_ASCII_re = re.compile(r'\A[\x00-\x7f]*\Z')
def is_ascii_str(text):
......@@ -148,6 +150,7 @@ DEFAULT_ESCAPES = {
'u':url_escape,
'trim':trim,
'entity':html_entities_escape,
'encoding':encoding
}
......@@ -337,7 +337,7 @@ class ScopeTest(unittest.TestCase):
""")
# test via inheritance
print l.get_template("main").code
#print l.get_template("main").code
assert result_lines(l.get_template("main").render()) == [
"this is main. x is 12",
"this is a, x is 12"
......
# -*- coding: utf-8 -*-
from mako.template import Template
import unittest
from util import result_lines, flatten_result
......@@ -18,6 +20,14 @@ class FilterTest(unittest.TestCase):
return lambda x: "MYFILTER->%s<-%s" % (x, y)
assert flatten_result(t.render(x="this is x", myfilter=myfilter, y="this is y")) == "MYFILTER->this is x<-this is y"
def test_builtin_func(self):
t = Template("""
${x | encoding('utf-8')}
""")
udata = u"""Alors vous imaginez ma surprise, au lever du jour, quand une drôle de petit voix m’a réveillé. Elle disait: « S’il vous plaît… dessine-moi un mouton! »"""
utf8data = udata.encode('utf-8')
assert flatten_result(t.render_unicode(x=utf8data)) == udata
def test_def(self):
t = Template("""
<%def name="foo()" filter="myfilter">
......
......@@ -3,8 +3,8 @@ try:
file_content = open(path)
except:
raise "Should never execute here"
doc_content = ''.join(file_content.readlines()).decode('utf-8')
doc_content = ''.join(file_content.readlines())
file_content.close()
%>
${doc_content}
${doc_content | encoding('utf-8')}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment