Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue with multiple dollar escape #423

Merged
merged 1 commit into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading