summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorianb <devnull@localhost>2008-11-22 17:47:07 +0000
committerianb <devnull@localhost>2008-11-22 17:47:07 +0000
commit6651d6c7e59e141829045fe733103f255a9eb776 (patch)
tree45e07571f367cdafee3ca80abe18db92acada317
parentca8c5c11d97ede79fc869d3a8ca7bdb4cd76301c (diff)
downloadtempita-6651d6c7e59e141829045fe733103f255a9eb776.tar.gz
Fix whitespace stripping for statements at the beginning and end of the template
-rw-r--r--docs/index.txt4
-rw-r--r--tempita/__init__.py32
-rw-r--r--tests/test_template.txt5
3 files changed, 26 insertions, 15 deletions
diff --git a/docs/index.txt b/docs/index.txt
index 65cd520..f976ce9 100644
--- a/docs/index.txt
+++ b/docs/index.txt
@@ -473,7 +473,9 @@ svn trunk
* Added ``{{inherit}}`` and ``{{def}}`` for doing template inheritance.
-* Make error message annotation slightly more robust
+* Make error message annotation slightly more robust.
+
+* Fix whitespace stripping for the beginning and end of lines.
0.2
---
diff --git a/tempita/__init__.py b/tempita/__init__.py
index 6d4af7a..4a3a771 100644
--- a/tempita/__init__.py
+++ b/tempita/__init__.py
@@ -644,9 +644,9 @@ def lex(s, name=None, trim_whitespace=True):
chunks = trim_lex(chunks)
return chunks
-statement_re = re.compile(r'^(?:if |elif |else |for |py:)')
-single_statements = ['endif', 'endfor', 'continue', 'break']
-trail_whitespace_re = re.compile(r'\n[\t ]*$')
+statement_re = re.compile(r'^(?:if |elif |else |for |def |inherit |default |py:)')
+single_statements = ['endif', 'endfor', 'enddef', 'continue', 'break']
+trail_whitespace_re = re.compile(r'\n\r?[\t ]*$')
lead_whitespace_re = re.compile(r'^[\t ]*\n')
def trim_lex(tokens):
@@ -679,17 +679,25 @@ def trim_lex(tokens):
if (not isinstance(next, basestring)
or not isinstance(prev, basestring)):
continue
- if ((not prev or trail_whitespace_re.search(prev))
- and (not next or lead_whitespace_re.search(next))):
+ if ((not prev or trail_whitespace_re.search(prev)
+ or (i == 1 and not prev.strip()))
+ and (not next or lead_whitespace_re.search(next)
+ or (i == len(tokens)-2 and not next.strip()))):
if prev:
- m = trail_whitespace_re.search(prev)
- # +1 to leave the leading \n on:
- prev = prev[:m.start()+1]
- tokens[i-1] = prev
+ if i == 1 and not prev.strip():
+ tokens[i-1] = ''
+ else:
+ m = trail_whitespace_re.search(prev)
+ # +1 to leave the leading \n on:
+ prev = prev[:m.start()+1]
+ tokens[i-1] = prev
if next:
- m = lead_whitespace_re.search(next)
- next = next[m.end():]
- tokens[i+1] = next
+ if i == len(tokens)-2 and not next.strip():
+ tokens[i+1] = ''
+ else:
+ m = lead_whitespace_re.search(next)
+ next = next[m.end():]
+ tokens[i+1] = next
return tokens
diff --git a/tests/test_template.txt b/tests/test_template.txt
index 7481aaa..ec2bd75 100644
--- a/tests/test_template.txt
+++ b/tests/test_template.txt
@@ -119,6 +119,8 @@ contains a directive/statement (if/for, etc)::
'x=1\n'
>>> sub('{{if 1}}\nx={{x}}\n{{endif}}\n', x=1)
'x=1\n'
+ >>> sub(' {{if 1}} \nx={{x}}\n {{endif}} \n', x=1)
+ 'x=1\n'
There is a special directive that will create a default value
for a variable, if no value is given::
@@ -160,8 +162,7 @@ Then we'll define a template that inherits::
... ''')
>>> print tmpl.substitute(master='test').strip()
This is the parent test. The block: some text
- Then the body:
- Hi there!
+ Then the body: Hi there!
>>> tmpl2 = Template('''\
... {{def block(arg='hi_'+master):}}hey {{master}}: {{arg}}{{enddef}}
... ''', get_template=get_template, default_inherit='super_test')