74 lines
2.6 KiB
Python
74 lines
2.6 KiB
Python
""" python3 -m docgen.emitters site <ir.json> -o DIR [--theme lucid]
|
|
|
|
Writes index.html, viewer.html, site.css and the graph — self-contained, offline.
|
|
"""
|
|
|
|
import argparse
|
|
import json
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
from ..ir import check
|
|
from ..ops import classify
|
|
from ..style import Style, StyleError
|
|
from .site import write
|
|
|
|
|
|
def main(argv=None):
|
|
p = argparse.ArgumentParser(prog="python3 -m docgen.emitters site")
|
|
p.add_argument("ir", type=Path)
|
|
p.add_argument("--output", "-o", type=Path, required=True, help="Directory.")
|
|
p.add_argument("--style", default="lucid")
|
|
p.add_argument("--theme", default=None)
|
|
p.add_argument("--title", default="")
|
|
p.add_argument("--no-graph", action="store_true")
|
|
args = p.parse_args(argv)
|
|
|
|
try:
|
|
data = json.loads(args.ir.read_text())
|
|
except (OSError, json.JSONDecodeError) as e:
|
|
print(f"Error: could not read {args.ir}: {e}", file=sys.stderr)
|
|
return 1
|
|
problems = check(data)
|
|
if problems:
|
|
print(f"Error: {args.ir} is not a valid IR ({len(problems)}):", file=sys.stderr)
|
|
for pr in problems[:5]:
|
|
print(f" {pr}", file=sys.stderr)
|
|
return 1
|
|
|
|
try:
|
|
style = Style.load(args.style, theme=args.theme)
|
|
except StyleError as e:
|
|
print(f"Error: {e}", file=sys.stderr)
|
|
return 1
|
|
|
|
args.output.mkdir(parents=True, exist_ok=True)
|
|
graph_name = None
|
|
if not args.no_graph:
|
|
# Whatever the structure asks for, so the page carries the right picture.
|
|
verdict = classify(data)
|
|
if verdict["emitter"] == "erd":
|
|
from .erd import emit as draw
|
|
(args.output / "graph.svg").write_text(draw(data, style))
|
|
graph_name = "graph.svg"
|
|
elif verdict["emitter"] == "dot":
|
|
from .dot import emit as dot_emit, have_graphviz, render
|
|
if have_graphviz():
|
|
opts = verdict.get("options") or {}
|
|
(args.output / "graph.svg").write_bytes(
|
|
render(dot_emit(data, style, rankdir=opts.get("rankdir")))
|
|
)
|
|
graph_name = "graph.svg"
|
|
else:
|
|
print(" note: graphviz absent — the site is text only", file=sys.stderr)
|
|
else:
|
|
print(f" note: {verdict['kind']} — {verdict['why']}", file=sys.stderr)
|
|
print(" no diagram on the page; the index is the artifact", file=sys.stderr)
|
|
|
|
files = write(data, style, args.output, graph=graph_name, title=args.title)
|
|
for f in files:
|
|
print(f" site {f}")
|
|
if graph_name:
|
|
print(f" site {args.output / graph_name}")
|
|
return 0
|