src/Pure/Thy/present.ML
author wenzelm
Wed May 24 19:09:36 2000 +0200 (2000-05-24)
changeset 8965 d46b36785c70
parent 8808 204f4ebbba64
child 9044 28ee037278a6
permissions -rw-r--r--
proper token_translation for latex mode;
     1 (*  Title:      Pure/Thy/present.ML
     2     ID:         $Id$
     3     Author:     Stefan Berghofer and Markus Wenzel, TU Muenchen
     4     License:    GPL (GNU GENERAL PUBLIC LICENSE)
     5 
     6 Theory presentation (HTML, graph files, (PDF)LaTeX documents).
     7 *)
     8 
     9 signature BASIC_PRESENT =
    10 sig
    11   val section: string -> unit
    12 end;
    13 
    14 signature PRESENT =
    15 sig
    16   include BASIC_PRESENT
    17   val init: bool -> string -> string list -> string -> Path.T option -> Url.T option * bool -> unit
    18   val finish: unit -> unit
    19   val init_theory: string -> unit
    20   val verbatim_source: string -> (unit -> Symbol.symbol list) -> unit
    21   type token
    22   val basic_token: OuterLex.token -> token
    23   val markup_token: string * string -> token
    24   val markup_env_token: string * string -> token
    25   val verbatim_token: string -> token
    26   val old_symbol_source: string -> (unit -> Symbol.symbol list) -> unit
    27   val token_source: string -> (unit -> token list) -> unit
    28   val begin_theory: string -> string list -> (Path.T * bool) list -> theory -> theory
    29   val result: string -> string -> thm -> unit
    30   val results: string -> string -> thm list -> unit
    31   val theorem: string -> thm -> unit
    32   val theorems: string -> thm list -> unit
    33   val chapter: string -> unit
    34   val subsection: string -> unit
    35   val subsubsection: string -> unit
    36   val setup: (theory -> theory) list
    37   val get_info: theory -> {session: string list, is_local: bool}
    38 end;
    39 
    40 structure Present: PRESENT =
    41 struct
    42 
    43 
    44 (** paths **)
    45 
    46 val output_path = Path.variable "ISABELLE_BROWSER_INFO";
    47 
    48 fun top_path sess_path offset =
    49   Path.append (Path.appends (replicate (offset + length sess_path) Path.parent));
    50 
    51 val tex_ext = Path.ext "tex";
    52 val tex_path = tex_ext o Path.basic;
    53 val html_ext = Path.ext "html";
    54 val html_path = html_ext o Path.basic;
    55 val graph_path = Path.ext "graph" o Path.basic;
    56 val index_path = Path.basic "index.html";
    57 val doc_path = Path.basic "document";
    58 val doc_indexN = "session";
    59 
    60 val session_path = Path.basic ".session";
    61 val session_entries_path = Path.unpack ".session/entries";
    62 val pre_index_path = Path.unpack ".session/pre-index";
    63 
    64 
    65 
    66 (** additional theory data **)
    67 
    68 structure BrowserInfoArgs =
    69 struct
    70   val name = "Pure/browser_info";
    71   type T = {session: string list, is_local: bool};
    72 
    73   val empty = {session = [], is_local = false};
    74   val copy = I;
    75   fun prep_ext _ = empty;
    76   fun merge _ = empty;
    77   fun print _ _ = ();
    78 end;
    79 
    80 structure BrowserInfoData = TheoryDataFun(BrowserInfoArgs);
    81 
    82 fun get_info thy =
    83   if Theory.eq_thy (thy, ProtoPure.thy) then BrowserInfoArgs.empty
    84   else BrowserInfoData.get thy;
    85 
    86 
    87 
    88 (** graphs **)
    89 
    90 type graph_node =
    91   {name: string, ID: string, dir: string, unfold: bool,
    92    path: string, parents: string list};
    93 
    94 fun get_graph path = map (fn (a :: b :: c :: d :: e :: r) =>
    95   {name = unenclose a, ID = unenclose b,
    96    dir = unenclose c, unfold = (d = "+"),
    97    path = unenclose (if d = "+" then e else d),
    98    parents = map unenclose (fst (split_last (if d = "+" then tl r else r)))})
    99      (map (filter_out (equal "") o space_explode " ") (split_lines (File.read path)));
   100 
   101 fun put_graph gr path =
   102     File.write path (cat_lines (map (fn {name, ID, dir, unfold, path, parents} =>
   103       "\"" ^ name ^ "\" \"" ^ ID ^ "\" \"" ^ dir ^ (if unfold then "\" + \"" else "\" \"") ^
   104       path ^ "\" > " ^ space_implode " " (map quote parents) ^ " ;") gr));
   105 
   106 fun dir_of sess = space_implode "/" (case sess of
   107   [] => ["Pure"] | [x] => [x, "base"] | xs => xs);
   108 
   109 fun ID_of sess s = dir_of sess ^ "/" ^ s;
   110 
   111 
   112 (* retrieve graph data from theory loader database *)
   113 
   114 fun make_graph remote_path unfold curr_sess = map (fn name =>
   115   let
   116     val {session, is_local} = get_info (ThyInfo.theory name);
   117     val path' = Path.append (Path.make session) (html_path name)
   118   in
   119     {name = name, ID = ID_of session name, dir = dir_of session,
   120      path = if null session then "" else
   121             if is_some remote_path andalso not is_local then
   122               Url.pack (Url.append (the remote_path) (Url.file path'))
   123             else Path.pack (top_path curr_sess 2 path'),
   124      unfold = unfold andalso (length session = 1),
   125      parents = map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s)
   126        (ThyInfo.get_preds name)}
   127   end) (ThyInfo.names ());
   128 
   129 fun add_graph_entry' (entry as {ID, ...}) (gr : graph_node list) =
   130   filter_out (fn entry' => #ID entry' = ID) gr @ [entry];
   131 
   132 
   133 
   134 (** global browser info state **)
   135 
   136 (* type theory_info *)
   137 
   138 type theory_info = {tex_source: Buffer.T, html_source: Buffer.T, html: Buffer.T};
   139 
   140 fun make_theory_info (tex_source, html_source, html) =
   141   {tex_source = tex_source, html_source = html_source, html = html}: theory_info;
   142 
   143 val empty_theory_info = make_theory_info (Buffer.empty, Buffer.empty, Buffer.empty);
   144 
   145 fun map_theory_info f {tex_source, html_source, html} =
   146   make_theory_info (f (tex_source, html_source, html));
   147 
   148 
   149 (* type browser_info *)
   150 
   151 type browser_info = {theories: theory_info Symtab.table, tex_index: Buffer.T,
   152   html_index: Buffer.T, graph: graph_node list, all_graph: graph_node list};
   153 
   154 fun make_browser_info (theories, tex_index, html_index, graph, all_graph) =
   155   {theories = theories, tex_index = tex_index, html_index = html_index,
   156     graph = graph, all_graph = all_graph}: browser_info;
   157 
   158 val empty_browser_info = make_browser_info (Symtab.empty, Buffer.empty, Buffer.empty, [], []);
   159 
   160 fun initial_browser_info remote_path graph_path curr_sess = make_browser_info
   161   (Symtab.empty, Buffer.empty, Buffer.empty, make_graph remote_path false curr_sess,
   162    if_none (try get_graph graph_path) (make_graph remote_path true [""]));
   163 
   164 fun map_browser_info f {theories, tex_index, html_index, graph, all_graph} =
   165   make_browser_info (f (theories, tex_index, html_index, graph, all_graph));
   166 
   167 
   168 (* state *)
   169 
   170 val browser_info = ref empty_browser_info;
   171 
   172 fun change_browser_info f = browser_info := map_browser_info f (! browser_info);
   173 
   174 fun init_theory_info name info =
   175   change_browser_info (fn (theories, tex_index, html_index, graph, all_graph) =>
   176     (Symtab.update ((name, info), theories), tex_index, html_index, graph, all_graph));
   177 
   178 fun change_theory_info name f =
   179   change_browser_info (fn (info as (theories, tex_index, html_index, graph, all_graph)) =>
   180     (case Symtab.lookup (theories, name) of
   181       None => (warning ("Browser info: cannot access theory document " ^ quote name); info)
   182     | Some info => (Symtab.update ((name, map_theory_info f info), theories),
   183         tex_index, html_index, graph, all_graph)));
   184 
   185 
   186 fun add_tex_index txt =
   187   change_browser_info (fn (theories, tex_index, html_index, graph, all_graph) =>
   188     (theories, Buffer.add txt tex_index, html_index, graph, all_graph));
   189 
   190 fun add_html_index txt =
   191   change_browser_info (fn (theories, tex_index, html_index, graph, all_graph) =>
   192     (theories, tex_index, Buffer.add txt html_index, graph, all_graph));
   193 
   194 (*add entry to graph for current session*)
   195 fun add_graph_entry e =
   196   change_browser_info (fn (theories, tex_index, html_index, graph, all_graph) =>
   197     (theories, tex_index, html_index, add_graph_entry' e graph, all_graph));
   198 
   199 (*add entry to graph for all sessions*)
   200 fun add_all_graph_entry e =
   201   change_browser_info (fn (theories, tex_index, html_index, graph, all_graph) =>
   202     (theories, tex_index, html_index, graph, add_graph_entry' e all_graph));
   203 
   204 fun add_tex_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
   205   (Buffer.add txt tex_source, html_source, html));
   206 
   207 fun add_html_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
   208   (tex_source, Buffer.add txt html_source, html));
   209 
   210 fun add_html name txt = change_theory_info name (fn (tex_source, html_source, html) =>
   211   (tex_source, html_source, Buffer.add txt html));
   212 
   213 
   214 
   215 (** global session state **)
   216 
   217 (* session_info *)
   218 
   219 type session_info =
   220   {name: string, parent: string, session: string, path: string list, html_prefix: Path.T,
   221     doc_format: string, doc_prefixes: (Path.T * Path.T option) option, graph_path: Path.T,
   222     all_graph_path: Path.T, remote_path: Url.T option};
   223 
   224 fun make_session_info (name, parent, session, path, html_prefix, doc_format, doc_prefixes,
   225     graph_path, all_graph_path, remote_path) =
   226   {name = name, parent = parent, session = session, path = path, html_prefix = html_prefix,
   227     doc_format = doc_format, doc_prefixes = doc_prefixes, graph_path = graph_path,
   228     all_graph_path = all_graph_path, remote_path = remote_path}: session_info;
   229 
   230 
   231 (* state *)
   232 
   233 val session_info = ref (None: session_info option);
   234 
   235 fun with_session x f = (case ! session_info of None => x | Some info => f info);
   236 fun with_context f = f (PureThy.get_name (Context.the_context ()));
   237 
   238 
   239 
   240 (** HTML output **)
   241 
   242 (* maintain index *)
   243 
   244 val session_entries =
   245   HTML.session_entries o
   246     map (fn name => (Url.file (Path.append (Path.basic name) index_path), name));
   247 
   248 fun get_entries dir =
   249   split_lines (File.read (Path.append dir session_entries_path));
   250 
   251 fun put_entries entries dir =
   252   File.write (Path.append dir session_entries_path) (cat_lines entries);
   253 
   254 
   255 fun create_index dir =
   256   File.read (Path.append dir pre_index_path) ^
   257     session_entries (get_entries dir) ^ HTML.end_index
   258   |> File.write (Path.append dir index_path);
   259 
   260 fun update_index dir name =
   261   (case try get_entries dir of
   262     None => warning ("Browser info: cannot access session index of " ^ quote (Path.pack dir))
   263   | Some es => (put_entries ((es \ name) @ [name]) dir; create_index dir));
   264 
   265 
   266 (* init session *)
   267 
   268 fun name_of_session elems = space_implode "/" ("Isabelle" :: elems);
   269 
   270 fun could_copy inpath outpath =
   271   if File.exists inpath then (File.copy inpath outpath; true)
   272   else false;
   273 
   274 fun init false _ _ _ _ _ = (browser_info := empty_browser_info; session_info := None)
   275   | init true doc path name dump_path (remote_path, first_time) =
   276       let
   277         val parent_name = name_of_session (take (length path - 1, path));
   278         val session_name = name_of_session path;
   279         val sess_prefix = Path.make path;
   280 
   281         val out_path = Path.expand output_path;
   282         val html_prefix = Path.append out_path sess_prefix;
   283 
   284         val (doc_prefixes, document_path) =
   285           if doc = "" then (None, None)
   286           else (Some (Path.append html_prefix doc_path, dump_path), Some (Path.ext doc doc_path));
   287 
   288         val graph_prefix = Path.appends [out_path, Path.unpack "graph/data", sess_prefix];
   289         val graph_path = Path.append graph_prefix (Path.basic "session.graph");
   290         val graph_lnk = Url.file (top_path path 0 (Path.appends
   291           [Path.unpack "graph/data", sess_prefix, Path.basic "medium.html"]));
   292         val graph_up_lnk =
   293           (Url.file (top_path path 2 (Path.append sess_prefix index_path)), session_name);
   294         val codebase = Url.file (top_path path 1 Path.current);
   295         val all_graph_path = Path.appends [out_path, Path.unpack "graph/data",
   296           Path.basic (hd path), Path.basic "all_sessions.graph"];
   297 
   298         val _ = (File.mkdir html_prefix; File.mkdir graph_prefix);
   299 
   300         fun prep_readme readme =
   301           let val readme_path = Path.basic readme in
   302             if could_copy readme_path (Path.append html_prefix readme_path) then Some readme_path
   303             else None
   304           end;
   305         val opt_readme =
   306           (case prep_readme "README.html" of None => prep_readme "README" | some => some);
   307 
   308         val parent_index_path = Path.append Path.parent index_path;
   309         val index_up_lnk = if first_time then
   310             Url.append (the remote_path) (Url.file (Path.append sess_prefix parent_index_path))
   311           else Url.file parent_index_path;
   312 
   313         val index_text = HTML.begin_index (index_up_lnk, parent_name)
   314           (Url.file index_path, session_name) (apsome Url.file opt_readme)
   315             (apsome Url.file document_path) graph_lnk;
   316       in
   317         File.mkdir (Path.append html_prefix session_path);
   318         File.write (Path.append html_prefix session_entries_path) "";
   319         if is_some doc_prefixes then File.copy_all doc_path html_prefix else ();
   320         seq (fn (name, txt) => File.write (Path.append graph_prefix (Path.basic name)) txt)
   321           (HTML.applet_pages session_name codebase graph_up_lnk (length path = 1));
   322         session_info := Some (make_session_info (name, parent_name, session_name, path,
   323           html_prefix, doc, doc_prefixes, graph_path, all_graph_path, remote_path));
   324         browser_info := initial_browser_info remote_path all_graph_path path;
   325         add_html_index index_text
   326       end;
   327 
   328 
   329 (* finish session *)
   330 
   331 fun isatool_document doc_format doc_prefix =
   332   let
   333     val cmd = "$ISATOOL document -c -o '" ^ doc_format ^ "' '" ^ File.sysify_path doc_prefix ^ "'";
   334   in
   335     writeln cmd;
   336     if system cmd <> 0 orelse not (File.exists (Path.ext doc_format doc_prefix)) then
   337       error "Failed to build document"
   338     else ()
   339   end;
   340 
   341 fun write_tex src name path = Buffer.write (Path.append path (tex_path name)) src;
   342 
   343 fun write_texes src name (path, None) = write_tex src name path
   344   | write_texes src name (path, Some path') = (write_tex src name path; write_tex src name path');
   345 
   346 
   347 fun finish () = with_session ()
   348     (fn {name, html_prefix, doc_format, doc_prefixes, graph_path, all_graph_path, path, ...} =>
   349   let
   350     val {theories, tex_index, html_index, graph, all_graph} = ! browser_info;
   351 
   352     fun finish_node (a, {tex_source, html_source = _, html}) =
   353      (doc_prefixes |> apsome (write_texes tex_source a);
   354       Buffer.write (Path.append html_prefix (html_path a)) (Buffer.add HTML.end_theory html));
   355   in
   356     seq finish_node (Symtab.dest theories);
   357     Buffer.write (Path.append html_prefix pre_index_path) html_index;
   358     doc_prefixes |> apsome (write_texes tex_index doc_indexN);
   359     doc_prefixes |> apsome (isatool_document doc_format o #1);
   360     put_graph graph graph_path;
   361     put_graph all_graph all_graph_path;
   362     create_index html_prefix;
   363     update_index (Path.append html_prefix Path.parent) name;
   364     browser_info := empty_browser_info;
   365     session_info := None
   366   end);
   367 
   368 
   369 (* theory elements *)
   370 
   371 fun init_theory name = with_session () (fn _ => init_theory_info name empty_theory_info);
   372 
   373 fun verbatim_source name mk_text =
   374   with_session () (fn _ => add_html_source name (HTML.verbatim_source (mk_text ())));
   375 
   376 
   377 type token = Latex.token;
   378 val basic_token = Latex.Basic;
   379 val markup_token = Latex.Markup;
   380 val markup_env_token = Latex.MarkupEnv;
   381 val verbatim_token = Latex.Verbatim;
   382 
   383 fun old_symbol_source name mk_text =
   384   with_session () (fn _ => add_tex_source name (Latex.old_symbol_source name (mk_text ())));
   385 
   386 fun token_source name mk_toks =
   387   with_session () (fn _ => add_tex_source name (Latex.token_source (mk_toks ())));
   388 
   389 
   390 fun parent_link remote_path curr_session name =
   391   let
   392     val {session, is_local} = get_info (ThyInfo.theory name);
   393     val path = Path.append (Path.make session) (html_path name)
   394   in (if null session then None else
   395      Some (if is_some remote_path andalso not is_local then
   396        Url.append (the remote_path) (Url.file path)
   397      else Url.file (top_path curr_session 0 path)), name)
   398   end;
   399 
   400 fun begin_theory name raw_parents orig_files thy =
   401   with_session thy (fn {session, path, html_prefix, remote_path, ...} =>
   402   let
   403     val parents = map (parent_link remote_path path) raw_parents;
   404     val ml_path = ThyLoad.ml_path name;
   405     val files = map (apsnd Some) orig_files @
   406       (if is_some (ThyLoad.check_file ml_path) then [(ml_path, None)] else []);
   407 
   408     fun prep_file (raw_path, loadit) =
   409       (case ThyLoad.check_file raw_path of
   410         Some (path, _) =>
   411           let
   412             val base = Path.base path;
   413             val base_html = html_ext base;
   414           in
   415             File.write (Path.append html_prefix base_html)
   416               (HTML.ml_file (Url.file base) (File.read path));
   417             (Some (Url.file base_html), Url.file raw_path, loadit)
   418           end
   419       | None => (warning ("Browser info: expected to find ML file" ^ quote (Path.pack raw_path));
   420           (None, Url.file raw_path, loadit)));
   421 
   422     val files_html = map prep_file files;
   423 
   424     fun prep_html_source (tex_source, html_source, html) =
   425       let
   426         val txt = HTML.begin_theory (Url.file index_path, session)
   427           name parents files_html (Buffer.content html_source)
   428       in (tex_source, Buffer.empty, Buffer.add txt html) end;
   429 
   430     fun make_entry unfold all =
   431       {name = name, ID = ID_of path name, dir = dir_of path, unfold = unfold,
   432        path = Path.pack (top_path (if all then [""] else path) 2
   433          (Path.append (Path.make path) (html_path name))),
   434        parents = map (fn s => ID_of (#session (get_info (ThyInfo.theory s))) s) raw_parents};
   435 
   436   in
   437     change_theory_info name prep_html_source;
   438     add_all_graph_entry (make_entry (length path = 1) true);
   439     add_graph_entry (make_entry true false);
   440     add_html_index (HTML.theory_entry (Url.file (html_path name), name));
   441     add_tex_index (Latex.theory_entry name);
   442     BrowserInfoData.put {session = path, is_local = is_some remote_path} thy
   443   end);
   444 
   445 
   446 fun result k a thm = with_session () (fn _ => with_context add_html (HTML.result k a thm));
   447 fun results k a thms = with_session () (fn _ => with_context add_html (HTML.results k a thms));
   448 fun theorem a thm = with_session () (fn _ => with_context add_html (HTML.theorem a thm));
   449 fun theorems a thms = with_session () (fn _ => with_context add_html (HTML.theorems a thms));
   450 fun chapter s = with_session () (fn _ => with_context add_html (HTML.chapter s));
   451 fun section s = with_session () (fn _ => with_context add_html (HTML.section s));
   452 fun subsection s = with_session () (fn _ => with_context add_html (HTML.subsection s));
   453 fun subsubsection s = with_session () (fn _ => with_context add_html (HTML.subsubsection s));
   454 
   455 
   456 
   457 (** theory setup **)
   458 
   459 val setup = [BrowserInfoData.init];
   460 
   461 
   462 end;
   463 
   464 
   465 structure BasicPresent: BASIC_PRESENT = Present;
   466 open BasicPresent;