-
Notifications
You must be signed in to change notification settings - Fork 5
/
copypage.py
executable file
·70 lines (58 loc) · 1.84 KB
/
copypage.py
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
#!/usr/bin/python3
"""Assemble a web page"""
from mako.template import Template
from mako import exceptions
import os
import os.path as osp
import shutil
from html5print import HTMLBeautifier
import myArgs
class CopyPage:
"""Construct a page from a template"""
def __init__(self, src="src", dst="dist", root="."):
self.src = src
self.dst = dst
self.root = root
def include(self, name, **kwargs):
"""Render a template with traceback"""
with open(osp.join(self.src, name), "rt") as fp:
template = fp.read()
try:
html = Template(template).render(
**kwargs, include=self.include, copy=self.copy, link=self.link
)
except Exception:
print(exceptions.text_error_template().render())
raise
return html
def copy(self, fname):
"""Copy a file and return its name"""
_, ext = osp.splitext(fname)
spath = osp.join(self.src, fname)
oname = fname
path = osp.join(self.dst, oname)
os.makedirs(osp.dirname(path), exist_ok=True)
if ext in [".css"]:
content = self.include(fname)
with open(path, "wt") as fp:
fp.write(content)
else:
shutil.copyfile(spath, path)
return osp.relpath(oname, self.root)
def link(self, fname):
"""Link a file that is expected"""
return fname
def assemble(self, page):
"""Assemble a web page"""
html = self.include(page)
html = HTMLBeautifier.beautify(html, 4)
path = osp.join(self.dst, page)
with open(path, "wt") as fp:
fp.write(html)
if __name__ == "__main__":
args = myArgs.Parse()
cp = CopyPage()
try:
cp.assemble(osp.basename(args.extra_[0]))
except Exception as e:
print(e)