-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgenja.py
107 lines (86 loc) · 3.45 KB
/
genja.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
#!/usr/bin/python
import os
import shutil
import ruamel.yaml as yaml
from dateutil.parser import parse as parse_date
from jinja2 import FileSystemLoader, Environment, pass_context
TEMPLATES_PATH = 'templates'
STATIC_PATH = 'static'
CONTENT_PATH = 'content'
OUTPUT_PATH = 'output'
@pass_context
def relative_url(context, targetpath):
basepath = context['page']
basedir = os.path.dirname(basepath)
targetdir = os.path.dirname(os.path.join('.', targetpath))
diff = os.path.relpath(targetdir, basedir)
return os.path.join(diff, os.path.basename(targetpath))
def load_yaml(filepath):
with open(filepath) as f:
loader = yaml.YAML(typ='safe', pure=True)
return loader.load(f.read())
class Genja(object):
def __init__(self, *, templates=None, static=None, content=None, output=None):
self._templates = TEMPLATES_PATH if templates is None else templates
self._static = STATIC_PATH if static is None else static
self._content = CONTENT_PATH if content is None else content
self._output = OUTPUT_PATH if output is None else output
def clean(self):
# Remove output directory content if it exists
if os.path.exists(self._output):
for (dirpath, dirnames, filenames) in os.walk(self._output):
for filename in filenames:
os.unlink(os.path.join(dirpath, filename))
for dirname in dirnames:
shutil.rmtree(os.path.join(dirpath, dirname))
else:
os.makedirs(self._output, exist_ok=True)
def build(self):
self._build_content()
self._build_static()
def watch(self):
pass
def serve(self):
pass
def _build_static(self):
# Copy static content
for item in os.listdir(self._static):
source = os.path.join(self._static, item)
destination = os.path.join(self._output, item)
if os.path.isdir(source):
shutil.copytree(source, destination)
else:
shutil.copy2(source, destination)
def _build_content(self):
# Create environment for Jinja2
env = Environment(
loader=FileSystemLoader([self._templates, self._content]),
)
# Configure environment
env.globals.update({
'load_yaml': lambda fp: load_yaml(os.path.join(self._content, fp))
})
env.filters.update({
'relative': relative_url,
})
# Render all *.html pages if the name does not start with an underscore
for (dirpath, dirnames, filenames) in os.walk(self._content):
for filename in filenames:
filepath = os.path.join(dirpath, filename)[len(self._content) + 1:]
if filepath.startswith('_') or not filepath.endswith('.html'):
print('Ignoring {}'.format(filepath))
continue
print('Rendering {}'.format(filepath))
template = env.get_template(filepath)
# Create output file
output_filepath = os.path.join(self._output, filepath)
os.makedirs(os.path.dirname(output_filepath), exist_ok=True)
with open(output_filepath, 'w') as f:
rendered = template.render(
page=filepath,
)
f.write(rendered)
if __name__ == '__main__':
genja = Genja()
genja.clean()
genja.build()