-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhtml_template.py
More file actions
76 lines (58 loc) · 2.51 KB
/
html_template.py
File metadata and controls
76 lines (58 loc) · 2.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import argparse
import os
import re
import sys
RE_INSTRUCTION = re.compile(r'{{(?P<instruction>.*)}}')
class Page:
def __init__(self, name: str):
self.name = name
def __repr__(self):
return f"Page({self.name})"
class Template:
def __init__(self, template_folder : str, template_name: str, page: Page|None = None):
if page is None:
page = Page(template_name)
self.page = page
self.template_folder = template_folder
self.template_name = template_name
def insert(self, template_name : str) -> str:
"""
Include a template file.
"""
return Template(self.template_folder, template_name, self.page).parse()
def eval(self, instruction: str, page : Page) -> str:
"""
Evaluate the instruction and return the result.
"""
return eval(instruction, {"page": page, "insert": self.insert})
def parse(self) -> str:
file_path = f"{self.template_folder}/{self.template_name}.template.html"
if not os.path.exists(file_path):
file_path = f"{self.template_folder}/{self.template_name}.template"
if not os.path.exists(file_path):
raise FileNotFoundError(f"Template file {file_path} not found.")
with open(file_path, 'r') as file:
template = file.read()
for match in RE_INSTRUCTION.finditer(template):
instruction = match.group('instruction')
try:
result = self.eval(instruction, self.page)
except Exception as e:
raise RuntimeError(f"Error evaluating instruction '{instruction}' in template '{self.template_name}': {e}")
else:
template = template.replace(match.group(0), str(result))
return template
def main():
parser = argparse.ArgumentParser(description="Parse a template file.")
parser.add_argument("template_folder", type=str, help="The folder containing the template files.")
parser.add_argument("template_name", type=str, help="The name of the template file (without extension).")
parser.add_argument("--output", '-o', type=str, help="Output file name (optional).", default=None)
args = parser.parse_args()
output = open(args.output, 'w') if args.output else sys.stdout
template_folder = args.template_folder
template_name = args.template_name
template_parser = Template(template_folder, template_name)
result = template_parser.parse()
print(result, file=output)
if __name__ == "__main__":
main()