]> matita.cs.unibo.it Git - helm.git/blob - helm/ocaml/cic_notation/cicNotationPres.ml
avoid generating multiple times the same xref/href
[helm.git] / helm / ocaml / cic_notation / cicNotationPres.ml
1 (* Copyright (C) 2004-2005, HELM Team.
2  * 
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.
6  * 
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.
11  * 
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.
16  *
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,
20  * MA  02111-1307, USA.
21  * 
22  * For details, see the HELM World-Wide-Web page,
23  * http://helm.cs.unibo.it/
24  *)
25
26 module P = Mpresentation
27
28 type mathml_markup = boxml_markup Mpresentation.mpres
29 and boxml_markup = mathml_markup Box.box
30
31 type markup = mathml_markup
32
33 let atop_attributes = [None, "linethickness", "0pt"]
34
35 let to_unicode = Utf8Macro.unicode_of_tex
36
37 (* let rec make_attributes l1 = function
38   | [] -> []
39   | None :: tl -> make_attributes (List.tl l1) tl
40   | Some s :: tl ->
41       let p,n = List.hd l1 in
42       prerr_endline (Printf.sprintf "make_attributes %s %s" n s);
43       (p,n,s) :: make_attributes (List.tl l1) tl *)
44 let rec make_attributes l1 = function
45   | [] -> []
46   | hd :: tl ->
47       (match !hd with
48       | None -> make_attributes (List.tl l1) tl
49       | Some s ->
50           let p,n = List.hd l1 in
51           prerr_endline (Printf.sprintf "make_attributes %s %s" n s);
52           hd := None;
53           (p,n,s) :: make_attributes (List.tl l1) tl)
54
55 let box_of_mpres =
56   function
57     Mpresentation.Mobject (_, box) -> box
58   | mpres -> Box.Object ([], mpres)
59
60 let mpres_of_box =
61   function
62     Box.Object (_, mpres) -> mpres
63   | box -> Mpresentation.Mobject ([], box)
64
65 let rec genuine_math =
66   function
67   | Mpresentation.Mobject ([], obj) -> not (genuine_box obj)
68   | _ -> true
69 and genuine_box =
70   function
71   | Box.Object ([], mpres) -> not (genuine_math mpres)
72   | _ -> true
73
74 let rec eligible_math =
75   function
76   | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> eligible_math mpres
77   | Mpresentation.Mobject ([], _) -> false
78   | _ -> true
79
80 let rec promote_to_math =
81   function
82   | Mpresentation.Mobject ([], Box.Object ([], mpres)) -> promote_to_math mpres
83   | math -> math
84
85 let small_skip =
86   Mpresentation.Mspace (RenderingAttrs.small_skip_attributes `MathML)
87
88 let box_of mathonly spec attrs children =
89   match children with
90     | [t] -> t
91     | _ ->
92         let kind, spacing, indent = spec in
93         let dress children =
94           if spacing then
95             CicNotationUtil.dress small_skip children
96           else
97             children
98         in
99           if mathonly then Mpresentation.Mrow (attrs, dress children)
100           else
101             let attrs' =
102               (if spacing then RenderingAttrs.spacing_attributes `BoxML else [])
103               @ (if indent then RenderingAttrs.indent_attributes `BoxML else [])
104               @ attrs
105             in
106               match kind with
107                 | CicNotationPt.H ->
108                     if List.for_all eligible_math children then
109                       Mpresentation.Mrow (attrs',
110                         dress (List.map promote_to_math children))
111                     else
112                       mpres_of_box (Box.H (attrs',
113                         List.map box_of_mpres children))
114 (*                 | CicNotationPt.H when List.for_all genuine_math children ->
115                     Mpresentation.Mrow (attrs', dress children) *)
116                 | CicNotationPt.V ->
117                     mpres_of_box (Box.V (attrs',
118                       List.map box_of_mpres children))
119                 | CicNotationPt.HV ->
120                     mpres_of_box (Box.HV (attrs',
121                       List.map box_of_mpres children))
122                 | CicNotationPt.HOV ->
123                     mpres_of_box (Box.HOV (attrs',
124                       List.map box_of_mpres children))
125
126 let open_paren        = Mpresentation.Mo ([], "(")
127 let closed_paren      = Mpresentation.Mo ([], ")")
128 let open_brace        = Mpresentation.Mo ([], "{")
129 let closed_brace      = Mpresentation.Mo ([], "}")
130 let hidden_substs     = Mpresentation.Mtext ([], "{...}")
131 let open_box_paren    = Box.Text ([], "(")
132 let closed_box_paren  = Box.Text ([], ")")
133 let semicolon         = Mpresentation.Mo ([], ";")
134 let toggle_action children =
135   Mpresentation.Maction ([None, "actiontype", "toggle"], children)
136
137 type child_pos = [ `None | `Left | `Right | `Inner ]
138
139 let pp_assoc =
140   function
141   | Gramext.LeftA -> "LeftA"
142   | Gramext.RightA -> "RightA"
143   | Gramext.NonA -> "NonA"
144
145 let pp_pos =
146   function
147       `None -> "`None"
148     | `Left -> "`Left"
149     | `Right -> "`Right"
150     | `Inner -> "`Inner"
151
152 let is_atomic t =
153   let rec aux_mpres = function
154     | P.Mi _
155     | P.Mo _
156     | P.Mn _
157     | P.Ms _
158     | P.Mtext _
159     | P.Mspace _ -> true
160     | P.Mobject (_, box) -> aux_box box
161     | P.Maction (_, [mpres])
162     | P.Mrow (_, [mpres]) -> aux_mpres mpres
163     | _ -> false
164   and aux_box = function
165     | Box.Space _
166     | Box.Ink _
167     | Box.Text _ -> true
168     | Box.Object (_, mpres) -> aux_mpres mpres
169     | Box.H (_, [box])
170     | Box.V (_, [box])
171     | Box.HV (_, [box])
172     | Box.HOV (_, [box])
173     | Box.Action (_, [box]) -> aux_box box
174     | _ -> false
175   in
176   aux_mpres t
177
178 let add_parens child_prec child_assoc child_pos curr_prec t =
179   if is_atomic t then t
180   else if child_prec < curr_prec
181     || (child_prec = curr_prec &&
182         child_assoc = Gramext.LeftA &&
183         child_pos <> `Left)
184     || (child_prec = curr_prec &&
185         child_assoc = Gramext.RightA &&
186         child_pos <> `Right)
187   then  (* parens should be added *)
188     match t with
189     | Mpresentation.Mobject (_, box) ->
190         mpres_of_box (Box.H ([], [ open_box_paren; box; closed_box_paren ]))
191     | mpres -> Mpresentation.Mrow ([], [open_paren; t; closed_paren])
192   else
193     t
194
195 let render ids_to_uris =
196   let module A = CicNotationPt in
197   let module P = Mpresentation in
198   let use_unicode = true in
199   let lookup_uri = function
200     | None -> None
201     | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
202   in
203   let make_href xmlattrs xref uris =
204     let xref_uri = lookup_uri !xref in
205     let raw_uris = List.map UriManager.string_of_uri !uris in
206     let uri =
207       match xref_uri, raw_uris with
208       | None, [] -> None
209       | Some uri, [] -> Some uri
210       | None, raw_uris -> Some (String.concat " " raw_uris)
211       | Some uri, raw_uris -> Some (String.concat " " (uri :: raw_uris))
212     in
213     uris := [];
214     xmlattrs
215     @ make_attributes [Some "helm", "xref"; Some "xlink", "href"]
216         [xref; ref uri]
217   in
218   let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
219   (* when mathonly is true no boxes should be generated, only mrows *)
220   (* "xref" is  *)
221   let rec aux xmlattrs mathonly xref pos prec uris t =
222     match t with
223     | A.AttributedTerm _ ->
224         aux_attributes xmlattrs mathonly xref pos prec uris t
225     | A.Num (literal, _) ->
226         let attrs =
227           (RenderingAttrs.number_attributes `MathML)
228           @ make_href xmlattrs xref uris
229         in
230         P.Mn (attrs, literal)
231     | A.Symbol (literal, _) ->
232         let attrs =
233           (RenderingAttrs.symbol_attributes `MathML)
234           @ make_href xmlattrs xref uris
235         in
236         P.Mo (attrs, to_unicode literal)
237     | A.Ident (literal, subst)
238     | A.Uri (literal, subst) ->
239         let attrs =
240           (RenderingAttrs.ident_attributes `MathML)
241           @ make_href xmlattrs xref (ref [])
242         in
243         let name = P.Mi (attrs, to_unicode literal) in
244         (match subst with
245         | Some []
246         | None -> name
247         | Some substs ->
248             let substs' =
249               box_of mathonly (A.H, false, false) []
250                 (open_brace
251                 :: (CicNotationUtil.dress semicolon
252                     (List.map
253                       (fun (name, t) ->
254                         box_of mathonly (A.H, false, false) [] [
255                           P.Mi ([], name);
256                           P.Mo ([], to_unicode "\\def");
257                           aux [] mathonly xref pos prec uris t ])
258                       substs))
259                 @ [ closed_brace ])
260 (*                 (CicNotationUtil.dress semicolon
261                   (List.map
262                     (fun (var, t) ->
263                       let var_uri = UriManager.uri_of_string var in
264                       let var_name = UriManager.name_of_uri var_uri in
265                       let href_attr = Some "xlink", "href", var in
266                       box_of mathonly (A.H, false, false) [] [
267                         P.Mi ([href_attr], var_name);
268                         P.Mo ([], to_unicode "\\def");
269                         aux [] mathonly xref pos prec uris t ])
270                     substs)) *)
271             in
272             let substs_maction = toggle_action [ hidden_substs; substs' ] in
273             box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
274     | A.Literal l -> aux_literal xmlattrs xref prec uris l
275     | A.UserInput -> P.Mtext ([], "%")
276     | A.Layout l -> aux_layout mathonly xref pos prec uris l
277     | A.Magic _
278     | A.Variable _ -> assert false  (* should have been instantiated *)
279     | t ->
280         prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
281         assert false
282   and aux_attributes xmlattrs mathonly xref pos prec uris t =
283     let new_level = ref None in
284     let new_xref = ref None in
285     let new_uris = ref [] in
286     let new_xmlattrs = ref [] in
287     let rec aux_attribute =
288       function
289       | A.AttributedTerm (attr, t) ->
290           (match attr with
291           | `Loc _
292           | `Raw _ -> ()
293           | `Level (child_prec, child_assoc) ->
294               new_level := Some (child_prec, child_assoc)
295           | `IdRef xref -> new_xref := Some xref
296           | `Href hrefs -> new_uris := hrefs
297           | `XmlAttrs attrs -> new_xmlattrs := attrs);
298           aux_attribute t
299       | t ->
300           (match !new_level with
301           | None -> aux !new_xmlattrs mathonly new_xref pos prec new_uris t
302           | Some (child_prec, child_assoc) ->
303               let t' = 
304                 aux !new_xmlattrs mathonly new_xref pos child_prec new_uris t
305               in
306               add_parens child_prec child_assoc pos prec t')
307     in
308     aux_attribute t
309 (*     function
310     | `Loc _
311     | `Raw _ -> aux xmlattrs mathonly xref pos prec uris t
312     | `Level (child_prec, child_assoc) ->
313         let t' = aux xmlattrs mathonly xref pos child_prec uris t in
314         add_parens child_prec child_assoc pos prec t'
315     | `IdRef xref -> aux xmlattrs mathonly (Some xref) pos prec uris t
316     | `Href uris' -> aux xmlattrs mathonly xref pos prec uris' t
317     | `XmlAttrs xmlattrs -> aux xmlattrs mathonly xref pos prec uris t *)
318   and aux_literal xmlattrs xref prec uris l =
319     let attrs = make_href xmlattrs xref uris in
320     (match l with
321     | `Symbol s -> P.Mo (attrs, to_unicode s)
322     | `Keyword s -> P.Mo (attrs, to_unicode s)
323     | `Number s  -> P.Mn (attrs, to_unicode s))
324   and aux_layout mathonly xref pos prec uris l =
325     let attrs = make_xref xref in
326     let invoke' t = aux [] true (ref None) pos prec uris t in
327     match l with
328     | A.Sub (t1, t2) -> P.Msub (attrs, invoke' t1, invoke' t2)
329     | A.Sup (t1, t2) -> P.Msup (attrs, invoke' t1, invoke' t2)
330     | A.Below (t1, t2) -> P.Munder (attrs, invoke' t1, invoke' t2)
331     | A.Above (t1, t2) -> P.Mover (attrs, invoke' t1, invoke' t2)
332     | A.Frac (t1, t2)
333     | A.Over (t1, t2) -> P.Mfrac (attrs, invoke' t1, invoke' t2)
334     | A.Atop (t1, t2) ->
335         P.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2)
336     | A.Sqrt t -> P.Msqrt (attrs, invoke' t)
337     | A.Root (t1, t2) -> P.Mroot (attrs, invoke' t1, invoke' t2)
338     | A.Box ((_, spacing, _) as kind, terms) ->
339         let children =
340           aux_children mathonly spacing xref pos prec uris
341             (CicNotationUtil.ungroup terms)
342         in
343         box_of mathonly kind attrs children
344     | A.Group terms ->
345         let children =
346           aux_children mathonly false xref pos prec uris
347             (CicNotationUtil.ungroup terms)
348         in
349         box_of mathonly (A.H, false, false) attrs children
350     | A.Break -> assert false (* TODO? *)
351   and aux_children mathonly spacing xref pos prec uris terms =
352     let find_clusters =
353       let rec aux_list first clusters acc =
354         function
355             [] when acc = [] -> List.rev clusters
356           | [] -> aux_list first (List.rev acc :: clusters) [] []
357           | (A.Layout A.Break) :: tl when acc = [] ->
358               aux_list first clusters [] tl
359           | (A.Layout A.Break) :: tl ->
360               aux_list first (List.rev acc :: clusters) [] tl
361           | [hd] ->
362               let pos' = 
363                 if first then
364                   pos
365                 else
366                   match pos with
367                       `None -> `Right
368                     | `Inner -> `Inner
369                     | `Right -> `Right
370                     | `Left -> `Inner
371               in
372                 aux_list false clusters
373                   (aux [] mathonly xref pos' prec uris hd :: acc) []
374           | hd :: tl ->
375               let pos' =
376                 match pos, first with
377                     `None, true -> `Left
378                   | `None, false -> `Inner
379                   | `Left, true -> `Left
380                   | `Left, false -> `Inner
381                   | `Right, _ -> `Inner
382                   | `Inner, _ -> `Inner
383               in
384                 aux_list false clusters
385                   (aux [] mathonly xref pos' prec uris hd :: acc) tl
386       in
387         aux_list true [] []
388     in
389     let boxify_pres =
390       function
391           [t] -> t
392         | tl -> box_of mathonly (A.H, spacing, false) [] tl
393     in
394       List.map boxify_pres (find_clusters terms)
395   in
396   aux [] false (ref None) `None 0 (ref [])
397
398 let rec print_box (t: boxml_markup) =
399   Box.box2xml print_mpres t
400 and print_mpres (t: mathml_markup) =
401   Mpresentation.print_mpres print_box t
402
403 let print_xml = print_mpres
404
405 (* let render_to_boxml id_to_uri t =
406   let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
407   Xml.add_xml_declaration xml_stream *)
408