Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated mako to 0.3.5.

  • Loading branch information...
commit be96cfe444e22a3b38cd0710f868a0e6c9853a2e 1 parent cbd9370
@tav authored
View
2  mako/__init__.py
@@ -5,5 +5,5 @@
# the MIT License: http://www.opensource.org/licenses/mit-license.php
-__version__ = '0.3.3'
+__version__ = '0.3.5'
View
12 mako/codegen.py
@@ -20,7 +20,8 @@ def compile(node,
buffer_filters=None,
imports=None,
source_encoding=None,
- generate_magic_comment=True):
+ generate_magic_comment=True,
+ disable_unicode=False):
"""Generate module source code given a parsetree node,
uri, and optional source filename"""
@@ -43,7 +44,8 @@ def compile(node,
buffer_filters,
imports,
source_encoding,
- generate_magic_comment),
+ generate_magic_comment,
+ disable_unicode),
node)
return buf.getvalue()
@@ -55,7 +57,8 @@ def __init__(self,
buffer_filters,
imports,
source_encoding,
- generate_magic_comment):
+ generate_magic_comment,
+ disable_unicode):
self.uri = uri
self.filename = filename
self.default_filters = default_filters
@@ -63,6 +66,7 @@ def __init__(self,
self.imports = imports
self.source_encoding = source_encoding
self.generate_magic_comment = generate_magic_comment
+ self.disable_unicode = disable_unicode
class _GenerateRenderMethod(object):
"""A template visitor object which generates the
@@ -586,6 +590,8 @@ def create_filter_callable(self, args, target, is_expression):
def locate_encode(name):
if re.match(r'decode\..+', name):
return "filters." + name
+ elif self.compiler.disable_unicode:
+ return filters.NON_UNICODE_ESCAPES.get(name, name)
else:
return filters.DEFAULT_ESCAPES.get(name, name)
View
2  mako/exceptions.py
@@ -247,7 +247,7 @@ def text_error_template(lookup=None):
Traceback (most recent call last):
% for (filename, lineno, function, line) in tback.traceback:
File "${filename}", line ${lineno}, in ${function or '?'}
- ${line | unicode.strip}
+ ${line | trim}
% endfor
${tback.errorname}: ${tback.message}
""")
View
20 mako/filters.py
@@ -5,7 +5,7 @@
# the MIT License: http://www.opensource.org/licenses/mit-license.php
-import re, cgi, urllib, htmlentitydefs, codecs
+import re, urllib, htmlentitydefs, codecs
from StringIO import StringIO
from mako import util
@@ -16,12 +16,23 @@
'"' : '"', # also " in html-only
"'" : ''' # also ' in html-only
}
+
# XXX: " is valid in HTML and XML
# ' is not valid HTML, but is valid XML
-def html_escape(string):
- return cgi.escape(string, True)
+def legacy_html_escape(string):
+ """legacy HTML escape for non-unicode mode."""
+
+ return re.sub(r'([&<"\'>])', lambda m: xml_escapes[m.group()], string)
+
+try:
+ import markupsafe
+ def html_escape(string):
+ return markupsafe.escape(string)
+except ImportError:
+ html_escape = legacy_html_escape
+
def xml_escape(string):
return re.sub(r'([&<"\'>])', lambda m: xml_escapes[m.group()], string)
@@ -173,3 +184,6 @@ def htmlentityreplace_errors(ex):
'unicode':'str'
})
+NON_UNICODE_ESCAPES = DEFAULT_ESCAPES.copy()
+NON_UNICODE_ESCAPES['h'] = 'filters.legacy_html_escape'
+
View
25 mako/parsetree.py
@@ -268,20 +268,22 @@ def _parse_attributes(self, expressions, nonexpressions):
for key in self.attributes:
if key in expressions:
expr = []
- for x in re.split(r'(\${.+?})', self.attributes[key]):
- m = re.match(r'^\${(.+?)}$', x)
+ for x in re.compile(r'(\${.+?})',
+ re.S).split(self.attributes[key]):
+ m = re.compile(r'^\${(.+?)}$', re.S).match(x)
if m:
- code = ast.PythonCode(m.group(1), **self.exception_kwargs)
- undeclared_identifiers = undeclared_identifiers.union(
- code.undeclared_identifiers
- )
- expr.append("(%s)" % m.group(1))
+ code = ast.PythonCode(m.group(1).rstrip(),
+ **self.exception_kwargs)
+ undeclared_identifiers = \
+ undeclared_identifiers.union(
+ code.undeclared_identifiers)
+ expr.append('(%s)' % m.group(1))
else:
if x:
expr.append(repr(x))
self.parsed_attributes[key] = " + ".join(expr) or repr('')
elif key in nonexpressions:
- if re.search(r'${.+?}', self.attributes[key]):
+ if re.search(r'\${.+?}', self.attributes[key]):
raise exceptions.CompileException(
"Attibute '%s' in tag '%s' does not allow embedded "
"expressions" % (key, self.keyword),
@@ -334,9 +336,9 @@ class NamespaceTag(Tag):
def __init__(self, keyword, attributes, **kwargs):
super(NamespaceTag, self).__init__(
keyword, attributes,
- (),
+ ('file',),
('name','inheritable',
- 'file','import','module'),
+ 'import','module'),
(), **kwargs)
self.name = attributes.get('name', '__anon_%s' % hex(abs(id(self))))
@@ -391,7 +393,8 @@ def declared_identifiers(self):
def undeclared_identifiers(self):
res = []
for c in self.function_decl.defaults:
- res += list(ast.PythonCode(c, **self.exception_kwargs).undeclared_identifiers)
+ res += list(ast.PythonCode(c, **self.exception_kwargs).
+ undeclared_identifiers)
return res + list(self.filter_args.\
undeclared_identifiers.\
difference(filters.DEFAULT_ESCAPES.keys())
View
6 mako/template.py
@@ -363,7 +363,8 @@ def _compile_text(template, text, filename):
buffer_filters=template.buffer_filters,
imports=template.imports,
source_encoding=lexer.encoding,
- generate_magic_comment=template.disable_unicode)
+ generate_magic_comment=template.disable_unicode,
+ disable_unicode=template.disable_unicode)
cid = identifier
if not util.py3k and isinstance(cid, unicode):
@@ -389,7 +390,8 @@ def _compile_module_file(template, text, filename, outputpath):
buffer_filters=template.buffer_filters,
imports=template.imports,
source_encoding=lexer.encoding,
- generate_magic_comment=True)
+ generate_magic_comment=True,
+ disable_unicode=template.disable_unicode)
# make tempfiles in the same location as the ultimate
# location. this ensures they're on the same filesystem,
Please sign in to comment.
Something went wrong with that request. Please try again.