Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Permit escaping of double braces in scaffolds, see #862

  • Loading branch information...
commit 1930ebfdc99e2ee274cc172281da4d73e4f1f66d 2 parents 58951c0 + 7aa3cb8
@mcdonc mcdonc authored
View
5 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.
View
2  CONTRIBUTORS.txt
@@ -218,3 +218,5 @@ Contributors
- Andreas Zeidler, 2013/08/15
- Matthew Wilkes, 2013/08/23
+
+- Takahiro Fujiwara, 2013/08/28
View
13 pyramid/scaffolds/template.py
@@ -35,7 +35,8 @@ def render_template(self, content, vars, filename=None):
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 double_bracerepl(match):
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
View
25 pyramid/tests/test_scaffolds/test_template.py
@@ -11,7 +11,7 @@ def test_render_template_success(self):
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 @@ def test_render_template_expr_value_is_None(self):
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 @@ def test_write_files_path_exists(self):
'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
-
-
+
+
Please sign in to comment.
Something went wrong with that request. Please try again.