Flatten the sources, only leave the XPath module packed.
[tatoo.git] / src / html.ml
diff --git a/src/html.ml b/src/html.ml
new file mode 100644 (file)
index 0000000..6c45c93
--- /dev/null
@@ -0,0 +1,92 @@
+open Format
+module M = Map.Make(struct type t = int let compare = compare end)
+
+let info = Hashtbl.create 2017
+
+
+let add_info (nodeid:int) (i:int) s =
+  let m = try Hashtbl.find info nodeid with Not_found -> M.empty in
+  let old_s = try M.find i m with Not_found -> "" in
+  let s' = old_s ^ s in
+  let m' = M.add i s' m in
+  Hashtbl.replace info nodeid m'
+
+let buff = Buffer.create 20
+let fmt = formatter_of_buffer buff
+
+let trace nodeid i =
+  let () = pp_print_flush fmt ();
+    Buffer.clear buff
+  in
+  kfprintf (fun fmt ->
+    pp_print_flush fmt ();
+    let s = Buffer.contents buff in
+    add_info nodeid i s) fmt
+
+
+let gen_trace (type s)  = (); fun t tree ->
+  let module T = (val (t) : Tree.Sig.S with type t = s) in
+  let rec loop odot ohtml node parent =
+    if node == T.nil then () else begin
+      let s_node = "node" ^ (string_of_int (T.preorder tree node)) in
+      fprintf odot "%s[ id=\"%s\" label=\"%s\"];\n"
+        s_node s_node (Utils.QName.to_string (T.tag tree node));
+      let m =
+        try
+          Hashtbl.find info (T.preorder tree node)
+        with Not_found -> M.empty
+      in
+      fprintf ohtml "data['%s'] = new Array();\n" s_node;
+      M.iter
+        (fun i s -> fprintf ohtml "data['%s'][%i] = '%s';\n" s_node i s)
+        m;
+      if parent != T.nil then
+        fprintf odot "node%i -> %s;\n"
+          (T.preorder tree parent) s_node;
+      loop odot ohtml (T.first_child tree node) node;
+      loop odot ohtml (T.next_sibling tree node) parent
+    end
+  in
+  ignore (Sys.command "mkdir -p tests/trace");
+  let odot_ = open_out "tests/trace/trace.dot" in
+  let ohtml_ = open_out "tests/trace/trace.html" in
+  let odot = formatter_of_out_channel odot_ in
+  let ohtml = formatter_of_out_channel ohtml_ in
+  fprintf odot "digraph G {\n node[shape=box, style=filled, fillcolor=white];splines=false;";
+  fprintf ohtml "<html>\
+<head><title></title>
+<link rel='stylesheet' type='text/css' href='trace.css' />\
+<meta http-equiv='content-type' content='text/html;charset=utf-8'/>\
+</head>\
+<body>\
+<div id='data' > </div>\
+<script type='text/javascript'>";
+  loop odot ohtml (T.root tree) (T.nil);
+  fprintf odot "\n}\n%!";
+  pp_print_flush odot ();
+  close_out odot_;
+  ignore (Sys.command "dot -o tests/trace/trace.svg -Tsvg tests/trace/trace.dot");
+  ignore (Sys.command "./tools/add_onclick.sh tests/trace/trace.svg > tests/trace/trace2.svg");
+  fprintf ohtml "var activate = function (id) {\
+  var d = document.getElementById('data');
+  var msg = '';
+  for (i=0; i < data[id].length; i++)
+     msg += ('<p>' + i + ':') + data[id][i] + '</p>\\n';
+  d.innerHTML = msg;
+  return;
+  };\n";
+
+  fprintf ohtml "</script>\n<div id='svg'>\n";
+  let fi = open_in "tests/trace/trace2.svg" in
+  try
+    while true do
+      let s = input_line fi in
+      fprintf ohtml "%s\n" s;
+    done
+  with
+    End_of_file ->
+      fprintf ohtml "</div></body></html>\n%!";
+      pp_print_flush ohtml ();
+      close_out ohtml_;
+      close_in fi
+