-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate.py
More file actions
executable file
·220 lines (181 loc) · 7.05 KB
/
generate.py
File metadata and controls
executable file
·220 lines (181 loc) · 7.05 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
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env python3
'''
Einfacher Generator für statische Websites
- Seiten in Markdown-Dateien mit Frontmatter
- Vorlagen im Mustache-Format
- Bilder werden in verschiedene Größen verkleinert
'''
import chevron
import frontmatter
import functools
import http.server
import markdown
import os
from PIL import Image, ImageOps
import re
import shutil
import sys
import webbrowser
import yaml
config = {}
templates = {}
pages = []
def load_config():
'''
Einstellungen aus Datei config.yaml lesen
'''
global config
config = yaml.load(open('config.yaml'), Loader=yaml.Loader)
def load_templates():
'''
Seitenvorlagen lesen
'''
global templates
dir = config['templates']
for filename in os.listdir(dir):
if not filename.endswith('.mustache'):
continue
name = os.path.splitext(filename)[0]
templates[name] = open(os.path.join(dir, filename)).read()
def collect_pages(root_dir, dir = '.'):
global pages
source_dir = os.path.join(root_dir, dir)
out_dir = os.path.join(config['output'], dir)
os.makedirs(out_dir, exist_ok=True)
with os.scandir(source_dir) as it:
for entry in it:
if entry.name.startswith('.'):
continue
if entry.is_dir():
collect_pages(root_dir, os.path.join(dir, entry.name))
else:
source_path = os.path.join(source_dir, entry.name)
print("SOURCE", source_path)
basename = os.path.splitext(entry.name)[0]
if entry.name.endswith('.md'):
out_filename = basename + '.html'
out_path = os.path.join(out_dir, out_filename)
article = frontmatter.load(source_path)
pages.append({
'source_path': source_path,
'out_path': out_path,
'dir': dir,
'basename': basename,
'is_index': basename == 'index',
'in_menu': basename != 'index' and not ('hide' in article.metadata and article.metadata['hide']),
'filename': out_filename,
'meta': article.metadata
})
elif entry.name.endswith('~'):
continue
else:
out_path = os.path.join(out_dir, entry.name)
pages.append({
'source_path': source_path,
'out_path': out_path,
'dir': dir,
'basename': basename,
'is_index': basename == 'index',
'in_menu': False,
'filename': entry.name,
})
def generate_pages():
'''
HTML-Seiten erzeugen und ins Zielverzeichnis kopieren.
Dateien im Markdown-Format werden nach HTML gewandelt und
über die Seitenvorlage 'article' in eine vollständige
HTML-Datei mit Kopf und Fuß gewandelt.
'''
for page in pages:
source_path = page['source_path']
out_path = page['out_path']
if 'meta' in page:
print(f"Generiere {out_path}")
with open(out_path, 'w') as out:
article = frontmatter.load(source_path)
if 'filter' in page['meta']:
mod = __import__(page['meta']['filter'], fromlist=[None])
filter = getattr(mod, 'filter')
page['meta'] = filter(page['meta'], config=config)
rendered_markdown = markdown.markdown(article.content)
content = chevron.render(
template=re.sub('{{>', '{{>', rendered_markdown),
partials_path='partials/',
data={
'page': page,
'site': config,
'pages': pages
},
warn=True
)
template = page['meta']['template'] if 'template' in page['meta'] else 'article'
html = chevron.render(
template=templates[template],
partials_path='partials/',
data={
'page': page,
'site': config,
'content': content,
'pages': pages
},
warn=True
)
out.write(html)
else:
print(f"Kopiere {out_path}")
shutil.copyfile(source_path, out_path)
def generate_images(root_dir, dir = '.'):
'''
Bilder in verschiedene Größen verkleinern und ins Zielverzeichnis kopieren.
'''
source_dir = os.path.join(root_dir, dir)
out_dir = os.path.join(config['output'], 'images', dir)
os.makedirs(out_dir, exist_ok=True)
with os.scandir(source_dir) as it:
for entry in it:
if entry.name.startswith('.'):
continue
if entry.is_dir():
generate_images(root_dir, os.path.join(dir, entry.name))
else:
source_path = os.path.join(source_dir, entry.name)
source_mtime = os.stat(source_path).st_mtime
base, ext = os.path.splitext(entry.name)
for size in config['image_sizes']:
out_path = os.path.join(out_dir, base + '-' + str(size) + ext)
try:
if os.stat(out_path).st_mtime > source_mtime:
continue
except:
pass
print(f"Bild {out_path}")
try:
image = Image.open(source_path)
#image.thumbnail((size, size))
thumb = ImageOps.fit(image, (size, size), Image.ANTIALIAS)
thumb.save(out_path)
except:
print(f"Konnte Bild {source_path} nicht verkleinern")
if __name__ == '__main__':
load_config()
if len(sys.argv) == 2:
if sys.argv[1] == 'serve':
# Wenn 'serve' angegeben, starte einen Webserver und öffne den Browser
port = 8000
print(f'Webserver auf http://localhost:{port}')
webbrowser.open(f'http://localhost:{port}')
handler = functools.partial(
http.server.SimpleHTTPRequestHandler, directory=config['output']
)
httpd = http.server.HTTPServer(('', port), handler)
httpd.serve_forever()
elif sys.argv[1] == 'clean':
shutil.rmtree(config['output'])
else:
# Sonst wandle Seiten, Bilder und kopiere sie zusammen mit den statischen Dateien
load_templates()
collect_pages(config['pages'])
print(yaml.dump(pages))
shutil.copytree(config['assets'], config['output'], dirs_exist_ok=True)
generate_pages()
generate_images(config['images'])