Skip to content

Instantly share code, notes, and snippets.

@burke
Last active March 30, 2017 01:40
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save burke/c893c8172cebf66a950c84e3e8b04c08 to your computer and use it in GitHub Desktop.
Save burke/c893c8172cebf66a950c84e3e8b04c08 to your computer and use it in GitHub Desktop.

CSS-ish styling for graphviz DOT

digraph {
  rb_find_file_safe [class="emph,strong"];
  search_required [class=emph];
  open_load_file [class=emph];
  rb_find_file -> rb_find_file_safe;
  search_required -> rb_find_file_safe;
  rb_require_internal -> search_required;
  load_file -> open_load_file;
  {rank=same search_required rb_find_file_safe open_load_file};
}
gvpr -c -f neato.gv < neato.dot | dot -Tsvg > neato.svg

https://i.imgur.com/Di6imWz.png

# Default Styles
BEG_G {
allDefault($G, "fontname", "helvetica");
allDefault($G, "fontsize", "10px");
nodeDefault($G, "style", "rounded,filled");
nodeDefault($G, "shape", "box");
nodeDefault($G, "fillcolor", "#cee1ff");
nodeDefault($G, "fontcolor", "#333333");
}
# Classes
N [class($, "emph")] {
fillcolor = "#ffccff";
penwidth = "3";
}
N [class($, "strong")] {
penwidth = "3";
fontcolor = "red";
}
###########################################################
BEGIN {
void graphDefault(graph_t g, string k, string v) {
setDflt(g, "G", k, v);
}
void edgeDefault(graph_t g, string k, string v) {
setDflt(g, "E", k, v);
}
void nodeDefault(graph_t g, string k, string v) {
setDflt(g, "N", k, v);
}
void allDefault(graph_t g, string k, string v) {
setDflt(g, "N", k, v);
setDflt(g, "E", k, v);
setDflt(g, "G", k, v);
}
int class(node_t n, string klass) {
int c = 0;
int hasClass = 0;
string newClasses = "";
string classes[int];
if (!hasAttr(n, "class")) return 0;
unset(classes);
tokens(aget(n, "class"), classes, ",");
for (classes[c]) {
if (classes[c] == klass) {
hasClass = 1;
}
}
if (!hasClass) return 0;
unset(classes);
tokens(aget(n, "class"), classes, ",");
for (classes[c]) {
if (classes[c] != klass) {
if (newClasses == "") {
newClasses = classes[c];
} else {
newClasses = sprintf("%s,%s", newClasses, classes[c]);
}
}
}
aset(n, "class", newClasses);
return 1;
}
}
N [hasAttr($, "class") && (aget($, "class") != "" && aget($, "class") != 0)] {
printf(2, "diagram.gvpr: unsupported class: %s\n", aget($, "class"));
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment