Skip to content

Commit

Permalink
Fix issue with multiple dollar escape
Browse files Browse the repository at this point in the history
  • Loading branch information
malthe committed Apr 8, 2024
1 parent 1b1277a commit 13e934e
Show file tree
Hide file tree
Showing 4 changed files with 22 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
20 changes: 10 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,17 @@ def __call__(self, name, engine):
part = text[:m.start()]
text = text[m.start():]

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

if part:
i = 0
while 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 13e934e

Please sign in to comment.