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/
30 module Ast = CicNotationPt
31 module Mpres = Mpresentation
33 type mathml_markup = boxml_markup Mpres.mpres
34 and boxml_markup = mathml_markup Box.box
36 type markup = mathml_markup
38 let atop_attributes = [None, "linethickness", "0pt"]
40 let to_unicode = Utf8Macro.unicode_of_tex
42 let rec make_attributes l1 = function
46 | None -> make_attributes (List.tl l1) tl
48 let p,n = List.hd l1 in
49 (p,n,s) :: make_attributes (List.tl l1) tl)
53 | Mpresentation.Mobject (attrs, box) ->
56 | mpres -> Box.Object ([], mpres)
60 | Box.Object (attrs, 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 rec add_mpres_attributes new_attr = function
89 | Mpresentation.Mobject (attr, box) ->
90 Mpresentation.Mobject (attr, add_box_attributes new_attr box)
92 Mpresentation.set_attr (new_attr @ Mpresentation.get_attr mpres) mpres
93 and add_box_attributes new_attr = function
94 | Box.Object (attr, mpres) ->
95 Box.Object (attr, add_mpres_attributes new_attr mpres)
96 | box -> Box.set_attr (new_attr @ Box.get_attr box) box
98 let box_of mathonly spec attrs children =
100 | [t] -> add_mpres_attributes attrs t
102 let kind, spacing, indent = spec in
105 CicNotationUtil.dress small_skip children
109 if mathonly then Mpresentation.Mrow (attrs, dress children)
112 (if spacing then RenderingAttrs.spacing_attributes `BoxML else [])
113 @ (if indent then RenderingAttrs.indent_attributes `BoxML else [])
118 if List.for_all eligible_math children then
119 Mpresentation.Mrow (attrs',
120 dress (List.map promote_to_math children))
122 mpres_of_box (Box.H (attrs',
123 List.map box_of_mpres children))
124 (* | Ast.H when List.for_all genuine_math children ->
125 Mpresentation.Mrow (attrs', dress children) *)
127 mpres_of_box (Box.V (attrs',
128 List.map box_of_mpres children))
130 mpres_of_box (Box.HV (attrs',
131 List.map box_of_mpres children))
133 mpres_of_box (Box.HOV (attrs',
134 List.map box_of_mpres children))
136 let open_paren = Mpresentation.Mo ([], "(")
137 let closed_paren = Mpresentation.Mo ([], ")")
138 let open_bracket = Mpresentation.Mo ([], "[")
139 let closed_bracket = Mpresentation.Mo ([], "]")
140 let open_brace = Mpresentation.Mo ([], "{")
141 let closed_brace = Mpresentation.Mo ([], "}")
142 let hidden_substs = Mpresentation.Mtext ([], "{...}")
143 let hidden_lctxt = Mpresentation.Mtext ([], "[...]")
144 let open_box_paren = Box.Text ([], "(")
145 let closed_box_paren = Box.Text ([], ")")
146 let semicolon = Mpresentation.Mo ([], ";")
147 let toggle_action children =
148 Mpresentation.Maction ([None, "actiontype", "toggle"], children)
150 type child_pos = [ `Left | `Right | `Inner ]
154 | Gramext.LeftA -> "LeftA"
155 | Gramext.RightA -> "RightA"
156 | Gramext.NonA -> "NonA"
159 let rec aux_mpres = function
165 | Mpres.Mspace _ -> true
166 | Mpres.Mobject (_, box) -> aux_box box
167 | Mpres.Maction (_, [mpres])
168 | Mpres.Mrow (_, [mpres]) -> aux_mpres mpres
170 and aux_box = function
174 | Box.Object (_, mpres) -> aux_mpres mpres
179 | Box.Action (_, [box]) -> aux_box box
184 let add_parens child_prec curr_prec t =
186 ((*prerr_endline ("NOT adding parens around ATOMIC: "^
187 BoxPp.render_to_string (function x::_->x|_->assert false)
188 ~map_unicode_to_tex:false 80 t);*)t)
189 else if child_prec >= 0 && child_prec < curr_prec then
191 (*prerr_endline ("adding parens around: "^
192 BoxPp.render_to_string (function x::_->x|_->assert false)
193 ~map_unicode_to_tex:false 80 t);*)
195 | Mpresentation.Mobject (_, box) ->
196 mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
197 | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
199 ((*prerr_endline ("NOT adding parens around: "^
200 BoxPp.render_to_string (function x::_->x|_->assert false)
201 ~map_unicode_to_tex:false 80 t);*)t)
203 let lookup_uri ids_to_uris id =
205 let uri = Hashtbl.find ids_to_uris id in
206 Some (UriManager.string_of_uri uri)
207 with Not_found -> None
210 let render ~lookup_uri ?(prec=(-1)) =
211 let module A = Ast in
212 let module P = Mpresentation in
213 (* let use_unicode = true in *)
214 let make_href xmlattrs xref =
218 match lookup_uri xref with
220 | Some uri -> uri :: uris)
223 let xmlattrs_uris, xmlattrs =
224 let xref_attrs, other_attrs =
226 (function Some "xlink", "href", _ -> true | _ -> false)
229 List.map (fun (_, _, uri) -> uri) xref_attrs,
233 match xmlattrs_uris @ xref_uris with
236 Some (String.concat " "
237 (HExtlib.list_uniq (List.sort String.compare uris)))
240 match !xref with [] -> None | xrefs -> Some (String.concat " " xrefs)
244 @ make_attributes [Some "helm", "xref"; Some "xlink", "href"]
249 match !xref with [] -> None | xrefs -> Some (String.concat " " xrefs)
252 make_attributes [Some "helm","xref"] [xrefs]
254 (* when mathonly is true no boxes should be generated, only mrows *)
256 let rec aux xmlattrs mathonly xref prec t =
258 | A.AttributedTerm _ ->
259 aux_attributes xmlattrs mathonly xref prec t
260 | A.Num (literal, _) ->
262 (RenderingAttrs.number_attributes `MathML)
263 @ make_href xmlattrs xref
265 Mpres.Mn (attrs, literal)
266 | A.Symbol (literal, _) ->
268 (RenderingAttrs.symbol_attributes `MathML)
269 @ make_href xmlattrs xref
271 Mpres.Mo (attrs, to_unicode literal)
272 | A.Ident (literal, subst)
273 | A.Uri (literal, subst) ->
275 (RenderingAttrs.ident_attributes `MathML)
276 @ make_href xmlattrs xref
278 let name = Mpres.Mi (attrs, to_unicode literal) in
284 box_of mathonly (A.H, false, false) []
286 :: (CicNotationUtil.dress semicolon
289 box_of mathonly (A.H, false, false) [] [
291 Mpres.Mo ([], to_unicode "\\def");
292 aux [] mathonly xref prec t ])
296 let substs_maction = toggle_action [ hidden_substs; substs' ] in
297 box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
299 let local_context l =
300 box_of mathonly (A.H, false, false) []
301 ([ Mpres.Mtext ([], "[") ] @
302 (CicNotationUtil.dress (Mpres.Mtext ([], ";"))
305 | None -> Mpres.Mtext ([], "_")
306 | Some t -> aux xmlattrs mathonly xref prec t) l)) @
307 [ Mpres.Mtext ([], "]")])
309 let lctxt_maction = toggle_action [ hidden_lctxt; local_context l ] in
310 box_of mathonly (A.H, false, false) []
311 ([Mpres.Mtext ([], "?"^string_of_int n) ]
312 @ (if l <> [] then [lctxt_maction] else []))
313 | A.Literal l -> aux_literal xmlattrs xref prec l
314 | A.UserInput -> Mpres.Mtext ([], "%")
315 | A.Layout l -> aux_layout mathonly xref prec l
317 | A.Variable _ -> assert false (* should have been instantiated *)
319 prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
321 and aux_attributes xmlattrs mathonly xref prec t =
322 let reset = ref false in
323 let inferred_level = ref None in
324 let expected_level = ref None in
325 let new_xref = ref [] in
326 let new_xmlattrs = ref [] in
327 let rec aux_attribute =
329 | A.AttributedTerm (attr, t) ->
333 | `Level (-1) -> reset := true
334 | `Level child_prec ->
335 (* assert (!expected_level = None); *)
336 expected_level := !inferred_level;
337 inferred_level := Some child_prec
338 | `IdRef xref -> new_xref := xref :: !new_xref
339 | `XmlAttrs attrs -> new_xmlattrs := attrs @ !new_xmlattrs);
343 match !expected_level with
347 (match !inferred_level with
348 | None -> aux !new_xmlattrs mathonly new_xref prec t
350 let t' = aux !new_xmlattrs mathonly new_xref child_prec t in
352 ("inferred: "^string_of_int child_prec^
353 " exp: "^string_of_int prec ^
354 " term: "^BoxPp.render_to_string ~map_unicode_to_tex:false
355 (function x::_->x|_->assert false) 80 t');*)
357 then ((*prerr_endline "reset";*)t')
358 else add_parens child_prec prec t')
360 (* prerr_endline (CicNotationPp.pp_term t); *)
362 and aux_literal xmlattrs xref prec l =
363 let attrs = make_href xmlattrs xref in
365 | `Symbol s -> Mpres.Mo (attrs, to_unicode s)
366 | `Keyword s -> Mpres.Mtext (attrs, to_unicode s)
367 | `Number s -> Mpres.Mn (attrs, to_unicode s))
368 and aux_layout mathonly xref prec l =
369 let attrs = make_xref xref in
370 let invoke' t = aux [] true (ref []) prec t in
371 (* use the one below to reset precedence and associativity *)
372 let invoke_reinit t = aux [] mathonly xref ~-1 t in
374 | A.Sup (A.Layout (A.Sub (t1,t2)), t3)
375 | A.Sup (A.AttributedTerm (_,A.Layout (A.Sub (t1,t2))), t3)
376 -> Mpres.Msubsup (attrs, invoke' t1, invoke_reinit t2, invoke_reinit t3)
377 | A.Sub (t1, t2) -> Mpres.Msub (attrs, invoke' t1, invoke_reinit t2)
378 | A.Sup (t1, t2) -> Mpres.Msup (attrs, invoke' t1, invoke_reinit t2)
379 | A.Below (t1, t2) -> Mpres.Munder (attrs, invoke' t1, invoke_reinit t2)
380 | A.Above (t1, t2) -> Mpres.Mover (attrs, invoke' t1, invoke_reinit t2)
383 Mpres.Mfrac (attrs, invoke_reinit t1, invoke_reinit t2)
385 Mpres.Mfrac (atop_attributes @ attrs, invoke_reinit t1,
387 | A.InfRule (t1, t2, t3) ->
388 Mpres.Mstyle ([None,"mathsize","big"],
391 Mpres.Mstyle ([None,"scriptlevel","0"],invoke_reinit t1),
392 Mpres.Mstyle ([None,"scriptlevel","0"],invoke_reinit t2));
393 Mpres.Mstyle ([None,"scriptlevel","2"],
395 (RenderingAttrs.small_skip_attributes `MathML));
396 Mpres.Mstyle ([None,"scriptlevel","1"],invoke_reinit t3)]))
397 | A.Sqrt t -> Mpres.Msqrt (attrs, invoke_reinit t)
399 Mpres.Mroot (attrs, invoke_reinit t1, invoke_reinit t2)
400 | A.Box ((_, spacing, _) as kind, terms) ->
402 aux_children mathonly spacing xref prec
403 (CicNotationUtil.ungroup terms)
405 box_of mathonly kind attrs children
406 | A.Mstyle (l,terms) ->
408 (List.map (fun (k,v) -> None,k,v) l,
409 box_of mathonly (A.H, false, false) attrs
410 (aux_children mathonly false xref prec
411 (CicNotationUtil.ungroup terms)))
412 | A.Mpadded (l,terms) ->
414 (List.map (fun (k,v) -> None,k,v) l,
415 box_of mathonly (A.H, false, false) attrs
416 (aux_children mathonly false xref prec
417 (CicNotationUtil.ungroup terms)))
418 | A.Maction alternatives ->
419 toggle_action (List.map invoke_reinit alternatives)
422 aux_children mathonly false xref prec
423 (CicNotationUtil.ungroup terms)
425 box_of mathonly (A.H, false, false) attrs children
426 | A.Break -> assert false (* TODO? *)
427 and aux_children mathonly spacing xref prec terms =
429 let rec aux_list first clusters acc =
431 [] when acc = [] -> List.rev clusters
432 | [] -> aux_list first (List.rev acc :: clusters) [] []
433 | (A.Layout A.Break) :: tl when acc = [] ->
434 aux_list first clusters [] tl
435 | (A.Layout A.Break) :: tl ->
436 aux_list first (List.rev acc :: clusters) [] tl
438 aux_list false clusters
439 (aux [] mathonly xref prec hd :: acc) []
441 aux_list false clusters
442 (aux [] mathonly xref prec hd :: acc) tl
449 | tl -> box_of mathonly (A.H, spacing, false) [] tl
451 List.map boxify_pres (find_clusters terms)
453 aux [] false (ref []) prec
455 let rec print_box (t: boxml_markup) =
456 Box.box2xml print_mpres t
457 and print_mpres (t: mathml_markup) =
458 Mpresentation.print_mpres print_box t
460 let print_xml = print_mpres
462 (* let render_to_boxml id_to_uri t =
463 let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
464 Xml.add_xml_declaration xml_stream *)