]> matita.cs.unibo.it Git - helm.git/blobdiff - 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
index c4679bef7adb95e1f20ebd4702ce4e0f86d2ddbd..34c4c652002e15865e34f892c781a557a1148a9b 100644 (file)
@@ -38,11 +38,10 @@ let to_unicode = Utf8Macro.unicode_of_tex
 let rec make_attributes l1 = function
   | [] -> []
   | hd :: tl ->
-      (match !hd with
+      (match hd with
       | None -> make_attributes (List.tl l1) tl
       | Some s ->
           let p,n = List.hd l1 in
-          hd := None;
           (p,n,s) :: make_attributes (List.tl l1) tl)
 
 let box_of_mpres =
@@ -203,49 +202,75 @@ let render ids_to_uris =
   let module A = Ast in
   let module P = Mpresentation in
   let use_unicode = true in
-  let lookup_uri = function
-    | None -> None
-    | Some id -> (try Some (Hashtbl.find ids_to_uris id) with Not_found -> None)
+  let lookup_uri id =
+    (try
+      let uri = Hashtbl.find ids_to_uris id in
+      Some uri
+    with Not_found -> None)
   in
-  let make_href xmlattrs xref uris =
-    let xref_uri = lookup_uri !xref in
-    let raw_uris = List.map UriManager.string_of_uri !uris in
-    let uri =
-      match xref_uri, raw_uris with
-      | None, [] -> None
-      | Some uri, [] -> Some uri
-      | None, raw_uris -> Some (String.concat " " raw_uris)
-      | Some uri, raw_uris -> Some (String.concat " " (uri :: raw_uris))
+  let make_href xmlattrs xref =
+    let xref_uris =
+      List.fold_right
+        (fun xref uris ->
+          match lookup_uri xref with
+          | None -> uris
+          | Some uri -> uri :: uris)
+        !xref []
     in
-    uris := [];
+    let xmlattrs_uris, xmlattrs =
+      let xref_attrs, other_attrs =
+        List.partition
+          (function Some "xlink", "href", _ -> true | _ -> false)
+          xmlattrs
+      in
+      List.map (fun (_, _, uri) -> uri) xref_attrs,
+      other_attrs
+    in
+    let uris =
+      match xmlattrs_uris @ xref_uris with
+      | [] -> None
+      | uris ->
+          Some (String.concat " "
+            (HExtlib.list_uniq (List.sort String.compare uris)))
+    in
+    let xrefs =
+      match !xref with [] -> None | xrefs -> Some (String.concat " " xrefs)
+    in
+    xref := [];
     xmlattrs
     @ make_attributes [Some "helm", "xref"; Some "xlink", "href"]
-        [xref; ref uri]
+        [xrefs; uris]
+  in
+  let make_xref xref =
+    let xrefs =
+      match !xref with [] -> None | xrefs -> Some (String.concat " " xrefs)
+    in
+    xref := [];
+    make_attributes [Some "helm","xref"] [xrefs]
   in
-  let make_xref xref = make_attributes [Some "helm","xref"] [xref] in
   (* when mathonly is true no boxes should be generated, only mrows *)
   (* "xref" is  *)
-  let rec aux xmlattrs mathonly xref pos prec uris t =
+  let rec aux xmlattrs mathonly xref pos prec t =
     match t with
     | A.AttributedTerm _ ->
-        aux_attributes xmlattrs mathonly xref pos prec uris t
+        aux_attributes xmlattrs mathonly xref pos prec t
     | A.Num (literal, _) ->
         let attrs =
           (RenderingAttrs.number_attributes `MathML)
-          @ make_href xmlattrs xref uris
+          @ make_href xmlattrs xref
         in
         Mpres.Mn (attrs, literal)
     | A.Symbol (literal, _) ->
         let attrs =
           (RenderingAttrs.symbol_attributes `MathML)
-          @ make_href xmlattrs xref uris
+          @ make_href xmlattrs xref
         in
         Mpres.Mo (attrs, to_unicode literal)
     | A.Ident (literal, subst)
     | A.Uri (literal, subst) ->
         let attrs =
           (RenderingAttrs.ident_attributes `MathML)
-          @ make_href xmlattrs xref uris
+          @ make_href xmlattrs xref
         in
         let name = Mpres.Mi (attrs, to_unicode literal) in
         (match subst with
@@ -261,35 +286,23 @@ let render ids_to_uris =
                         box_of mathonly (A.H, false, false) [] [
                           Mpres.Mi ([], name);
                           Mpres.Mo ([], to_unicode "\\def");
-                          aux [] mathonly xref pos prec uris t ])
+                          aux [] mathonly xref pos prec t ])
                       substs))
                 @ [ closed_brace ])
-(*                 (CicNotationUtil.dress semicolon
-                  (List.map
-                    (fun (var, t) ->
-                      let var_uri = UriManager.uri_of_string var in
-                      let var_name = UriManager.name_of_uri var_uri in
-                      let href_attr = Some "xlink", "href", var in
-                      box_of mathonly (A.H, false, false) [] [
-                        Mpres.Mi ([href_attr], var_name);
-                        Mpres.Mo ([], to_unicode "\\def");
-                        aux [] mathonly xref pos prec uris t ])
-                    substs)) *)
             in
             let substs_maction = toggle_action [ hidden_substs; substs' ] in
             box_of mathonly (A.H, false, false) [] [ name; substs_maction ])
-    | A.Literal l -> aux_literal xmlattrs xref prec uris l
+    | A.Literal l -> aux_literal xmlattrs xref prec l
     | A.UserInput -> Mpres.Mtext ([], "%")
-    | A.Layout l -> aux_layout mathonly xref pos prec uris l
+    | A.Layout l -> aux_layout mathonly xref pos prec l
     | A.Magic _
     | A.Variable _ -> assert false  (* should have been instantiated *)
     | t ->
         prerr_endline ("unexpected ast: " ^ CicNotationPp.pp_term t);
         assert false
-  and aux_attributes xmlattrs mathonly xref pos prec uris t =
+  and aux_attributes xmlattrs mathonly xref pos prec t =
     let new_level = ref None in
-    let new_xref = ref None in
-    let new_uris = ref [] in
+    let new_xref = ref [] in
     let new_xmlattrs = ref [] in
     let rec aux_attribute =
       function
@@ -299,65 +312,58 @@ let render ids_to_uris =
           | `Raw _ -> ()
           | `Level (child_prec, child_assoc) ->
               new_level := Some (child_prec, child_assoc)
-          | `IdRef xref -> new_xref := Some xref
-          | `Href hrefs -> new_uris := hrefs
-          | `XmlAttrs attrs -> new_xmlattrs := attrs);
+          | `IdRef xref -> new_xref := xref :: !new_xref
+          | `XmlAttrs attrs -> new_xmlattrs := attrs @ !new_xmlattrs);
           aux_attribute t
       | t ->
           (match !new_level with
-          | None -> aux !new_xmlattrs mathonly new_xref pos prec new_uris t
+          | None -> aux !new_xmlattrs mathonly new_xref pos prec t
           | Some (child_prec, child_assoc) ->
               let t' = 
-                aux !new_xmlattrs mathonly new_xref pos child_prec new_uris t
+                aux !new_xmlattrs mathonly new_xref pos child_prec t
               in
               add_parens child_prec child_assoc pos prec t')
     in
     aux_attribute t
-(*     function
-    | `Loc _
-    | `Raw _ -> aux xmlattrs mathonly xref pos prec uris t
-    | `Level (child_prec, child_assoc) ->
-        let t' = aux xmlattrs mathonly xref pos child_prec uris t in
-        add_parens child_prec child_assoc pos prec t'
-    | `IdRef xref -> aux xmlattrs mathonly (Some xref) pos prec uris t
-    | `Href uris' -> aux xmlattrs mathonly xref pos prec uris' t
-    | `XmlAttrs xmlattrs -> aux xmlattrs mathonly xref pos prec uris t *)
-  and aux_literal xmlattrs xref prec uris l =
-    let attrs = make_href xmlattrs xref uris in
+  and aux_literal xmlattrs xref prec l =
+    let attrs = make_href xmlattrs xref in
     (match l with
     | `Symbol s -> Mpres.Mo (attrs, to_unicode s)
     | `Keyword s -> Mpres.Mo (attrs, to_unicode s)
     | `Number s  -> Mpres.Mn (attrs, to_unicode s))
-  and aux_layout mathonly xref pos prec uris l =
+  and aux_layout mathonly xref pos prec l =
     let attrs = make_xref xref in
-    let invoke' t = aux [] true (ref None) pos prec uris t in
+    let invoke' t = aux [] true (ref []) pos prec t in
       (* use the one below to reset precedence and associativity *)
-    let invoke_reinit t = aux [] true (ref None) `None 0 uris t in
+    let invoke_reinit t = aux [] true (ref []) `None 0 t in
     match l with
     | A.Sub (t1, t2) -> Mpres.Msub (attrs, invoke' t1, invoke_reinit t2)
     | A.Sup (t1, t2) -> Mpres.Msup (attrs, invoke' t1, invoke_reinit t2)
     | A.Below (t1, t2) -> Mpres.Munder (attrs, invoke' t1, invoke_reinit t2)
     | A.Above (t1, t2) -> Mpres.Mover (attrs, invoke' t1, invoke_reinit t2)
     | A.Frac (t1, t2)
-    | A.Over (t1, t2) -> Mpres.Mfrac (attrs, invoke_reinit t1, invoke_reinit t2)
+    | A.Over (t1, t2) ->
+        Mpres.Mfrac (attrs, invoke_reinit t1, invoke_reinit t2)
     | A.Atop (t1, t2) ->
-        Mpres.Mfrac(atop_attributes @ attrs, invoke_reinit t1, invoke_reinit t2)
+        Mpres.Mfrac (atop_attributes @ attrs, invoke_reinit t1,
+          invoke_reinit t2)
     | A.Sqrt t -> Mpres.Msqrt (attrs, invoke_reinit t)
-    | A.Root (t1, t2) -> Mpres.Mroot (attrs, invoke_reinit t1, invoke_reinit t2)
+    | A.Root (t1, t2) ->
+        Mpres.Mroot (attrs, invoke_reinit t1, invoke_reinit t2)
     | A.Box ((_, spacing, _) as kind, terms) ->
         let children =
-          aux_children mathonly spacing xref pos prec uris
+          aux_children mathonly spacing xref pos prec
             (CicNotationUtil.ungroup terms)
         in
         box_of mathonly kind attrs children
     | A.Group terms ->
        let children =
-          aux_children mathonly false xref pos prec uris
+          aux_children mathonly false xref pos prec
             (CicNotationUtil.ungroup terms)
         in
         box_of mathonly (A.H, false, false) attrs children
     | A.Break -> assert false (* TODO? *)
-  and aux_children mathonly spacing xref pos prec uris terms =
+  and aux_children mathonly spacing xref pos prec terms =
     let find_clusters =
       let rec aux_list first clusters acc =
        function
@@ -379,7 +385,7 @@ let render ids_to_uris =
                    | `Left -> `Inner
              in
                aux_list false clusters
-                  (aux [] mathonly xref pos' prec uris hd :: acc) []
+                  (aux [] mathonly xref pos' prec hd :: acc) []
          | hd :: tl ->
              let pos' =
                match pos, first with
@@ -391,7 +397,7 @@ let render ids_to_uris =
                  | `Inner, _ -> `Inner
              in
                aux_list false clusters
-                  (aux [] mathonly xref pos' prec uris hd :: acc) tl
+                  (aux [] mathonly xref pos' prec hd :: acc) tl
       in
        aux_list true [] []
     in
@@ -402,7 +408,7 @@ let render ids_to_uris =
     in
       List.map boxify_pres (find_clusters terms)
   in
-  aux [] false (ref None) `None 0 (ref [])
+  aux [] false (ref []) `None 0
 
 let rec print_box (t: boxml_markup) =
   Box.box2xml print_mpres t