-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfn-generator.py
executable file
·56 lines (48 loc) · 1.53 KB
/
fn-generator.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
#!/usr/bin/env python3
import os
import yaml
from jinja2 import Environment, FileSystemLoader
from jinja2_markdown import MarkdownExtension
from distutils.dir_util import copy_tree
# Attempt to create output directory
try:
os.mkdir('_output/')
except FileExistsError:
pass
# Copy _assets into _output
copy_tree('_assets', '_output')
# Populate yaml data
print("Indexing data files...")
data = {}
for file in os.listdir('_data/'):
if file.endswith('.yaml'):
dataset = file[:-5]
print(" {}".format(dataset))
with open(os.path.join('_data/', file)) as stream:
data[dataset] = yaml.safe_load(stream)
# Find all template pages
print("Indexing template pages...")
pages = []
for file in os.listdir('_source/'):
if file.endswith('.html'):
page = file[:-5]
print(" {}".format(page))
pages.append(page)
data['pages'] = pages
# Create jinja2 environment
environment = Environment(
loader = FileSystemLoader(['_source', '_includes'])
)
environment.add_extension(MarkdownExtension)
# Process template pages
print("Generating pages...")
for file in os.listdir('_source/'):
if file.endswith('.html'):
page = file[:-5]
data['page'] = page
print(' {}: {} => {}'.format(page, os.path.join('_source/', file), os.path.join('_output/', file)))
template = environment.get_template(file)
file = open(os.path.join('_output/', file), 'w')
file.write(template.render(data=data))
file.close()
print("Done!")