From 694435b844760ca0443cf87bf73a537f06c37d7d Mon Sep 17 00:00:00 2001 From: Ned Batchelder Date: Mon, 11 Nov 2019 13:08:43 -0500 Subject: Templite {% joined %} is more convenient than trailing hyphens --- coverage/htmlfiles/pyfile.html | 52 +++++++++++++++++++++--------------------- coverage/templite.py | 19 +++++++++++---- 2 files changed, 41 insertions(+), 30 deletions(-) (limited to 'coverage') diff --git a/coverage/htmlfiles/pyfile.html b/coverage/htmlfiles/pyfile.html index 01659dd8..eb0f99c8 100644 --- a/coverage/htmlfiles/pyfile.html +++ b/coverage/htmlfiles/pyfile.html @@ -68,34 +68,34 @@
- {# These are the source lines, which are very sensitive to whitespace. -#} - {# The `{ # - # }` below are comments which slurp up the following space. -#} {% for line in lines -%} -

{#-#} - {{line.number}}{#-#} - {{line.html}} {#-#} - {% if line.context_list -%} - {#-#} - {% endif -%} - {# Things that should float right in the line. -#} - {#-#} - {% if line.annotate -%} - {{line.annotate}}{#-#} - {{line.annotate_long}}{#-#} - {% endif -%} - {% if line.contexts -%} - {#-#} - {% endif -%} - {#-#} - {# Things that should appear below the line. -#} - {% if line.context_list -%} - {#-#} - {% for context in line.context_list -%} - {{context}}{#-#} - {% endfor -%} - {#-#} - {% endif -%} + {% joined %} +

+ {{line.number}} + {{line.html}}  + {% if line.context_list %} + + {% endif %} + {# Things that should float right in the line. #} + + {% if line.annotate %} + {{line.annotate}} + {{line.annotate_long}} + {% endif %} + {% if line.contexts %} + + {% endif %} + + {# Things that should appear below the line. #} + {% if line.context_list %} + + {% for context in line.context_list %} + {{context}} + {% endfor %} + + {% endif %}

+ {% endjoined %} {% endfor %}
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)) -- cgit v1.2.1