-
Notifications
You must be signed in to change notification settings - Fork 0
/
mk_website.py
executable file
·138 lines (128 loc) · 5.06 KB
/
mk_website.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
137
138
#!/usr/bin/env python3
import sys
import os
import json
from subprocess import Popen, PIPE, run
custom_page_map = {
"README.md" : "index.html",
"INSTALL.md": "install.html",
"LICENSE": "license.html"
}
#
# mkpage wrapes the mkpage command from mkpage
# @param output_filename is the output file
# @param templates is an array of Go style templates
# @data is a dict structure that will be passed into mkpage as JSON
#
def mkpage(output_filename, templates = [], data = []):
cmd = ['mkpage', '-o', output_filename]
for item in data:
cmd.append(item)
for tmpl in templates:
cmd.append(tmpl)
with Popen(cmd, stdout = PIPE, stderr = PIPE) as proc:
err = proc.stderr.read().strip().decode('utf-8')
if err != '':
print("{} error: {}".format(' '.join(cmd[0:3]), err))
return err
out = proc.stdout.read().strip().decode('utf-8')
if out != "":
print("{}".format(out))
return ""
#
# frontmatter extracts front matter from a Markdown document
# returning the results as a python dictionary. Currently
# supports only JSON style front matter.
#
# @param input_filename is the Markdown file containing front matter
# @return a Python dict of the front matter found or an empty dict
# if none found.
#
def frontmatter(input_filename):
cmd = ['frontmatter', '-json', '-i', input_filename]
src = ''
with Popen(cmd, stdout = PIPE, stderr = PIPE) as proc:
err = proc.stderr.read().strip().decode('utf-8')
if err != '':
print("{} error: {}".format(' '.join(cmd[0:3]), err))
out = proc.stdout.read().strip().decode('utf-8')
if not isinstance(out, str):
out = out.encode('utf-8')
if (out.startswith("{") and out.endswith("}")) or (out.startswith("[") and out.endswith("]")):
try:
result = json.loads(out) #.encode('utf-8'))
except Exception as e:
print("Warning {} has invalid metadata {}".format(input_filename, e))
sys.exit(1)
return result
elif out != "":
print("WARNING: Front matter isn't JSON for {}, {}".format( input_filename, out))
return {}
#
# mkpage_version_no shows the version number of mkpage cli referenced.
#
# @param cli_name either mkpage or frontmatter
# @return version number found or system exit with error
#
def mkpage_version_no(cli_name):
cmd = [cli_name, '-version']
p = Popen(cmd, stdout = PIPE, stderr = PIPE)
(version, err) = p.communicate()
if err.decode('utf-8') != '':
print("ERROR: {} -version, {}",format(cli_name, err.decode('utf-8')))
sys.exit(1)
return version.decode('utf-8')
#
# main - build our site based on the Markdown docs we find.
# @params args is the command line arguments, currently not used.
#
def main(args):
# Find the .md files and render them with template unless nav.md.
# crawl docs_dir and ingest files into data collection.
for path, folders, files in os.walk("."):
for filename in files:
in_name = ""
out_name = ""
nav_name = os.path.join(path, "nav.md")
if filename in custom_page_map:
in_name = os.path.join(path, filename)
out_name = os.path.join(path, custom_page_map[filename])
elif filename.endswith(".md") and not filename == "nav.md":
basename, ext = os.path.splitext(filename)
in_name = os.path.join(path, filename)
out_name = os.path.join(path, basename + ".html")
if in_name != "" and out_name != "":
print("Ingesting {}".format(in_name))
metadata = json.dumps(frontmatter(in_name))
#NOTE: Processing metadata should happen here.
page_data = []
if len(metadata):
page_data.append("front_matter=json:{}".format(metadata))
if os.path.exists(nav_name):
page_data.append("nav={}".format(nav_name))
if in_name.endswith("LICENSE"):
with open(in_name) as f:
src = f.read()
page_data.append("content=markdown:{}".format(src))
else:
page_data.append("content={}".format(in_name))
err = mkpage(out_name, [ "page.tmpl" ], page_data)
if err != "":
print("Failed {} -> {}, {}".format(in_name, out_name, err))
sys.exit(1)
else:
print("Wrote {}".format(out_name))
# Write out message showing version of mkpage, frontmatter
# and dataset used.
print("Built using", end = " ")
for i, app_name in enumerate([ "mkpage", "frontmatter", "dataset" ]):
version = mkpage_version_no(app_name).strip()
if i == 2:
print(" and", end = " ")
elif i > 0:
print(",", end = " ")
print("{}".format(version), end = "")
print("")
sys.exit(0)
if __name__ == "__main__":
main(sys.argv)