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"]
34 let keyword_attributes = [None, "mathcolor", "blue"]
36 let mpres_arrow = Mpresentation.Mo (binder_attributes, "->")
37 (* TODO unicode symbol "to" *)
38 let mpres_implicit = Mpresentation.Mtext ([], "?")
43 Utf8Macro.expand (String.sub s 1 (String.length s - 1))
45 with Utf8Macro.Macro_not_found _ -> s
47 let rec make_attributes l1 = function
49 | None :: tl -> make_attributes (List.tl l1) tl
51 let p,n = List.hd l1 in
52 (p,n,s) :: make_attributes (List.tl l1) tl
56 Mpresentation.Mobject (_, box) -> box
57 | mpres -> Box.Object ([], mpres)
61 Box.Object (_, mpres) -> mpres
62 | box -> Mpresentation.Mobject ([], box)
66 | Mpresentation.Mobject _ -> false
69 let box_of mathonly spec attrs children =
73 let kind, spacing, indent = spec in
76 CicNotationUtil.dress (Mpresentation.Mtext ([], " ")) children
80 if mathonly then Mpresentation.Mrow (attrs, dress children)
83 (if spacing then [None, "spacing", "0.5em"] else [])
84 @ (if indent then [None, "indent", "0em 0.5em"] else [])
88 | CicNotationPt.H when List.for_all genuine_math children ->
89 Mpresentation.Mrow (attrs', dress children)
91 mpres_of_box (Box.H (attrs', List.map box_of_mpres children))
93 mpres_of_box (Box.V (attrs', List.map box_of_mpres children))
95 mpres_of_box (Box.HV (attrs', List.map box_of_mpres children))
96 | CicNotationPt.HOV ->
97 mpres_of_box (Box.HOV (attrs', List.map box_of_mpres children))
99 let open_paren = Mpresentation.Mo ([], "(")
100 let closed_paren = Mpresentation.Mo ([], ")")
101 let open_box_paren = Box.Text ([], "(")
102 let closed_box_paren = Box.Text ([], ")")
104 type child_pos = [ `None | `Left | `Right | `Inner ]
108 | Gramext.LeftA -> "LeftA"
109 | Gramext.RightA -> "RightA"
110 | Gramext.NonA -> "NonA"
120 let module P = Mpresentation in
121 let rec aux_mpres = function
128 | P.Mobject (_, box) -> aux_box box
129 | P.Maction (_, [mpres])
130 | P.Mrow (_, [mpres]) -> aux_mpres mpres
132 and aux_box = function
136 | Box.Object (_, mpres) -> aux_mpres mpres
141 | Box.Action (_, [box]) -> aux_box box
146 let add_parens child_prec child_assoc child_pos curr_prec t =
147 (* prerr_endline (Printf.sprintf "add_parens %d %s %s %d" child_prec
148 (pp_assoc child_assoc) (pp_pos child_pos) (curr_prec)); *)
149 if is_atomic t then t
150 else if child_prec < curr_prec
151 || (child_prec = curr_prec &&
152 child_assoc = Gramext.LeftA &&
154 || (child_prec = curr_prec &&
155 child_assoc = Gramext.RightA &&
157 then (* parens should be added *)
159 | Mpresentation.Mobject (_, box) ->
160 mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
161 | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
165 let render ids_to_uris =
166 let module A = CicNotationPt in
167 let module P = Mpresentation in
168 let use_unicode = true in
169 let lookup_uri = function
171 | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
173 let make_href xmlattrs xref uris =
174 let xref_uri = lookup_uri xref in
175 let raw_uris = List.map UriManager.string_of_uri uris in
177 match xref_uri, raw_uris with
179 | Some uri, [] -> Some uri
180 | None, raw_uris -> Some (String.concat " " raw_uris)
181 | Some uri, raw_uris -> Some (String.concat " " (uri :: raw_uris))
184 @ make_attributes [Some "helm", "xref"; Some "xlink", "href"] [xref; uri]
186 let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
187 let make_box = function
188 | P.Mobject (attrs, box) ->
191 | m -> Box.Object ([], m)
193 (* when mathonly is true no boxes should be generated, only mrows *)
194 let rec aux xmlattrs mathonly xref pos prec uris t =
196 | A.AttributedTerm (attr, t) ->
197 aux_attribute xmlattrs mathonly xref pos prec uris t attr
198 | A.Ident (literal, _) ->
199 P.Mi (make_href xmlattrs xref [], to_unicode literal)
200 | A.Num (literal, _) ->
201 P.Mn (make_href xmlattrs xref [], to_unicode literal)
202 | A.Symbol (literal, _) ->
203 P.Mo (make_href xmlattrs xref uris, to_unicode literal)
204 | A.Uri (literal, _) ->
205 P.Mi (make_href xmlattrs xref [], to_unicode literal)
206 | A.Literal l -> aux_literal xmlattrs xref prec uris l
207 | A.Layout l -> aux_layout mathonly xref pos prec uris l
209 | A.Variable _ -> assert false (* should have been instantiated *)
211 prerr_endline (CicNotationPp.pp_term t);
213 and aux_attribute xmlattrs mathonly xref pos prec uris t =
215 | `Loc _ -> aux xmlattrs mathonly xref pos prec uris t
216 | `Level (child_prec, child_assoc) ->
217 let t' = aux xmlattrs mathonly xref pos child_prec uris t in
218 add_parens child_prec child_assoc pos prec t'
219 | `IdRef xref -> aux xmlattrs mathonly (Some xref) pos prec uris t
220 | `Href uris' -> aux xmlattrs mathonly xref pos prec uris' t
221 | `XmlAttrs xmlattrs -> aux xmlattrs mathonly xref pos prec uris t
222 and aux_literal xmlattrs xref prec uris l =
223 let attrs = make_href xmlattrs xref uris in
225 | `Symbol s -> P.Mo (attrs, to_unicode s)
226 | `Keyword s -> P.Mo (attrs, to_unicode s)
227 | `Number s -> P.Mn (attrs, to_unicode s)
228 and aux_layout mathonly xref pos prec uris l =
229 let attrs = make_xref xref in
230 let invoke' t = aux [] true None pos prec uris t in
232 | A.Sub (t1, t2) -> P.Msub (attrs, invoke' t1, invoke' t2)
233 | A.Sup (t1, t2) -> P.Msup (attrs, invoke' t1, invoke' t2)
234 | A.Below (t1, t2) -> P.Munder (attrs, invoke' t1, invoke' t2)
235 | A.Above (t1, t2) -> P.Mover (attrs, invoke' t1, invoke' t2)
237 | A.Over (t1, t2) -> P.Mfrac (attrs, invoke' t1, invoke' t2)
239 P.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2)
240 | A.Sqrt t -> P.Msqrt (attrs, invoke' t)
241 | A.Root (t1, t2) -> P.Mroot (attrs, invoke' t1, invoke' t2)
242 | A.Box ((_, spacing, _) as kind, terms) ->
243 let children = aux_children mathonly spacing xref pos prec uris (CicNotationUtil.ungroup terms) in
244 box_of mathonly kind attrs children
246 let children = aux_children false mathonly xref pos prec uris (CicNotationUtil.ungroup terms) in
247 box_of mathonly (A.H, false, false) attrs children
248 | A.Break -> assert false (* TODO? *)
249 and aux_children mathonly spacing xref pos prec uris terms =
251 let rec aux_list first clusters acc =
253 [] when acc = [] -> List.rev clusters
254 | [] -> aux_list first (List.rev acc :: clusters) [] []
255 | (A.Layout A.Break) :: tl when acc = [] -> aux_list first clusters [] tl
256 | (A.Layout A.Break) :: tl -> aux_list first (List.rev acc :: clusters) [] tl
268 aux_list false clusters (aux [] mathonly xref pos' prec uris hd :: acc) []
271 match pos, first with
273 | `None, false -> `Inner
274 | `Left, true -> `Left
275 | `Left, false -> `Inner
276 | `Right, _ -> `Inner
277 | `Inner, _ -> `Inner
279 aux_list false clusters (aux [] mathonly xref pos' prec uris hd :: acc) tl
286 | tl -> box_of mathonly (A.H, spacing, false) [] tl
288 List.map boxify_pres (find_clusters terms)
290 aux [] false None `None 0 []
292 let render_to_boxml id_to_uri t =
293 let rec print_box (t: CicNotationPres.boxml_markup) =
294 Box.box2xml print_mpres t
295 and print_mpres (t: CicNotationPres.mathml_markup) =
296 Mpresentation.print_mpres print_box t
298 let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
299 Ast2pres.add_xml_declaration xml_stream