-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate.py
137 lines (75 loc) · 2.98 KB
/
generate.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""Script for generating Nodezator's static website.
https://nodezator.com
"""
### standard library imports
from pathlib import Path
from shutil import copytree, rmtree
from string import Template
from collections import deque
from operator import truediv
from functools import reduce
from ast import literal_eval
### grab relevant paths
HERE = Path(__file__).parent
sourcepath = HERE / 'content'
targetpath = HERE / 'output'
### make sure 'output' folder exists and is empty
if targetpath.is_dir():
rmtree(str(targetpath))
else:
targetpath.mkdir()
### copy images folder as-is
copytree(str(sourcepath / 'images'), str(targetpath / 'images'))
### grab templates
templates_dir = sourcepath / '_templates'
page_template = Template((templates_dir / 'page.html').read_text(encoding='utf-8'))
redirect_template = Template((templates_dir / 'redirect.html').read_text(encoding='utf-8'))
### grab default values
with open(str(sourcepath / '_defaults.pyl'), mode='r', encoding='utf-8') as f:
defaults = literal_eval(f.read())
### create a deque
dirnames = deque()
### grab website content data (pages, posts, etc.)
with open(str(sourcepath / '_site.pyl'), mode='r', encoding='utf-8') as f:
data = literal_eval(f.read())
### iterate over the website content data generating the .html pages
for key, value in data.items():
## if key is a string which ends with '.html', it refers to html content
## that must be built; in this case, the value is expected to be a dict
## with extra data about the page
if key.endswith('.html'):
## grab content
parts = Path(key).parts
content_source = reduce(truediv, parts, sourcepath)
with open(str(content_source), mode='r', encoding='utf-8') as f:
value['content'] = f.read()
## ensure directories exist on target path
dirnames.clear()
parent = content_source.parent
while True:
if parent != sourcepath:
dirnames.appendleft(parent.name)
else:
break
parent = parent.parent
current = targetpath
for dirname in dirnames:
current = current / dirname
if not current.exists():
current.mkdir()
## prepare data
page_data = defaults.copy()
page_data.update(value)
html_content = page_template.substitute(page_data)
## define destination
content_destination = reduce(truediv, Path(key).parts, targetpath)
## otherwise, if value is a string, treat is as an URL for redirection
elif isinstance(value, str):
## prepare data
html_content = redirect_template.substitute(link=value)
## prepare/define destination
subdirpath = targetpath / key
subdirpath.mkdir()
content_destination = subdirpath / 'index.html'
## copy the generated html content to its final destination
content_destination.write_text(html_content, encoding='utf-8')