101 lines
3.8 KiB
GDScript3
101 lines
3.8 KiB
GDScript3
|
|
class_name DotExporter
|
|||
|
|
extends RefCounted
|
|||
|
|
## Graphviz DOT export. Each table is an HTML-like label listing its entries;
|
|||
|
|
## pipes and inline [[references]] are edges from the entry's port to the
|
|||
|
|
## target table (references are dashed). Import is not supported.
|
|||
|
|
|
|||
|
|
## Entries beyond this many are folded into one "... and N more" row, unless
|
|||
|
|
## they have a connection.
|
|||
|
|
const MAX_ROWS := 30
|
|||
|
|
|
|||
|
|
|
|||
|
|
## DOT source for a collection Dictionary.
|
|||
|
|
static func export_dot(collection: Dictionary) -> String:
|
|||
|
|
var c := JsonIO.canonical(collection)
|
|||
|
|
var out: Array[String] = []
|
|||
|
|
out.append("digraph TableTower {")
|
|||
|
|
out.append("\tlabel=\"%s\";" % escape_string(str(c["name"])))
|
|||
|
|
out.append("\tlabelloc=t;")
|
|||
|
|
out.append("\trankdir=LR;")
|
|||
|
|
out.append("\tnode [shape=plain, fontname=\"Helvetica\"];")
|
|||
|
|
out.append("\tedge [fontname=\"Helvetica\", fontsize=10];")
|
|||
|
|
|
|||
|
|
var tables: Array = c["tables"]
|
|||
|
|
var node_of := {}
|
|||
|
|
for i in tables.size():
|
|||
|
|
node_of[tables[i]["id"]] = "t%d" % i
|
|||
|
|
|
|||
|
|
var links := JsonIO.links(c)
|
|||
|
|
var linked := {} # "ti:ei" -> true
|
|||
|
|
for link in links:
|
|||
|
|
linked["%d:%d" % [link["table_index"], link["entry_index"]]] = true
|
|||
|
|
|
|||
|
|
for i in tables.size():
|
|||
|
|
var t: Dictionary = tables[i]
|
|||
|
|
var title := JsonIO.strip_bbcode(str(t["name"]))
|
|||
|
|
if t.has("dice"):
|
|||
|
|
title += " (%s)" % t["dice"]
|
|||
|
|
var dmax := JsonIO.dice_max(str(t.get("dice", "")))
|
|||
|
|
var rows: Array[String] = []
|
|||
|
|
rows.append("<TR><TD PORT=\"head\" BGCOLOR=\"#dddddd\"><B>%s</B></TD></TR>" % escape_html(title))
|
|||
|
|
var entries: Array = t["entries"]
|
|||
|
|
var shown := 0
|
|||
|
|
var hidden := 0
|
|||
|
|
for ei in entries.size():
|
|||
|
|
var e: Dictionary = entries[ei]
|
|||
|
|
if shown >= MAX_ROWS and not linked.has("%d:%d" % [i, ei]):
|
|||
|
|
hidden += 1
|
|||
|
|
continue
|
|||
|
|
shown += 1
|
|||
|
|
rows.append("<TR><TD PORT=\"e%d\" ALIGN=\"LEFT\">%s</TD></TR>" % [ei, escape_html(_row_text(e, dmax))])
|
|||
|
|
if hidden > 0:
|
|||
|
|
rows.append("<TR><TD ALIGN=\"LEFT\"><I>… and %d more</I></TD></TR>" % hidden)
|
|||
|
|
out.append("\t%s [label=<<TABLE BORDER=\"1\" CELLBORDER=\"0\" CELLSPACING=\"0\" CELLPADDING=\"4\">%s</TABLE>>];" % [node_of[t["id"]], "".join(rows)])
|
|||
|
|
|
|||
|
|
var missing := {}
|
|||
|
|
var edges: Array[String] = []
|
|||
|
|
for link in links:
|
|||
|
|
var target: String
|
|||
|
|
if link["target"] != "":
|
|||
|
|
target = "%s:head:w" % node_of[link["target"]]
|
|||
|
|
else:
|
|||
|
|
var key: String = str(link["target_name"]).to_lower()
|
|||
|
|
if not missing.has(key):
|
|||
|
|
missing[key] = {"id": "m%d" % missing.size(), "name": link["target_name"]}
|
|||
|
|
target = "%s:w" % missing[key]["id"]
|
|||
|
|
var from := "%s:e%d:e" % [node_of[link["from_table"]], link["entry_index"]]
|
|||
|
|
var attrs: Array[String] = []
|
|||
|
|
if link["label"] != "":
|
|||
|
|
attrs.append("label=\"%s\"" % escape_string(str(link["label"])))
|
|||
|
|
if link["kind"] == "ref":
|
|||
|
|
attrs.append("style=dashed")
|
|||
|
|
if link["target"] == "":
|
|||
|
|
attrs.append("color=red")
|
|||
|
|
edges.append("\t%s -> %s%s;" % [from, target, (" [" + ", ".join(attrs) + "]") if not attrs.is_empty() else ""])
|
|||
|
|
for key in missing:
|
|||
|
|
out.append("\t%s [label=\"%s\", shape=box, style=dashed, color=red, fontcolor=red];" % [missing[key]["id"], escape_string("missing: " + str(missing[key]["name"]))])
|
|||
|
|
out.append_array(edges)
|
|||
|
|
out.append("}")
|
|||
|
|
return "\n".join(out) + "\n"
|
|||
|
|
|
|||
|
|
|
|||
|
|
## Escapes text for a double-quoted DOT string.
|
|||
|
|
static func escape_string(text: String) -> String:
|
|||
|
|
return text.replace("\\", "\\\\").replace("\"", "\\\"").replace("\r", "").replace("\n", "\\n")
|
|||
|
|
|
|||
|
|
|
|||
|
|
## Escapes text for an HTML-like DOT label.
|
|||
|
|
static func escape_html(text: String) -> String:
|
|||
|
|
return text.replace("&", "&").replace("<", "<").replace(">", ">").replace("\"", """).replace("\n", " ")
|
|||
|
|
|
|||
|
|
|
|||
|
|
static func _row_text(e: Dictionary, dmax: int) -> String:
|
|||
|
|
var plain := JsonIO.strip_bbcode(str(e["text"]))
|
|||
|
|
if e.has("range"):
|
|||
|
|
var r: Array = e["range"]
|
|||
|
|
return "%s: %s" % [JsonIO.range_text(int(r[0]), int(r[1]), dmax), plain]
|
|||
|
|
var w: Variant = e.get("weight", 1)
|
|||
|
|
if w != 1:
|
|||
|
|
return "%s × %s" % [str(w), plain]
|
|||
|
|
return plain
|