6c45c930197fffcc763e0beef43da3feb654a4f0
[tatoo.git] / src / auto / html.ml
1 open Format
2 module M = Map.Make(struct type t = int let compare = compare end)
3
4 let info = Hashtbl.create 2017
5
6
7 let add_info (nodeid:int) (i:int) s =
8   let m = try Hashtbl.find info nodeid with Not_found -> M.empty in
9   let old_s = try M.find i m with Not_found -> "" in
10   let s' = old_s ^ s in
11   let m' = M.add i s' m in
12   Hashtbl.replace info nodeid m'
13
14 let buff = Buffer.create 20
15 let fmt = formatter_of_buffer buff
16
17 let trace nodeid i =
18   let () = pp_print_flush fmt ();
19     Buffer.clear buff
20   in
21   kfprintf (fun fmt ->
22     pp_print_flush fmt ();
23     let s = Buffer.contents buff in
24     add_info nodeid i s) fmt
25
26
27 let gen_trace (type s)  = (); fun t tree ->
28   let module T = (val (t) : Tree.Sig.S with type t = s) in
29   let rec loop odot ohtml node parent =
30     if node == T.nil then () else begin
31       let s_node = "node" ^ (string_of_int (T.preorder tree node)) in
32       fprintf odot "%s[ id=\"%s\" label=\"%s\"];\n"
33         s_node s_node (Utils.QName.to_string (T.tag tree node));
34       let m =
35         try
36           Hashtbl.find info (T.preorder tree node)
37         with Not_found -> M.empty
38       in
39       fprintf ohtml "data['%s'] = new Array();\n" s_node;
40       M.iter
41         (fun i s -> fprintf ohtml "data['%s'][%i] = '%s';\n" s_node i s)
42         m;
43       if parent != T.nil then
44         fprintf odot "node%i -> %s;\n"
45           (T.preorder tree parent) s_node;
46       loop odot ohtml (T.first_child tree node) node;
47       loop odot ohtml (T.next_sibling tree node) parent
48     end
49   in
50   ignore (Sys.command "mkdir -p tests/trace");
51   let odot_ = open_out "tests/trace/trace.dot" in
52   let ohtml_ = open_out "tests/trace/trace.html" in
53   let odot = formatter_of_out_channel odot_ in
54   let ohtml = formatter_of_out_channel ohtml_ in
55   fprintf odot "digraph G {\n node[shape=box, style=filled, fillcolor=white];splines=false;";
56   fprintf ohtml "<html>\
57 <head><title></title>
58 <link rel='stylesheet' type='text/css' href='trace.css' />\
59 <meta http-equiv='content-type' content='text/html;charset=utf-8'/>\
60 </head>\
61 <body>\
62 <div id='data' > </div>\
63 <script type='text/javascript'>";
64   loop odot ohtml (T.root tree) (T.nil);
65   fprintf odot "\n}\n%!";
66   pp_print_flush odot ();
67   close_out odot_;
68   ignore (Sys.command "dot -o tests/trace/trace.svg -Tsvg tests/trace/trace.dot");
69   ignore (Sys.command "./tools/add_onclick.sh tests/trace/trace.svg > tests/trace/trace2.svg");
70   fprintf ohtml "var activate = function (id) {\
71   var d = document.getElementById('data');
72   var msg = '';
73   for (i=0; i < data[id].length; i++)
74      msg += ('<p>' + i + ':') + data[id][i] + '</p>\\n';
75   d.innerHTML = msg;
76   return;
77   };\n";
78
79   fprintf ohtml "</script>\n<div id='svg'>\n";
80   let fi = open_in "tests/trace/trace2.svg" in
81   try
82     while true do
83       let s = input_line fi in
84       fprintf ohtml "%s\n" s;
85     done
86   with
87     End_of_file ->
88       fprintf ohtml "</div></body></html>\n%!";
89       pp_print_flush ohtml ();
90       close_out ohtml_;
91       close_in fi
92