Skip to content
Myr

add comments about lstrip code

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by David Lord · committed by GitHub · parent 7cf0e6d · Browse files · 1 file+37 added−20 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+37 added−20 removed

  • other:+37 added−20 removed

1 file+37 added−20 removed

modified:jinja2/lexer.py+37 added-20 removed
21 unmodified lines
22from jinja2._compat import implements_iterator, intern, iteritems, text_type23from jinja2.exceptions import TemplateSyntaxError24from jinja2.utils import LRUCache2526from ast import literal_eval # to support scientific notation2726# cache for the lexers. Exists in order to be able to have multiple27# environments with the same lexer28_lexer_cache = LRUCache(50)29
391 unmodified lines
421 return lexer422423424class OptionalLStrip(tuple):425 """A special tuple for marking a point in the state that can have426 lstrip applied.427 """428429 # Even though it looks like a no-op, creating instances fails430 # without this.431 def __new__(cls, *members, **kwargs):432 return super(OptionalLStrip, cls).__new__(cls, members)433434
30 unmodified lines
465466 # block suffix if trimming is enabled467 block_suffix_re = environment.trim_blocks and '\\n?' or ''468465 # strip leading spaces in blocks/comments if lstrip_blocks is enabled466 # use for example '{%+' to disable lstrip_blocks behavior467 self.lstrip_blocks_unless_re = c(r'[^ \t]') if environment.lstrip_blocks else None469 # If lstrip is enabled, it should not be applied if there is any470 # non-whitespace between the newline and block.471 self.lstrip_unless_re = c(r"[^ \t]") if environment.lstrip_blocks else None472473 self.newline_sequence = environment.newline_sequence474 self.keep_trailing_newline = environment.keep_trailing_newline475
113 unmodified lines
589 def tokeniter(self, source, name, filename=None, state=None):590 """This method tokenizes the text and returns the tokens in a591 generator. Use this method if you just want to tokenize a template.592 """589 neg_lstrip_re = self.lstrip_blocks_unless_re593 source = text_type(source)594 lines = source.splitlines()595 if self.keep_trailing_newline and source:596 for newline in ('\r\n', '\r', '\n'):
6 unmodified lines
603 stack = ['root']604 if state is not None and state != 'root':605 assert state in ('variable', 'block'), 'invalid state'606 stack.append(state + '_begin')604 else:605 state = 'root'607 statetokens = self.rules[stack[-1]]608 source_length = len(source)608609 balancing_stack = []610 lstrip_unless_re = self.lstrip_unless_re611612 while 1:613 # tokenizer loop614 for regex, tokens, new_state in statetokens:
15 unmodified lines
630 if isinstance(tokens, tuple):631 groups = m.groups()632633 if isinstance(tokens, OptionalLStrip):633 # state supports lstrip, determine override (-/+)634 strip_sign = next(group for group in groups[2::2]635 if group is not None)636 if strip_sign == '-':637 groups = (groups[0].rstrip(),) + groups[1:]638 elif strip_sign != '+' and neg_lstrip_re is not None \639 and not m.groupdict().get('variable_begin'):640 # no override, but lstrip_blocks enabled641 l_pos = groups[0].rfind('\n') + 1642 if not neg_lstrip_re.search(groups[0], l_pos):643 groups = (groups[0][:l_pos],) + groups[1:]634 # Rule supports lstrip. Match will look like635 # text, block type, whitespace control, type, control, ...636 text = groups[0]637638 # Skipping the text and first type, every other group is the639 # whitespace control for each type. One of the groups will be640 # -, +, or empty string instead of None.641 strip_sign = next(g for g in groups[2::2] if g is not None)642643 if strip_sign == "-":644 # Strip all whitespace between the text and the tag.645 groups = (text.rstrip(),) + groups[1:]646 elif (647 # Not marked for preserving whitespace.648 strip_sign != "+"649 # lstrip is enabled.650 and lstrip_unless_re is not None651 # Not a variable expression.652 and not m.groupdict().get("variable_begin")653 ):654 # The start of text between the last newline and the tag.655 l_pos = text.rfind('\n') + 1656657 # If there's only whitespace between the newline and the658 # tag, strip it.659 if not lstrip_unless_re.search(text, l_pos):660 groups = (text[:l_pos],) + groups[1:]661662 for idx, token in enumerate(tokens):663 # failure group664 if token.__class__ is Failure:665 raise token(lineno, filename)
93 unmodified lines