Commit 41c92afc by Pieter van de Bruggen

Refactoring the match handling logic.

parent 50bdc535
...@@ -43,6 +43,14 @@ class Template(object): ...@@ -43,6 +43,14 @@ class Template(object):
if match is None: if match is None:
break break
pos = self._handle_match(template, match, buffer)
# Save the rest of the template.
buffer.append(template[pos:])
return buffer
def _handle_match(self, template, match, buffer):
# Normalize the captures dictionary. # Normalize the captures dictionary.
captures = match.groupdict() captures = match.groupdict()
if captures['change'] is not None: if captures['change'] is not None:
...@@ -67,21 +75,18 @@ class Template(object): ...@@ -67,21 +75,18 @@ class Template(object):
captures['whitespace'] = '' captures['whitespace'] = ''
# TODO: Process the remaining tag types. # TODO: Process the remaining tag types.
print captures['name']
fetch = lambda view: unicode(view.get(captures['name']))
if captures['tag'] == '!': if captures['tag'] == '!':
pass pass
elif captures['tag'] in ['{', '&']: elif captures['tag'] in ['{', '&']:
def unescapedTag(view): buffer.append(fetch)
return view.get(captures['name'])
buffer.append(unescapedTag)
elif captures['tag'] == '': elif captures['tag'] == '':
def escapedTag(view): buffer.append(lambda view: cgi.escape(fetch(view), True))
return cgi.escape(view.get(captures['name'])) else:
buffer.append(escapedTag) print 'Error!'
# Save the rest of the template. return pos
buffer.append(template[pos:])
return buffer
def render(self, encoding=None): def render(self, encoding=None):
parsed = self._parse(self.template) parsed = self._parse(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