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