dominikbraun / graph

A library for creating generic graph data structures and modifying, analyzing, and visualizing them.
https://graph.dominikbraun.io
Apache License 2.0
1.82k stars 96 forks source link

draw.DOT escaping #95

Open Zincr0 opened 1 year ago

Zincr0 commented 1 year ago

Using draw.DOT over a network with valid golang string nodes, generates invalid .gv files if any string contain some special characters

example:

    g := graph.New(graph.StringHash, graph.Directed())

    _ = g.AddVertex("#cat:\"&(")
    _ = g.AddVertex("@dog")
    _ = g.AddVertex("\"big bird\"")

    _ = g.AddEdge("#cat:\"&(", "@dog")
    _ = g.AddEdge("#cat:\"&(", "\"big bird\"")

    file, _ := os.Create("./mygraph.gv")
    _ = draw.DOT(g, file)

Will generate

strict digraph {

    "#cat:"&(" [  weight=0 ];

    "#cat:"&(" -> ""big bird"" [  weight=0 ];

    "#cat:"&(" -> "@dog" [  weight=0 ];

    "@dog" [  weight=0 ];

    ""big bird"" [  weight=0 ];

}

which will fail trying to use dot -Tsvg -O mygraph.gv since it has non escaped characters

dot -Tsvg -O mygraph.gv
Error: mygraph.gv: syntax error in line 3 near '&'

I think a flag to optionaly escape html entities may be a good alternative

Zincr0 commented 1 year ago

Note: strconv.Quote() seems to be doing the job quite well in my latest tests

dominikbraun commented 1 year ago

I've investigated the issue a bit. There are two separate challenges:

  1. Escaping HTML entities. This might be a new option for draw.DOT and is a feature on its own.
  2. Handling escaped double quotes (\") like in your example. The escaping backslash being removed is a bug, and it seems to happen when rendering the template: https://go.dev/play/p/DvuB5B8ywIV

I'll focus on the \" bug for now. Maybe the escaping backslash can be preserved somehow.