1 (* Copyright (C) 2000, HELM Team.
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.
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.
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.
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,
22 * For details, see the HELM World-Wide-Web page,
23 * http://cs.unibo.it/helm/.
26 (**************************************************************************)
30 (* Andrea Asperti <asperti@cs.unibo.it> *)
33 (**************************************************************************)
41 | Mtext of attr * string
44 | Mgliph of attr * string
45 | Mrow of attr * 'a mpres list
46 | Mfrac of attr * 'a mpres * 'a mpres
47 | Msqrt of attr * 'a mpres
48 | Mroot of attr * 'a mpres * 'a mpres
49 | Mstyle of attr * 'a mpres
50 | Merror of attr * 'a mpres
51 | Mpadded of attr * 'a mpres
52 | Mphantom of attr * 'a mpres
53 | Mfenced of attr * 'a mpres list
54 | Menclose of attr * 'a mpres
55 | Msub of attr * 'a mpres * 'a mpres
56 | Msup of attr * 'a mpres * 'a mpres
57 | Msubsup of attr * 'a mpres * 'a mpres *'a mpres
58 | Munder of attr * 'a mpres * 'a mpres
59 | Mover of attr * 'a mpres * 'a mpres
60 | Munderover of attr * 'a mpres * 'a mpres *'a mpres
61 (* | Multiscripts of ??? NOT IMPLEMEMENTED *)
62 | Mtable of attr * 'a row list
63 | Maction of attr * 'a mpres list
64 | Mobject of attr * 'a
65 and 'a row = Mtr of attr * 'a mtd list
66 and 'a mtd = Mtd of attr * 'a mpres
67 and attr = (string option * string * string) list
70 let smallskip = Mspace([None,"width","0.5em"]);;
71 let indentation = Mspace([None,"width","1em"]);;
74 Mrow([],[indentation;elem]);;
76 let standard_tbl_attr =
77 [None,"align","baseline 1";None,"equalrows","false";None,"columnalign","left"]
80 let two_rows_table attr a b =
81 Mtable(attr@standard_tbl_attr,
83 Mtr([],[Mtd([],b)])]);;
85 let two_rows_table_with_brackets attr a b op =
86 (* only the open bracket is added; the closed bracket must be in b *)
87 Mtable(attr@standard_tbl_attr,
88 [Mtr([],[Mtd([],Mrow([],[Mtext([],"(");a]))]);
89 Mtr([],[Mtd([],Mrow([],[indentation;op;b]))])]);;
91 let two_rows_table_without_brackets attr a b op =
92 Mtable(attr@standard_tbl_attr,
94 Mtr([],[Mtd([],Mrow([],[indentation;op;b]))])]);;
96 let row_with_brackets attr a b op =
97 (* by analogy with two_rows_table_with_brackets we only add the
99 Mrow(attr,[Mtext([],"(");a;op;b;Mtext([],")")])
101 let row_without_brackets attr a b op =
107 let print_mpres obj_printer mpres =
108 let module X = Xml in
111 Mi (attr,s) -> X.xml_nempty ~prefix "mi" attr (X.xml_cdata s)
112 | Mn (attr,s) -> X.xml_nempty ~prefix "mn" attr (X.xml_cdata s)
115 let len = String.length s in
116 if len > 1 && s.[0] = '\\'
117 then String.sub s 1 (len - 1)
120 X.xml_nempty ~prefix "mo" attr (X.xml_cdata s)
121 | Mtext (attr,s) -> X.xml_nempty ~prefix "mtext" attr (X.xml_cdata s)
122 | Mspace attr -> X.xml_empty ~prefix "mspace" attr
123 | Ms (attr,s) -> X.xml_nempty ~prefix "ms" attr (X.xml_cdata s)
124 | Mgliph (attr,s) -> X.xml_nempty ~prefix "mgliph" attr (X.xml_cdata s)
125 (* General Layout Schemata *)
127 X.xml_nempty ~prefix "mrow" attr
128 [< (List.fold_right (fun x i -> [< (aux x) ; i >]) l [<>])
130 | Mfrac (attr,m1,m2) ->
131 X.xml_nempty ~prefix "mfrac" attr [< aux m1; aux m2 >]
133 X.xml_nempty ~prefix "msqrt" attr [< aux m >]
134 | Mroot (attr,m1,m2) ->
135 X.xml_nempty ~prefix "mroot" attr [< aux m1; aux m2 >]
136 | Mstyle (attr,m) -> X.xml_nempty ~prefix "mstyle" attr [< aux m >]
137 | Merror (attr,m) -> X.xml_nempty ~prefix "merror" attr [< aux m >]
138 | Mpadded (attr,m) -> X.xml_nempty ~prefix "mpadded" attr [< aux m >]
139 | Mphantom (attr,m) -> X.xml_nempty ~prefix "mphantom" attr [< aux m >]
140 | Mfenced (attr,l) ->
141 X.xml_nempty ~prefix "mfenced" attr
142 [< (List.fold_right (fun x i -> [< (aux x) ; i >]) l [<>])
144 | Menclose (attr,m) -> X.xml_nempty ~prefix "menclose" attr [< aux m >]
145 (* Script and Limit Schemata *)
146 | Msub (attr,m1,m2) ->
147 X.xml_nempty ~prefix "msub" attr [< aux m1; aux m2 >]
148 | Msup (attr,m1,m2) ->
149 X.xml_nempty ~prefix "msup" attr [< aux m1; aux m2 >]
150 | Msubsup (attr,m1,m2,m3) ->
151 X.xml_nempty ~prefix "msubsup" attr [< aux m1; aux m2; aux m3 >]
152 | Munder (attr,m1,m2) ->
153 X.xml_nempty ~prefix "munder" attr [< aux m1; aux m2 >]
154 | Mover (attr,m1,m2) ->
155 X.xml_nempty ~prefix "mover" attr [< aux m1; aux m2 >]
156 | Munderover (attr,m1,m2,m3) ->
157 X.xml_nempty ~prefix "munderover" attr [< aux m1; aux m2; aux m3 >]
158 (* | Multiscripts of ??? NOT IMPLEMEMENTED *)
159 (* Tables and Matrices *)
160 | Mtable (attr, rl) ->
161 X.xml_nempty ~prefix "mtable" attr
162 [< (List.fold_right (fun x i -> [< (aux_mrow x) ; i >]) rl [<>]) >]
163 (* Enlivening Expressions *)
164 | Maction (attr, l) ->
165 X.xml_nempty ~prefix "maction" attr
166 [< (List.fold_right (fun x i -> [< (aux x) ; i >]) l [<>]) >]
167 | Mobject (attr, obj) ->
168 let box_stream = obj_printer obj in
169 X.xml_nempty ~prefix "semantics" attr
170 [< X.xml_nempty ~prefix "annotation-xml" [None, "encoding", "BoxML"]
174 let module X = Xml in
177 X.xml_nempty ~prefix "mtr" attr
178 [< (List.fold_right (fun x i -> [< (aux_mtd x) ; i >]) l [<>])
181 let module X = Xml in
183 Mtd (attr,m) -> X.xml_nempty ~prefix "mtd" attr
185 X.xml_nempty ~prefix "mphantom" []
186 (X.xml_nempty ~prefix "mtext" [] (X.xml_cdata "(")) >]
191 let document_of_mpres pres =
192 [< Xml.xml_cdata "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n" ;
194 Xml.xml_nempty ~prefix "math"
195 [Some "xmlns","m","http://www.w3.org/1998/Math/MathML" ;
196 Some "xmlns","helm","http://www.cs.unibo.it/helm" ;
197 Some "xmlns","xlink","http://www.w3.org/1999/xlink"
198 ] (Xml.xml_nempty ~prefix "mstyle" [None, "mathvariant", "normal"; None,
199 "rowspacing", "0.6ex"] (print_mpres (fun _ -> assert false) pres))
202 let get_attr = function
223 | Msubsup (attr, _, _, _)
227 | Munder (attr, _, _)
228 | Munderover (attr, _, _, _) ->
231 let set_attr attr = function
232 | Maction (_, x) -> Maction (attr, x)
233 | Menclose (_, x) -> Menclose (attr, x)
234 | Merror (_, x) -> Merror (attr, x)
235 | Mfenced (_, x) -> Mfenced (attr, x)
236 | Mfrac (_, x, y) -> Mfrac (attr, x, y)
237 | Mgliph (_, x) -> Mgliph (attr, x)
238 | Mi (_, x) -> Mi (attr, x)
239 | Mn (_, x) -> Mn (attr, x)
240 | Mo (_, x) -> Mo (attr, x)
241 | Mobject (_, x) -> Mobject (attr, x)
242 | Mover (_, x, y) -> Mover (attr, x, y)
243 | Mpadded (_, x) -> Mpadded (attr, x)
244 | Mphantom (_, x) -> Mphantom (attr, x)
245 | Mroot (_, x, y) -> Mroot (attr, x, y)
246 | Mrow (_, x) -> Mrow (attr, x)
247 | Ms (_, x) -> Ms (attr, x)
248 | Mspace _ -> Mspace attr
249 | Msqrt (_, x) -> Msqrt (attr, x)
250 | Mstyle (_, x) -> Mstyle (attr, x)
251 | Msub (_, x, y) -> Msub (attr, x, y)
252 | Msubsup (_, x, y, z) -> Msubsup (attr, x, y, z)
253 | Msup (_, x, y) -> Msup (attr, x, y)
254 | Mtable (_, x) -> Mtable (attr, x)
255 | Mtext (_, x) -> Mtext (attr, x)
256 | Munder (_, x, y) -> Munder (attr, x, y)
257 | Munderover (_, x, y, z) -> Munderover (attr, x, y, z)