-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathgenerator.py
62 lines (54 loc) · 1.79 KB
/
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
57
58
59
60
61
62
from jinja2 import Template
import mistune
import os
data_folder = 'data/'
data_files = sorted(os.listdir(data_folder))
data = []
listbytag = {}
renderer = mistune.Renderer(escape=True, hard_wrap=True)
markdown = mistune.Markdown(renderer=renderer)
output_folder = 'hosts/'
hosttemplate = ''
with open('templates/hostpage.html') as tf:
hosttemplate = Template(tf.read())
indextemplate = ''
with open('templates/index.html') as tf:
indextemplate = Template(tf.read())
# read data
for file in data_files:
with open(data_folder + file) as f:
t = f.read().split('---')
header = t[1].strip().split('\n')
markdown_content = t[2].strip()
data_point = {}
for line in header:
key = line.split(':')[0].strip()
value = line.split(':')[1].strip()
data_point[key] = value
data_point['tags'] = [tag.strip() for tag in data_point['tags'].split(',')]
data_point['file'] = file[:-2]+'html'
data_point['rendered_markdown'] = markdown(markdown_content)
data.append(data_point)
# render host files
for host in data:
html = hosttemplate.render(name=host['name'],color=host['color'],tags=host['tags'],markdown=host['rendered_markdown'])
file = output_folder+host['file']
with open(file,'w') as f:
f.write(html)
f.close()
print('Wrote ' + file)
# list by tags
for i in range(len(data)):
for tag in data[i]['tags']:
if tag in listbytag.keys():
listbytag[tag].append(i)
else:
listbytag[tag] = [i]
tagslist = list(listbytag.keys())
tagslist.sort()
# render home page
with open('index.html','w') as f:
html = indextemplate.render(data=data,tagslist=tagslist,listbytag=listbytag)
f.write(html)
f.close()
print('\nWrote index.html')