WIP: basic Graph based on d3JS's disjoint force directed graph.
Code taken from: https://observablehq.com/@d3/disjoint-force-directed-graph
This commit is contained in:
parent
ae07c563a8
commit
0073dd8b84
2 changed files with 239 additions and 0 deletions
|
@ -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('<!-- REPLACE_THIS_WITH_GRAPH -->',
|
||||
json.dumps(graph)))
|
||||
logging.info("Generated {} files".format(files_generated))
|
||||
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue