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 type mathml_markup = boxml_markup Mpresentation.mpres
27 and boxml_markup = mathml_markup Box.box
29 type markup = mathml_markup
31 let atop_attributes = [None, "linethickness", "0pt"]
32 let binder_attributes = [None, "mathcolor", "blue"]
33 let indent_attributes = [None, "indent", "1em"]
35 let mpres_arrow = Mpresentation.Mo (binder_attributes, "->")
36 (* TODO unicode symbol "to" *)
37 let mpres_implicit = Mpresentation.Mtext ([], "?")
42 Utf8Macro.expand (String.sub s 1 (String.length s - 1))
44 with Utf8Macro.Macro_not_found _ -> s
46 let rec make_attributes l1 = function
48 | None :: tl -> make_attributes (List.tl l1) tl
50 let p,n = List.hd l1 in
51 (p,n,s) :: make_attributes (List.tl l1) tl
55 Mpresentation.Mobject (_, box) -> box
56 | mpres -> Box.Object ([], mpres)
60 Box.Object (_, mpres) -> mpres
61 | box -> Mpresentation.Mobject ([], box)
65 | Mpresentation.Mobject _ -> false
68 let box_of mathonly spec attrs children =
72 let kind, spacing, indent = spec in
73 let rec dress = function
76 | hd :: tl -> hd :: Mpresentation.Mtext ([], " ") :: dress tl
78 if mathonly then Mpresentation.Mrow (attrs, dress children)
81 if spacing then [None, "spacing", "0.5em"] else []
82 @ if indent then [None, "indent", "0em 0.5em"] else []
86 | CicNotationPt.H when List.for_all genuine_math children ->
87 Mpresentation.Mrow (attrs', children)
89 mpres_of_box (Box.H (attrs', List.map box_of_mpres children))
91 mpres_of_box (Box.V (attrs', List.map box_of_mpres children))
93 mpres_of_box (Box.HV (attrs', List.map box_of_mpres children))
94 | CicNotationPt.HOV ->
95 mpres_of_box (Box.HOV (attrs', List.map box_of_mpres children))
97 let open_paren = Mpresentation.Mo ([], "(")
98 let closed_paren = Mpresentation.Mo ([], ")")
99 let open_box_paren = Box.Text ([], "(")
100 let closed_box_paren = Box.Text ([], ")")
102 type child_pos = [ `None | `Left | `Right | `Inner ]
106 | Gramext.LeftA -> "LeftA"
107 | Gramext.RightA -> "RightA"
108 | Gramext.NonA -> "NonA"
118 let module P = Mpresentation in
119 let rec aux_mpres = function
126 | P.Mobject (_, box) -> aux_box box
127 | P.Maction (_, [mpres])
128 | P.Mrow (_, [mpres]) -> aux_mpres mpres
130 and aux_box = function
134 | Box.Object (_, mpres) -> aux_mpres mpres
139 | Box.Action (_, [box]) -> aux_box box
144 let add_parens child_prec child_assoc child_pos curr_prec t =
145 (* prerr_endline (Printf.sprintf "add_parens %d %s %s %d" child_prec
146 (pp_assoc child_assoc) (pp_pos child_pos) (curr_prec)); *)
147 if is_atomic t then t
148 else if child_prec < curr_prec
149 || (child_prec = curr_prec &&
150 child_assoc = Gramext.LeftA &&
152 || (child_prec = curr_prec &&
153 child_assoc = Gramext.RightA &&
155 then (* parens should be added *)
157 | Mpresentation.Mobject (_, box) ->
158 mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
159 | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
163 let render ids_to_uris =
164 let module A = CicNotationPt in
165 let module P = Mpresentation in
166 let use_unicode = true in
167 let lookup_uri = function
169 | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
171 let make_href xref uris =
172 let xref_uri = lookup_uri xref in
173 let raw_uris = List.map UriManager.string_of_uri uris in
175 match xref_uri, raw_uris with
177 | Some uri, [] -> Some uri
178 | None, raw_uris -> Some (String.concat " " raw_uris)
179 | Some uri, raw_uris -> Some (String.concat " " (uri :: raw_uris))
181 make_attributes [Some "helm", "xref"; Some "xlink", "href"] [xref; uri]
183 let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
184 let make_box = function
185 | P.Mobject (attrs, box) ->
188 | m -> Box.Object ([], m)
190 (* when mathonly is true no boxes should be generated, only mrows *)
191 let rec aux mathonly xref pos prec uris t =
193 | A.AttributedTerm (attr, t) ->
194 aux_attribute mathonly xref pos prec uris t attr
195 | A.Ident (literal, _) -> P.Mi (make_href xref [], to_unicode literal)
196 | A.Num (literal, _) -> P.Mn (make_href xref [], to_unicode literal)
197 | A.Symbol (literal, _) -> P.Mo (make_href xref uris, to_unicode literal)
198 | A.Uri (literal, _) -> P.Mi (make_href xref [], to_unicode literal)
199 | A.Literal l -> aux_literal xref prec uris l
200 | A.Layout l -> aux_layout mathonly xref pos prec uris l
202 | A.Variable _ -> assert false (* should have been instantiated *)
204 prerr_endline (CicNotationPp.pp_term t);
206 and aux_attribute mathonly xref pos prec uris t =
208 | `Loc _ -> aux mathonly xref pos prec uris t
209 | `Level (child_prec, child_assoc) ->
210 let t' = aux mathonly xref pos child_prec uris t in
211 add_parens child_prec child_assoc pos prec t'
212 | `IdRef xref -> aux mathonly (Some xref) pos prec uris t
213 | `Href uris' -> aux mathonly xref pos prec uris' t
214 and aux_literal xref prec uris l =
215 let attrs = make_href xref uris in
218 | `Keyword s -> P.Mo (attrs, to_unicode s)
219 | `Number s -> P.Mn (attrs, to_unicode s)
220 and aux_layout mathonly xref pos prec uris l =
221 let attrs = make_xref xref in
222 let invoke' t = aux true None pos prec uris t in
224 | A.Sub (t1, t2) -> P.Msub (attrs, invoke' t1, invoke' t2)
225 | A.Sup (t1, t2) -> P.Msup (attrs, invoke' t1, invoke' t2)
226 | A.Below (t1, t2) -> P.Munder (attrs, invoke' t1, invoke' t2)
227 | A.Above (t1, t2) -> P.Mover (attrs, invoke' t1, invoke' t2)
229 | A.Over (t1, t2) -> P.Mfrac (attrs, invoke' t1, invoke' t2)
231 P.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2)
232 | A.Sqrt t -> P.Msqrt (attrs, invoke' t)
233 | A.Root (t1, t2) -> P.Mroot (attrs, invoke' t1, invoke' t2)
234 | A.Box (kind, terms) ->
235 let children = aux_children mathonly xref pos prec uris terms in
236 box_of mathonly kind attrs children
237 and aux_children mathonly xref pos prec uris terms =
238 let rec aux_list first =
250 [aux mathonly xref pos' prec uris t]
253 match pos, first with
255 | `None, false -> `Inner
256 | `Left, true -> `Left
257 | `Left, false -> `Inner
258 | `Right, _ -> `Inner
259 | `Inner, _ -> `Inner
261 (aux mathonly xref pos' prec uris t) :: aux_list false tl
264 [t] -> [aux mathonly xref pos prec uris t]
265 | tl -> aux_list true tl
267 aux false None `None 0 []
269 let render_to_boxml id_to_uri t =
270 let rec print_box (t: CicNotationPres.boxml_markup) =
271 Box.box2xml print_mpres t
272 and print_mpres (t: CicNotationPres.mathml_markup) =
273 Mpresentation.print_mpres print_box t
275 let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
276 Ast2pres.add_xml_declaration xml_stream