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