Commit 75254d67 by Christopher Lambacher

Optionally support markupsafe

This will allow the namesapce to provide information about
whether something is to be escaped or not.
parent 172ad891
......@@ -4,6 +4,15 @@ import collections
import os
import copy
try:
import markupsafe
escape = markupsafe.escape
literal = markupsafe.Markup
except ImportError:
escape = lambda x: cgi.escape(unicode(x))
literal = unicode
class Modifiers(dict):
"""Dictionary with a decorator for assigning functions to keys."""
......@@ -89,7 +98,7 @@ class Template(object):
elif (not it and section[2] == '^') or (it and section[2] != '^'):
replacer = inner
template = template.replace(section, replacer)
template = literal(template.replace(section, replacer))
return template
......@@ -129,7 +138,7 @@ class Template(object):
if not raw and raw is not 0:
return ''
return cgi.escape(unicode(raw))
return escape(raw)
@modifiers.set('!')
def _render_comment(self, tag_name):
......@@ -153,7 +162,7 @@ class Template(object):
@modifiers.set('&')
def render_unescaped(self, tag_name):
"""Render a tag without escaping it."""
return unicode(self.view.get(tag_name, ''))
return literal(self.view.get(tag_name, ''))
def render(self, encoding=None):
template = self._render_sections(self.template, self.view)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment