1 (* Copyright (C) 2004-2005, 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://helm.cs.unibo.it/
26 type mathml_markup = boxml_markup Mpresentation.mpres
27 and boxml_markup = mathml_markup Box.box
29 type markup = mathml_markup
31 let atop_attributes = [None, "linethickness", "0pt"]
33 let to_unicode = Utf8Macro.unicode_of_tex
35 let rec make_attributes l1 = function
37 | None :: tl -> make_attributes (List.tl l1) tl
39 let p,n = List.hd l1 in
40 (p,n,s) :: make_attributes (List.tl l1) tl
44 Mpresentation.Mobject (_, box) -> box
45 | mpres -> Box.Object ([], mpres)
49 Box.Object (_, mpres) -> mpres
50 | box -> Mpresentation.Mobject ([], box)
52 let rec genuine_math =
54 | Mpresentation.Mobject ([], obj) -> not (genuine_box obj)
58 | Box.Object ([], mpres) -> not (genuine_math mpres)
61 let rec eligible_math =
63 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> eligible_math mpres
64 | Mpresentation.Mobject ([], _) -> false
67 let rec promote_to_math =
69 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> promote_to_math mpres
72 let small_skip = Mpresentation.Mspace [None, "width", "0.5em"]
74 let box_of mathonly spec attrs children =
78 let kind, spacing, indent = spec in
81 CicNotationUtil.dress small_skip children
85 if mathonly then Mpresentation.Mrow (attrs, dress children)
88 (if spacing then [None, "spacing", "0.5em"] else [])
89 @ (if indent then [None, "indent", "0.5em"] else [])
94 if List.for_all eligible_math children then
95 Mpresentation.Mrow (attrs',
96 dress (List.map promote_to_math children))
98 mpres_of_box (Box.H (attrs',
99 List.map box_of_mpres children))
100 (* | CicNotationPt.H when List.for_all genuine_math children ->
101 Mpresentation.Mrow (attrs', dress children) *)
103 mpres_of_box (Box.V (attrs',
104 List.map box_of_mpres children))
105 | CicNotationPt.HV ->
106 mpres_of_box (Box.HV (attrs',
107 List.map box_of_mpres children))
108 | CicNotationPt.HOV ->
109 mpres_of_box (Box.HOV (attrs',
110 List.map box_of_mpres children))
112 let open_paren = Mpresentation.Mo ([], "(")
113 let closed_paren = Mpresentation.Mo ([], ")")
114 let open_brace = Mpresentation.Mo ([], "{")
115 let closed_brace = Mpresentation.Mo ([], "}")
116 let hidden_substs = Mpresentation.Mtext ([], "{...}")
117 let open_box_paren = Box.Text ([], "(")
118 let closed_box_paren = Box.Text ([], ")")
119 let semicolon = Mpresentation.Mo ([], ";")
120 let toggle_action children =
121 Mpresentation.Maction ([None, "actiontype", "toggle"], children)
123 type child_pos = [ `None | `Left | `Right | `Inner ]
127 | Gramext.LeftA -> "LeftA"
128 | Gramext.RightA -> "RightA"
129 | Gramext.NonA -> "NonA"
139 let module P = Mpresentation in
140 let rec aux_mpres = function
147 | P.Mobject (_, box) -> aux_box box
148 | P.Maction (_, [mpres])
149 | P.Mrow (_, [mpres]) -> aux_mpres mpres
151 and aux_box = function
155 | Box.Object (_, mpres) -> aux_mpres mpres
160 | Box.Action (_, [box]) -> aux_box box
165 let add_parens child_prec child_assoc child_pos curr_prec t =
166 if is_atomic t then t
167 else if child_prec < curr_prec
168 || (child_prec = curr_prec &&
169 child_assoc = Gramext.LeftA &&
171 || (child_prec = curr_prec &&
172 child_assoc = Gramext.RightA &&
174 then (* parens should be added *)
176 | Mpresentation.Mobject (_, box) ->
177 mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
178 | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
182 let render ids_to_uris =
183 let module A = CicNotationPt in
184 let module P = Mpresentation in
185 let use_unicode = true in
186 let lookup_uri = function
188 | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
190 let make_href xmlattrs xref uris =
191 let xref_uri = lookup_uri xref in
192 let raw_uris = List.map UriManager.string_of_uri uris in
194 match xref_uri, raw_uris with
196 | Some uri, [] -> Some uri
197 | None, raw_uris -> Some (String.concat " " raw_uris)
198 | Some uri, raw_uris -> Some (String.concat " " (uri :: raw_uris))
201 @ make_attributes [Some "helm", "xref"; Some "xlink", "href"] [xref; uri]
203 let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
204 (* when mathonly is true no boxes should be generated, only mrows *)
205 let rec aux xmlattrs mathonly xref pos prec uris t =
207 | A.AttributedTerm (attr, t) ->
208 aux_attribute xmlattrs mathonly xref pos prec uris t attr
209 | A.Num (literal, _) ->
211 (RenderingAttrs.number_attributes `MathML)
212 @ make_href xmlattrs xref uris
214 P.Mn (attrs, literal)
215 | A.Symbol (literal, _) ->
217 (RenderingAttrs.symbol_attributes `MathML)
218 @ make_href xmlattrs xref uris
220 P.Mo (attrs, to_unicode literal)
221 | A.Ident (literal, subst)
222 | A.Uri (literal, subst) ->
224 (RenderingAttrs.ident_attributes `MathML)
225 @ make_href xmlattrs xref []
227 let name = P.Mi (attrs, to_unicode literal) in
233 box_of mathonly (A.H, false, false) []
235 :: (CicNotationUtil.dress semicolon
238 box_of mathonly (A.H, false, false) [] [
240 P.Mo ([], to_unicode "\\def");
241 aux [] mathonly xref pos prec uris t ])
244 (* (CicNotationUtil.dress semicolon
247 let var_uri = UriManager.uri_of_string var in
248 let var_name = UriManager.name_of_uri var_uri in
249 let href_attr = Some "xlink", "href", var in
250 box_of mathonly (A.H, false, false) [] [
251 P.Mi ([href_attr], var_name);
252 P.Mo ([], to_unicode "\\def");
253 aux [] mathonly xref pos prec uris t ])
256 let substs_maction = toggle_action [ hidden_substs; substs' ] in
257 box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
258 | A.Literal l -> aux_literal xmlattrs xref prec uris l
259 | A.UserInput -> P.Mtext ([], "%")
260 | A.Layout l -> aux_layout mathonly xref pos prec uris l
262 | A.Variable _ -> assert false (* should have been instantiated *)
264 prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
266 and aux_attribute xmlattrs mathonly xref pos prec uris t =
269 | `Raw _ -> aux xmlattrs mathonly xref pos prec uris t
270 | `Level (child_prec, child_assoc) ->
271 let t' = aux xmlattrs mathonly xref pos child_prec uris t in
272 add_parens child_prec child_assoc pos prec t'
273 | `IdRef xref -> aux xmlattrs mathonly (Some xref) pos prec uris t
274 | `Href uris' -> aux xmlattrs mathonly xref pos prec uris' t
275 | `XmlAttrs xmlattrs -> aux xmlattrs mathonly xref pos prec uris t
276 and aux_literal xmlattrs xref prec uris l =
277 let attrs = make_href xmlattrs xref uris in
279 | `Symbol s -> P.Mo (attrs, to_unicode s)
280 | `Keyword s -> P.Mo (attrs, to_unicode s)
281 | `Number s -> P.Mn (attrs, to_unicode s))
282 and aux_layout mathonly xref pos prec uris l =
283 let attrs = make_xref xref in
284 let invoke' t = aux [] true None pos prec uris t in
286 | A.Sub (t1, t2) -> P.Msub (attrs, invoke' t1, invoke' t2)
287 | A.Sup (t1, t2) -> P.Msup (attrs, invoke' t1, invoke' t2)
288 | A.Below (t1, t2) -> P.Munder (attrs, invoke' t1, invoke' t2)
289 | A.Above (t1, t2) -> P.Mover (attrs, invoke' t1, invoke' t2)
291 | A.Over (t1, t2) -> P.Mfrac (attrs, invoke' t1, invoke' t2)
293 P.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2)
294 | A.Sqrt t -> P.Msqrt (attrs, invoke' t)
295 | A.Root (t1, t2) -> P.Mroot (attrs, invoke' t1, invoke' t2)
296 | A.Box ((_, spacing, _) as kind, terms) ->
298 aux_children mathonly spacing xref pos prec uris
299 (CicNotationUtil.ungroup terms)
301 box_of mathonly kind attrs children
304 aux_children mathonly false xref pos prec uris
305 (CicNotationUtil.ungroup terms)
307 box_of mathonly (A.H, false, false) attrs children
308 | A.Break -> assert false (* TODO? *)
309 and aux_children mathonly spacing xref pos prec uris terms =
311 let rec aux_list first clusters acc =
313 [] when acc = [] -> List.rev clusters
314 | [] -> aux_list first (List.rev acc :: clusters) [] []
315 | (A.Layout A.Break) :: tl when acc = [] ->
316 aux_list first clusters [] tl
317 | (A.Layout A.Break) :: tl ->
318 aux_list first (List.rev acc :: clusters) [] tl
330 aux_list false clusters
331 (aux [] mathonly xref pos' prec uris hd :: acc) []
334 match pos, first with
336 | `None, false -> `Inner
337 | `Left, true -> `Left
338 | `Left, false -> `Inner
339 | `Right, _ -> `Inner
340 | `Inner, _ -> `Inner
342 aux_list false clusters
343 (aux [] mathonly xref pos' prec uris hd :: acc) tl
350 | tl -> box_of mathonly (A.H, spacing, false) [] tl
352 List.map boxify_pres (find_clusters terms)
354 aux [] false None `None 0 []
356 let rec print_box (t: CicNotationPres.boxml_markup) =
357 Box.box2xml print_mpres t
358 and print_mpres (t: CicNotationPres.mathml_markup) =
359 Mpresentation.print_mpres print_box t
361 let render_to_boxml id_to_uri t =
362 let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
363 Xml.add_xml_declaration xml_stream