-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathrender.py
85 lines (68 loc) · 2.3 KB
/
render.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
from StringIO import StringIO
import yaml
import sys
def render_attr(attr):
buffer = StringIO()
if attr.get("required", False):
req = "*"
else:
req = ""
if attr.get("specific for", False):
if isinstance(attr.get("specific for"), list):
spec = ', '.join(attr.get("specific for"))
else:
spec = attr.get("specific for")
spec = " specific for " + spec
else:
spec = ""
buffer.write("{}{} <i><small>{}</small></i>".format(attr["name"], req, spec))
children = attr.get("children", [])
if children:
buffer.write("\n<ul>\n")
for ch in children:
buffer.write("<li>\n")
buffer.write(render_attr(ch))
buffer.write("</li>\n")
buffer.write("</ul>")
return buffer.getvalue()
def render_sl(sl):
return """<a href="{ref}">{name}</a>""".format(**sl)
def render_ont(ont):
if ont.get("abbr", False):
abbr = ", {}".format(ont["abbr"])
else:
abbr = ""
return """<a href="{}">{}{}</a>""".format(ont["ref"], ont["name"], abbr)
def render_html(doc):
buffer = StringIO()
buffer.write("<tr style='border-bottom: 1px solid #ddd;'>\n")
buffer.write("<td>{}</td>\n".format(doc["section name"]))
buffer.write("<td style='padding-top: 15px'><ul>\n")
for attr in doc["attributes"]:
buffer.write("<li>{}</li>\n".format(render_attr(attr)))
buffer.write("</ul></td>")
buffer.write("<td><ul>")
for sl in doc["source list"]:
buffer.write("<li>{0}</li>\n".format(render_sl(sl)))
buffer.write("</ul></td>\n")
buffer.write("<td><ul>")
for ont in doc["recommended ontologies"]:
buffer.write("<li>{0}</li>\n".format(render_ont(ont)))
buffer.write("</ul></td>\n")
buffer.write("</tr>")
return buffer.getvalue()
def main(filename):
with open(filename, "r") as f:
docs = yaml.load(f)
print "<table style='border-collapse:collapse'>"
print "<tr>" \
"<th>Section</th>" \
"<th>Attributes</th>" \
"<th>Source list</th>" \
"<th>Recommended ontologies</th>" \
"</tr>"
for doc in docs["sections"]:
print render_html(doc)
print "</table>"
if __name__=='__main__':
main(sys.argv[1])