src/Pure/Thy/present.ML
author wenzelm
Wed Mar 13 14:57:16 2013 +0100 (2013-03-13)
changeset 51415 8a33d581718b
parent 51401 f390b59b4b4a
child 51419 5313abe76bd4
permissions -rw-r--r--
show expanded path, to avoid odd /foo/bar/$ISABELLE_BROWSER_INFO/baz;
     1 (*  Title:      Pure/Thy/present.ML
     2     Author:     Markus Wenzel and Stefan Berghofer, TU Muenchen
     3 
     4 Theory presentation: HTML, graph files, (PDF)LaTeX documents.
     5 *)
     6 
     7 signature BASIC_PRESENT =
     8 sig
     9   val no_document: ('a -> 'b) -> 'a -> 'b  (*not thread-safe!*)
    10 end;
    11 
    12 signature PRESENT =
    13 sig
    14   include BASIC_PRESENT
    15   val session_name: theory -> string
    16   val read_variant: string -> string * string
    17   val init: bool -> bool -> Path.T -> string -> bool -> string -> (string * string) list ->
    18     string * string -> bool * string -> bool -> theory list -> unit  (*not thread-safe!*)
    19   val finish: unit -> unit  (*not thread-safe!*)
    20   val init_theory: string -> unit
    21   val theory_source: string -> (unit -> HTML.text) -> unit
    22   val theory_output: string -> string -> unit
    23   val begin_theory: int -> Path.T -> theory -> theory
    24   val drafts: string -> Path.T list -> Path.T
    25 end;
    26 
    27 structure Present: PRESENT =
    28 struct
    29 
    30 
    31 (** paths **)
    32 
    33 val tex_ext = Path.ext "tex";
    34 val tex_path = tex_ext o Path.basic;
    35 val html_ext = Path.ext "html";
    36 val html_path = html_ext o Path.basic;
    37 val index_path = Path.basic "index.html";
    38 val readme_html_path = Path.basic "README.html";
    39 val readme_path = Path.basic "README";
    40 val documentN = "document";
    41 val document_path = Path.basic documentN;
    42 val doc_indexN = "session";
    43 val graph_path = Path.basic "session.graph";
    44 val graph_pdf_path = Path.basic "session_graph.pdf";
    45 val graph_eps_path = Path.basic "session_graph.eps";
    46 
    47 fun show_path path = Path.implode (Path.expand (Path.append (File.pwd ()) path));
    48 
    49 
    50 
    51 (** additional theory data **)
    52 
    53 structure Browser_Info = Theory_Data
    54 (
    55   type T = {chapter: string, name: string};
    56   val empty = {chapter = Context.PureN, name = Context.PureN}: T;
    57   fun extend _ = empty;
    58   fun merge _ = empty;
    59 );
    60 
    61 val session_name = #name o Browser_Info.get;
    62 val session_chapter_name = (fn {chapter, name} => [chapter, name]) o Browser_Info.get;
    63 
    64 
    65 
    66 (** graphs **)
    67 
    68 fun ID_of sess s = space_implode "/" (sess @ [s]);
    69 fun ID_of_thy thy = ID_of (session_chapter_name thy) (Context.theory_name thy);
    70 
    71 fun theory_link (curr_chapter, curr_session) thy =
    72   let
    73     val {chapter, name = session} = Browser_Info.get thy;
    74     val link = html_path (Context.theory_name thy);
    75   in
    76     if curr_session = session then SOME link
    77     else if curr_chapter = chapter then
    78       SOME (Path.appends [Path.parent, Path.basic session, link])
    79     else if chapter = Context.PureN then NONE
    80     else SOME (Path.appends [Path.parent, Path.parent, Path.basic chapter, Path.basic session, link])
    81   end;
    82 
    83 (*retrieve graph data from initial collection of theories*)
    84 fun init_graph (curr_chapter, curr_session) = rev o map (fn thy =>
    85   let
    86     val {chapter, name = session_name} = Browser_Info.get thy;
    87     val thy_name = Context.theory_name thy;
    88     val path =
    89       (case theory_link (curr_chapter, curr_session) thy of
    90         NONE => ""
    91       | SOME p => Path.implode p);
    92     val entry =
    93      {name = thy_name,
    94       ID = ID_of [chapter, session_name] thy_name,
    95       dir = session_name,
    96       path = path,
    97       unfold = false,
    98       parents = map ID_of_thy (Theory.parents_of thy),
    99       content = []};
   100   in (0, entry) end);
   101 
   102 fun ins_graph_entry (i, entry as {ID, ...}) (gr: (int * Graph_Display.node) list) =
   103   (i, entry) :: filter_out (fn (_, entry') => #ID entry' = ID) gr;
   104 
   105 
   106 
   107 (** global browser info state **)
   108 
   109 (* type theory_info *)
   110 
   111 type theory_info = {tex_source: Buffer.T, html_source: Buffer.T, html: Buffer.T};
   112 
   113 fun make_theory_info (tex_source, html_source, html) =
   114   {tex_source = tex_source, html_source = html_source, html = html}: theory_info;
   115 
   116 val empty_theory_info = make_theory_info (Buffer.empty, Buffer.empty, Buffer.empty);
   117 
   118 fun map_theory_info f {tex_source, html_source, html} =
   119   make_theory_info (f (tex_source, html_source, html));
   120 
   121 
   122 (* type browser_info *)
   123 
   124 type browser_info = {theories: theory_info Symtab.table, files: (Path.T * string) list,
   125   tex_index: (int * string) list, html_index: (int * string) list,
   126   graph: (int * Graph_Display.node) list};
   127 
   128 fun make_browser_info (theories, files, tex_index, html_index, graph) =
   129   {theories = theories, files = files, tex_index = tex_index, html_index = html_index,
   130     graph = graph}: browser_info;
   131 
   132 val empty_browser_info = make_browser_info (Symtab.empty, [], [], [], []);
   133 
   134 fun init_browser_info session thys =
   135   make_browser_info (Symtab.empty, [], [], [], init_graph session thys);
   136 
   137 fun map_browser_info f {theories, files, tex_index, html_index, graph} =
   138   make_browser_info (f (theories, files, tex_index, html_index, graph));
   139 
   140 
   141 (* state *)
   142 
   143 val browser_info = Unsynchronized.ref empty_browser_info;
   144 fun change_browser_info f =
   145   CRITICAL (fn () => Unsynchronized.change browser_info (map_browser_info f));
   146 
   147 val suppress_tex_source = Unsynchronized.ref false;
   148 fun no_document f x = Unsynchronized.setmp suppress_tex_source true f x;
   149 
   150 fun init_theory_info name info =
   151   change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
   152     (Symtab.update (name, info) theories, files, tex_index, html_index, graph));
   153 
   154 fun change_theory_info name f =
   155   change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
   156     (case Symtab.lookup theories name of
   157       NONE => error ("Browser info: cannot access theory document " ^ quote name)
   158     | SOME info => (Symtab.update (name, map_theory_info f info) theories, files,
   159         tex_index, html_index, graph)));
   160 
   161 
   162 fun add_file file =
   163   change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
   164     (theories, file :: files, tex_index, html_index, graph));
   165 
   166 fun add_tex_index txt =
   167   change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
   168     (theories, files, txt :: tex_index, html_index, graph));
   169 
   170 fun add_html_index txt =
   171   change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
   172     (theories, files, tex_index, txt :: html_index, graph));
   173 
   174 fun add_graph_entry entry =
   175   change_browser_info (fn (theories, files, tex_index, html_index, graph) =>
   176     (theories, files, tex_index, html_index, ins_graph_entry entry graph));
   177 
   178 fun add_tex_source name txt =
   179   if ! suppress_tex_source then ()
   180   else change_theory_info name (fn (tex_source, html_source, html) =>
   181     (Buffer.add txt tex_source, html_source, html));
   182 
   183 fun add_html_source name txt = change_theory_info name (fn (tex_source, html_source, html) =>
   184   (tex_source, Buffer.add txt html_source, html));
   185 
   186 
   187 
   188 (** global session state **)
   189 
   190 (* session_info *)
   191 
   192 type session_info =
   193   {name: string, chapter: string, info_path: Path.T,
   194     info: bool, doc_format: string, doc_graph: bool, doc_output: Path.T option,
   195     documents: (string * string) list, doc_dump: (bool * string), verbose: bool,
   196     readme: Path.T option};
   197 
   198 fun make_session_info
   199   (name, chapter, info_path, info, doc_format, doc_graph, doc_output,
   200     documents, doc_dump, verbose, readme) =
   201   {name = name, chapter = chapter, info_path = info_path, info = info,
   202     doc_format = doc_format, doc_graph = doc_graph, doc_output = doc_output,
   203     documents = documents, doc_dump = doc_dump, verbose = verbose,
   204     readme = readme}: session_info;
   205 
   206 
   207 (* state *)
   208 
   209 val session_info = Unsynchronized.ref (NONE: session_info option);
   210 
   211 fun with_session_info x f = (case ! session_info of NONE => x | SOME info => f info);
   212 
   213 
   214 
   215 (** document preparation **)
   216 
   217 (* document variants *)
   218 
   219 fun read_variant str =
   220   (case space_explode "=" str of
   221     [name] => (name, "")
   222   | [name, tags] => (name, tags)
   223   | _ => error ("Malformed document variant specification: " ^ quote str));
   224 
   225 
   226 (* init session *)
   227 
   228 fun init build info info_path doc doc_graph document_output doc_variants
   229     (chapter, name) (doc_dump as (_, dump_prefix)) verbose thys =
   230   if not build andalso not info andalso doc = "" andalso dump_prefix = "" then
   231     (browser_info := empty_browser_info; session_info := NONE)
   232   else
   233     let
   234       val doc_output =
   235         if document_output = "" then NONE else SOME (Path.explode document_output);
   236 
   237       val documents =
   238         if doc = "" then []
   239         else if not (can File.check_dir document_path) then
   240           (if verbose then Output.physical_stderr "Warning: missing document directory\n"
   241            else (); [])
   242         else doc_variants;
   243 
   244       val readme =
   245         if File.exists readme_html_path then SOME readme_html_path
   246         else if File.exists readme_path then SOME readme_path
   247         else NONE;
   248 
   249       val docs =
   250         (case readme of NONE => [] | SOME p => [(Url.File p, "README")]) @
   251           map (fn (name, _) => (Url.File (Path.ext doc (Path.basic name)), name)) documents;
   252     in
   253       session_info :=
   254         SOME (make_session_info (name, chapter, info_path, info, doc,
   255           doc_graph, doc_output, documents, doc_dump, verbose, readme));
   256       browser_info := init_browser_info (chapter, name) thys;
   257       add_html_index (0, HTML.begin_session_index name docs (Url.explode "medium.html"))
   258     end;
   259 
   260 
   261 (* isabelle tool wrappers *)
   262 
   263 fun isabelle_document {verbose, purge} format name tags dir =
   264   let
   265     val s = "\"$ISABELLE_TOOL\" document" ^ (if purge then " -c" else "") ^ " -o '" ^ format ^ "' \
   266       \-n '" ^ name ^ "' -t '" ^ tags ^ "' " ^ File.shell_path dir ^ " 2>&1";
   267     val doc_path = Path.appends [dir, Path.parent, Path.basic name |> Path.ext format];
   268     val _ = if verbose then writeln s else ();
   269     val (out, rc) = Isabelle_System.bash_output s;
   270     val _ =
   271       if not (File.exists doc_path) orelse rc <> 0 then
   272         cat_error out ("Failed to build document " ^ quote (show_path doc_path))
   273       else if verbose then writeln out
   274       else ();
   275   in doc_path end;
   276 
   277 fun isabelle_browser graph = Isabelle_System.with_tmp_dir "browser" (fn dir =>
   278   let
   279     val pdf_path = Path.append dir graph_pdf_path;
   280     val eps_path = Path.append dir graph_eps_path;
   281     val graph_path = Path.append dir graph_path;
   282     val _ = Graph_Display.write_graph_browser graph_path graph;
   283     val args = "-o " ^ File.shell_path pdf_path ^ " " ^ File.shell_path graph_path;
   284   in
   285     if Isabelle_System.isabelle_tool "browser" args = 0 andalso
   286       File.exists pdf_path andalso File.exists eps_path
   287     then (File.read pdf_path, File.read eps_path)
   288     else error "Failed to prepare dependency graph"
   289   end);
   290 
   291 
   292 (* finish session -- output all generated text *)
   293 
   294 fun sorted_index index = map snd (sort (int_ord o pairself fst) (rev index));
   295 fun index_buffer index = Buffer.add (implode (sorted_index index)) Buffer.empty;
   296 
   297 fun write_tex src name path =
   298   File.write_buffer (Path.append path (tex_path name)) src;
   299 
   300 fun write_tex_index tex_index path =
   301   write_tex (index_buffer tex_index |> Buffer.add Latex.tex_trailer) doc_indexN path;
   302 
   303 
   304 fun finish () =
   305   with_session_info () (fn {name, chapter, info, info_path, doc_format, doc_graph, doc_output,
   306     documents, doc_dump = (dump_copy, dump_prefix), verbose, readme, ...} =>
   307   let
   308     val {theories, files, tex_index, html_index, graph} = ! browser_info;
   309     val thys = Symtab.dest theories;
   310 
   311     val chapter_prefix = Path.append info_path (Path.basic chapter);
   312     val session_prefix = Path.append chapter_prefix (Path.basic name);
   313 
   314     fun finish_html (a, {html, ...}: theory_info) =
   315       File.write_buffer (Path.append session_prefix (html_path a))
   316         (Buffer.add HTML.end_document html);
   317 
   318     val sorted_graph = sorted_index graph;
   319     val opt_graphs =
   320       if doc_graph andalso (not (null documents) orelse dump_prefix <> "") then
   321         SOME (isabelle_browser sorted_graph)
   322       else NONE;
   323 
   324     val _ =
   325       if info then
   326        (Isabelle_System.mkdirs session_prefix;
   327         File.write_buffer (Path.append session_prefix index_path)
   328           (index_buffer html_index |> Buffer.add HTML.end_document);
   329         (case readme of NONE => () | SOME path => File.copy path session_prefix);
   330         Graph_Display.write_graph_browser (Path.append session_prefix graph_path) sorted_graph;
   331         Isabelle_System.isabelle_tool "browser" "-b";
   332         File.copy (Path.explode "~~/lib/browser/GraphBrowser.jar") session_prefix;
   333         List.app (fn (a, txt) => File.write (Path.append session_prefix (Path.basic a)) txt)
   334           (HTML.applet_pages name (Url.File index_path, name));
   335         File.copy (Path.explode "~~/etc/isabelle.css") session_prefix;
   336         List.app finish_html thys;
   337         List.app (uncurry File.write) files;
   338         if verbose then Output.physical_stderr ("Browser info at " ^ show_path session_prefix ^ "\n")
   339         else ())
   340       else ();
   341 
   342     fun prepare_sources doc_copy doc_dir =
   343      (Isabelle_System.mkdirs doc_dir;
   344       if doc_copy then Isabelle_System.copy_dir document_path doc_dir else ();
   345       Isabelle_System.isabelle_tool "latex"
   346         ("-o sty " ^ File.shell_path (Path.append doc_dir (Path.basic "root.tex")));
   347       (case opt_graphs of NONE => () | SOME (pdf, eps) =>
   348         (File.write (Path.append doc_dir graph_pdf_path) pdf;
   349           File.write (Path.append doc_dir graph_eps_path) eps));
   350       write_tex_index tex_index doc_dir;
   351       List.app (fn (a, {tex_source, ...}) => write_tex tex_source a doc_dir) thys);
   352 
   353     val _ =
   354       if dump_prefix = "" then ()
   355       else
   356         let
   357           val path = Path.explode dump_prefix;
   358           val _ = prepare_sources dump_copy path;
   359         in
   360           if verbose then
   361             Output.physical_stderr ("Document sources at " ^ show_path path ^ "\n")
   362           else ()
   363         end;
   364 
   365     fun document_job doc_prefix backdrop (name, tags) =
   366       let
   367         val _ =
   368           File.eq (document_path, doc_prefix) andalso
   369             error ("Overlap of document input and output directory " ^ Path.print doc_prefix);
   370         val dir = Path.append doc_prefix (Path.basic name);
   371         val copy = not (File.eq (document_path, dir));
   372         val _ = prepare_sources copy dir;
   373         fun inform doc =
   374           if verbose orelse not backdrop then
   375             Output.physical_stderr ("Document at " ^ show_path doc ^ "\n")
   376           else ();
   377       in
   378         fn () =>
   379           (isabelle_document {verbose = true, purge = backdrop} doc_format name tags dir, inform)
   380       end;
   381 
   382     val jobs =
   383       (if info orelse is_none doc_output then
   384         map (document_job session_prefix true) documents
   385        else []) @
   386       (case doc_output of
   387         NONE => []
   388       | SOME path => map (document_job path false) documents);
   389 
   390     val _ = jobs |> Par_List.map (fn job => job ()) |> List.app (op |>);
   391   in
   392     browser_info := empty_browser_info;
   393     session_info := NONE
   394   end);
   395 
   396 
   397 (* theory elements *)
   398 
   399 fun init_theory name = with_session_info () (fn _ => init_theory_info name empty_theory_info);
   400 
   401 fun theory_source name mk_text =
   402   with_session_info () (fn _ => add_html_source name (HTML.theory_source (mk_text ())));
   403 
   404 fun theory_output name s =
   405   with_session_info () (fn _ => add_tex_source name (Latex.isabelle_file name s));
   406 
   407 
   408 fun begin_theory update_time dir thy =
   409     with_session_info thy (fn {name = session_name, chapter, info_path, ...} =>
   410   let
   411     val name = Context.theory_name thy;
   412     val parents = Theory.parents_of thy;
   413     val parent_specs = parents |> map (fn parent =>
   414       (Option.map Url.File (theory_link (chapter, session_name) parent),
   415         (Context.theory_name parent)));
   416 
   417     val files = [];  (* FIXME *)
   418     val files_html = files |> map (fn (raw_path, loadit) =>
   419       let
   420         val path = File.check_file (File.full_path dir raw_path);
   421         val base = Path.base path;
   422         val base_html = html_ext base;
   423         (* FIXME retain file path!? *)
   424         val session_prefix = Path.appends [info_path, Path.basic chapter, Path.basic name];
   425         val _ =
   426           add_file (Path.append session_prefix base_html,
   427             HTML.external_file (Url.File base) (File.read path));
   428       in (Url.File base_html, Url.File raw_path, loadit) end);
   429 
   430     fun prep_html_source (tex_source, html_source, html) =
   431       let val txt = HTML.begin_theory name parent_specs files_html (Buffer.content html_source)
   432       in (tex_source, Buffer.empty, Buffer.add txt html) end;
   433 
   434     val entry =
   435      {name = name,
   436       ID = ID_of [chapter, session_name] name,
   437       dir = session_name,
   438       unfold = true,
   439       path = Path.implode (html_path name),
   440       parents = map ID_of_thy parents,
   441       content = []};
   442   in
   443     change_theory_info name prep_html_source;
   444     add_graph_entry (update_time, entry);
   445     add_html_index (update_time, HTML.theory_entry (Url.File (html_path name), name));
   446     add_tex_index (update_time, Latex.theory_entry name);
   447     Browser_Info.put {chapter = chapter, name = session_name} thy
   448   end);
   449 
   450 
   451 
   452 (** draft document output **)
   453 
   454 fun drafts doc_format src_paths = Isabelle_System.with_tmp_dir "drafts" (fn dir =>
   455   let
   456     fun prep_draft path i =
   457       let
   458         val base = Path.base path;
   459         val name =
   460           (case Path.implode (#1 (Path.split_ext base)) of
   461             "" => "DUMMY"
   462           | s => s)  ^ serial_string ();
   463       in
   464         if File.exists path then
   465           (((name, base, File.read path), (i, Latex.theory_entry name)), i + 1)
   466         else error ("Bad file: " ^ Path.print path)
   467       end;
   468     val (srcs, tex_index) = split_list (fst (fold_map prep_draft src_paths 0));
   469 
   470     val doc_path = Path.append dir document_path;
   471     val _ = Isabelle_System.mkdirs doc_path;
   472     val root_path = Path.append doc_path (Path.basic "root.tex");
   473     val _ = File.copy (Path.explode "~~/lib/texinputs/draft.tex") root_path;
   474     val _ = Isabelle_System.isabelle_tool "latex" ("-o sty " ^ File.shell_path root_path);
   475     val _ = Isabelle_System.isabelle_tool "latex" ("-o syms " ^ File.shell_path root_path);
   476 
   477     fun known name =
   478       let val ss = split_lines (File.read (Path.append doc_path (Path.basic name)))
   479       in member (op =) ss end;
   480     val known_syms = known "syms.lst";
   481     val known_ctrls = known "ctrls.lst";
   482 
   483     val _ = srcs |> List.app (fn (name, base, txt) =>
   484       Symbol.explode txt
   485       |> Latex.symbol_source (known_syms, known_ctrls) (Path.implode base)
   486       |> File.write (Path.append doc_path (tex_path name)));
   487     val _ = write_tex_index tex_index doc_path;
   488 
   489     val result =
   490       isabelle_document {verbose = false, purge = true} doc_format documentN "" doc_path;
   491     val result' = Isabelle_System.create_tmp_path documentN doc_format;
   492     val _ = File.copy result result';
   493   in result' end);
   494 
   495 end;
   496 
   497 structure Basic_Present: BASIC_PRESENT = Present;
   498 open Basic_Present;