diff options
| -rw-r--r-- | CHANGES.txt | 5 | ||||
| -rw-r--r-- | CONTRIBUTORS.txt | 2 | ||||
| -rw-r--r-- | pyramid/scaffolds/template.py | 13 | ||||
| -rw-r--r-- | pyramid/tests/test_scaffolds/test_template.py | 25 |
4 files changed, 38 insertions, 7 deletions
diff --git a/CHANGES.txt b/CHANGES.txt index a527f7c1e..41ecbde0f 100644 --- a/CHANGES.txt +++ b/CHANGES.txt @@ -4,6 +4,11 @@ Next Release Features -------- +- It is now possible to escape double braces in Pyramid scaffolds (unescaped, + these represent replacement values). You can use ``\{\{a\}\}`` to + represent a "bare" ``{{a}}``. See + https://github.com/Pylons/pyramid/pull/862 + - Add ``localizer`` property (reified) to the request. See https://github.com/Pylons/pyramid/issues/508. diff --git a/CONTRIBUTORS.txt b/CONTRIBUTORS.txt index 591dcd7e4..0ddaebf15 100644 --- a/CONTRIBUTORS.txt +++ b/CONTRIBUTORS.txt @@ -218,3 +218,5 @@ Contributors - Andreas Zeidler, 2013/08/15 - Matthew Wilkes, 2013/08/23 + +- Takahiro Fujiwara, 2013/08/28 diff --git a/pyramid/scaffolds/template.py b/pyramid/scaffolds/template.py index 39d0e4b3f..d88f5b2a6 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 @@ -148,7 +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<escape_braced>[^\\]*?)\\}\\}') + +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 d7cf638b6..2e961c516 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,21 @@ class TestTemplate(unittest.TestCase): result = inst.render_template('{{a}}', {'a':None}) self.assertEqual(result, b'') + 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 @@ -90,7 +105,7 @@ class TestTemplate(unittest.TestCase): 'overwrite':False, 'interactive':False, }) - + def test_write_files_path_missing(self): L = [] inst = self._makeOne() @@ -132,9 +147,9 @@ class DummyOptions(object): simulate = False overwrite = False interactive = False - + class DummyCommand(object): options = DummyOptions() verbosity = 1 - - + + |
