From 06e290786eec127290be976dcfce08081b01c08d Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sat, 5 May 2012 07:01:28 -0700 Subject: Work with ParsedTemplate instance instead of parse_tree list. --- pystache/parsed.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'pystache/parsed.py') diff --git a/pystache/parsed.py b/pystache/parsed.py index a37565b..d791be4 100644 --- a/pystache/parsed.py +++ b/pystache/parsed.py @@ -10,7 +10,7 @@ This module is meant only for internal use. class ParsedTemplate(object): - def __init__(self, parse_tree): + def __init__(self): """ Arguments: @@ -30,11 +30,14 @@ class ParsedTemplate(object): * RenderEngine._make_get_section() """ - self._parse_tree = parse_tree + self._parse_tree = [] def __repr__(self): return "[%s]" % (", ".join([repr(part) for part in self._parse_tree])) + def add(self, node): + self._parse_tree.append(node) + def render(self, context): """ Returns: a string of type unicode. -- cgit v1.2.1 From f94aa621f125d59ebaf19e9bf110e23a04a10637 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sat, 5 May 2012 11:39:46 -0700 Subject: Started decoupling Parser from RenderEngine. --- pystache/parsed.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) (limited to 'pystache/parsed.py') diff --git a/pystache/parsed.py b/pystache/parsed.py index d791be4..0055837 100644 --- a/pystache/parsed.py +++ b/pystache/parsed.py @@ -38,7 +38,7 @@ class ParsedTemplate(object): def add(self, node): self._parse_tree.append(node) - def render(self, context): + def render(self, engine, context): """ Returns: a string of type unicode. @@ -47,9 +47,10 @@ class ParsedTemplate(object): def get_unicode(val): if callable(val): return val(context) - return val + if isinstance(val, basestring): + return val + return val.render(engine, context) parts = map(get_unicode, self._parse_tree) s = ''.join(parts) return unicode(s) - -- cgit v1.2.1 From e4108154d7394e218695747b18543376d03ead42 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sat, 5 May 2012 14:44:21 -0700 Subject: Parser no longer requires a RenderEngine instance to parse. --- pystache/parsed.py | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) (limited to 'pystache/parsed.py') diff --git a/pystache/parsed.py b/pystache/parsed.py index 0055837..eb138cd 100644 --- a/pystache/parsed.py +++ b/pystache/parsed.py @@ -45,9 +45,7 @@ class ParsedTemplate(object): """ # We avoid use of the ternary operator for Python 2.4 support. def get_unicode(val): - if callable(val): - return val(context) - if isinstance(val, basestring): + if type(val) is unicode: return val return val.render(engine, context) parts = map(get_unicode, self._parse_tree) -- cgit v1.2.1 From 8f9da1a7e1c86081233ac6c5a949292e36e25570 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sun, 6 May 2012 11:17:40 -0700 Subject: Simplified ParsedTemplate.__repr__(). --- pystache/parsed.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'pystache/parsed.py') diff --git a/pystache/parsed.py b/pystache/parsed.py index eb138cd..e3c746f 100644 --- a/pystache/parsed.py +++ b/pystache/parsed.py @@ -33,7 +33,7 @@ class ParsedTemplate(object): self._parse_tree = [] def __repr__(self): - return "[%s]" % (", ".join([repr(part) for part in self._parse_tree])) + return repr(self._parse_tree) def add(self, node): self._parse_tree.append(node) -- cgit v1.2.1 From 331a48d0984b37dafd07efb2f3883ab6fafb2b34 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sun, 6 May 2012 11:40:55 -0700 Subject: ParsedTemplate docstring udpates and removed RenderEngine.render_parsed(). --- pystache/parsed.py | 30 +++++++++--------------------- 1 file changed, 9 insertions(+), 21 deletions(-) (limited to 'pystache/parsed.py') diff --git a/pystache/parsed.py b/pystache/parsed.py index e3c746f..e94c644 100644 --- a/pystache/parsed.py +++ b/pystache/parsed.py @@ -3,39 +3,27 @@ """ Exposes a class that represents a parsed (or compiled) template. -This module is meant only for internal use. - """ class ParsedTemplate(object): def __init__(self): - """ - Arguments: - - parse_tree: a list, each element of which is either-- - - (1) a unicode string, or - (2) a "rendering" callable that accepts a ContextStack instance - and returns a unicode string. - - The possible rendering callables are the return values of the - following functions: - - * RenderEngine._make_get_escaped() - * RenderEngine._make_get_inverse() - * RenderEngine._make_get_literal() - * RenderEngine._make_get_partial() - * RenderEngine._make_get_section() - - """ self._parse_tree = [] def __repr__(self): return repr(self._parse_tree) def add(self, node): + """ + Arguments: + + node: a unicode string or node object instance. A node object + instance must have a `render(engine, stack)` method that + accepts a RenderEngine instance and a ContextStack instance and + returns a unicode string. + + """ self._parse_tree.append(node) def render(self, engine, context): -- cgit v1.2.1 From 449425708f9c7ec76b0dd72830cfe7349ff46ae0 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Wed, 17 Oct 2012 01:55:10 -0700 Subject: Adjust ParsedTemplate docstrings. --- pystache/parsed.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) (limited to 'pystache/parsed.py') diff --git a/pystache/parsed.py b/pystache/parsed.py index e94c644..a7c0aae 100644 --- a/pystache/parsed.py +++ b/pystache/parsed.py @@ -8,6 +8,16 @@ Exposes a class that represents a parsed (or compiled) template. class ParsedTemplate(object): + """ + Represents a parsed or compiled template. + + An instance wraps a list of unicode strings and node objects. A node + object must have a `render(engine, stack)` method that accepts a + RenderEngine instance and a ContextStack instance and returns a unicode + string. + + """ + def __init__(self): self._parse_tree = [] @@ -18,10 +28,8 @@ class ParsedTemplate(object): """ Arguments: - node: a unicode string or node object instance. A node object - instance must have a `render(engine, stack)` method that - accepts a RenderEngine instance and a ContextStack instance and - returns a unicode string. + node: a unicode string or node object instance. See the class + docstring for information. """ self._parse_tree.append(node) -- cgit v1.2.1 From 5b00b6cd392a1f5f317cc18893198bb5d62c565d Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Fri, 19 Oct 2012 16:35:36 -0700 Subject: Rename variable name from "val" to "node". --- pystache/parsed.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) (limited to 'pystache/parsed.py') diff --git a/pystache/parsed.py b/pystache/parsed.py index a7c0aae..372d96c 100644 --- a/pystache/parsed.py +++ b/pystache/parsed.py @@ -40,10 +40,10 @@ class ParsedTemplate(object): """ # We avoid use of the ternary operator for Python 2.4 support. - def get_unicode(val): - if type(val) is unicode: - return val - return val.render(engine, context) + def get_unicode(node): + if type(node) is unicode: + return node + return node.render(engine, context) parts = map(get_unicode, self._parse_tree) s = ''.join(parts) -- cgit v1.2.1