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