Skip to content

Commit

Permalink
Merge pull request #423 from malthe/issue-422-interpolation-multiple-…
Browse files Browse the repository at this point in the history
…escape

Fix issue with multiple dollar escape
  • Loading branch information
malthe authored Apr 8, 2024
2 parents 1b1277a + a5bd665 commit 6a94e9f
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 11 deletions.
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ Changes

In next release ...

- ...
- Fix an issue where $-sign interpolation escaping would not work
correctly when more than two such symbols appeared next to each
other.
(`#422 <https://github.com/malthe/chameleon/issues/422>`_)

4.5.3 (2024-04-05)
------------------
Expand Down
21 changes: 11 additions & 10 deletions src/chameleon/compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,11 +277,11 @@ class TranslationContext(Node):

class Interpolator:
braces_required_regex = re.compile(
r'(\$)?\$({(?P<expression>.*)})', re.DOTALL
r'\$({(?P<expression>.*)})', re.DOTALL
)

braces_optional_regex = re.compile(
r'(\$)?\$({(?P<expression>.*)}|(?P<variable>[A-Za-z][A-Za-z0-9_]*))',
r'\$({(?P<expression>.*)}|(?P<variable>[A-Za-z][A-Za-z0-9_]*))',
re.DOTALL,
)

Expand Down Expand Up @@ -335,6 +335,7 @@ def __call__(self, name, engine):

while text:
matched = text

m = self.regex.search(matched)
if m is None:
text = text.replace('$$', '$')
Expand All @@ -344,18 +345,18 @@ def __call__(self, name, engine):
part = text[:m.start()]
text = text[m.start():]

skip = text.startswith('$$')
if skip:
part = part + '$'

if part:
i = 0
length = len(part)
while i < length and part[-i - 1] == '$':
i += 1
skip = i & 1
part = part.replace('$$', '$')
node = ast.Str(s=part)
nodes.append(node)

if skip:
text = text[2:]
continue
if skip:
text = text[1:]
continue

if not body:
target = name
Expand Down
4 changes: 4 additions & 0 deletions src/chameleon/tests/inputs/007-content-interpolation.pt
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
${None or
'Hello world'}
$leftalone
$${'fred'}
$$${'fred'}
$$$${'fred'}
$$$$${'fred'}
<div>${None}</div>
<div>${1 &lt; 2 and 'Hello world' or None}</div>
<div>${} is ignored.</div>
Expand Down
4 changes: 4 additions & 0 deletions src/chameleon/tests/outputs/007.pt
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@

Hello world
$leftalone
${'fred'}
$fred
$${'fred'}
$$fred
<div></div>
<div>Hello world</div>
<div>${} is ignored.</div>
Expand Down

0 comments on commit 6a94e9f

Please sign in to comment.