Skip to content

Commit 15ac720

Browse files
add more test patterns
1 parent d568be3 commit 15ac720

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,8 @@ from PyWSGIRef import BETA
175175
BETA.enable()
176176
```
177177
Currently to be tested are:<br/>
178+
- PyHTML python script blocks<br/>
179+
- PyHTML python if clause blocks<br/>
178180
- PyHTML include static resource blocks<br/><br/>
179181

180182
Thanks a lot for helping improving PyWSGIRef!

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[project]
22
name = "PyWSGIRef"
3-
version = "1.1.8"
3+
version = "1.1.9"
44
authors = [
55
{name="Leander Kafemann", email="[email protected]" },
66
]

src/PyWSGIRef/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ def about():
1717
"""
1818
Returns information about your release and other projects by Leander Kafemann
1919
"""
20-
return {"Version": (1, 1, 8), "Author": "Leander Kafemann", "date": "13.06.2025",\
21-
"recommend": ("Büro by LK", "pyimager by LK"), "feedbackTo": "[email protected]"}
20+
return {"Version": (1, 1, 9), "Author": "Leander Kafemann", "date": "21.06.2025",\
21+
"recommend": ("Buero by LK", "pyimager by LK"), "feedbackTo": "[email protected]"}
2222

2323
SCHABLONEN = TemplateDict()
2424

src/PyWSGIRef/patterns.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,8 @@
66

77
SCRIPT_PATTERN = r"<\{\{evalPyHTML-script: (.*?) :script-\}\}\>"
88

9-
STYLE_PATTERN = r"<\{\{evalPyHTML-style: (.*?) :style-\}\}\>"
9+
STYLE_PATTERN = r"<\{\{evalPyHTML-style: (.*?) :style-\}\}\>"
10+
11+
EVAL_BLOCK_PATTERN = r"<\{\{evalPyHTML-eval: (.*?) :eval-\}\}\>"
12+
13+
IF_BLOCK_PATTERN = r"<\{\{evalPyHTML-if: (.*?)\}\}\>(.*?)((<\{\{evalPyHTML-else\}\}\>(.*?))?)<\{\{evalPyHTML-endif\}\}\>"

src/PyWSGIRef/pyhtml.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,46 @@ def _replace_style_blocks(self):
7676
replacement = f"<style>{style_content}</style>"
7777
self.html = self.html[:idx] + replacement + self.html[idxEnd:]
7878

79+
def _replace_eval_blocks(self, context=None):
80+
"""
81+
Replaces eval replacement phrases with evaluated Python expressions.
82+
WARNING: This method uses eval, which can execute arbitrary code.
83+
"""
84+
if context is None:
85+
context = {}
86+
for match in re.finditer(EVAL_BLOCK_PATTERN, self.html, re.DOTALL):
87+
idx, idxEnd = match.span()
88+
code = match.group(1).strip()
89+
try:
90+
# eval für Ausdrücke, exec für Statements
91+
result = str(eval(code, {}, context))
92+
except Exception as e:
93+
result = f"<b>EvalError: {e}</b>"
94+
self.html = self.html[:idx] + result + self.html[idxEnd:]
95+
96+
def _replace_if_blocks(self, context=None):
97+
"""
98+
Replaces with html code based on the evaluation of conditions in if blocks.
99+
"""
100+
if context is None:
101+
context = {}
102+
# re.DOTALL für mehrzeilige Blöcke
103+
while True:
104+
match = re.search(IF_BLOCK_PATTERN, self.html, re.DOTALL)
105+
if not match:
106+
break
107+
condition = match.group(1).strip()
108+
if_content = match.group(2)
109+
else_content = match.group(5) if match.group(5) is not None else ""
110+
try:
111+
if eval(condition, {}, context):
112+
replacement = if_content
113+
else:
114+
replacement = else_content
115+
except Exception as e:
116+
replacement = f"<b>IfEvalError: {e}</b>"
117+
self.html = self.html[:match.start()] + replacement + self.html[match.end():]
118+
79119
def decode(self):
80120
"""
81121
Decodes the HTML content by replacing specific phrases and applying modern styling.
@@ -87,6 +127,8 @@ def decode(self):
87127
self._replace_style_blocks()
88128
if BETA.value:
89129
self._replace_includes()
130+
self._replace_eval_blocks()
131+
self._replace_if_blocks()
90132

91133
def decoded(self) -> str:
92134
"""

0 commit comments

Comments
 (0)