Commit 5dd7fa7c by Chris Jerdonek

Moved the default delimiters into pystache.defaults.

parent 01a2daf4
...@@ -38,6 +38,9 @@ STRING_ENCODING = sys.getdefaultencoding() ...@@ -38,6 +38,9 @@ STRING_ENCODING = sys.getdefaultencoding()
# strings that arise from files. # strings that arise from files.
FILE_ENCODING = sys.getdefaultencoding() FILE_ENCODING = sys.getdefaultencoding()
# The delimiters to start with when parsing.
DELIMITERS = (u'{{', u'}}')
# How to handle missing tags when rendering a template. # How to handle missing tags when rendering a template.
MISSING_TAGS = MissingTags.ignore MISSING_TAGS = MissingTags.ignore
......
# coding: utf-8 # coding: utf-8
""" """
Provides a class for parsing template strings. Exposes a parse() function to parse template strings.
This module is only meant for internal use by the renderengine module.
""" """
import re import re
from pystache.defaults import DELIMITERS
from pystache.parsed import ParsedTemplate from pystache.parsed import ParsedTemplate
DEFAULT_DELIMITERS = (u'{{', u'}}')
END_OF_LINE_CHARACTERS = [u'\r', u'\n'] END_OF_LINE_CHARACTERS = [u'\r', u'\n']
NON_BLANK_RE = re.compile(ur'^(.)', re.M) NON_BLANK_RE = re.compile(ur'^(.)', re.M)
# TODO: add some unit tests for this.
def parse(template, delimiters=None): def parse(template, delimiters=None):
""" """
Parse a unicode template string and return a ParsedTemplate instance. Parse a unicode template string and return a ParsedTemplate instance.
Arguments:
template: a unicode template string.
delimiters: a 2-tuple of delimiters. Defaults to the package default.
""" """
parser = _Parser(delimiters) parser = _Parser(delimiters)
return parser.parse(template) return parser.parse(template)
def _compile_template_re(delimiters=None): def _compile_template_re(delimiters):
""" """
Return a regular expresssion object (re.RegexObject) instance. Return a regular expresssion object (re.RegexObject) instance.
""" """
if delimiters is None:
delimiters = DEFAULT_DELIMITERS
# The possible tag type characters following the opening tag, # The possible tag type characters following the opening tag,
# excluding "=" and "{". # excluding "=" and "{".
tag_types = "!>&/#^" tag_types = "!>&/#^"
...@@ -184,7 +186,7 @@ class _Parser(object): ...@@ -184,7 +186,7 @@ class _Parser(object):
def __init__(self, delimiters=None): def __init__(self, delimiters=None):
if delimiters is None: if delimiters is None:
delimiters = DEFAULT_DELIMITERS delimiters = DELIMITERS
self._delimiters = delimiters self._delimiters = delimiters
......
...@@ -7,6 +7,7 @@ Unit tests of parser.py. ...@@ -7,6 +7,7 @@ Unit tests of parser.py.
import unittest import unittest
from pystache.defaults import DELIMITERS
from pystache.parser import _compile_template_re as make_re from pystache.parser import _compile_template_re as make_re
...@@ -19,7 +20,7 @@ class RegularExpressionTestCase(unittest.TestCase): ...@@ -19,7 +20,7 @@ class RegularExpressionTestCase(unittest.TestCase):
Test getting a key from a dictionary. Test getting a key from a dictionary.
""" """
re = make_re() re = make_re(DELIMITERS)
match = re.search("b {{test}}") match = re.search("b {{test}}")
self.assertEqual(match.start(), 1) self.assertEqual(match.start(), 1)
......
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