From 2bd385beb755caee756b9e9c671a2854a430edd3 Mon Sep 17 00:00:00 2001 From: Ian Bicking Date: Thu, 2 Sep 2010 01:12:34 -0500 Subject: Fix cases where whitespace isn't stripped properly (when there are multiple directive-only lines next to each other). --- tempita/__init__.py | 17 ++++++++++++----- tests/test_template.txt | 29 +++++++++++++++++++++++++++-- 2 files changed, 39 insertions(+), 7 deletions(-) diff --git a/tempita/__init__.py b/tempita/__init__.py index c4c872d..175ccb4 100644 --- a/tempita/__init__.py +++ b/tempita/__init__.py @@ -647,8 +647,8 @@ def lex(s, name=None, trim_whitespace=True, line_offset=0): chunks = trim_lex(chunks) return chunks -statement_re = re.compile(r'^(?:if |elif |else |for |def |inherit |default |py:)') -single_statements = ['endif', 'endfor', 'enddef', 'continue', 'break'] +statement_re = re.compile(r'^(?:if |elif |for |def |inherit |default |py:)') +single_statements = ['else', 'endif', 'endfor', 'enddef', 'continue', 'break'] trail_whitespace_re = re.compile(r'\n\r?[\t ]*$') lead_whitespace_re = re.compile(r'^[\t ]*\n') @@ -663,6 +663,7 @@ def trim_lex(tokens): >>> trim_lex(tokens) [('if x', (1, 3)), 'x\n', ('endif', (3, 3)), 'y'] """ + last_trim = None for i in range(len(tokens)): current = tokens[i] if isinstance(tokens[i], basestring_): @@ -682,12 +683,17 @@ def trim_lex(tokens): if (not isinstance(next, basestring_) or not isinstance(prev, basestring_)): continue - if ((not prev or trail_whitespace_re.search(prev) - or (i == 1 and not prev.strip())) + prev_ok = not prev or trail_whitespace_re.search(prev) + if i == 1 and not prev.strip(): + prev_ok = True + if last_trim is not None and last_trim + 2 == i and not prev.strip(): + prev_ok = 'last' + if (prev_ok and (not next or lead_whitespace_re.search(next) or (i == len(tokens)-2 and not next.strip()))): if prev: - if i == 1 and not prev.strip(): + if ((i == 1 and not prev.strip()) + or prev_ok == 'last'): tokens[i-1] = '' else: m = trail_whitespace_re.search(prev) @@ -695,6 +701,7 @@ def trim_lex(tokens): prev = prev[:m.start()+1] tokens[i-1] = prev if next: + last_trim = i if i == len(tokens)-2 and not next.strip(): tokens[i+1] = '' else: diff --git a/tests/test_template.txt b/tests/test_template.txt index 60c3afb..7c66a7f 100644 --- a/tests/test_template.txt +++ b/tests/test_template.txt @@ -9,7 +9,7 @@ example:: 'Hi Ian' >>> Template('Hi {{repr(name)}}').substitute(name='Ian') "Hi 'Ian'" - >>> Template('Hi {{name+1}}').substitute(name='Ian') + >>> Template('Hi {{name+1}}').substitute(name='Ian') # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... TypeError: cannot concatenate 'str' and 'int' objects at line 1 column 6 @@ -59,7 +59,7 @@ Also Python blocks:: And some syntax errors:: - >>> t = Template('{{if x}}', name='foo.html') + >>> t = Template('{{if x}}', name='foo.html') # doctest: +IGNORE_EXCEPTION_DETAIL Traceback (most recent call last): ... TemplateError: No {{endif}} at line 1 column 3 in foo.html @@ -180,3 +180,28 @@ Then we'll define a template that inherits:: >>> print tmpl2.substitute(master='test2').strip() Something: +Whitespace +========== + +Whitespace is removed from templates when a directive is on a line by +itself. For example:: + + >>> #import sys; sys.debug = True + >>> tmpl = Template('''\ + ... {{for i, item in enumerate(['a', 'b'])}} + ... {{if i % 2 == 0}} + ...
+ ... {{else}} + ...
+ ... {{endif}} + ... {{item}} + ...
+ ... {{endfor}}''') + >>> print tmpl.substitute() +
+ a +
+
+ b +
+ -- cgit v1.2.1