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