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