TableTower/io/dot_exporter.gd
RobinFiredrake 0f3e8c278f Remove duplicated code found by the coderstanding audit
- Style.menu_button / font_colors / field_colors replace five copies of the
  "copy a bar button's skin onto a MenuButton" block, nine single-colour
  font loops and four field colour blocks.
- GroupCard uses TableCard.vbox/hbox instead of its own copies.
- InspectorKit.focused_tag / clear / refocus / as_text_link replace the
  focus-keeping render frame and "Edit as text" link shared by the
  condition and effects builders.
- InspectorCard._setting_line replaces three copies of a bound setting field.
- Main.file_dialog / export_dialog replace four FileDialog setups.
- JsonIO.entry_line / finish_import replace copies in canvas_io,
  dot_exporter and graphml_io.
- Dead code: TableCard's unused inline-editor fields and the no-op
  --edit-details / --edit-effects flags, an unused error counter in the
  tray, and an unreachable "settings not available" fallback.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-09 08:21:26 -06:00

89 lines
3.5 KiB
GDScript

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(JsonIO.entry_line(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("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;").replace("\"", "&quot;").replace("\n", " ")