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 (p,n,s) :: make_attributes (List.tl l1) tl
46 Mpresentation.Mobject (_, box) -> box
47 | mpres -> Box.Object ([], mpres)
51 Box.Object (_, mpres) -> mpres
52 | box -> Mpresentation.Mobject ([], box)
54 let rec genuine_math =
56 | Mpresentation.Mobject ([], obj) -> not (genuine_box obj)
60 | Box.Object ([], mpres) -> not (genuine_math mpres)
63 let rec eligible_math =
65 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> eligible_math mpres
66 | Mpresentation.Mobject ([], _) -> false
69 let rec promote_to_math =
71 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> promote_to_math mpres
75 Mpresentation.Mspace (RenderingAttrs.small_skip_attributes `MathML)
77 let box_of mathonly spec attrs children =
81 let kind, spacing, indent = spec in
84 CicNotationUtil.dress small_skip children
88 if mathonly then Mpresentation.Mrow (attrs, dress children)
91 (if spacing then RenderingAttrs.spacing_attributes `BoxML else [])
92 @ (if indent then RenderingAttrs.indent_attributes `BoxML else [])
97 if List.for_all eligible_math children then
98 Mpresentation.Mrow (attrs',
99 dress (List.map promote_to_math children))
101 mpres_of_box (Box.H (attrs',
102 List.map box_of_mpres children))
103 (* | CicNotationPt.H when List.for_all genuine_math children ->
104 Mpresentation.Mrow (attrs', dress children) *)
106 mpres_of_box (Box.V (attrs',
107 List.map box_of_mpres children))
108 | CicNotationPt.HV ->
109 mpres_of_box (Box.HV (attrs',
110 List.map box_of_mpres children))
111 | CicNotationPt.HOV ->
112 mpres_of_box (Box.HOV (attrs',
113 List.map box_of_mpres children))
115 let open_paren = Mpresentation.Mo ([], "(")
116 let closed_paren = Mpresentation.Mo ([], ")")
117 let open_brace = Mpresentation.Mo ([], "{")
118 let closed_brace = Mpresentation.Mo ([], "}")
119 let hidden_substs = Mpresentation.Mtext ([], "{...}")
120 let open_box_paren = Box.Text ([], "(")
121 let closed_box_paren = Box.Text ([], ")")
122 let semicolon = Mpresentation.Mo ([], ";")
123 let toggle_action children =
124 Mpresentation.Maction ([None, "actiontype", "toggle"], children)
126 type child_pos = [ `None | `Left | `Right | `Inner ]
130 | Gramext.LeftA -> "LeftA"
131 | Gramext.RightA -> "RightA"
132 | Gramext.NonA -> "NonA"
142 let rec aux_mpres = function
149 | P.Mobject (_, box) -> aux_box box
150 | P.Maction (_, [mpres])
151 | P.Mrow (_, [mpres]) -> aux_mpres mpres
153 and aux_box = function
157 | Box.Object (_, mpres) -> aux_mpres mpres
162 | Box.Action (_, [box]) -> aux_box box
167 let add_parens child_prec child_assoc child_pos curr_prec t =
168 if is_atomic t then t
169 else if child_prec < curr_prec
170 || (child_prec = curr_prec &&
171 child_assoc = Gramext.LeftA &&
173 || (child_prec = curr_prec &&
174 child_assoc = Gramext.RightA &&
176 then (* parens should be added *)
178 | Mpresentation.Mobject (_, box) ->
179 mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
180 | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
184 let render ids_to_uris =
185 let module A = CicNotationPt in
186 let module P = Mpresentation in
187 let use_unicode = true in
188 let lookup_uri = function
190 | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
192 let make_href xmlattrs xref uris =
193 let xref_uri = lookup_uri xref in
194 let raw_uris = List.map UriManager.string_of_uri uris in
196 match xref_uri, raw_uris with
198 | Some uri, [] -> Some uri
199 | None, raw_uris -> Some (String.concat " " raw_uris)
200 | Some uri, raw_uris -> Some (String.concat " " (uri :: raw_uris))
203 @ make_attributes [Some "helm", "xref"; Some "xlink", "href"] [xref; uri]
205 let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
206 (* when mathonly is true no boxes should be generated, only mrows *)
207 let rec aux xmlattrs mathonly xref pos prec uris t =
209 | A.AttributedTerm _ ->
210 aux_attributes xmlattrs mathonly xref pos prec uris t
211 | A.Num (literal, _) ->
213 (RenderingAttrs.number_attributes `MathML)
214 @ make_href xmlattrs xref uris
216 P.Mn (attrs, literal)
217 | A.Symbol (literal, _) ->
219 (RenderingAttrs.symbol_attributes `MathML)
220 @ make_href xmlattrs xref uris
222 P.Mo (attrs, to_unicode literal)
223 | A.Ident (literal, subst)
224 | A.Uri (literal, subst) ->
226 (RenderingAttrs.ident_attributes `MathML)
227 @ make_href xmlattrs xref []
229 let name = P.Mi (attrs, to_unicode literal) in
235 box_of mathonly (A.H, false, false) []
237 :: (CicNotationUtil.dress semicolon
240 box_of mathonly (A.H, false, false) [] [
242 P.Mo ([], to_unicode "\\def");
243 aux [] mathonly xref pos prec uris t ])
246 (* (CicNotationUtil.dress semicolon
249 let var_uri = UriManager.uri_of_string var in
250 let var_name = UriManager.name_of_uri var_uri in
251 let href_attr = Some "xlink", "href", var in
252 box_of mathonly (A.H, false, false) [] [
253 P.Mi ([href_attr], var_name);
254 P.Mo ([], to_unicode "\\def");
255 aux [] mathonly xref pos prec uris t ])
258 let substs_maction = toggle_action [ hidden_substs; substs' ] in
259 box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
260 | A.Literal l -> aux_literal xmlattrs xref prec uris l
261 | A.UserInput -> P.Mtext ([], "%")
262 | A.Layout l -> aux_layout mathonly xref pos prec uris l
264 | A.Variable _ -> assert false (* should have been instantiated *)
266 prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
268 and aux_attributes xmlattrs mathonly xref pos prec uris t =
269 let new_level = ref None in
270 let new_xref = ref None in
271 let new_uris = ref [] in
272 let new_xmlattrs = ref [] in
273 let rec aux_attribute =
275 | A.AttributedTerm (attr, t) ->
279 | `Level (child_prec, child_assoc) ->
280 new_level := Some (child_prec, child_assoc)
281 | `IdRef xref -> new_xref := Some xref
282 | `Href hrefs -> new_uris := hrefs
283 | `XmlAttrs attrs -> new_xmlattrs := attrs);
286 (match !new_level with
287 | None -> aux !new_xmlattrs mathonly !new_xref pos prec !new_uris t
288 | Some (child_prec, child_assoc) ->
290 aux !new_xmlattrs mathonly !new_xref pos child_prec !new_uris t
292 add_parens child_prec child_assoc pos prec t')
297 | `Raw _ -> aux xmlattrs mathonly xref pos prec uris t
298 | `Level (child_prec, child_assoc) ->
299 let t' = aux xmlattrs mathonly xref pos child_prec uris t in
300 add_parens child_prec child_assoc pos prec t'
301 | `IdRef xref -> aux xmlattrs mathonly (Some xref) pos prec uris t
302 | `Href uris' -> aux xmlattrs mathonly xref pos prec uris' t
303 | `XmlAttrs xmlattrs -> aux xmlattrs mathonly xref pos prec uris t *)
304 and aux_literal xmlattrs xref prec uris l =
305 let attrs = make_href xmlattrs xref uris in
307 | `Symbol s -> P.Mo (attrs, to_unicode s)
308 | `Keyword s -> P.Mo (attrs, to_unicode s)
309 | `Number s -> P.Mn (attrs, to_unicode s))
310 and aux_layout mathonly xref pos prec uris l =
311 let attrs = make_xref xref in
312 let invoke' t = aux [] true None pos prec uris t in
314 | A.Sub (t1, t2) -> P.Msub (attrs, invoke' t1, invoke' t2)
315 | A.Sup (t1, t2) -> P.Msup (attrs, invoke' t1, invoke' t2)
316 | A.Below (t1, t2) -> P.Munder (attrs, invoke' t1, invoke' t2)
317 | A.Above (t1, t2) -> P.Mover (attrs, invoke' t1, invoke' t2)
319 | A.Over (t1, t2) -> P.Mfrac (attrs, invoke' t1, invoke' t2)
321 P.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2)
322 | A.Sqrt t -> P.Msqrt (attrs, invoke' t)
323 | A.Root (t1, t2) -> P.Mroot (attrs, invoke' t1, invoke' t2)
324 | A.Box ((_, spacing, _) as kind, terms) ->
326 aux_children mathonly spacing xref pos prec uris
327 (CicNotationUtil.ungroup terms)
329 box_of mathonly kind attrs children
332 aux_children mathonly false xref pos prec uris
333 (CicNotationUtil.ungroup terms)
335 box_of mathonly (A.H, false, false) attrs children
336 | A.Break -> assert false (* TODO? *)
337 and aux_children mathonly spacing xref pos prec uris terms =
339 let rec aux_list first clusters acc =
341 [] when acc = [] -> List.rev clusters
342 | [] -> aux_list first (List.rev acc :: clusters) [] []
343 | (A.Layout A.Break) :: tl when acc = [] ->
344 aux_list first clusters [] tl
345 | (A.Layout A.Break) :: tl ->
346 aux_list first (List.rev acc :: clusters) [] tl
358 aux_list false clusters
359 (aux [] mathonly xref pos' prec uris hd :: acc) []
362 match pos, first with
364 | `None, false -> `Inner
365 | `Left, true -> `Left
366 | `Left, false -> `Inner
367 | `Right, _ -> `Inner
368 | `Inner, _ -> `Inner
370 aux_list false clusters
371 (aux [] mathonly xref pos' prec uris hd :: acc) tl
378 | tl -> box_of mathonly (A.H, spacing, false) [] tl
380 List.map boxify_pres (find_clusters terms)
382 aux [] false None `None 0 []
384 let rec print_box (t: CicNotationPres.boxml_markup) =
385 Box.box2xml print_mpres t
386 and print_mpres (t: CicNotationPres.mathml_markup) =
387 Mpresentation.print_mpres print_box t
389 let print_xml = print_mpres
391 (* let render_to_boxml id_to_uri t =
392 let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
393 Xml.add_xml_declaration xml_stream *)