]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_transformations/box.ml
snapshot (first version in which some extensions work, e.g. infix +)
[helm.git] / helm / ocaml / cic_transformations / box.ml
1 (* Copyright (C) 2000, HELM Team.
2  * 
3  * This file is part of HELM, an Hypertextual, Electronic
4  * Library of Mathematics, developed at the Computer Science
5  * Department, University of Bologna, Italy.
6  * 
7  * HELM is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  * 
12  * HELM is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with HELM; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://cs.unibo.it/helm/.
24  *)
25
26 (*************************************************************************)
27 (*                                                                       *)
28 (*                           PROJECT HELM                                *)
29 (*                                                                       *)
30 (*                Andrea Asperti <asperti@cs.unibo.it>                   *)
31 (*                             13/2/2004                                 *)
32 (*                                                                       *)
33 (*************************************************************************)
34
35 type 
36   'expr box =
37     Text of attr * string
38   | Space of attr
39   | Ink of attr
40   | H of attr * ('expr box) list
41   | V of attr * ('expr box) list
42   | Object of attr * 'expr
43   | Action of attr * ('expr box) list
44
45 and attr = (string option * string * string) list
46
47 let smallskip = Space([None,"width","0.5em"]);;
48 let skip = Space([None,"width","1em"]);;
49
50 let indent t = H([],[skip;t]);;
51
52 (* BoxML prefix *)
53 let prefix = "b";;
54  
55 let box2xml ~obj2xml box =
56   let rec aux =
57    let module X = Xml in
58     function
59         Text (attr,s) -> X.xml_nempty ~prefix "text" attr (X.xml_cdata s)
60       | Space attr -> X.xml_empty ~prefix "space" attr
61       | Ink attr -> X.xml_empty ~prefix "ink" attr
62       | H (attr,l) ->
63           X.xml_nempty ~prefix "h" attr 
64             [< (List.fold_right (fun x i -> [< (aux x) ; i >]) l [<>])
65             >]
66       | V (attr,l) ->
67           X.xml_nempty ~prefix "v" attr 
68             [< (List.fold_right (fun x i -> [< (aux x) ; i >]) l [<>])
69             >]
70       | Object (attr,m) ->
71           X.xml_nempty ~prefix "obj" attr [< obj2xml m >]
72       | Action (attr,l) ->
73           X.xml_nempty ~prefix "action" attr 
74             [< (List.fold_right (fun x i -> [< (aux x) ; i >]) l [<>]) >]
75   in
76   aux box
77 ;;
78
79 let rec map f = function
80   | (Text _) as box -> box
81   | (Space _) as box -> box
82   | (Ink _) as box -> box
83   | H (attr, l) -> H (attr, List.map (map f) l)
84   | V (attr, l) -> V (attr, List.map (map f) l)
85   | Action (attr, l) -> Action (attr, List.map (map f) l)
86   | Object (attr, obj) -> Object (attr, f obj)
87 ;;
88
89 (*
90 let document_of_box ~obj2xml pres =
91  [< Xml.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
92     Xml.xml_cdata "\n";
93     Xml.xml_nempty ~prefix "box"
94      [Some "xmlns","m","http://www.w3.org/1998/Math/MathML" ;
95       Some "xmlns","b","http://helm.cs.unibo.it/2003/BoxML" ;
96       Some "xmlns","helm","http://www.cs.unibo.it/helm" ;
97       Some "xmlns","xlink","http://www.w3.org/1999/xlink"
98      ] (print_box pres)
99  >]
100 *)
101
102 let b_h a b = H(a,b)
103 let b_v a b = V(a,b)
104 let b_text a b = Text(a,b)
105 let b_object b = Object ([],b)
106 let b_indent = indent
107 let b_space = Space [None, "width", "0.5em"]
108 let b_kw = b_text [None, "color", "red"]
109