From 5dd7fa7c8df7f186abda7ac463c44fc70953cc27 Mon Sep 17 00:00:00 2001 From: Chris Jerdonek Date: Sat, 5 May 2012 16:23:50 -0700 Subject: Moved the default delimiters into pystache.defaults. --- pystache/defaults.py | 3 +++ pystache/parser.py | 20 +++++++++++--------- pystache/tests/test_parser.py | 3 ++- 3 files changed, 16 insertions(+), 10 deletions(-) (limited to 'pystache') diff --git a/pystache/defaults.py b/pystache/defaults.py index a510cfe..0a20328 100644 --- a/pystache/defaults.py +++ b/pystache/defaults.py @@ -38,6 +38,9 @@ STRING_ENCODING = sys.getdefaultencoding() # strings that arise from files. FILE_ENCODING = sys.getdefaultencoding() +# The delimiters to start with when parsing. +DELIMITERS = (u'{{', u'}}') + # How to handle missing tags when rendering a template. MISSING_TAGS = MissingTags.ignore diff --git a/pystache/parser.py b/pystache/parser.py index 5215c64..9bfc30c 100644 --- a/pystache/parser.py +++ b/pystache/parser.py @@ -1,39 +1,41 @@ # coding: utf-8 """ -Provides a class for parsing template strings. - -This module is only meant for internal use by the renderengine module. +Exposes a parse() function to parse template strings. """ import re +from pystache.defaults import DELIMITERS from pystache.parsed import ParsedTemplate -DEFAULT_DELIMITERS = (u'{{', u'}}') END_OF_LINE_CHARACTERS = [u'\r', u'\n'] NON_BLANK_RE = re.compile(ur'^(.)', re.M) +# TODO: add some unit tests for this. def parse(template, delimiters=None): """ 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) return parser.parse(template) -def _compile_template_re(delimiters=None): +def _compile_template_re(delimiters): """ Return a regular expresssion object (re.RegexObject) instance. """ - if delimiters is None: - delimiters = DEFAULT_DELIMITERS - # The possible tag type characters following the opening tag, # excluding "=" and "{". tag_types = "!>&/#^" @@ -184,7 +186,7 @@ class _Parser(object): def __init__(self, delimiters=None): if delimiters is None: - delimiters = DEFAULT_DELIMITERS + delimiters = DELIMITERS self._delimiters = delimiters diff --git a/pystache/tests/test_parser.py b/pystache/tests/test_parser.py index 4aa0959..92248ea 100644 --- a/pystache/tests/test_parser.py +++ b/pystache/tests/test_parser.py @@ -7,6 +7,7 @@ Unit tests of parser.py. import unittest +from pystache.defaults import DELIMITERS from pystache.parser import _compile_template_re as make_re @@ -19,7 +20,7 @@ class RegularExpressionTestCase(unittest.TestCase): Test getting a key from a dictionary. """ - re = make_re() + re = make_re(DELIMITERS) match = re.search("b {{test}}") self.assertEqual(match.start(), 1) -- cgit v1.2.1