Commit 369faa88 by Chris Jerdonek

Tightened up code in Template._render().

 * Used re.split() instead of re.search() to avoid string slicing.
 * Used "".join() once instead of concatenating strings.
 * Removed the need for repeating logic on loop exit (i.e. an additional
   application of self._render_tags(template) and output += ...).
parent c2fcba50
...@@ -124,23 +124,30 @@ class Template(object): ...@@ -124,23 +124,30 @@ class Template(object):
# The section contents include white space to comply with the spec's # The section contents include white space to comply with the spec's
# requirement that sections not alter surrounding whitespace. # requirement that sections not alter surrounding whitespace.
section = r"%(otag)s[\#|^]([^\}]*)%(ctag)s(.+?)%(otag)s/\1%(ctag)s" section = r"%(otag)s([#|^])([^\}]*)%(ctag)s(.+?)%(otag)s/\2%(ctag)s" % tags
self.section_re = re.compile(section % tags, re.M|re.S) self.section_re = re.compile(section, re.M|re.S)
tag = r"%(otag)s(#|=|&|!|>|\{)?(.+?)\1?%(ctag)s+" tag = r"%(otag)s(#|=|&|!|>|\{)?(.+?)\1?%(ctag)s+" % tags
self.tag_re = re.compile(tag % tags) self.tag_re = re.compile(tag)
def _render(self, template): def _render(self, template):
output = '' output = []
while True: while True:
match = self.section_re.search(template) parts = self.section_re.split(template, maxsplit=1)
if match is None:
start = self._render_tags(parts[0])
output.append(start)
if len(parts) < 2:
# Then there was no match.
break break
section, section_key, section_contents = match.group(0, 1, 2) section_type, section_key, section_contents, template = parts[1:]
section_key = section_key.strip() section_key = section_key.strip()
section_value = self.context.get(section_key, None) section_value = self.context.get(section_key, None)
replacer = '' replacer = ''
# Callable # Callable
...@@ -149,31 +156,27 @@ class Template(object): ...@@ -149,31 +156,27 @@ class Template(object):
# Dictionary # Dictionary
elif section_value and hasattr(section_value, 'keys') and hasattr(section_value, '__getitem__'): elif section_value and hasattr(section_value, 'keys') and hasattr(section_value, '__getitem__'):
if section[2] != '^': if section_type != '^':
replacer = self._render_dictionary(section_contents, section_value) replacer = self._render_dictionary(section_contents, section_value)
# Lists # Lists
elif section_value and hasattr(section_value, '__iter__'): elif section_value and hasattr(section_value, '__iter__'):
if section[2] != '^': if section_type != '^':
replacer = self._render_list(section_contents, section_value) replacer = self._render_list(section_contents, section_value)
# Other objects # Other objects
elif section_value and isinstance(section_value, object): elif section_value and isinstance(section_value, object):
if section[2] != '^': if section_type != '^':
replacer = self._render_dictionary(section_contents, section_value) replacer = self._render_dictionary(section_contents, section_value)
# Falsey and Negated or Truthy and Not Negated # Falsey and Negated or Truthy and Not Negated
elif (not section_value and section[2] == '^') or (section_value and section[2] != '^'): elif (not section_value and section_type == '^') or (section_value and section_type != '^'):
replacer = self._render_dictionary(section_contents, section_value) replacer = self._render_dictionary(section_contents, section_value)
# Render template prior to section too # Render template prior to section too
output += self._render_tags(template[0:match.start()]) + replacer output.append(replacer)
template = template[match.end():]
# Render remainder
output += self._render_tags(template)
output = "".join(output)
return output return output
def _render_tags(self, template): def _render_tags(self, template):
......
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