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
46 (p,n,s) :: make_attributes (List.tl l1) tl)
50 | Mpresentation.Mobject (attrs, box) ->
53 | mpres -> Box.Object ([], mpres)
57 | Box.Object (attrs, mpres) ->
60 | box -> Mpresentation.Mobject ([], box)
62 let rec genuine_math =
64 | Mpresentation.Mobject ([], obj) -> not (genuine_box obj)
68 | Box.Object ([], mpres) -> not (genuine_math mpres)
71 let rec eligible_math =
73 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> eligible_math mpres
74 | Mpresentation.Mobject ([], _) -> false
77 let rec promote_to_math =
79 | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> promote_to_math mpres
83 Mpresentation.Mspace (RenderingAttrs.small_skip_attributes `MathML)
85 let rec add_mpres_attributes new_attr = function
86 | Mpresentation.Mobject (attr, box) ->
87 Mpresentation.Mobject (attr, add_box_attributes new_attr box)
89 Mpresentation.set_attr (new_attr @ Mpresentation.get_attr mpres) mpres
90 and add_box_attributes new_attr = function
91 | Box.Object (attr, mpres) ->
92 Box.Object (attr, add_mpres_attributes new_attr mpres)
93 | box -> Box.set_attr (new_attr @ Box.get_attr box) box
95 let box_of mathonly spec attrs children =
97 | [t] -> add_mpres_attributes attrs t
99 let kind, spacing, indent = spec in
102 CicNotationUtil.dress small_skip children
106 if mathonly then Mpresentation.Mrow (attrs, dress children)
109 (if spacing then RenderingAttrs.spacing_attributes `BoxML else [])
110 @ (if indent then RenderingAttrs.indent_attributes `BoxML else [])
115 if List.for_all eligible_math children then
116 Mpresentation.Mrow (attrs',
117 dress (List.map promote_to_math children))
119 mpres_of_box (Box.H (attrs',
120 List.map box_of_mpres children))
121 (* | Ast.H when List.for_all genuine_math children ->
122 Mpresentation.Mrow (attrs', dress children) *)
124 mpres_of_box (Box.V (attrs',
125 List.map box_of_mpres children))
127 mpres_of_box (Box.HV (attrs',
128 List.map box_of_mpres children))
130 mpres_of_box (Box.HOV (attrs',
131 List.map box_of_mpres children))
133 let open_paren = Mpresentation.Mo ([], "(")
134 let closed_paren = Mpresentation.Mo ([], ")")
135 let open_brace = Mpresentation.Mo ([], "{")
136 let closed_brace = Mpresentation.Mo ([], "}")
137 let hidden_substs = Mpresentation.Mtext ([], "{...}")
138 let open_box_paren = Box.Text ([], "(")
139 let closed_box_paren = Box.Text ([], ")")
140 let semicolon = Mpresentation.Mo ([], ";")
141 let toggle_action children =
142 Mpresentation.Maction ([None, "actiontype", "toggle"], children)
144 type child_pos = [ `None | `Left | `Right | `Inner ]
148 | Gramext.LeftA -> "LeftA"
149 | Gramext.RightA -> "RightA"
150 | Gramext.NonA -> "NonA"
160 let rec aux_mpres = function
166 | Mpres.Mspace _ -> true
167 | Mpres.Mobject (_, box) -> aux_box box
168 | Mpres.Maction (_, [mpres])
169 | Mpres.Mrow (_, [mpres]) -> aux_mpres mpres
171 and aux_box = function
175 | Box.Object (_, mpres) -> aux_mpres mpres
180 | Box.Action (_, [box]) -> aux_box box
185 let add_parens child_prec child_assoc child_pos curr_prec t =
186 if is_atomic t then t
187 else if child_prec < curr_prec
188 || (child_prec = curr_prec &&
189 child_assoc = Gramext.LeftA &&
191 || (child_prec = curr_prec &&
192 child_assoc = Gramext.RightA &&
194 then (* parens should be added *)
196 | Mpresentation.Mobject (_, box) ->
197 mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
198 | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
202 let render ids_to_uris =
203 let module A = Ast in
204 let module P = Mpresentation in
205 let use_unicode = true in
206 let lookup_uri = function
208 | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
210 let make_href xmlattrs xref uris =
211 let xref_uri = lookup_uri !xref in
212 let raw_uris = List.map UriManager.string_of_uri !uris in
214 match xref_uri, raw_uris with
216 | Some uri, [] -> Some uri
217 | None, raw_uris -> Some (String.concat " " raw_uris)
218 | Some uri, raw_uris -> Some (String.concat " " (uri :: raw_uris))
222 @ make_attributes [Some "helm", "xref"; Some "xlink", "href"]
225 let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
226 (* when mathonly is true no boxes should be generated, only mrows *)
228 let rec aux xmlattrs mathonly xref pos prec uris t =
230 | A.AttributedTerm _ ->
231 aux_attributes xmlattrs mathonly xref pos prec uris t
232 | A.Num (literal, _) ->
234 (RenderingAttrs.number_attributes `MathML)
235 @ make_href xmlattrs xref uris
237 Mpres.Mn (attrs, literal)
238 | A.Symbol (literal, _) ->
240 (RenderingAttrs.symbol_attributes `MathML)
241 @ make_href xmlattrs xref uris
243 Mpres.Mo (attrs, to_unicode literal)
244 | A.Ident (literal, subst)
245 | A.Uri (literal, subst) ->
247 (RenderingAttrs.ident_attributes `MathML)
248 @ make_href xmlattrs xref (ref [])
250 let name = Mpres.Mi (attrs, to_unicode literal) in
256 box_of mathonly (A.H, false, false) []
258 :: (CicNotationUtil.dress semicolon
261 box_of mathonly (A.H, false, false) [] [
263 Mpres.Mo ([], to_unicode "\\def");
264 aux [] mathonly xref pos prec uris t ])
267 (* (CicNotationUtil.dress semicolon
270 let var_uri = UriManager.uri_of_string var in
271 let var_name = UriManager.name_of_uri var_uri in
272 let href_attr = Some "xlink", "href", var in
273 box_of mathonly (A.H, false, false) [] [
274 Mpres.Mi ([href_attr], var_name);
275 Mpres.Mo ([], to_unicode "\\def");
276 aux [] mathonly xref pos prec uris t ])
279 let substs_maction = toggle_action [ hidden_substs; substs' ] in
280 box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
281 | A.Literal l -> aux_literal xmlattrs xref prec uris l
282 | A.UserInput -> Mpres.Mtext ([], "%")
283 | A.Layout l -> aux_layout mathonly xref pos prec uris l
285 | A.Variable _ -> assert false (* should have been instantiated *)
287 prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
289 and aux_attributes xmlattrs mathonly xref pos prec uris t =
290 let new_level = ref None in
291 let new_xref = ref None in
292 let new_uris = ref [] in
293 let new_xmlattrs = ref [] in
294 let rec aux_attribute =
296 | A.AttributedTerm (attr, t) ->
300 | `Level (child_prec, child_assoc) ->
301 new_level := Some (child_prec, child_assoc)
302 | `IdRef xref -> new_xref := Some xref
303 | `Href hrefs -> new_uris := hrefs
304 | `XmlAttrs attrs -> new_xmlattrs := attrs);
307 (match !new_level with
308 | None -> aux !new_xmlattrs mathonly new_xref pos prec new_uris t
309 | Some (child_prec, child_assoc) ->
311 aux !new_xmlattrs mathonly new_xref pos child_prec new_uris t
313 add_parens child_prec child_assoc pos prec t')
318 | `Raw _ -> aux xmlattrs mathonly xref pos prec uris t
319 | `Level (child_prec, child_assoc) ->
320 let t' = aux xmlattrs mathonly xref pos child_prec uris t in
321 add_parens child_prec child_assoc pos prec t'
322 | `IdRef xref -> aux xmlattrs mathonly (Some xref) pos prec uris t
323 | `Href uris' -> aux xmlattrs mathonly xref pos prec uris' t
324 | `XmlAttrs xmlattrs -> aux xmlattrs mathonly xref pos prec uris t *)
325 and aux_literal xmlattrs xref prec uris l =
326 let attrs = make_href xmlattrs xref uris in
328 | `Symbol s -> Mpres.Mo (attrs, to_unicode s)
329 | `Keyword s -> Mpres.Mo (attrs, to_unicode s)
330 | `Number s -> Mpres.Mn (attrs, to_unicode s))
331 and aux_layout mathonly xref pos prec uris l =
332 let attrs = make_xref xref in
333 let invoke' t = aux [] true (ref None) pos prec uris t in
335 | A.Sub (t1, t2) -> Mpres.Msub (attrs, invoke' t1, invoke' t2)
336 | A.Sup (t1, t2) -> Mpres.Msup (attrs, invoke' t1, invoke' t2)
337 | A.Below (t1, t2) -> Mpres.Munder (attrs, invoke' t1, invoke' t2)
338 | A.Above (t1, t2) -> Mpres.Mover (attrs, invoke' t1, invoke' t2)
340 | A.Over (t1, t2) -> Mpres.Mfrac (attrs, invoke' t1, invoke' t2)
342 Mpres.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2)
343 | A.Sqrt t -> Mpres.Msqrt (attrs, invoke' t)
344 | A.Root (t1, t2) -> Mpres.Mroot (attrs, invoke' t1, invoke' t2)
345 | A.Box ((_, spacing, _) as kind, terms) ->
347 aux_children mathonly spacing xref pos prec uris
348 (CicNotationUtil.ungroup terms)
350 box_of mathonly kind attrs children
353 aux_children mathonly false xref pos prec uris
354 (CicNotationUtil.ungroup terms)
356 box_of mathonly (A.H, false, false) attrs children
357 | A.Break -> assert false (* TODO? *)
358 and aux_children mathonly spacing xref pos prec uris terms =
360 let rec aux_list first clusters acc =
362 [] when acc = [] -> List.rev clusters
363 | [] -> aux_list first (List.rev acc :: clusters) [] []
364 | (A.Layout A.Break) :: tl when acc = [] ->
365 aux_list first clusters [] tl
366 | (A.Layout A.Break) :: tl ->
367 aux_list first (List.rev acc :: clusters) [] tl
379 aux_list false clusters
380 (aux [] mathonly xref pos' prec uris hd :: acc) []
383 match pos, first with
385 | `None, false -> `Inner
386 | `Left, true -> `Left
387 | `Left, false -> `Inner
388 | `Right, _ -> `Inner
389 | `Inner, _ -> `Inner
391 aux_list false clusters
392 (aux [] mathonly xref pos' prec uris hd :: acc) tl
399 | tl -> box_of mathonly (A.H, spacing, false) [] tl
401 List.map boxify_pres (find_clusters terms)
403 aux [] false (ref None) `None 0 (ref [])
405 let rec print_box (t: boxml_markup) =
406 Box.box2xml print_mpres t
407 and print_mpres (t: mathml_markup) =
408 Mpresentation.print_mpres print_box t
410 let print_xml = print_mpres
412 (* let render_to_boxml id_to_uri t =
413 let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
414 Xml.add_xml_declaration xml_stream *)