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 P = Mpresentation
28 type mathml_markup = boxml_markup Mpresentation.mpres
29 and boxml_markup = mathml_markup Box.box
31 type markup = mathml_markup
33 let atop_attributes = [None, "linethickness", "0pt"]
35 let to_unicode = Utf8Macro.unicode_of_tex
37 (* let rec make_attributes l1 = function
39 | None :: tl -> make_attributes (List.tl l1) tl
41 let p,n = List.hd l1 in
42 prerr_endline (Printf.sprintf "make_attributes %s %s" n s);
43 (p,n,s) :: make_attributes (List.tl l1) tl *)
44 let rec make_attributes l1 = function
48 | None -> make_attributes (List.tl l1) tl
50 let p,n = List.hd l1 in
51 prerr_endline (Printf.sprintf "make_attributes %s %s" n s);
53 (p,n,s) :: make_attributes (List.tl l1) tl)
57 Mpresentation.Mobject (_, box) -> box
58 | mpres -> Box.Object ([], mpres)
62 Box.Object (_, mpres) -> mpres
63 | box -> Mpresentation.Mobject ([], box)
65 let rec genuine_math =
67 | Mpresentation.Mobject ([], obj) -> not (genuine_box obj)
71 | Box.Object ([], mpres) -> not (genuine_math mpres)
74 let rec eligible_math =
76 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> eligible_math mpres
77 | Mpresentation.Mobject ([], _) -> false
80 let rec promote_to_math =
82 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> promote_to_math mpres
86 Mpresentation.Mspace (RenderingAttrs.small_skip_attributes `MathML)
88 let box_of mathonly spec attrs children =
92 let kind, spacing, indent = spec in
95 CicNotationUtil.dress small_skip children
99 if mathonly then Mpresentation.Mrow (attrs, dress children)
102 (if spacing then RenderingAttrs.spacing_attributes `BoxML else [])
103 @ (if indent then RenderingAttrs.indent_attributes `BoxML else [])
108 if List.for_all eligible_math children then
109 Mpresentation.Mrow (attrs',
110 dress (List.map promote_to_math children))
112 mpres_of_box (Box.H (attrs',
113 List.map box_of_mpres children))
114 (* | CicNotationPt.H when List.for_all genuine_math children ->
115 Mpresentation.Mrow (attrs', dress children) *)
117 mpres_of_box (Box.V (attrs',
118 List.map box_of_mpres children))
119 | CicNotationPt.HV ->
120 mpres_of_box (Box.HV (attrs',
121 List.map box_of_mpres children))
122 | CicNotationPt.HOV ->
123 mpres_of_box (Box.HOV (attrs',
124 List.map box_of_mpres children))
126 let open_paren = Mpresentation.Mo ([], "(")
127 let closed_paren = Mpresentation.Mo ([], ")")
128 let open_brace = Mpresentation.Mo ([], "{")
129 let closed_brace = Mpresentation.Mo ([], "}")
130 let hidden_substs = Mpresentation.Mtext ([], "{...}")
131 let open_box_paren = Box.Text ([], "(")
132 let closed_box_paren = Box.Text ([], ")")
133 let semicolon = Mpresentation.Mo ([], ";")
134 let toggle_action children =
135 Mpresentation.Maction ([None, "actiontype", "toggle"], children)
137 type child_pos = [ `None | `Left | `Right | `Inner ]
141 | Gramext.LeftA -> "LeftA"
142 | Gramext.RightA -> "RightA"
143 | Gramext.NonA -> "NonA"
153 let rec aux_mpres = function
160 | P.Mobject (_, box) -> aux_box box
161 | P.Maction (_, [mpres])
162 | P.Mrow (_, [mpres]) -> aux_mpres mpres
164 and aux_box = function
168 | Box.Object (_, mpres) -> aux_mpres mpres
173 | Box.Action (_, [box]) -> aux_box box
178 let add_parens child_prec child_assoc child_pos curr_prec t =
179 if is_atomic t then t
180 else if child_prec < curr_prec
181 || (child_prec = curr_prec &&
182 child_assoc = Gramext.LeftA &&
184 || (child_prec = curr_prec &&
185 child_assoc = Gramext.RightA &&
187 then (* parens should be added *)
189 | Mpresentation.Mobject (_, box) ->
190 mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
191 | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
195 let render ids_to_uris =
196 let module A = CicNotationPt in
197 let module P = Mpresentation in
198 let use_unicode = true in
199 let lookup_uri = function
201 | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
203 let make_href xmlattrs xref uris =
204 let xref_uri = lookup_uri !xref in
205 let raw_uris = List.map UriManager.string_of_uri !uris in
207 match xref_uri, raw_uris with
209 | Some uri, [] -> Some uri
210 | None, raw_uris -> Some (String.concat " " raw_uris)
211 | Some uri, raw_uris -> Some (String.concat " " (uri :: raw_uris))
215 @ make_attributes [Some "helm", "xref"; Some "xlink", "href"]
218 let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
219 (* when mathonly is true no boxes should be generated, only mrows *)
221 let rec aux xmlattrs mathonly xref pos prec uris t =
223 | A.AttributedTerm _ ->
224 aux_attributes xmlattrs mathonly xref pos prec uris t
225 | A.Num (literal, _) ->
227 (RenderingAttrs.number_attributes `MathML)
228 @ make_href xmlattrs xref uris
230 P.Mn (attrs, literal)
231 | A.Symbol (literal, _) ->
233 (RenderingAttrs.symbol_attributes `MathML)
234 @ make_href xmlattrs xref uris
236 P.Mo (attrs, to_unicode literal)
237 | A.Ident (literal, subst)
238 | A.Uri (literal, subst) ->
240 (RenderingAttrs.ident_attributes `MathML)
241 @ make_href xmlattrs xref (ref [])
243 let name = P.Mi (attrs, to_unicode literal) in
249 box_of mathonly (A.H, false, false) []
251 :: (CicNotationUtil.dress semicolon
254 box_of mathonly (A.H, false, false) [] [
256 P.Mo ([], to_unicode "\\def");
257 aux [] mathonly xref pos prec uris t ])
260 (* (CicNotationUtil.dress semicolon
263 let var_uri = UriManager.uri_of_string var in
264 let var_name = UriManager.name_of_uri var_uri in
265 let href_attr = Some "xlink", "href", var in
266 box_of mathonly (A.H, false, false) [] [
267 P.Mi ([href_attr], var_name);
268 P.Mo ([], to_unicode "\\def");
269 aux [] mathonly xref pos prec uris t ])
272 let substs_maction = toggle_action [ hidden_substs; substs' ] in
273 box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
274 | A.Literal l -> aux_literal xmlattrs xref prec uris l
275 | A.UserInput -> P.Mtext ([], "%")
276 | A.Layout l -> aux_layout mathonly xref pos prec uris l
278 | A.Variable _ -> assert false (* should have been instantiated *)
280 prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
282 and aux_attributes xmlattrs mathonly xref pos prec uris t =
283 let new_level = ref None in
284 let new_xref = ref None in
285 let new_uris = ref [] in
286 let new_xmlattrs = ref [] in
287 let rec aux_attribute =
289 | A.AttributedTerm (attr, t) ->
293 | `Level (child_prec, child_assoc) ->
294 new_level := Some (child_prec, child_assoc)
295 | `IdRef xref -> new_xref := Some xref
296 | `Href hrefs -> new_uris := hrefs
297 | `XmlAttrs attrs -> new_xmlattrs := attrs);
300 (match !new_level with
301 | None -> aux !new_xmlattrs mathonly new_xref pos prec new_uris t
302 | Some (child_prec, child_assoc) ->
304 aux !new_xmlattrs mathonly new_xref pos child_prec new_uris t
306 add_parens child_prec child_assoc pos prec t')
311 | `Raw _ -> aux xmlattrs mathonly xref pos prec uris t
312 | `Level (child_prec, child_assoc) ->
313 let t' = aux xmlattrs mathonly xref pos child_prec uris t in
314 add_parens child_prec child_assoc pos prec t'
315 | `IdRef xref -> aux xmlattrs mathonly (Some xref) pos prec uris t
316 | `Href uris' -> aux xmlattrs mathonly xref pos prec uris' t
317 | `XmlAttrs xmlattrs -> aux xmlattrs mathonly xref pos prec uris t *)
318 and aux_literal xmlattrs xref prec uris l =
319 let attrs = make_href xmlattrs xref uris in
321 | `Symbol s -> P.Mo (attrs, to_unicode s)
322 | `Keyword s -> P.Mo (attrs, to_unicode s)
323 | `Number s -> P.Mn (attrs, to_unicode s))
324 and aux_layout mathonly xref pos prec uris l =
325 let attrs = make_xref xref in
326 let invoke' t = aux [] true (ref None) pos prec uris t in
328 | A.Sub (t1, t2) -> P.Msub (attrs, invoke' t1, invoke' t2)
329 | A.Sup (t1, t2) -> P.Msup (attrs, invoke' t1, invoke' t2)
330 | A.Below (t1, t2) -> P.Munder (attrs, invoke' t1, invoke' t2)
331 | A.Above (t1, t2) -> P.Mover (attrs, invoke' t1, invoke' t2)
333 | A.Over (t1, t2) -> P.Mfrac (attrs, invoke' t1, invoke' t2)
335 P.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2)
336 | A.Sqrt t -> P.Msqrt (attrs, invoke' t)
337 | A.Root (t1, t2) -> P.Mroot (attrs, invoke' t1, invoke' t2)
338 | A.Box ((_, spacing, _) as kind, terms) ->
340 aux_children mathonly spacing xref pos prec uris
341 (CicNotationUtil.ungroup terms)
343 box_of mathonly kind attrs children
346 aux_children mathonly false xref pos prec uris
347 (CicNotationUtil.ungroup terms)
349 box_of mathonly (A.H, false, false) attrs children
350 | A.Break -> assert false (* TODO? *)
351 and aux_children mathonly spacing xref pos prec uris terms =
353 let rec aux_list first clusters acc =
355 [] when acc = [] -> List.rev clusters
356 | [] -> aux_list first (List.rev acc :: clusters) [] []
357 | (A.Layout A.Break) :: tl when acc = [] ->
358 aux_list first clusters [] tl
359 | (A.Layout A.Break) :: tl ->
360 aux_list first (List.rev acc :: clusters) [] tl
372 aux_list false clusters
373 (aux [] mathonly xref pos' prec uris hd :: acc) []
376 match pos, first with
378 | `None, false -> `Inner
379 | `Left, true -> `Left
380 | `Left, false -> `Inner
381 | `Right, _ -> `Inner
382 | `Inner, _ -> `Inner
384 aux_list false clusters
385 (aux [] mathonly xref pos' prec uris hd :: acc) tl
392 | tl -> box_of mathonly (A.H, spacing, false) [] tl
394 List.map boxify_pres (find_clusters terms)
396 aux [] false (ref None) `None 0 (ref [])
398 let rec print_box (t: boxml_markup) =
399 Box.box2xml print_mpres t
400 and print_mpres (t: mathml_markup) =
401 Mpresentation.print_mpres print_box t
403 let print_xml = print_mpres
405 (* let render_to_boxml id_to_uri t =
406 let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
407 Xml.add_xml_declaration xml_stream *)