Commit 0de74f66 by Chris Jerdonek

Added pretty-printing to the nodes of the ParsedTemplate.

parent 37b4e3dc
...@@ -26,6 +26,11 @@ def parse(template, delimiters=None): ...@@ -26,6 +26,11 @@ def parse(template, delimiters=None):
delimiters: a 2-tuple of delimiters. Defaults to the package default. delimiters: a 2-tuple of delimiters. Defaults to the package default.
Examples:
>>> parse("Hey {{#you}}{{name}}!{{/you}}")
['Hey ', _SectionNode(key='you', index_begin=12, index_end=21, parsed=[_EscapeNode(key='name'), '!'])]
""" """
parser = _Parser(delimiters) parser = _Parser(delimiters)
return parser.parse(template) return parser.parse(template)
...@@ -66,9 +71,23 @@ class ParsingError(Exception): ...@@ -66,9 +71,23 @@ class ParsingError(Exception):
## Node types ## Node types
def _format(obj, exclude=None):
if exclude is None:
exclude = []
exclude.append('key')
attrs = obj.__dict__
names = list(set(attrs.keys()) - set(exclude))
names.sort()
names.insert(0, 'key')
args = ["%s=%s" % (name, repr(attrs[name])) for name in names]
return "%s(%s)" % (obj.__class__.__name__, ", ".join(args))
class _CommentNode(object): class _CommentNode(object):
def __repr__(self):
return _format(self)
def render(self, engine, context): def render(self, engine, context):
return u'' return u''
...@@ -78,15 +97,21 @@ class _ChangeNode(object): ...@@ -78,15 +97,21 @@ class _ChangeNode(object):
def __init__(self, delimiters): def __init__(self, delimiters):
self.delimiters = delimiters self.delimiters = delimiters
def __repr__(self):
return _format(self)
def render(self, engine, context): def render(self, engine, context):
return u'' return u''
class _TagNode(object): class _EscapeNode(object):
def __init__(self, key): def __init__(self, key):
self.key = key self.key = key
def __repr__(self):
return _format(self)
def render(self, engine, context): def render(self, engine, context):
s = engine.fetch_string(context, self.key) s = engine.fetch_string(context, self.key)
return engine.escape(s) return engine.escape(s)
...@@ -97,6 +122,9 @@ class _LiteralNode(object): ...@@ -97,6 +122,9 @@ class _LiteralNode(object):
def __init__(self, key): def __init__(self, key):
self.key = key self.key = key
def __repr__(self):
return _format(self)
def render(self, engine, context): def render(self, engine, context):
s = engine.fetch_string(context, self.key) s = engine.fetch_string(context, self.key)
return engine.literal(s) return engine.literal(s)
...@@ -108,6 +136,9 @@ class _PartialNode(object): ...@@ -108,6 +136,9 @@ class _PartialNode(object):
self.key = key self.key = key
self.indent = indent self.indent = indent
def __repr__(self):
return _format(self)
def render(self, engine, context): def render(self, engine, context):
template = engine.resolve_partial(self.key) template = engine.resolve_partial(self.key)
# Indent before rendering. # Indent before rendering.
...@@ -122,6 +153,9 @@ class _InvertedNode(object): ...@@ -122,6 +153,9 @@ class _InvertedNode(object):
self.key = key self.key = key
self.parsed_section = parsed_section self.parsed_section = parsed_section
def __repr__(self):
return _format(self)
def render(self, engine, context): def render(self, engine, context):
# TODO: is there a bug because we are not using the same # TODO: is there a bug because we are not using the same
# logic as in fetch_string()? # logic as in fetch_string()?
...@@ -138,13 +172,16 @@ class _SectionNode(object): ...@@ -138,13 +172,16 @@ class _SectionNode(object):
# TODO: the template_ and parsed_template_ arguments don't both seem # TODO: the template_ and parsed_template_ arguments don't both seem
# to be necessary. Can we remove one of them? For example, if # to be necessary. Can we remove one of them? For example, if
# callable(data) is True, then the initial parsed_template isn't used. # callable(data) is True, then the initial parsed_template isn't used.
def __init__(self, key, parsed_contents, delimiters, template, section_begin_index, section_end_index): def __init__(self, key, parsed, delimiters, template, index_begin, index_end):
self.delimiters = delimiters self.delimiters = delimiters
self.key = key self.key = key
self.parsed_contents = parsed_contents self.parsed = parsed
self.template = template self.template = template
self.section_begin_index = section_begin_index self.index_begin = index_begin
self.section_end_index = section_end_index self.index_end = index_end
def __repr__(self):
return _format(self, exclude=['delimiters', 'template'])
def render(self, engine, context): def render(self, engine, context):
data = engine.fetch_section_data(context, self.key) data = engine.fetch_section_data(context, self.key)
...@@ -167,13 +204,13 @@ class _SectionNode(object): ...@@ -167,13 +204,13 @@ class _SectionNode(object):
# https://github.com/defunkt/pystache/issues/113 # https://github.com/defunkt/pystache/issues/113
# #
# TODO: should we check the arity? # TODO: should we check the arity?
val = val(self.template[self.section_begin_index:self.section_end_index]) val = val(self.template[self.index_begin:self.index_end])
val = engine._render_value(val, context, delimiters=self.delimiters) val = engine._render_value(val, context, delimiters=self.delimiters)
parts.append(val) parts.append(val)
continue continue
context.push(val) context.push(val)
parts.append(engine.render_parsed(self.parsed_contents, context)) parts.append(engine.render_parsed(self.parsed, context))
context.pop() context.pop()
return unicode(''.join(parts)) return unicode(''.join(parts))
...@@ -310,7 +347,7 @@ class _Parser(object): ...@@ -310,7 +347,7 @@ class _Parser(object):
return _ChangeNode(delimiters) return _ChangeNode(delimiters)
if tag_type == '': if tag_type == '':
return _TagNode(tag_key) return _EscapeNode(tag_key)
if tag_type == '&': if tag_type == '&':
return _LiteralNode(tag_key) return _LiteralNode(tag_key)
......
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