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