Newer
Older
#! /usr/bin/env python3
from __future__ import print_function
from os.path import join, basename
from glob import glob
import re
section_nr = [1]
def normalize_nodes(nodes, text_icon):
for i in range(len(nodes)):
node = nodes[i]
if isinstance(node, str):
node = {"text": node, "icon": text_icon}
assign_section_number = False
if "section_nr" in node and node["section_nr"] is not False:
if node["section_nr"] is True:
assign_section_number = True
else:
section_nr[0] = node["section_nr"]
if "section" in node:
if node.get("section_nr") is not False:
assign_section_number = True
if assign_section_number:
node["section_nr"] = section_nr[0]
section_nr[0] += 1
nodes[i] = node
if "nodes" in node:
normalize_nodes(node["nodes"], text_icon)
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 __init__(self, default_icon, number_sections):
self.number_sections = number_sections
def render(settings, outf, node, indent=0, skip=1):
icon = node["icon"]
attrs["icon"] = icon
if "opened" in node and int(bool(node["opened"])):
attrs["opened"] = True
if (settings.number_sections
and "section_nr" in node
and node["section_nr"] is not False):
text = "%d. %s" % (node["section_nr"], text)
if "section" in node:
text = "<b>%s</b>" % text
if not skip:
"<li data-jstree='%s'>" % dumps(attrs, sort_keys=True).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(settings, 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]+)-(.*)(\.[a-z]+)$")
def get_section_id_and_display_name(trunk, include_extension):
from os.path import splitext
fn_match = FN_REGEX.match(trunk)
if fn_match is not None:
section_id = fn_match.group(1)
if include_extension:
fname = fn_match.group(2) + fn_match.group(3)
else:
fname = fn_match.group(2)
display_name = basename(fname)
else:
section_id, display_name = trunk.split("/")
if not include_extension:
display_name, _ = splitext(display_name)
try:
section_id = int(section_id)
except ValueError:
pass
return section_id, display_name
def blacklisted_glob(basedir, pattern, blacklist_regexps):
return sorted(
name
for name in glob(join(basedir, pattern))
if not any(bl_re.match(name[len(basedir)+1:])
for bl_re in blacklist_regexps))
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("--binder-urlroot", metavar="URL",
help="(without the trailing slash)")
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("--default-icon", metavar="ICON_STR",
default="fa fa-file-o")
parser.add_argument("--text-icon", metavar="ICON_STR",
default="fa fa-file-o")
parser.add_argument("--number-sections", action="store_true")
parser.add_argument("--blacklist-file", metavar="PATTERN_FILE")
parser.add_argument("input_file", metavar="FILE")
args = parser.parse_args()
if args.blacklist_file is not None:
blacklist_regexps = []
with open(args.blacklist_file, "rt") as bl_file:
import fnmatch
for pattern in bl_file:
blacklist_regexps.append(
re.compile(fnmatch.translate(pattern.strip())))
from yaml import load
with open(args.input_file, "rb") as inf:
root_node = load(inf)
normalize_nodes([root_node], args.text_icon)
section_dict = {}
find_section_nodes(section_dict, root_node)
# {{{ demos
if args.ipynb_dir is not None:
for fn in blacklisted_glob(args.ipynb_dir, join("*", "*.ipynb"),
blacklist_regexps):
section_id, display_name = get_section_id_and_display_name(
trunk, include_extension=False)
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.binder_urlroot:
binder_url = args.binder_urlroot + "/" + trunk
sub_nodes.append({
"text": "Run interactively (on mybinder.org)",
"link": binder_url,
})
main_link = binder_url
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": "Demo: " + display_name,
"icon": "fa fa-keyboard-o",
"nodes": sub_nodes,
}
if section_id in section_dict:
section_dict[section_id]["nodes"].append(demo_node)
# }}}
# {{{ python source
if args.py_dir is not None:
for fn in blacklisted_glob(args.py_dir, join("*", "*.py"),
blacklist_regexps):
section_id, display_name = get_section_id_and_display_name(
trunk, include_extension=True)
"text": "Code: " + display_name,
"link": args.py_urlroot + "/" + trunk,
"icon": "fa fa-file-text-o",
}
if section_id in section_dict:
section_dict[section_id]["nodes"].append(src_node)
# }}}
# {{{ notes
if args.pdf_dir is not None:
for fn in blacklisted_glob(args.pdf_dir, join("*.pdf"),
blacklist_regexps):
if "autosave" in fn:
continue
trunk = fn[len(args.pdf_dir)+1:]
section_id, display_name = get_section_id_and_display_name(
trunk, include_extension=True)
"text": "PDF: " + basename(display_name),
"link": args.pdf_urlroot + "/" + trunk,
"icon": "fa fa-book",
}
if section_id in section_dict:
section_dict[section_id]["nodes"].insert(0, notes_node)
with open(args.output_file, "wt", encoding="utf-8") as outf:
render(
RenderSettings(
default_icon=args.default_icon,
number_sections=args.number_sections),
outf, root_node)
if __name__ == "__main__":
main()
# vim: foldmethod=marker