From 3f5ec66c3da4404bd0ecc5064eebe8b72d3d6381 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sergio=20Mart=C3=ADnez=20Portela?= Date: Fri, 9 Jun 2023 21:04:04 +0200 Subject: [PATCH] Fix: render graph.json, a dependency, before node-centered-graph. --- scripts/generate.py | 34 +++++++++++++++++++--------------- 1 file changed, 19 insertions(+), 15 deletions(-) diff --git a/scripts/generate.py b/scripts/generate.py index b818a9a..026a960 100644 --- a/scripts/generate.py +++ b/scripts/generate.py @@ -288,6 +288,19 @@ def regen_all(src_top, dest_top, *, docs=None, db=None): backlink_graph[main_headline_id] = set() backlink_graph[main_headline_id].add(backlink) + # Output graph files + 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)) + # Render docs after we've built the graph # Render main headlines full_graph_info = { "nodes": graph, "backlinks": backlink_graph, "main_headlines": main_headlines_by_path } @@ -298,7 +311,6 @@ def regen_all(src_top, dest_top, *, docs=None, db=None): f.write(render_as_document(main_headline, main_headline.doc, headlineLevel=0, graph=full_graph_info, title=org_rw.token_list_to_plaintext(main_headline.title.contents))) - # Render all headlines for headline in all_headlines: endpath = os.path.join(dest_top, headline.id + ".node.html") @@ -316,18 +328,6 @@ def regen_all(src_top, dest_top, *, docs=None, db=None): title=org_rw.token_list_to_plaintext(headline.title.contents))) files_generated += 1 - # Output graph files - 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)) cur.close() db.commit() @@ -712,13 +712,17 @@ def render_connections(headline_id, content, graph): # TODO: Cache results # TODO: Avoid querying graph API on script + # TODO: Properly render outgouing links logging.info("Generating centered graph for {}".format(headline_id)) import subprocess this_dir = os.path.dirname(os.path.abspath(__file__)) os.makedirs('cache', exist_ok=True) subprocess.check_call(['python3', os.path.join(this_dir, 'gen-centered-graph.py'), headline_id, 'cache/' + headline_id + '.svg']) - with open('cache/' + headline_id + '.svg') as f: - content.append("
{}
".format(f.read())) + try: + with open('cache/' + headline_id + '.svg') as f: + content.append("
{}
".format(f.read())) + except FileNotFoundError: + logging.exception('Graph file not produced on headline: "{}"'.format(headline_id)) def render(headline, doc, graph, headlineLevel): try: