diff --git a/scripts/generate.py b/scripts/generate.py index 7b2aff0..921f805 100644 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -1,5 +1,6 @@ #!/usr/bin/env python3 +import json import html import logging import os @@ -51,6 +52,7 @@ def main(src_top, dest_top): files_generated = 0 os.makedirs(dest_top, exist_ok=True) + graph = {} for doc in docs: relpath = os.path.relpath(doc.path, src_top) changed = False @@ -106,10 +108,47 @@ def main(src_top, dest_top): for headline in headlines: endpath = os.path.join(dest_top, headline.id + ".node.html") + links = [] + for l in headline.get_links(): + if l.value.startswith('http://') or l.value.startswith('https://'): + pass # Ignore for now, external URL + elif l.value.startswith('id:'): + links.append({'target': l.value}) + elif l.value.startswith('attachment:'): + pass # Ignore, attachment + elif l.value.startswith('* '): + pass # Ignore, internal + elif l.value.startswith('./'): + pass # TODO: Properly handle + else: + raise NotImplementedError('On document {}, link to {}'.format(doc.path, l.value)) + + if headline.parent: + if isinstance(headline.parent, org_rw.Headline): + links.append({ + "target": headline.parent.id, + "relation": "contained-in" + }) + graph[headline.id] = { + "title": headline.title.strip(), + "links": links, + "depth": headline.depth, + } + with open(endpath, "wt") as f: f.write(as_document(render(headline, doc, headlineLevel=0))) files_generated += 1 + graphpath = os.path.join(dest_top, "graph.json") + graph_explorer_path = os.path.join(dest_top, "graph.html") + with open(graphpath, "wt") as f: + json.dump(obj=graph, fp=f, indent=2) + graph_explorer_path = os.path.join(dest_top, "graph.html") + with open(graph_explorer_path, 'wt') as f: + with open(os.path.join(os.path.dirname(os.path.abspath(dest_top)), 'static', 'graph_explorer.html'), 'rt') as template: + source = template.read() + f.write(source.replace('', + json.dumps(graph))) logging.info("Generated {} files".format(files_generated)) diff --git a/static/graph_explorer.html b/static/graph_explorer.html new file mode 100644 index 0000000..f903080 --- /dev/null +++ b/static/graph_explorer.html @@ -0,0 +1,200 @@ + + + + + Graph Explorer + + + + + + +
+
+ + + +