From 06f5528f0b0b2c9a54afe0362b7b657c935844e1 Mon Sep 17 00:00:00 2001
From: Karl-Hermann Wieners <karl-hermann.wieners@mpimet.mpg.de>
Date: Fri, 25 Aug 2017 15:07:59 +0000
Subject: [PATCH] Disabled evaluation of standard Jinja blocks ({% ... %}) and
 comments ({# ... #}). This allows handling templates with standard Jinja code
 and shell scripts using array length expansion (${#ARRAY})

---
 CHANGES.txt |  3 +++
 mkexp       |  2 ++
 test.py     | 18 ++++++++++++++++++
 3 files changed, 23 insertions(+)

diff --git a/CHANGES.txt b/CHANGES.txt
index 8da6574..4762b2e 100644
--- a/CHANGES.txt
+++ b/CHANGES.txt
@@ -13,6 +13,9 @@ Templates
 ---------
 * Changed retrieval of templates for config files that alter EXP_ID
   Now tries config file name before using EXP_ID as template name
+* Disabled evaluation of standard Jinja blocks ({% ... %}) and comments
+  ({# ... #}). This allows handling templates with standard Jinja code and shell
+  scripts using array length expansion (${#ARRAY})
 
 Release 0.4.2
 =============
diff --git a/mkexp b/mkexp
index cc781b6..b7f4bdb 100755
--- a/mkexp
+++ b/mkexp
@@ -297,6 +297,8 @@ template_env = Environment(
     variable_end_string = '}',
     line_statement_prefix = '#%',
     line_comment_prefix = '#%#',
+    block_start_string = '{%DiSaBlEdAnDuNlIkElY',
+    comment_start_string = '{#DiSaBlEdAnDuNlIkElY',
     extensions=['jinja2.ext.do']
 )    
 
diff --git a/test.py b/test.py
index 3459f58..f9eb11b 100644
--- a/test.py
+++ b/test.py
@@ -407,6 +407,24 @@ class ContentTestCase(MkexpTestCase):
         result = align(result)
         self.assertMultiLineEqual(expected, result)
 
+class JinjaTemplateTestCase(MkexpSimpleTestCase):
+
+    def test_ignore_blocks(self):
+        self.run_test("""
+            {% set answer = 42 %}
+        """, """
+            {% set answer = 42 %}
+        """)
+
+    def test_ignore_comments(self):
+        self.run_test("""
+            {# no comment #}
+            ${#ARRAY}
+        """, """
+            {# no comment #}
+            ${#ARRAY}
+        """)
+
 class DefaultEnvironmentTestCase(MkexpSimpleTestCase):
 
     def test_basic(self):
-- 
GitLab