]> matita.cs.unibo.it Git - helm.git/blob - helm/www/lambdadelta/bin/xhtbl/textUnparser.ml
- we begin the new site based on ld_web
[helm.git] / helm / www / lambdadelta / bin / xhtbl / textUnparser.ml
1 module L = List
2 module P = Printf
3 module S = String
4
5 module T = Table
6 module F = Fold
7
8 type status = {
9    i: int;              (* indentation *)
10    out: string -> unit; (* output function *)
11 }
12
13 let home = {
14    i = 0; out = print_string
15 }
16
17 let indent st =
18    S.make st.i ' '
19
20 let add st = {st with i = st.i + 3}
21
22 let sub st = {st with i = st.i - 3}
23
24 let parent = function
25    | None       -> "key"
26    | Some false -> "col"
27    | Some true  -> "row"
28
29 let size ts =
30    P.sprintf "(%u, %u); (%u+%u, %u+%u); %s"
31       ts.T.y ts.T.x ts.T.rf ts.T.ri ts.T.cf ts.T.ci (parent ts.T.p)
32
33 let border tb =
34    let str = S.make 4 ' ' in
35    if tb.T.w then str.[0] <- 'W';
36    if tb.T.n then str.[1] <- 'N';
37    if tb.T.e then str.[2] <- 'E';
38    if tb.T.s then str.[3] <- 'S';
39    str
40
41 let css tc =
42    P.sprintf "\"%s\"" (S.concat " " tc)
43
44 let text = function
45    | T.Plain s       -> P.sprintf "\"%s\"" s
46    | T.Link (uri, s) -> P.sprintf "@(\"%s\" \"%s\")" uri s
47
48 let key = function
49    | T.Text sl       -> S.concat " + " (L.map text sl)
50    | T.Glue None     -> "*"
51    | T.Glue (Some i) -> P.sprintf "%u" i
52
53 let entry = function
54    | false -> "column"
55    | true  -> "row"
56
57 (****************************************************************************)
58
59 let open_table st t =
60    let str = 
61       P.sprintf "%s[{#%u: %s; %s; %s}\n"    
62          (indent st) t.T.ti (size t.T.ts) (border t.T.tb) (css t.T.tc)
63    in
64    st.out str; add st
65
66 let close_table st t =
67    let st = sub st in
68    let str = P.sprintf "%s]\n" (indent st) in
69    st.out str; st
70
71 let map_key st k =
72    let str = P.sprintf "%s%s\n" (indent st) (key k) in
73    st.out str; st
74    
75 let open_line b st =
76    let str = P.sprintf "%s%s\n" (indent st) (entry b) in
77    st.out str; st
78
79 let close_line b st = st
80
81 let open_entry b st = st
82
83 let close_entry b st sst = st
84
85 let cb = {
86    F.open_table = open_table; F.close_table = close_table;   
87    F.open_line = open_line; F.close_line = close_line;
88    F.open_entry = open_entry; F.close_entry = close_entry;
89    F.map_key = map_key;
90 }
91
92 let debug t =
93    let _ = F.fold_table cb home t in ()