added support for line statement prefixes (cheetah/mako/erb like)
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 7c0116f · Browse files · 3 files·+30 added−9 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+30 added−9 removed
- other:+30 added−9 removed
Provenance
- Commit
bf7c4ad- Tree
b7223ff- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
7c0116f
3 files changed+30 added−9 removed
modified:jinja2/environment.py+5 added-3 removed
28 unmodified lines29 variable_start_string='{{',30 variable_end_string='}}',31 comment_start_string='{#',32 comment_end_string='#}',33+ line_statement_prefix=None,34 trim_blocks=False,34- template_charset='utf-8',35 loader=None):36 """Here the possible initialization parameters:37 38 ========================= ============================================6 unmodified lines45 `comment_start_string` the string marking the begin of a46 comment. defaults to ``'{#'``.47 `comment_end_string` the string marking the end of a comment.48 defaults to ``'#}'``.49+ `line_statement_prefix` If given and a string, this will be used as50+ prefix for line based statements. See the51+ documentation for more details.52 `trim_blocks` If this is set to ``True`` the first newline53 after a block is removed (block, not54 variable tag!). Defaults to ``False``.52- `template_charset` the charset of the templates.55 `loader` the loader which should be used.56 ========================= ============================================57 """58 3 unmodified lines62 self.variable_start_string = variable_start_string63 self.variable_end_string = variable_end_string64 self.comment_start_string = comment_start_string65 self.comment_end_string = comment_end_string66+ self.line_statement_prefix = line_statement_prefix67 self.trim_blocks = trim_blocks65- self.template_charset = template_charset68 69 # defaults70 self.filters = DEFAULT_FILTERS.copy()71 self.tests = DEFAULT_TESTS.copy()82 unmodified linesmodified:jinja2/lexer.py+24 added-3 removed
194 unmodified lines195 environment.variable_start_string,196 environment.variable_end_string,197 environment.comment_start_string,198 environment.comment_end_string,199+ environment.line_statement_prefix,200 environment.trim_blocks))201 202 # use the cached lexer if possible203 if key in _lexer_cache:22 unmodified lines226 e = re.escape227 228 # lexing rules for tags229 tag_rules = [229- (eol_re, 'eol', None),230 (whitespace_re, None, None),231 (float_re, 'float', None),232 (integer_re, 'integer', None),233 (c(r'\b(?:%s)\b' % '|'.join(sorted(keywords, key=lambda x: -len(x)))),27 unmodified lines261 ]262 if not self.no_variable_block:263 root_tag_rules.append(('variable',264 environment.variable_start_string))265- root_tag_rules.sort(lambda a, b: cmp(len(b[1]), len(a[1])))265+ root_tag_rules.sort(key=lambda x: len(x[1]))266 267+ # now escape the rules. This is done here so that the escape268+ # signs don't count for the lengths of the tags.269+ root_tag_rules = [(a, e(b)) for a, b in root_tag_rules]270+271+ # if we have a line statement prefix we need an extra rule for272+ # that. We add this rule *after* all the others.273+ if environment.line_statement_prefix is not None:274+ prefix = e(environment.line_statement_prefix)275+ root_tag_rules.insert(0, ('linestatement', '^\s*' + prefix))276+277 # block suffix if trimming is enabled278 block_suffix_re = environment.trim_blocks and '\\n?' or ''279 280 # global lexing rules5 unmodified lines286 e(environment.block_start_string),287 e(environment.block_start_string),288 e(environment.block_end_string)289 )] + [280- '(?P<%s_begin>\s*%s\-|%s)' % (n, e(r), e(r))290+ '(?P<%s_begin>\s*%s\-|%s)' % (n, r, r)291 for n, r in root_tag_rules292 ])), ('data', '#bygroup'), '#bygroup'),293 # data294 (c('.+'), 'data', None)37 unmodified lines332 e(environment.variable_end_string)333 )), 'variable_end', '#pop')334 ] + tag_rules335 336+ # the same goes for the line_statement_prefix337+ if environment.line_statement_prefix is not None:338+ self.rules['linestatement_begin'] = [339+ (c(r'\s*(\n|$)'), 'linestatement_end', '#pop')340+ ] + tag_rules341+342 def tokenize(self, source, filename=None):343 """344 Works like `tokeniter` but returns a tokenstream of tokens and not a345 generator or token tuples. Additionally all token values are already346 converted into types and postprocessed. For example keywords are347 already keyword tokens, not named tokens, comments are removed,348 integers and floats converted, strings unescaped etc.349 """350+ source = unicode(source)351 def generate():352 for lineno, token, value in self.tokeniter(source, filename):353 if token in ('comment_begin', 'comment', 'comment_end'):354 continue355+ elif token == 'linestatement_begin':356+ token = 'block_begin'357+ elif token == 'linestatement_end':358+ token = 'block_end'359 elif token == 'data':360 try:361 value = str(value)362 except UnicodeError:160 unmodified linesmodified:jinja2/parser.py+1 added-3 removed
26 unmodified lines27 """28 29 def __init__(self, environment, source, filename=None):30 self.environment = environment31- if isinstance(source, str):32- source = source.decode(environment.template_charset, 'ignore')31 if isinstance(filename, unicode):32 filename = filename.encode('utf-8')35- self.source = source33+ self.source = unicode(source)34 self.filename = filename35 self.closed = False36 self.no_variable_block = self.environment.lexer.no_variable_block37 self.stream = environment.lexer.tokenize(source, filename)624 unmodified lines