summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNed Batchelder <ned@nedbatchelder.com>2009-10-25 13:03:44 -0400
committerNed Batchelder <ned@nedbatchelder.com>2009-10-25 13:03:44 -0400
commit88a2ab393509543432836fca3c0c186284ddb088 (patch)
treec5acb1822aebccd96a13728c94630a4dc02135b6
parent9d47577674f59b20551a85dec3e2911e6e617a3b (diff)
downloadpython-coveragepy-88a2ab393509543432836fca3c0c186284ddb088.tar.gz
Templite now allows comments within curly-hash markers.
-rw-r--r--coverage/templite.py6
-rw-r--r--test/test_templite.py10
2 files changed, 16 insertions, 0 deletions
diff --git a/coverage/templite.py b/coverage/templite.py
index 23c805b..3b2efeb 100644
--- a/coverage/templite.py
+++ b/coverage/templite.py
@@ -18,6 +18,10 @@ class Templite(object):
{% for var in list %}...{% endfor %}
+ Comments are within curly-hash markers::
+
+ {# This will be ignored #}
+
Construct a Templite with the template text, then use `render` against a
dictionary context to create a finished string.
@@ -61,6 +65,8 @@ class Templite(object):
def _prepare(self, text):
"""Convert Django-style data references into Python-native ones."""
+ # Remove comments.
+ text = re.sub(r"(?s){#.*?#}", "", text)
# Pull out loops.
text = re.sub(
r"(?s){% for ([a-z0-9_]+) in ([a-z0-9_.|]+) %}(.*?){% endfor %}",
diff --git a/test/test_templite.py b/test/test_templite.py
index 6102183..4c7545b 100644
--- a/test/test_templite.py
+++ b/test/test_templite.py
@@ -126,6 +126,16 @@ class TempliteTest(unittest.TestCase):
"123 and 123"
)
+ def test_comments(self):
+ # Single-line comments work:
+ self.try_render(
+ "Hello, {# Name goes here: #}{{name}}!",
+ {'name':'Ned'}, "Hello, Ned!")
+ # and so do multi-line comments:
+ self.try_render(
+ "Hello, {# Name\ngoes\nhere: #}{{name}}!",
+ {'name':'Ned'}, "Hello, Ned!")
+
if __name__ == '__main__':
unittest.main()