]> matita.cs.unibo.it Git - helm.git/blobdiff - matita/lablGraphviz.ml
added pretty printer for dot files (it may need to be moved elsewhere in the future)
[helm.git] / matita / lablGraphviz.ml
index d5e06c26564946746a9fe00799073e46554b45b5..ac5cd7537577fa31f2d9b28bb0c2ec6f5cd5f676 100644 (file)
@@ -27,6 +27,8 @@
 
 open Printf
 
+type attribute = string * string  (* <key, value> pair *)
+
 let png_flags = "-Tpng"
 let map_flags = "-Tcmapx"
 
@@ -116,3 +118,39 @@ let gTwopi = factory "twopi"
 let gCirco = factory "circo"
 let gFdp = factory "fdp"
 
+module Pp =
+  struct
+
+    module type GraphvizFormatter =
+      sig
+        val header: ?name:string -> Format.formatter -> unit
+        val node: string -> ?attrs:(attribute list) -> Format.formatter -> unit
+        val edge:
+          string -> string -> ?attrs:(attribute list) -> Format.formatter ->
+            unit
+        val raw: string -> Format.formatter -> unit
+        val trailer: Format.formatter -> unit
+      end
+
+    open Format
+
+    module Dot =
+      struct
+        let attribute fmt (k, v) = fprintf fmt "@[<hv2>%s=@,\"%s\",@]" k v
+        let attributes attrs fmt = List.iter (attribute fmt) attrs
+
+        let header ?(name = "g") fmt = fprintf fmt "@[<hv2>digraph %s {@," name
+        let node name ?(attrs = []) fmt =
+          fprintf fmt "@[<hov2>%s@ [" name;
+          attributes attrs fmt;
+          fprintf fmt "];@]@,"
+        let edge name1 name2 ?(attrs = []) fmt =
+          fprintf fmt "@[<hov2>%s ->@ %s@ [" name1 name2;
+          attributes attrs fmt;
+          fprintf fmt "];@]@,"
+        let raw s fmt = pp_print_string fmt s
+        let trailer fmt = fprintf fmt "@,}@]%!"
+      end
+
+  end
+