From db388f453e3b4fe018fc3403722ff5ce50579080 Mon Sep 17 00:00:00 2001 From: Devin Fee Date: Fri, 13 Apr 2012 17:12:43 -0700 Subject: escaped double quotes are now unescaped --- pyramid/scaffolds/template.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pyramid/scaffolds/template.py b/pyramid/scaffolds/template.py index 39d0e4b3f..b587adb73 100644 --- a/pyramid/scaffolds/template.py +++ b/pyramid/scaffolds/template.py @@ -35,7 +35,8 @@ class Template(object): content = native_(content, fsenc) try: return bytes_( - substitute_double_braces(content, TypeMapper(vars)), fsenc) + substitute_escaped_double_braces( + substitute_double_braces(content, TypeMapper(vars))), fsenc) except Exception as e: _add_except(e, ' in file %s' % filename) raise @@ -149,6 +150,14 @@ def substitute_double_braces(content, values): return values[value] return double_brace_pattern.sub(double_bracerepl, content) +escaped_double_brace_pattern = re.compile(r'\\{\\{(?P.*?)\\}\\}') + +def substitute_escaped_double_braces(content): + def escaped_double_bracerepl(match): + value = match.group('escape_braced').strip() + return "{{%(value)s}}" % locals() + return escaped_double_brace_pattern.sub(escaped_double_bracerepl, content) + def _add_except(exc, info): # pragma: no cover if not hasattr(exc, 'args') or exc.args is None: return -- cgit v1.2.3 From eeadb3a763fe2da39fbdc4c645ac786257b3bd73 Mon Sep 17 00:00:00 2001 From: Takahiro Fujiwara Date: Mon, 18 Feb 2013 23:56:22 +0900 Subject: Added a test case for templating with escaped double braces. --- pyramid/tests/test_scaffolds/test_template.py | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/pyramid/tests/test_scaffolds/test_template.py b/pyramid/tests/test_scaffolds/test_template.py index d7cf638b6..da6caffb9 100644 --- a/pyramid/tests/test_scaffolds/test_template.py +++ b/pyramid/tests/test_scaffolds/test_template.py @@ -11,7 +11,7 @@ class TestTemplate(unittest.TestCase): inst = self._makeOne() result = inst.render_template('{{a}} {{b}}', {'a':'1', 'b':'2'}) self.assertEqual(result, bytes_('1 2')) - + def test_render_template_expr_failure(self): inst = self._makeOne() self.assertRaises(AttributeError, inst.render_template, @@ -37,6 +37,11 @@ class TestTemplate(unittest.TestCase): result = inst.render_template('{{a}}', {'a':None}) self.assertEqual(result, b'') + def test_render_template_with_escaped_double_quotes(self): + inst = self._makeOne() + result = inst.render_template('{{a}} {{b}} \{\{a\}\} \{\{c\}\}', {'a':'1', 'b':'2'}) + self.assertEqual(result, bytes_('1 2 {{a}} {{c}}')) + def test_module_dir(self): import sys import pkg_resources @@ -90,7 +95,7 @@ class TestTemplate(unittest.TestCase): 'overwrite':False, 'interactive':False, }) - + def test_write_files_path_missing(self): L = [] inst = self._makeOne() @@ -132,9 +137,9 @@ class DummyOptions(object): simulate = False overwrite = False interactive = False - + class DummyCommand(object): options = DummyOptions() verbosity = 1 - - + + -- cgit v1.2.3 From de6b2875091e0b03e22e2e51ce0db938ecf4b98a Mon Sep 17 00:00:00 2001 From: Takahiro Fujiwara Date: Tue, 19 Feb 2013 02:20:12 +0900 Subject: Added edged test cases for templating with escpaed doubled braces. --- pyramid/scaffolds/template.py | 6 +++--- pyramid/tests/test_scaffolds/test_template.py | 12 +++++++++++- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/pyramid/scaffolds/template.py b/pyramid/scaffolds/template.py index b587adb73..d88f5b2a6 100644 --- a/pyramid/scaffolds/template.py +++ b/pyramid/scaffolds/template.py @@ -149,15 +149,15 @@ def substitute_double_braces(content, values): value = match.group('braced').strip() return values[value] return double_brace_pattern.sub(double_bracerepl, content) - -escaped_double_brace_pattern = re.compile(r'\\{\\{(?P.*?)\\}\\}') + +escaped_double_brace_pattern = re.compile(r'\\{\\{(?P[^\\]*?)\\}\\}') def substitute_escaped_double_braces(content): def escaped_double_bracerepl(match): value = match.group('escape_braced').strip() return "{{%(value)s}}" % locals() return escaped_double_brace_pattern.sub(escaped_double_bracerepl, content) - + def _add_except(exc, info): # pragma: no cover if not hasattr(exc, 'args') or exc.args is None: return diff --git a/pyramid/tests/test_scaffolds/test_template.py b/pyramid/tests/test_scaffolds/test_template.py index da6caffb9..2e961c516 100644 --- a/pyramid/tests/test_scaffolds/test_template.py +++ b/pyramid/tests/test_scaffolds/test_template.py @@ -37,11 +37,21 @@ class TestTemplate(unittest.TestCase): result = inst.render_template('{{a}}', {'a':None}) self.assertEqual(result, b'') - def test_render_template_with_escaped_double_quotes(self): + def test_render_template_with_escaped_double_braces(self): inst = self._makeOne() result = inst.render_template('{{a}} {{b}} \{\{a\}\} \{\{c\}\}', {'a':'1', 'b':'2'}) self.assertEqual(result, bytes_('1 2 {{a}} {{c}}')) + def test_render_template_with_breaking_escaped_braces(self): + inst = self._makeOne() + result = inst.render_template('{{a}} {{b}} \{\{a\} \{b\}\}', {'a':'1', 'b':'2'}) + self.assertEqual(result, bytes_('1 2 \{\{a\} \{b\}\}')) + + def test_render_template_with_escaped_single_braces(self): + inst = self._makeOne() + result = inst.render_template('{{a}} {{b}} \{a\} \{b', {'a':'1', 'b':'2'}) + self.assertEqual(result, bytes_('1 2 \{a\} \{b')) + def test_module_dir(self): import sys import pkg_resources -- cgit v1.2.3 From 7aa3cb81fac320ba1d35c221d5e7aec6f470361f Mon Sep 17 00:00:00 2001 From: Takahiro Fujiwara Date: Wed, 28 Aug 2013 13:36:53 +0900 Subject: Added me to the CONTRIBUTERS.txt. --- CONTRIBUTORS.txt | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 971c172f8..2bfc6c386 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -192,3 +192,5 @@ Contributors - Robert Jackiewicz, 2012/11/12 - John Anderson, 2012/11/14 + +- Takahiro Fujiwara, 2013/08/28 -- cgit v1.2.3