open Printf
 
+type attribute = string * string  (* <key, value> pair *)
+
 let png_flags = "-Tpng"
 let map_flags = "-Tcmapx"
 
 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
+
 
 (** {1 LablGtk "widget" for rendering Graphviz graphs and connecting to clicks
  * on nodes, edges, ...} *)
 
+type attribute = string * string  (* <key, value> pair *)
+
 class type graphviz_widget =
   object
 
      * (e.g.: [ "shape","rect"; "href","http://foo.bar.com/";
      *          "title","foo"; "alt","description"; "coords","41,6,113,54" ] *)
     method connect_href:
-      (GdkEvent.Button.t -> (string * string) list -> unit) -> unit
+      (GdkEvent.Button.t -> attribute list -> unit) -> unit
 
       (** {3 low level access to embedded widgets}
        * Containment hierarchy:
 val gCirco: ?packing:(GObj.widget -> unit) -> unit -> graphviz_widget
 val gFdp: ?packing:(GObj.widget -> unit) -> unit -> graphviz_widget
 
+(** {2 Pretty printer for generating Graphviz markup} *)
+
+module Pp:
+  sig
+
+    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
+
+    module Dot: GraphvizFormatter
+
+  end
+