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/
30 type markup = string Box.box
32 let to_unicode = Utf8Macro.unicode_of_tex
34 let open_paren = Box.Text ([], "(")
35 let closed_paren = Box.Text ([], ")")
36 let semicolon = Box.Text ([], ";")
38 let toggle_action children =
39 Box.Action ([None, "actiontype", "toggle"], children)
41 type child_pos = [ `Left | `Right | `Inner ]
45 | Gramext.LeftA -> "LeftA"
46 | Gramext.RightA -> "RightA"
47 | Gramext.NonA -> "NonA"
49 let rec is_atomic = function
53 | Box.Object _ -> true
58 | Box.Action (_, [box]) -> is_atomic box
61 let add_parens child_prec curr_prec t =
63 ((*prerr_endline ("NOT adding parens around ATOMIC: "^
64 BoxPp.render_to_string (function x::_->x|_->assert false)
65 ~map_unicode_to_tex:false 80 t);*)t)
66 else if child_prec >= 0 && child_prec < curr_prec then
68 (*prerr_endline ("adding parens around: "^
69 BoxPp.render_to_string (function x::_->x|_->assert false)
70 ~map_unicode_to_tex:false 80 t);*)
71 Box.H ([], [ open_paren; t; closed_paren ])
73 ((*prerr_endline ("NOT adding parens around: "^
74 BoxPp.render_to_string (function x::_->x|_->assert false)
75 ~map_unicode_to_tex:false 80 t);*)t)
77 let render status ?(prec=(-1)) =
80 | A.AttributedTerm _ ->
81 aux_attributes [] "" prec t
82 | A.Num (literal, _) -> Box.Text ([], literal)
83 | A.Symbol (literal, _) -> Box.Text ([], literal)
84 | A.Ident (literal, _) -> Box.Text ([], to_unicode literal)
88 (NotationUtil.dress (Box.Text ([], ";"))
91 | None -> Box.Text ([], "_")
92 | Some t -> aux prec t) l)) @
96 (Box.Text ([], "?"^string_of_int n)::
97 (if (l <> []) then [Box.H ([],local_context)] else [])))
98 | A.Literal l -> aux_literal prec l
99 | A.UserInput -> Box.Text ([], "%")
100 | A.Layout l -> aux_layout prec l
102 | A.Variable _ -> assert false (* should have been instantiated *)
104 prerr_endline ("unexpected ast: " ^ NotationPp.pp_term status t);
107 and aux_attributes xmlattrs xref prec t =
108 let reset = ref false in
109 let inferred_level = ref None in
110 let expected_level = ref None in
111 let new_xref = ref [] in
112 let new_xmlattrs = ref [] in
113 let rec aux_attribute =
115 | A.AttributedTerm (attr, t) ->
119 | `Level (-1) -> reset := true
120 | `Level child_prec ->
121 (* assert (!expected_level = None); *)
122 expected_level := !inferred_level;
123 inferred_level := Some child_prec
124 | `IdRef xref -> new_xref := xref :: !new_xref
125 | `XmlAttrs attrs -> new_xmlattrs := attrs @ !new_xmlattrs);
129 match !expected_level with
133 (match !inferred_level with
136 let t' = aux child_prec t in
138 ("inferred: "^string_of_int child_prec^
139 " exp: "^string_of_int prec ^
140 " term: "^BoxPp.render_to_string ~map_unicode_to_tex:false
141 (function x::_->x|_->assert false) 80 t');*)
143 then ((*prerr_endline "reset";*)t')
144 else add_parens child_prec prec t')
146 (* prerr_endline (NotationPp.pp_term t); *)
149 and aux_literal prec l =
151 | `Symbol s -> Box.Text ([], to_unicode s)
152 | `Keyword s -> Box.Text ([], to_unicode s)
153 | `Number s -> Box.Text ([], to_unicode s))
155 and aux_layout prec l =
157 let invoke' t = aux prec t in
158 (* use the one below to reset precedence and associativity *)
159 let invoke_reinit t = aux ~-1 t in
161 | A.Sup (A.Layout (A.Sub (t1,t2)), t3)
162 | A.Sup (A.AttributedTerm (_,A.Layout (A.Sub (t1,t2))), t3)
164 | A.Sub (t1, t2) -> assert false
165 | A.Sup (t1, t2) -> assert false
166 | A.Below (t1, t2) -> assert false
167 | A.Above (t1, t2) -> assert false
169 | A.Over (t1, t2) -> assert false
170 | A.Atop (t1, t2) -> assert false
171 | A.InfRule (t1, t2, t3) -> assert false
172 | A.Sqrt t -> assert false
173 | A.Root (t1, t2) -> assert false
174 | A.Box (a, terms) ->
175 let children = List.map (aux prec) terms in
177 | A.Mstyle (l,terms) -> assert false
178 | A.Mpadded (l,terms) -> assert false
179 | A.Maction alternatives ->
180 toggle_action (List.map invoke_reinit alternatives)
181 | A.Group terms -> assert false
182 | A.Break -> assert false
186 (* let render_to_boxml id_to_uri t =
187 let xml_stream = print_box (box_of_mpres (render id_to_uri t)) in
188 Xml.add_xml_declaration xml_stream *)
190 let render_context_entry status name = function
193 [Box.Text([],name); Box.Text([],":");
194 render status (TermContentPres.pp_ast status t)])
197 [Box.Text([],name); Box.Text([],Utf8Macro.unicode_of_tex"\\def");
198 render status (TermContentPres.pp_ast status t)])
200 let render_context status context =
204 render_context_entry status (to_unicode name) entry) context)
206 let render_sequent status (i,context,ty) =
208 [render_context status context;
209 Box.Ink [None,"width","4cm"; None,"height","2px"];
210 render status (TermContentPres.pp_ast status ty)])