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