diff --git a/CHANGES.rst b/CHANGES.rst index fed3aa0..36bbdb1 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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 `_) 4.5.3 (2024-04-05) ------------------ diff --git a/src/chameleon/compiler.py b/src/chameleon/compiler.py index 2ec2c7e..608ded1 100644 --- a/src/chameleon/compiler.py +++ b/src/chameleon/compiler.py @@ -277,11 +277,11 @@ class TranslationContext(Node): class Interpolator: braces_required_regex = re.compile( - r'(\$)?\$({(?P.*)})', re.DOTALL + r'\$({(?P.*)})', re.DOTALL ) braces_optional_regex = re.compile( - r'(\$)?\$({(?P.*)}|(?P[A-Za-z][A-Za-z0-9_]*))', + r'\$({(?P.*)}|(?P[A-Za-z][A-Za-z0-9_]*))', re.DOTALL, ) @@ -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('$$', '$') @@ -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 diff --git a/src/chameleon/tests/inputs/007-content-interpolation.pt b/src/chameleon/tests/inputs/007-content-interpolation.pt index c82ce97..5271887 100644 --- a/src/chameleon/tests/inputs/007-content-interpolation.pt +++ b/src/chameleon/tests/inputs/007-content-interpolation.pt @@ -14,6 +14,10 @@ ${None or 'Hello world'} $leftalone + $${'fred'} + $$${'fred'} + $$$${'fred'} + $$$$${'fred'}
${None}
${1 < 2 and 'Hello world' or None}
${} is ignored.
diff --git a/src/chameleon/tests/outputs/007.pt b/src/chameleon/tests/outputs/007.pt index 5e8783a..c813f11 100644 --- a/src/chameleon/tests/outputs/007.pt +++ b/src/chameleon/tests/outputs/007.pt @@ -13,6 +13,10 @@ Hello world $leftalone + ${'fred'} + $fred + $${'fred'} + $$fred
Hello world
${} is ignored.