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