-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_site.py
116 lines (92 loc) · 4.22 KB
/
generate_site.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
import os
import glob
from jinja2 import Template, Environment, FileSystemLoader
import xml.etree.ElementTree as ET
import json
def convert_name(name):
return name.lower().replace(' ', '-').replace('---', '-').replace("'", '').replace('?', '').replace('(','').replace(')', '').replace('&-', '').replace(',', '').replace('/-', '')
# Heavyweight way to get the data for a xref tag
# TODO: Consider caching each file as it loads. Especially for the current file.
def get_xref(xref, parent_path):
# split on #
xref_data = xref.split('#')
if(xref.startswith('#')):
file = parent_path + ".xml"
id = xref_data[1]
else:
file = xref_data[0] + ".xml"
id = xref_data[1]
# Open file xml file
tree = ET.parse(os.path.join('faqxml', file))
root = tree.getroot()
# Search for <entry id="<id>">
xpath = "[@id='" + id + "']"
node = root.find(".//entry[@id='" + id + "']")
# Return node to that entry
return node
# TODO: Support cross referencing somehow.
# xreffing is done via an entry with xref="". This points to another entry with an id.
# ids are optional, but must exist on a target. ids must also be unique.
# Generally ids are only expected to exist when an entry needs to be xrefable.
# Solution:
# xrefs should be hierarchical; i.e. they should indicate the file they are found in.
# thus if an xref is prefixed with a #, then it is in the same file. If it is an alphanum, then a diff file.
# In the latter case, another file will need to be loaded for a quick scan for the content.
# Format is: xref="#foo" (same file) and xref="dir/file#foo" foo in the dir/file file
output_dir='docs'
target_index = {}
# Find each xml file in faqxml
for file in glob.glob('faqxml/**/*.xml', recursive=True):
print("Parsing " + file)
tree = ET.parse(file)
faq_node=tree.getroot()
faq_name=faq_node.attrib['name']
# Load Leaf Template
template_file = open('templates/leaf.jinja2','r')
template_text = template_file.read()
template = Environment(loader=FileSystemLoader("templates/")).from_string(template_text)
#for target_node in faq_node.findall('./target'):
# if an xref entry
# load that content from elsewhere
# print(template.render(target=target_node))
parent_path = file.replace('faqxml/', '').replace('.xml', '')
target_dir = os.path.join(output_dir, parent_path)
# TODO: if os.path.isfile(target_dir)
if( not os.path.isdir(target_dir) ):
os.makedirs(target_dir)
targets=[]
for target_node in faq_node.findall('./target'):
name = target_node.attrib['name']
# Add the target details to generate an index page
targets.append(name)
# Generating page for the target
safe_name = convert_name(name)
page = template.render(faq_name=faq_name, get_xref=get_xref, safe_name=safe_name, target=target_node, parent_path=parent_path)
f = open(os.path.join(target_dir, safe_name + ".html"), "w")
f.write(page)
f.close()
# This is a URL snippet, so hardcode the /
target_index[name] = parent_path + "/" + safe_name + ".html"
# Load Branch Template
branch_template_file = open('templates/branch.jinja2','r')
branch_template_text = branch_template_file.read()
branch_template = Environment(loader=FileSystemLoader("templates/")).from_string(branch_template_text)
page=branch_template.render(faq_name=faq_name, convert_name=convert_name, targets=targets)
f = open(os.path.join(output_dir, parent_path, "index.html"), "w")
f.write(page)
f.close()
# Generate json index
f = open(os.path.join(output_dir, "faqindex.json"), "w")
f.write(json.dumps(target_index))
f.close()
def generate_html(infilename, tofilename, title):
template_file = open(infilename,'r')
template_text = template_file.read()
template = Environment(loader=FileSystemLoader("templates/")).from_string(template_text)
page=template.render()
f = open(os.path.join(output_dir, tofilename), "w")
f.write(page)
f.close()
generate_html('templates/index.jinja2', 'index.html', "FAQ")
generate_html('templates/about.jinja2', 'about.html', "About")
generate_html('templates/contribute.jinja2', 'contribute.html', "Contribute")