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