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