fixed awkward lexer bug in jinja that was yet untested
authorArmin Ronacher <armin.ronacher@active-4.com>
Sat, 17 Nov 2007 22:45:04 +0000 (23:45 +0100)
committerArmin Ronacher <armin.ronacher@active-4.com>
Sat, 17 Nov 2007 22:45:04 +0000 (23:45 +0100)
--HG--
branch : trunk

jdebug.py
jinja/lexer.py
jinja/parser.py
tests/test_lexer.py

index 219c876c65e4e5d55025ba48b6c201944d62bffe..f5463a60575d97f26eaa5f7c1d51631f33d35e3f 100644 (file)
--- a/jdebug.py
+++ b/jdebug.py
@@ -31,7 +31,7 @@ def p(x=None, f=None):
     print PythonTranslator(e, Parser(e, x, f).parse(), None).translate()
 
 def l(x):
-    for token in e.lexer.tokenize(x):
+    for item in e.lexer.tokenize(x):
         print '%5s  %-20s  %r' % (item.lineno,
                                   item.type,
                                   item.value)
index 3fb3aa043b0221806c4a6f4875d553a55fdbe549..974bc39cf117528c92af5d80763a44aa81b6223e 100644 (file)
@@ -276,19 +276,16 @@ class Lexer(object):
         # global lexing rules
         self.rules = {
             'root': [
-                # raw directive
-                (c('((?:\s*%s\-|%s)\s*raw\s*(?:\-%s\s*|%s%s))' % (
-                    e(environment.block_start_string),
-                    e(environment.block_start_string),
-                    e(environment.block_end_string),
-                    e(environment.block_end_string),
-                    block_suffix_re
-                )), (None,), 'raw'),
-                # normal directives
-                (c('(.*?)(?:%s)' % '|'.join([
-                    '(?P<%s_begin>\s*%s\-|%s)' % (n, e(r), e(r))
-                   for n, r in root_tag_rules
-                ])), ('data', '#bygroup'), '#bygroup'),
+                # directives
+                (c('(.*?)(?:%s)' % '|'.join(
+                    ['(?P<raw_begin>(?:\s*%s\-|%s)\s*raw\s*%s)' % (
+                        e(environment.block_start_string),
+                        e(environment.block_start_string),
+                        e(environment.block_end_string)
+                    )] + [
+                        '(?P<%s_begin>\s*%s\-|%s)' % (n, e(r), e(r))
+                        for n, r in root_tag_rules
+                    ])), ('data', '#bygroup'), '#bygroup'),
                 # data
                 (c('.+'), 'data', None)
             ],
@@ -310,14 +307,14 @@ class Lexer(object):
                 )), 'block_end', '#pop'),
             ] + tag_rules,
             # raw block
-            'raw': [
+            'raw_begin': [
                 (c('(.*?)((?:\s*%s\-|%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (
                     e(environment.block_start_string),
                     e(environment.block_start_string),
                     e(environment.block_end_string),
                     e(environment.block_end_string),
                     block_suffix_re
-                )), ('data', None), '#pop'),
+                )), ('data', 'raw_end'), '#pop'),
                 (c('(.)'), (Failure('Missing end of raw directive'),), None)
             ]
         }
index b42ba749b45fdf2a2f22f3698fc66b3f17999618..ba9317a90817824e8fff0f9568251496292de06c 100644 (file)
@@ -1080,6 +1080,10 @@ class Parser(object):
                 next()
                 push_variable()
                 self.stream.expect('variable_end')
+            elif token_type == 'raw_begin':
+                next()
+                push_data()
+                self.stream.expect('raw_end')
             elif token_type == 'block_begin':
                 next()
                 if test is not None and test(self.stream.current):
index cc85b198e4e199a09756019fbd3d6f84a011762e..1296ea9447a0645e0ee1f8ac4d5e0ba1f1d10022 100644 (file)
@@ -7,6 +7,7 @@
     :license: BSD, see LICENSE for more details.
 """
 
+RAW = '{% raw %}foo{% endraw %}|{%raw%}{{ bar }}|{% baz %}{%       endraw    %}'
 BALANCING = '''{% for item in seq %}${{'foo': item}|upper}{% endfor %}'''
 COMMENTS = '''\
 <ul>
@@ -17,6 +18,11 @@ COMMENTS = '''\
 BYTEFALLBACK = u'''{{ 'foo'|pprint }}|{{ 'bär'|pprint }}'''
 
 
+def test_raw(env):
+    tmpl = env.from_string(RAW)
+    assert tmpl.render() == 'foo|{{ bar }}|{% baz %}'
+
+
 def test_balancing():
     from jinja import Environment
     env = Environment('{%', '%}', '${', '}')