From 8bd0765ab6316b04d9c879f50aface533b9a4d5e Mon Sep 17 00:00:00 2001 From: toxicphreAK Date: Tue, 22 Nov 2022 07:38:33 +0100 Subject: [PATCH 1/2] Improve single line comment replacement --- chameleon.py | 16 ++-------------- 1 file changed, 2 insertions(+), 14 deletions(-) diff --git a/chameleon.py b/chameleon.py index 8eb0ec8..c4ff11c 100644 --- a/chameleon.py +++ b/chameleon.py @@ -460,23 +460,11 @@ def replace_comments(self): _content = self.eol.join(rows) # Single Line Comments - slc_pattern = re.compile(r"#.+") + slc_pattern = re.compile(r"#[^(\"|')]+?\n") s1_pattern = re.compile(r"\"([^\"]*)\"") s2_pattern = re.compile(r"'([^']*)'") - for line in rows: - match = slc_pattern.search(line) - if match: - # Single string, we don't do anything = won't break the script - res = [s1_pattern.search(line), s2_pattern.search(line)] - if any(res): - res = [r.group() for r in res if r and r.group().find("#") > -1] - else: - res = [] - if len(res) > 0: - continue - _content = _content.replace(match.group(), self.placeholder) - self.content = _content + self.content = slc_pattern.sub(f"{self.placeholder}\n", _content) def remove_comment_placeholders(self): while self.content.find(self.placeholder) >= 0: From 1d8b0010a4ea75afe33f45cd2a32664b4603a29d Mon Sep 17 00:00:00 2001 From: toxicphreAK Date: Tue, 22 Nov 2022 07:38:47 +0100 Subject: [PATCH 2/2] Bugfix function replacement pattern, as regex was not working --- chameleon.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chameleon.py b/chameleon.py index c4ff11c..ef4bbed 100644 --- a/chameleon.py +++ b/chameleon.py @@ -670,7 +670,7 @@ def replace_variables(self): match.group() + "\n" + "\n".join([f"{v} = {k}" for k, v in special_vars.items()]) ) - var_pattern = re.compile(r'\$[\w|_]+') + var_pattern = re.compile(r'\$\w+') matches = [match.group() for match in var_pattern.finditer(self.content)] matches = list(set(matches)) matches.sort(key=len)