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
42 | None -> make_attributes (List.tl l1) tl
44 let p,n = List.hd l1 in
45 (p,n,s) :: make_attributes (List.tl l1) tl)
49 | Mpresentation.Mobject (attrs, box) ->
52 | mpres -> Box.Object ([], mpres)
56 | Box.Object (attrs, mpres) ->
59 | box -> Mpresentation.Mobject ([], box)
61 let rec genuine_math =
63 | Mpresentation.Mobject ([], obj) -> not (genuine_box obj)
67 | Box.Object ([], mpres) -> not (genuine_math mpres)
70 let rec eligible_math =
72 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> eligible_math mpres
73 | Mpresentation.Mobject ([], _) -> false
76 let rec promote_to_math =
78 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> promote_to_math mpres
82 Mpresentation.Mspace (RenderingAttrs.small_skip_attributes `MathML)
84 let rec add_mpres_attributes new_attr = function
85 | Mpresentation.Mobject (attr, box) ->
86 Mpresentation.Mobject (attr, add_box_attributes new_attr box)
88 Mpresentation.set_attr (new_attr @ Mpresentation.get_attr mpres) mpres
89 and add_box_attributes new_attr = function
90 | Box.Object (attr, mpres) ->
91 Box.Object (attr, add_mpres_attributes new_attr mpres)
92 | box -> Box.set_attr (new_attr @ Box.get_attr box) box
94 let box_of mathonly spec attrs children =
96 | [t] -> add_mpres_attributes attrs t
98 let kind, spacing, indent = spec in
101 CicNotationUtil.dress small_skip children
105 if mathonly then Mpresentation.Mrow (attrs, dress children)
108 (if spacing then RenderingAttrs.spacing_attributes `BoxML else [])
109 @ (if indent then RenderingAttrs.indent_attributes `BoxML else [])
114 if List.for_all eligible_math children then
115 Mpresentation.Mrow (attrs',
116 dress (List.map promote_to_math children))
118 mpres_of_box (Box.H (attrs',
119 List.map box_of_mpres children))
120 (* | Ast.H when List.for_all genuine_math children ->
121 Mpresentation.Mrow (attrs', dress children) *)
123 mpres_of_box (Box.V (attrs',
124 List.map box_of_mpres children))
126 mpres_of_box (Box.HV (attrs',
127 List.map box_of_mpres children))
129 mpres_of_box (Box.HOV (attrs',
130 List.map box_of_mpres children))
132 let open_paren = Mpresentation.Mo ([], "(")
133 let closed_paren = Mpresentation.Mo ([], ")")
134 let open_brace = Mpresentation.Mo ([], "{")
135 let closed_brace = Mpresentation.Mo ([], "}")
136 let hidden_substs = Mpresentation.Mtext ([], "{...}")
137 let open_box_paren = Box.Text ([], "(")
138 let closed_box_paren = Box.Text ([], ")")
139 let semicolon = Mpresentation.Mo ([], ";")
140 let toggle_action children =
141 Mpresentation.Maction ([None, "actiontype", "toggle"], children)
143 type child_pos = [ `None | `Left | `Right | `Inner ]
147 | Gramext.LeftA -> "LeftA"
148 | Gramext.RightA -> "RightA"
149 | Gramext.NonA -> "NonA"
152 let rec aux_mpres = function
158 | Mpres.Mspace _ -> true
159 | Mpres.Mobject (_, box) -> aux_box box
160 | Mpres.Maction (_, [mpres])
161 | Mpres.Mrow (_, [mpres]) -> aux_mpres mpres
163 and aux_box = function
167 | Box.Object (_, mpres) -> aux_mpres mpres
172 | Box.Action (_, [box]) -> aux_box box
177 let add_parens child_prec child_assoc child_pos curr_prec t =
178 if is_atomic t then t
179 else if child_prec < curr_prec
180 || (child_prec = curr_prec &&
181 child_assoc = Gramext.LeftA &&
183 || (child_prec = curr_prec &&
184 child_assoc = Gramext.RightA &&
186 then (* parens should be added *)
188 | Mpresentation.Mobject (_, box) ->
189 mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
190 | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
194 let render ids_to_uris =
195 let module A = Ast in
196 let module P = Mpresentation in
197 let use_unicode = true in
200 let uri = Hashtbl.find ids_to_uris id in
201 Some (UriManager.string_of_uri uri)
202 with Not_found -> None)
204 let make_href xmlattrs xref =
208 match lookup_uri xref with
210 | Some uri -> uri :: uris)
213 let xmlattrs_uris, xmlattrs =
214 let xref_attrs, other_attrs =
216 (function Some "xlink", "href", _ -> true | _ -> false)
219 List.map (fun (_, _, uri) -> uri) xref_attrs,
223 match xmlattrs_uris @ xref_uris with
226 Some (String.concat " "
227 (HExtlib.list_uniq (List.sort String.compare uris)))
230 match !xref with [] -> None | xrefs -> Some (String.concat " " xrefs)
234 @ make_attributes [Some "helm", "xref"; Some "xlink", "href"]
239 match !xref with [] -> None | xrefs -> Some (String.concat " " xrefs)
242 make_attributes [Some "helm","xref"] [xrefs]
244 (* when mathonly is true no boxes should be generated, only mrows *)
246 let rec aux xmlattrs mathonly xref pos prec t =
248 | A.AttributedTerm _ ->
249 aux_attributes xmlattrs mathonly xref pos prec t
250 | A.Num (literal, _) ->
252 (RenderingAttrs.number_attributes `MathML)
253 @ make_href xmlattrs xref
255 Mpres.Mn (attrs, literal)
256 | A.Symbol (literal, _) ->
258 (RenderingAttrs.symbol_attributes `MathML)
259 @ make_href xmlattrs xref
261 Mpres.Mo (attrs, to_unicode literal)
262 | A.Ident (literal, subst)
263 | A.Uri (literal, subst) ->
265 (RenderingAttrs.ident_attributes `MathML)
266 @ make_href xmlattrs xref
268 let name = Mpres.Mi (attrs, to_unicode literal) in
274 box_of mathonly (A.H, false, false) []
276 :: (CicNotationUtil.dress semicolon
279 box_of mathonly (A.H, false, false) [] [
281 Mpres.Mo ([], to_unicode "\\def");
282 aux [] mathonly xref pos prec t ])
286 let substs_maction = toggle_action [ hidden_substs; substs' ] in
287 box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
288 | A.Literal l -> aux_literal xmlattrs xref prec l
289 | A.UserInput -> Mpres.Mtext ([], "%")
290 | A.Layout l -> aux_layout mathonly xref pos prec l
292 | A.Variable _ -> assert false (* should have been instantiated *)
294 prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
296 and aux_attributes xmlattrs mathonly xref pos prec t =
297 let new_level = ref None in
298 let new_xref = ref [] in
299 let new_xmlattrs = ref [] in
300 let new_pos = ref pos in
301 let rec aux_attribute =
303 | A.AttributedTerm (attr, t) ->
307 | `Level (child_prec, child_assoc) ->
308 new_level := Some (child_prec, child_assoc)
309 | `IdRef xref -> new_xref := xref :: !new_xref
310 | `ChildPos pos -> new_pos := pos
311 | `XmlAttrs attrs -> new_xmlattrs := attrs @ !new_xmlattrs);
314 (match !new_level with
315 | None -> aux !new_xmlattrs mathonly new_xref !new_pos prec t
316 | Some (child_prec, child_assoc) ->
318 aux !new_xmlattrs mathonly new_xref !new_pos child_prec t
320 add_parens child_prec child_assoc !new_pos prec t')
323 and aux_literal xmlattrs xref prec l =
324 let attrs = make_href xmlattrs xref in
326 | `Symbol s -> Mpres.Mo (attrs, to_unicode s)
327 | `Keyword s -> Mpres.Mo (attrs, to_unicode s)
328 | `Number s -> Mpres.Mn (attrs, to_unicode s))
329 and aux_layout mathonly xref pos prec l =
330 let attrs = make_xref xref in
331 let invoke' t = aux [] true (ref []) pos prec t in
332 (* use the one below to reset precedence and associativity *)
333 let invoke_reinit t = aux [] true (ref []) `None 0 t in
335 | A.Sub (t1, t2) -> Mpres.Msub (attrs, invoke' t1, invoke_reinit t2)
336 | A.Sup (t1, t2) -> Mpres.Msup (attrs, invoke' t1, invoke_reinit t2)
337 | A.Below (t1, t2) -> Mpres.Munder (attrs, invoke' t1, invoke_reinit t2)
338 | A.Above (t1, t2) -> Mpres.Mover (attrs, invoke' t1, invoke_reinit t2)
341 Mpres.Mfrac (attrs, invoke_reinit t1, invoke_reinit t2)
343 Mpres.Mfrac (atop_attributes @ attrs, invoke_reinit t1,
345 | A.Sqrt t -> Mpres.Msqrt (attrs, invoke_reinit t)
347 Mpres.Mroot (attrs, invoke_reinit t1, invoke_reinit t2)
348 | A.Box ((_, spacing, _) as kind, terms) ->
350 aux_children mathonly spacing xref pos prec
351 (CicNotationUtil.ungroup terms)
353 box_of mathonly kind attrs children
356 aux_children mathonly false xref pos prec
357 (CicNotationUtil.ungroup terms)
359 box_of mathonly (A.H, false, false) attrs children
360 | A.Break -> assert false (* TODO? *)
361 and aux_children mathonly spacing xref pos prec terms =
363 let rec aux_list first clusters acc =
365 [] when acc = [] -> List.rev clusters
366 | [] -> aux_list first (List.rev acc :: clusters) [] []
367 | (A.Layout A.Break) :: tl when acc = [] ->
368 aux_list first clusters [] tl
369 | (A.Layout A.Break) :: tl ->
370 aux_list first (List.rev acc :: clusters) [] tl
382 aux_list false clusters
383 (aux [] mathonly xref pos prec hd :: acc) []
386 match pos, first with
388 | `None, false -> `Inner
389 | `Left, true -> `Left
390 | `Left, false -> `Inner
391 | `Right, _ -> `Inner
392 | `Inner, _ -> `Inner
394 aux_list false clusters
395 (aux [] mathonly xref pos prec hd :: acc) tl
402 | tl -> box_of mathonly (A.H, spacing, false) [] tl
404 List.map boxify_pres (find_clusters terms)
406 aux [] false (ref []) `None 0
408 let rec print_box (t: boxml_markup) =
409 Box.box2xml print_mpres t
410 and print_mpres (t: mathml_markup) =
411 Mpresentation.print_mpres print_box t
413 let print_xml = print_mpres
415 (* let render_to_boxml id_to_uri t =
416 let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
417 Xml.add_xml_declaration xml_stream *)