diff options
author | Ned Batchelder <ned@nedbatchelder.com> | 2019-11-11 13:08:43 -0500 |
---|---|---|
committer | Ned Batchelder <ned@nedbatchelder.com> | 2019-11-11 13:09:59 -0500 |
commit | 694435b844760ca0443cf87bf73a537f06c37d7d (patch) | |
tree | d566b186426ed17872463250b1e67632e1f28490 /coverage/templite.py | |
parent | 99f8007f4210b3117c0bfc963ad6873e9b263408 (diff) | |
download | python-coveragepy-git-694435b844760ca0443cf87bf73a537f06c37d7d.tar.gz |
Templite {% joined %} is more convenient than trailing hyphens
Diffstat (limited to 'coverage/templite.py')
-rw-r--r-- | coverage/templite.py | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/coverage/templite.py b/coverage/templite.py index b546ef7c..7d4024e0 100644 --- a/coverage/templite.py +++ b/coverage/templite.py @@ -90,7 +90,10 @@ class Templite(object): {# This will be ignored #} - Any of these constructs can have a hypen at the end (`-}}`, `-%}`, `-#}`), + Lines between `{% joined %}` and `{% endjoined %}` will have lines stripped + and joined. Be careful, this could join words together! + + Any of these constructs can have a hyphen at the end (`-}}`, `-%}`, `-#}`), which will collapse the whitespace following the tag. Construct a Templite with the template text, then use `render` against a @@ -154,7 +157,7 @@ class Templite(object): # Split the text to form a list of tokens. tokens = re.split(r"(?s)({{.*?}}|{%.*?%}|{#.*?#})", text) - squash = False + squash = in_joined = False for token in tokens: if token.startswith('{'): @@ -196,6 +199,9 @@ class Templite(object): ) ) code.indent() + elif words[0] == 'joined': + ops_stack.append('joined') + in_joined = True elif words[0].startswith('end'): # Endsomething. Pop the ops stack. if len(words) != 1: @@ -206,12 +212,17 @@ class Templite(object): start_what = ops_stack.pop() if start_what != end_what: self._syntax_error("Mismatched end tag", end_what) - code.dedent() + if end_what == 'joined': + in_joined = False + else: + code.dedent() else: self._syntax_error("Don't understand tag", words[0]) else: # Literal content. If it isn't empty, output it. - if squash: + if in_joined: + token = re.sub(r"\s*\n\s*", "", token.strip()) + elif squash: token = token.lstrip() if token: buffered.append(repr(token)) |