]> matita.cs.unibo.it Git - helm.git/blobdiff - helm/ocaml/cic_notation/cicNotationPres.ml
avoid generating useless parens in mathml contextes that do not require them (e.g...
[helm.git] / helm / ocaml / cic_notation / cicNotationPres.ml
index 8d548e1ebf641c72b034589aead5edb8bd485a1a..c4679bef7adb95e1f20ebd4702ce4e0f86d2ddbd 100644 (file)
@@ -35,13 +35,6 @@ let atop_attributes = [None, "linethickness", "0pt"]
 
 let to_unicode = Utf8Macro.unicode_of_tex
 
-(* let rec make_attributes l1 = function
-  | [] -> []
-  | None :: tl -> make_attributes (List.tl l1) tl
-  | Some s :: tl ->
-      let p,n = List.hd l1 in
-      prerr_endline (Printf.sprintf "make_attributes %s %s" n s);
-      (p,n,s) :: make_attributes (List.tl l1) tl *)
 let rec make_attributes l1 = function
   | [] -> []
   | hd :: tl ->
@@ -49,18 +42,21 @@ let rec make_attributes l1 = function
       | None -> make_attributes (List.tl l1) tl
       | Some s ->
           let p,n = List.hd l1 in
-(*           prerr_endline (Printf.sprintf "make_attributes %s %s" n s); *)
           hd := None;
           (p,n,s) :: make_attributes (List.tl l1) tl)
 
 let box_of_mpres =
   function
-    Mpresentation.Mobject (_, box) -> box
+  | Mpresentation.Mobject (attrs, box) ->
+      assert (attrs = []);
+      box
   | mpres -> Box.Object ([], mpres)
 
 let mpres_of_box =
   function
-    Box.Object (_, mpres) -> mpres
+  | Box.Object (attrs, mpres) ->
+      assert (attrs = []);
+      mpres
   | box -> Mpresentation.Mobject ([], box)
 
 let rec genuine_math =
@@ -86,9 +82,19 @@ let rec promote_to_math =
 let small_skip =
   Mpresentation.Mspace (RenderingAttrs.small_skip_attributes `MathML)
 
+let rec add_mpres_attributes new_attr = function
+  | Mpresentation.Mobject (attr, box) ->
+      Mpresentation.Mobject (attr, add_box_attributes new_attr box)
+  | mpres ->
+      Mpresentation.set_attr (new_attr @ Mpresentation.get_attr mpres) mpres
+and add_box_attributes new_attr = function
+  | Box.Object (attr, mpres) ->
+      Box.Object (attr, add_mpres_attributes new_attr mpres)
+  | box -> Box.set_attr (new_attr @ Box.get_attr box) box
+
 let box_of mathonly spec attrs children =
   match children with
-    | [t] -> t
+    | [t] -> add_mpres_attributes attrs t
     | _ ->
        let kind, spacing, indent = spec in
        let dress children =
@@ -239,7 +245,7 @@ let render ids_to_uris =
     | A.Uri (literal, subst) ->
         let attrs =
           (RenderingAttrs.ident_attributes `MathML)
-          @ make_href xmlattrs xref (ref [])
+          @ make_href xmlattrs xref uris
         in
         let name = Mpres.Mi (attrs, to_unicode literal) in
         (match subst with
@@ -325,17 +331,19 @@ let render ids_to_uris =
   and aux_layout mathonly xref pos prec uris l =
     let attrs = make_xref xref in
     let invoke' t = aux [] true (ref None) pos prec uris t in
+      (* use the one below to reset precedence and associativity *)
+    let invoke_reinit t = aux [] true (ref None) `None 0 uris t in
     match l with
-    | A.Sub (t1, t2) -> Mpres.Msub (attrs, invoke' t1, invoke' t2)
-    | A.Sup (t1, t2) -> Mpres.Msup (attrs, invoke' t1, invoke' t2)
-    | A.Below (t1, t2) -> Mpres.Munder (attrs, invoke' t1, invoke' t2)
-    | A.Above (t1, t2) -> Mpres.Mover (attrs, invoke' t1, invoke' t2)
+    | 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' t1, invoke' t2)
+    | A.Over (t1, t2) -> Mpres.Mfrac (attrs, invoke_reinit t1, invoke_reinit t2)
     | A.Atop (t1, t2) ->
-        Mpres.Mfrac (atop_attributes @ attrs, invoke' t1, invoke' t2)
-    | A.Sqrt t -> Mpres.Msqrt (attrs, invoke' t)
-    | A.Root (t1, t2) -> Mpres.Mroot (attrs, invoke' t1, invoke' 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.Box ((_, spacing, _) as kind, terms) ->
         let children =
           aux_children mathonly spacing xref pos prec uris