From 523bf4cdafc8a571efca64f8c5ae061026af3fce Mon Sep 17 00:00:00 2001 From: Armin Ronacher Date: Sat, 17 Nov 2007 23:45:04 +0100 Subject: [PATCH] fixed awkward lexer bug in jinja that was yet untested --HG-- branch : trunk --- jdebug.py | 2 +- jinja/lexer.py | 27 ++++++++++++--------------- jinja/parser.py | 4 ++++ tests/test_lexer.py | 6 ++++++ 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/jdebug.py b/jdebug.py index 219c876..f5463a6 100644 --- 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) diff --git a/jinja/lexer.py b/jinja/lexer.py index 3fb3aa0..974bc39 100644 --- a/jinja/lexer.py +++ b/jinja/lexer.py @@ -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(?:\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) ] } diff --git a/jinja/parser.py b/jinja/parser.py index b42ba74..ba9317a 100644 --- a/jinja/parser.py +++ b/jinja/parser.py @@ -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): diff --git a/tests/test_lexer.py b/tests/test_lexer.py index cc85b19..1296ea9 100644 --- a/tests/test_lexer.py +++ b/tests/test_lexer.py @@ -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 = '''\