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