]> matita.cs.unibo.it Git - helm.git/blob - matita/matita/contribs/lambdadelta/bin/xhtbl/xmlUnparser.ml
update in lambdadelta
[helm.git] / matita / matita / contribs / lambdadelta / bin / xhtbl / xmlUnparser.ml
1 module A = Array
2 module F = Filename
3 module L = List
4 module P = Printf
5 module S = String
6
7 module O = Options
8 module T = Table
9 module M = Matrix
10
11 let xhtbl = "xhtbl"
12
13 let i = 0
14
15 let myself = F.basename (Sys.argv.(0))
16
17 let msg = P.sprintf "This file was generated by %s, do not edit" myself
18
19 let compose uri ext =
20    if uri.[pred (S.length uri)] = '/' then uri else
21    try
22       let i = S.index uri '#' in
23       let uri, fragment = S.sub uri 0 i, S.sub uri i (S.length uri - i) in
24       uri ^ ext ^ fragment
25    with Not_found -> uri ^ ext
26
27 let border cell =
28    let str = S.make 4 'n' in
29    if cell.M.cb.T.n then str.[0] <- 's';   
30    if cell.M.cb.T.e then str.[1] <- 's';
31    if cell.M.cb.T.s then str.[2] <- 's';
32    if cell.M.cb.T.w then str.[3] <- 's';
33    str :: cell.M.cc
34
35 let text baseuri ext = function
36    | T.Plain s              -> s
37    | T.Link (true, uri, s)  -> P.sprintf "<a href=\"%s\">%s</a>" uri s
38    | T.Link (false, uri, s) -> 
39       let uri = !O.baseuri ^ baseuri ^ compose uri ext in
40       P.sprintf "<a href=\"%s\">%s</a>" uri s
41
42 let name cell =
43    if cell.M.cn = "" then "" else P.sprintf " id=\"%s\"" cell.M.cn
44
45 let key cell =
46    if cell.M.ck = [] then "<br/>" else S.concat "" (L.map (text cell.M.cu cell.M.cx) cell.M.ck)
47
48 let ind i = S.make (2 * i) ' '
49
50 let out_cell och cell =
51    let cc = xhtbl :: border cell in
52    P.fprintf och "%s<td class=\"%s\"%s>%s</td>\n"
53       (ind (i+3)) (S.concat " " cc) (name cell) (key cell)
54
55 let out_row och row =
56    P.fprintf och "%s<tr class=\"%s\">\n" (ind (i+2)) xhtbl;
57    A.iter (out_cell och) row;
58    P.fprintf och "%s</tr>\n" (ind (i+2))
59
60 let out_space och (name, uri) =
61    let name = if name = "" then name else ":" ^ name in
62    P.fprintf och "                xmlns%s=\"%s\"\n" name uri
63
64 (****************************************************************************)
65
66 let open_out name spaces =
67    let fname = F.concat !O.output_dir (P.sprintf "%s.xsl" name) in
68    let spaces = ("xsl", "http://www.w3.org/1999/XSL/Transform") :: spaces in
69    let och = open_out fname in
70    P.fprintf och "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n";
71    P.fprintf och "<!-- %s -->\n\n" msg;   
72    P.fprintf och "<xsl:stylesheet version=\"1.0\"\n";
73    L.iter (out_space och) spaces;
74    P.fprintf och ">\n\n";
75    och
76
77 let output och name matrix =
78    P.fprintf och "<xsl:template name=\"%s\">\n" name;
79    P.fprintf och "%s<table class=\"%s\" cellpadding=\"4\" cellspacing=\"0\">\n" (ind (i+1)) xhtbl;
80    A.iter (out_row och) matrix.M.m;
81    P.fprintf och "%s</table>\n" (ind (i+1));
82    P.fprintf och "</xsl:template>\n\n"
83
84 let close_out och =
85    P.fprintf och "</xsl:stylesheet>\n";
86    close_out och
87
88 let map_incs och name =
89    P.fprintf och "<xsl:include href=\"%s.xsl\"/>\n" name
90
91 let map_tbls och name =
92    P.fprintf och "%s<xsl:when test=\"@name='%s'\">\n" (ind (i+2)) name;
93    P.fprintf och "%s<xsl:call-template name=\"%s\"/>\n" (ind (i+3)) name;
94    P.fprintf och "%s</xsl:when>\n" (ind (i+2))
95
96 let write_hook name incs tbls =
97    let och = open_out name [] in
98    L.iter (map_incs och) incs;
99    P.fprintf och "\n<xsl:template name=\"%s\">\n" name;
100    P.fprintf och "%s<xsl:choose>\n" (ind (i+1));
101    L.iter (map_tbls och) tbls;   
102    P.fprintf och "%s</xsl:choose>\n" (ind (i+1));
103    P.fprintf och "</xsl:template>\n\n";
104    close_out och