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 module Ast = CicNotationPt
27 module Mpres = Mpresentation
29 type mathml_markup = boxml_markup Mpres.mpres
30 and boxml_markup = mathml_markup Box.box
32 type markup = mathml_markup
34 let atop_attributes = [None, "linethickness", "0pt"]
36 let to_unicode = Utf8Macro.unicode_of_tex
38 (* let rec make_attributes l1 = function
40 | None :: tl -> make_attributes (List.tl l1) tl
42 let p,n = List.hd l1 in
43 prerr_endline (Printf.sprintf "make_attributes %s %s" n s);
44 (p,n,s) :: make_attributes (List.tl l1) tl *)
45 let rec make_attributes l1 = function
49 | None -> make_attributes (List.tl l1) tl
51 let p,n = List.hd l1 in
52 (* prerr_endline (Printf.sprintf "make_attributes %s %s" n s); *)
54 (p,n,s) :: make_attributes (List.tl l1) tl)
58 Mpresentation.Mobject (_, box) -> box
59 | mpres -> Box.Object ([], mpres)
63 Box.Object (_, mpres) -> mpres
64 | box -> Mpresentation.Mobject ([], box)
66 let rec genuine_math =
68 | Mpresentation.Mobject ([], obj) -> not (genuine_box obj)
72 | Box.Object ([], mpres) -> not (genuine_math mpres)
75 let rec eligible_math =
77 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> eligible_math mpres
78 | Mpresentation.Mobject ([], _) -> false
81 let rec promote_to_math =
83 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> promote_to_math mpres
87 Mpresentation.Mspace (RenderingAttrs.small_skip_attributes `MathML)
89 let box_of mathonly spec attrs children =
93 let kind, spacing, indent = spec in
96 CicNotationUtil.dress small_skip children
100 if mathonly then Mpresentation.Mrow (attrs, dress children)
103 (if spacing then RenderingAttrs.spacing_attributes `BoxML else [])
104 @ (if indent then RenderingAttrs.indent_attributes `BoxML else [])
109 if List.for_all eligible_math children then
110 Mpresentation.Mrow (attrs',
111 dress (List.map promote_to_math children))
113 mpres_of_box (Box.H (attrs',
114 List.map box_of_mpres children))
115 (* | Ast.H when List.for_all genuine_math children ->
116 Mpresentation.Mrow (attrs', dress children) *)
118 mpres_of_box (Box.V (attrs',
119 List.map box_of_mpres children))
121 mpres_of_box (Box.HV (attrs',
122 List.map box_of_mpres children))
124 mpres_of_box (Box.HOV (attrs',
125 List.map box_of_mpres children))
127 let open_paren = Mpresentation.Mo ([], "(")
128 let closed_paren = Mpresentation.Mo ([], ")")
129 let open_brace = Mpresentation.Mo ([], "{")
130 let closed_brace = Mpresentation.Mo ([], "}")
131 let hidden_substs = Mpresentation.Mtext ([], "{...}")
132 let open_box_paren = Box.Text ([], "(")
133 let closed_box_paren = Box.Text ([], ")")
134 let semicolon = Mpresentation.Mo ([], ";")
135 let toggle_action children =
136 Mpresentation.Maction ([None, "actiontype", "toggle"], children)
138 type child_pos = [ `None | `Left | `Right | `Inner ]
142 | Gramext.LeftA -> "LeftA"
143 | Gramext.RightA -> "RightA"
144 | Gramext.NonA -> "NonA"
154 let rec aux_mpres = function
160 | Mpres.Mspace _ -> true
161 | Mpres.Mobject (_, box) -> aux_box box
162 | Mpres.Maction (_, [mpres])
163 | Mpres.Mrow (_, [mpres]) -> aux_mpres mpres
165 and aux_box = function
169 | Box.Object (_, mpres) -> aux_mpres mpres
174 | Box.Action (_, [box]) -> aux_box box
179 let add_parens child_prec child_assoc child_pos curr_prec t =
180 if is_atomic t then t
181 else if child_prec < curr_prec
182 || (child_prec = curr_prec &&
183 child_assoc = Gramext.LeftA &&
185 || (child_prec = curr_prec &&
186 child_assoc = Gramext.RightA &&
188 then (* parens should be added *)
190 | Mpresentation.Mobject (_, box) ->
191 mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
192 | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
196 let render ids_to_uris =
197 let module A = Ast in
198 let module P = Mpresentation in
199 let use_unicode = true in
200 let lookup_uri = function
202 | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
204 let make_href xmlattrs xref uris =
205 let xref_uri = lookup_uri !xref in
206 let raw_uris = List.map UriManager.string_of_uri !uris in
208 match xref_uri, raw_uris with
210 | Some uri, [] -> Some uri
211 | None, raw_uris -> Some (String.concat " " raw_uris)
212 | Some uri, raw_uris -> Some (String.concat " " (uri :: raw_uris))
216 @ make_attributes [Some "helm", "xref"; Some "xlink", "href"]
219 let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
220 (* when mathonly is true no boxes should be generated, only mrows *)
222 let rec aux xmlattrs mathonly xref pos prec uris t =
224 | A.AttributedTerm _ ->
225 aux_attributes xmlattrs mathonly xref pos prec uris t
226 | A.Num (literal, _) ->
228 (RenderingAttrs.number_attributes `MathML)
229 @ make_href xmlattrs xref uris
231 Mpres.Mn (attrs, literal)
232 | A.Symbol (literal, _) ->
234 (RenderingAttrs.symbol_attributes `MathML)
235 @ make_href xmlattrs xref uris
237 Mpres.Mo (attrs, to_unicode literal)
238 | A.Ident (literal, subst)
239 | A.Uri (literal, subst) ->
241 (RenderingAttrs.ident_attributes `MathML)
242 @ make_href xmlattrs xref (ref [])
244 let name = Mpres.Mi (attrs, to_unicode literal) in
250 box_of mathonly (A.H, false, false) []
252 :: (CicNotationUtil.dress semicolon
255 box_of mathonly (A.H, false, false) [] [
257 Mpres.Mo ([], to_unicode "\\def");
258 aux [] mathonly xref pos prec uris t ])
261 (* (CicNotationUtil.dress semicolon
264 let var_uri = UriManager.uri_of_string var in
265 let var_name = UriManager.name_of_uri var_uri in
266 let href_attr = Some "xlink", "href", var in
267 box_of mathonly (A.H, false, false) [] [
268 Mpres.Mi ([href_attr], var_name);
269 Mpres.Mo ([], to_unicode "\\def");
270 aux [] mathonly xref pos prec uris t ])
273 let substs_maction = toggle_action [ hidden_substs; substs' ] in
274 box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
275 | A.Literal l -> aux_literal xmlattrs xref prec uris l
276 | A.UserInput -> Mpres.Mtext ([], "%")
277 | A.Layout l -> aux_layout mathonly xref pos prec uris l
279 | A.Variable _ -> assert false (* should have been instantiated *)
281 prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
283 and aux_attributes xmlattrs mathonly xref pos prec uris t =
284 let new_level = ref None in
285 let new_xref = ref None in
286 let new_uris = ref [] in
287 let new_xmlattrs = ref [] in
288 let rec aux_attribute =
290 | A.AttributedTerm (attr, t) ->
294 | `Level (child_prec, child_assoc) ->
295 new_level := Some (child_prec, child_assoc)
296 | `IdRef xref -> new_xref := Some xref
297 | `Href hrefs -> new_uris := hrefs
298 | `XmlAttrs attrs -> new_xmlattrs := attrs);
301 (match !new_level with
302 | None -> aux !new_xmlattrs mathonly new_xref pos prec new_uris t
303 | Some (child_prec, child_assoc) ->
305 aux !new_xmlattrs mathonly new_xref pos child_prec new_uris t
307 add_parens child_prec child_assoc pos prec t')
312 | `Raw _ -> aux xmlattrs mathonly xref pos prec uris t
313 | `Level (child_prec, child_assoc) ->
314 let t' = aux xmlattrs mathonly xref pos child_prec uris t in
315 add_parens child_prec child_assoc pos prec t'
316 | `IdRef xref -> aux xmlattrs mathonly (Some xref) pos prec uris t
317 | `Href uris' -> aux xmlattrs mathonly xref pos prec uris' t
318 | `XmlAttrs xmlattrs -> aux xmlattrs mathonly xref pos prec uris t *)
319 and aux_literal xmlattrs xref prec uris l =
320 let attrs = make_href xmlattrs xref uris in
322 | `Symbol s -> Mpres.Mo (attrs, to_unicode s)
323 | `Keyword s -> Mpres.Mo (attrs, to_unicode s)
324 | `Number s -> Mpres.Mn (attrs, to_unicode s))
325 and aux_layout mathonly xref pos prec uris l =
326 let attrs = make_xref xref in
327 let invoke' t = aux [] true (ref None) pos prec uris t in
329 | A.Sub (t1, t2) -> Mpres.Msub (attrs, invoke' t1, invoke' t2)
330 | A.Sup (t1, t2) -> Mpres.Msup (attrs, invoke' t1, invoke' t2)
331 | A.Below (t1, t2) -> Mpres.Munder (attrs, invoke' t1, invoke' t2)
332 | A.Above (t1, t2) -> Mpres.Mover (attrs, invoke' t1, invoke' t2)
334 | A.Over (t1, t2) -> Mpres.Mfrac (attrs, invoke' t1, invoke' t2)
336 Mpres.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2)
337 | A.Sqrt t -> Mpres.Msqrt (attrs, invoke' t)
338 | A.Root (t1, t2) -> Mpres.Mroot (attrs, invoke' t1, invoke' t2)
339 | A.Box ((_, spacing, _) as kind, terms) ->
341 aux_children mathonly spacing xref pos prec uris
342 (CicNotationUtil.ungroup terms)
344 box_of mathonly kind attrs children
347 aux_children mathonly false xref pos prec uris
348 (CicNotationUtil.ungroup terms)
350 box_of mathonly (A.H, false, false) attrs children
351 | A.Break -> assert false (* TODO? *)
352 and aux_children mathonly spacing xref pos prec uris terms =
354 let rec aux_list first clusters acc =
356 [] when acc = [] -> List.rev clusters
357 | [] -> aux_list first (List.rev acc :: clusters) [] []
358 | (A.Layout A.Break) :: tl when acc = [] ->
359 aux_list first clusters [] tl
360 | (A.Layout A.Break) :: tl ->
361 aux_list first (List.rev acc :: clusters) [] tl
373 aux_list false clusters
374 (aux [] mathonly xref pos' prec uris hd :: acc) []
377 match pos, first with
379 | `None, false -> `Inner
380 | `Left, true -> `Left
381 | `Left, false -> `Inner
382 | `Right, _ -> `Inner
383 | `Inner, _ -> `Inner
385 aux_list false clusters
386 (aux [] mathonly xref pos' prec uris hd :: acc) tl
393 | tl -> box_of mathonly (A.H, spacing, false) [] tl
395 List.map boxify_pres (find_clusters terms)
397 aux [] false (ref None) `None 0 (ref [])
399 let rec print_box (t: boxml_markup) =
400 Box.box2xml print_mpres t
401 and print_mpres (t: mathml_markup) =
402 Mpresentation.print_mpres print_box t
404 let print_xml = print_mpres
406 (* let render_to_boxml id_to_uri t =
407 let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
408 Xml.add_xml_declaration xml_stream *)