Newer
Older
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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
#! /usr/bin/env python3
from __future__ import print_function
from os.path import join, basename
from glob import glob
import re
def find_section_nodes(section_dict, node):
if "section" in node:
section_dict[node["section"]] = node
for subnode in node.get("nodes", []):
find_section_nodes(section_dict, subnode)
def render(outf, node, indent=0, skip=1):
attrs = {}
if "icon" in node:
attrs["icon"] = node["icon"]
text = node["text"]
if "section" in node:
text = "<b>%s</b>" % text
if not skip:
print(
indent * " ",
"<li data-jstree='%s'>" % repr(attrs).replace("'", '"'),
file=outf, sep="")
indent += 2
if "link" in node:
print(
indent * " ",
"<a href=\"%s\">%s</a>" % (
node["link"],
text),
file=outf, sep="")
else:
print(indent * " ", text, file=outf, sep="")
subnodes = node.get("nodes", [])
if subnodes:
print(indent * " ", "<ul>", file=outf, sep="")
indent += 2
for subnode in subnodes:
render(outf, subnode, indent, skip=max(0, skip - 1))
indent -= 2
print(indent * " ", "</ul>", file=outf, sep="")
if not skip:
indent -= 2
print(indent * " ", "</li>", file=outf, sep="")
FN_REGEX = re.compile(r"^([0-9][0-9])-(.*)(\.[a-z]+)$")
def main():
import argparse
parser = argparse.ArgumentParser(
description='Turn a YAML file into a jsTree-compatible data file')
parser.add_argument("-o", "--output-file", metavar="FILE", required=True)
parser.add_argument("--ipynb-dir", metavar="DIRECTORY")
parser.add_argument("--ipynb-urlroot", metavar="URL",
help="(without the trailing slash)")
parser.add_argument("--ipynb-as-py", action="store_true")
parser.add_argument("--ipynb-as-ipynb", action="store_true")
parser.add_argument("--py-dir", metavar="DIRECTORY")
parser.add_argument("--py-urlroot", metavar="URL",
help="(without the trailing slash)")
parser.add_argument("--pdf-dir", metavar="DIRECTORY")
parser.add_argument("--pdf-urlroot", metavar="URL",
help="(without the trailing slash)")
parser.add_argument("input_file", metavar="FILE")
args = parser.parse_args()
from yaml import load
with open(args.input_file, "rb") as inf:
root_node = load(inf)
section_dict = {}
find_section_nodes(section_dict, root_node)
# {{{ demos
if args.ipynb_dir is not None:
for fn in sorted(glob(join(args.ipynb_dir, "[0-9]*", "*.ipynb"))):
trunk = fn[len(args.ipynb_dir)+1:]
fn_match = FN_REGEX.match(trunk)
sec_nr = int(fn_match.group(1))
link_ipynb = args.ipynb_urlroot + "/" + trunk
link_html = link_ipynb.replace(".ipynb", ".html")
sub_nodes = [{
"text": "View on the web",
"link": link_html,
"icon": "fa fa-newspaper-o",
}]
if args.ipynb_as_py:
link_py = link_ipynb.replace(".ipynb", ".py")
sub_nodes.append({
"text": "Download Python script",
"link": link_py,
"icon": "fa fa-terminal",
})
if args.ipynb_as_ipynb:
sub_nodes.append({
"text": "Download IPython notebook",
"link": link_ipynb,
"icon": "fa fa-download",
})
demo_node = {
"text": "Demo: " + basename(fn_match.group(2)),
"link": link_html,
"icon": "fa fa-keyboard-o",
"nodes": sub_nodes,
}
section_dict[sec_nr]["nodes"].append(demo_node)
# }}}
# {{{ python source
if args.py_dir is not None:
for fn in sorted(glob(join(args.py_dir, "[0-9]*", "*.py"))):
trunk = fn[len(args.py_dir)+1:]
fn_match = FN_REGEX.match(trunk)
sec_nr = int(fn_match.group(1))
src_node = {
"text": "Code: " + basename(
fn_match.group(2)+fn_match.group(3)),
"link": args.py_urlroot + "/" + trunk,
"icon": "fa fa-file-text-o",
}
section_dict[sec_nr]["nodes"].append(src_node)
# }}}
# {{{ notes
if args.pdf_dir is not None:
for fn in sorted(glob(join(args.pdf_dir, "[0-9]*.pdf"))):
if "autosave" in fn:
continue
trunk = fn[len(args.pdf_dir)+1:]
fn_match = FN_REGEX.match(trunk)
sec_nr = int(fn_match.group(1))
notes_node = {
"text": "PDF: " + basename(
fn_match.group(2)+fn_match.group(3)),
"link": args.pdf_urlroot + "/" + trunk,
"icon": "fa fa-book",
}
section_dict[sec_nr]["nodes"].insert(0, notes_node)
# }}}
with open(args.output_file, "wt") as outf:
render(outf, root_node)
if __name__ == "__main__":
main()
# vim: foldmethod=marker