dorothy
dorothy copied to clipboard
Rank syntax support
I was unable to generate the syntax for {rank = same; X; Y; Z}
without using some dorothy internals.
Here's a small example of what I'd like to do: https://stackoverflow.com/a/44274606/1481316
This is also mentioned in the Dot User's Manual https://www.graphviz.org/pdf/dotguide.pdf on page 16.
Here's how I achieved the syntax, which as far as I can tell is called "Rank":
(defn rank
[attrs statements]
{:type ::rank
:statements (map #'dot/to-ast statements)
:attrs attrs})
(defmethod dot/dot* ::rank
[this]
(let [{:keys [attrs statements]} this]
(str "{\n"
(apply
str
(for [[k v] attrs]
(str (#'dot/escape-id k) \= (#'dot/escape-id v) ";\n")))
(apply str (interleave
(map dot/dot* statements)
(repeat ";\n")))
"} ")))
It's a lot like a graph, but without a name, and with the ability to set inline attrs. I decided to front-load the attrs, but ordering might matter, so that might not be ideal.
Was wondering how to do this myself. Turns out this is just a subgraph (the keyword subgraph
is optional).
The following two statements are equivalent:
subgraph {
rank = same; A; B; C;
}
{ rank = same; A; B; C; }
Having sugar for subgraphs would be nice though.