fixed awkward lexer bug in jinja that was yet untested
Before adoption: this commit came in with the history the project adopted, so no landing records it.
--HG-- branch : trunk
authored by Armin Ronacher · committed by Armin Ronacher · parent 256c304 · Browse files · 4 files·+23 added−16 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+23 added−16 removed
- tests:+6 added−0 removed
- other:+17 added−16 removed
Provenance
- Commit
523bf4c- Tree
5f1dad6- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
256c304
4 files changed+23 added−16 removed
modified:jinja/lexer.py+12 added-15 removed
274 unmodified lines275 276 # global lexing rules277 self.rules = {278 'root': [279- # raw directive280- (c('((?:\s*%s\-|%s)\s*raw\s*(?:\-%s\s*|%s%s))' % (281- e(environment.block_start_string),282- e(environment.block_start_string),283- e(environment.block_end_string),284- e(environment.block_end_string),285- block_suffix_re286- )), (None,), 'raw'),287- # normal directives288- (c('(.*?)(?:%s)' % '|'.join([289- '(?P<%s_begin>\s*%s\-|%s)' % (n, e(r), e(r))290- for n, r in root_tag_rules291- ])), ('data', '#bygroup'), '#bygroup'),279+ # directives280+ (c('(.*?)(?:%s)' % '|'.join(281+ ['(?P<raw_begin>(?:\s*%s\-|%s)\s*raw\s*%s)' % (282+ e(environment.block_start_string),283+ e(environment.block_start_string),284+ e(environment.block_end_string)285+ )] + [286+ '(?P<%s_begin>\s*%s\-|%s)' % (n, e(r), e(r))287+ for n, r in root_tag_rules288+ ])), ('data', '#bygroup'), '#bygroup'),289 # data290 (c('.+'), 'data', None)291 ],292 # comments13 unmodified lines306 block_suffix_re307 )), 'block_end', '#pop'),308 ] + tag_rules,309 # raw block313- 'raw': [310+ 'raw_begin': [311 (c('(.*?)((?:\s*%s\-|%s)\s*endraw\s*(?:\-%s\s*|%s%s))' % (312 e(environment.block_start_string),313 e(environment.block_start_string),314 e(environment.block_end_string),315 e(environment.block_end_string),316 block_suffix_re320- )), ('data', None), '#pop'),317+ )), ('data', 'raw_end'), '#pop'),318 (c('(.)'), (Failure('Missing end of raw directive'),), None)319 ]320 }321 192 unmodified linesmodified:jinja/parser.py+4 added
1078 unmodified lines1079 if token_type == 'variable_begin':1080 next()1081 push_variable()1082 self.stream.expect('variable_end')1083+ elif token_type == 'raw_begin':1084+ next()1085+ push_data()1086+ self.stream.expect('raw_end')1087 elif token_type == 'block_begin':1088 next()1089 if test is not None and test(self.stream.current):1090 if drop_needle:97 unmodified linesmodified:tests/test_lexer.py+6 added
5 unmodified lines6 :copyright: 2007 by Armin Ronacher.7 :license: BSD, see LICENSE for more details.8 """9 10+RAW = '{% raw %}foo{% endraw %}|{%raw%}{{ bar }}|{% baz %}{% endraw %}'11 BALANCING = '''{% for item in seq %}${{'foo': item}|upper}{% endfor %}'''12 COMMENTS = '''\13 <ul>14 <!--- for item in seq -->2 unmodified lines17 </ul>'''18 BYTEFALLBACK = u'''{{ 'foo'|pprint }}|{{ 'bär'|pprint }}'''19 20 21+def test_raw(env):22+ tmpl = env.from_string(RAW)23+ assert tmpl.render() == 'foo|{{ bar }}|{% baz %}'24+25+26 def test_balancing():27 from jinja import Environment28 env = Environment('{%', '%}', '${', '}')29 tmpl = env.from_string(BALANCING)31 unmodified linesmodified:jdebug.py+1 added-1 removed
29 unmodified lines30 x = e.loader.get_source(f)31 print PythonTranslator(e, Parser(e, x, f).parse(), None).translate()32 33 def l(x):34- for token in e.lexer.tokenize(x):34+ for item in e.lexer.tokenize(x):35 print '%5s %-20s %r' % (item.lineno,36 item.type,37 item.value)38 32 unmodified lines