src/Pure/Thy/present.scala
author wenzelm
Thu, 12 Nov 2020 16:27:31 +0100
changeset 72599 76550282267f
parent 72595 c806eeb9138c
child 72600 2fa4f25d9d07
permissions -rw-r--r--
more interrupts, notably for running latex;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/present.scala
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     3
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     4
Theory presentation: HTML.
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     5
*/
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     6
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     7
package isabelle
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     8
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
     9
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    10
import java.io.{File => JFile}
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    11
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    12
import scala.collection.immutable.SortedMap
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    13
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    14
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    15
object Present
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    16
{
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    17
  /* maintain chapter index -- NOT thread-safe */
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    18
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    19
  private val sessions_path = Path.basic(".sessions")
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    20
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    21
  private def read_sessions(dir: Path): List[(String, String)] =
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    22
  {
51416
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    23
    val path = dir + sessions_path
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    24
    if (path.is_file) {
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    25
      import XML.Decode._
65344
b99283eed13c clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents: 65089
diff changeset
    26
      list(pair(string, string))(Symbol.decode_yxml(File.read(path)))
51416
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    27
    }
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
    28
    else Nil
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    29
  }
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    30
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    31
  private def write_sessions(dir: Path, sessions: List[(String, String)])
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    32
  {
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    33
    import XML.Encode._
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    34
    File.write(dir + sessions_path, YXML.string_of_body(list(pair(string, string))(sessions)))
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    35
  }
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    36
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    37
  def update_chapter_index(browser_info: Path, chapter: String, new_sessions: List[(String, String)])
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    38
  {
72376
04bce3478688 clarified signature;
wenzelm
parents: 72375
diff changeset
    39
    val dir = Isabelle_System.make_directory(browser_info + Path.basic(chapter))
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    40
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    41
    val sessions0 =
51406
950b897f95bb proper path -- I/O was hidden due to permissiveness;
wenzelm
parents: 51402
diff changeset
    42
      try { read_sessions(dir) }
51987
7d8e0e3c553b tuned signature;
wenzelm
parents: 51416
diff changeset
    43
      catch { case _: XML.Error => Nil }
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    44
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    45
    val sessions = (SortedMap.empty[String, String] ++ sessions0 ++ new_sessions).toList
65988
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
    46
    write_sessions(dir, sessions)
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    47
65988
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
    48
    val title = "Isabelle/" + chapter + " sessions"
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
    49
    HTML.write_document(dir, "index.html",
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
    50
      List(HTML.title(title + " (" + Distribution.version + ")")),
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
    51
      HTML.chapter(title) ::
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
    52
       (if (sessions.isEmpty) Nil
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
    53
        else
65992
50daca61efd6 tuned signature;
wenzelm
parents: 65989
diff changeset
    54
          List(HTML.div("sessions",
50daca61efd6 tuned signature;
wenzelm
parents: 65989
diff changeset
    55
            List(HTML.description(
65988
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
    56
              sessions.map({ case (name, description) =>
69318
f3351bb4390e clarified presentation;
wenzelm
parents: 69255
diff changeset
    57
                val descr = Symbol.trim_blank_lines(description)
65989
68cd15585f46 tuned layout;
wenzelm
parents: 65988
diff changeset
    58
                (List(HTML.link(name + "/index.html", HTML.text(name))),
69318
f3351bb4390e clarified presentation;
wenzelm
parents: 69255
diff changeset
    59
                  if (descr == "") Nil
f3351bb4390e clarified presentation;
wenzelm
parents: 69255
diff changeset
    60
                  else HTML.break ::: List(HTML.pre(HTML.text(descr)))) })))))))
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
    61
  }
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    62
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    63
  def make_global_index(browser_info: Path)
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    64
  {
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    65
    if (!(browser_info + Path.explode("index.html")).is_file) {
72375
e48d93811ed7 clarified signature;
wenzelm
parents: 72309
diff changeset
    66
      Isabelle_System.make_directory(browser_info)
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    67
      File.copy(Path.explode("~~/lib/logo/isabelle.gif"),
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    68
        browser_info + Path.explode("isabelle.gif"))
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    69
      File.write(browser_info + Path.explode("index.html"),
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    70
        File.read(Path.explode("~~/lib/html/library_index_header.template")) +
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    71
        File.read(Path.explode("~~/lib/html/library_index_content.template")) +
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    72
        File.read(Path.explode("~~/lib/html/library_index_footer.template")))
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    73
    }
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    74
  }
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    75
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    76
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    77
  /* finish session */
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    78
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    79
  def finish(
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    80
    browser_info: Path,
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
    81
    graph_pdf: Bytes,
62631
c39614ddb80b clarified modules;
wenzelm
parents: 61500
diff changeset
    82
    info: Sessions.Info,
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    83
    name: String)
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    84
  {
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    85
    val session_prefix = browser_info + Path.basic(info.chapter) + Path.basic(name)
65089
1d219d76873b clarified fonts;
wenzelm
parents: 65083
diff changeset
    86
    val session_fonts = session_prefix + Path.explode("fonts")
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    87
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    88
    if (info.options.bool("browser_info")) {
72375
e48d93811ed7 clarified signature;
wenzelm
parents: 72309
diff changeset
    89
      Isabelle_System.make_directory(session_fonts)
62971
087e36ce0593 back to exact copy of non-text file (amending dcc8e1d34b18);
wenzelm
parents: 62631
diff changeset
    90
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
    91
      Bytes.write(session_prefix + session_graph_path, graph_pdf)
62971
087e36ce0593 back to exact copy of non-text file (amending dcc8e1d34b18);
wenzelm
parents: 62631
diff changeset
    92
66000
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65998
diff changeset
    93
      HTML.write_isabelle_css(session_prefix)
61500
56a167b31a7f Isabelle fonts via external component;
wenzelm
parents: 61484
diff changeset
    94
69374
ab66951166f3 clarified "hidden" terminology;
wenzelm
parents: 69366
diff changeset
    95
      for (entry <- Isabelle_Fonts.fonts(hidden = true))
69360
dc9a39c3f75d more explicit Isabelle_Fonts.Entry;
wenzelm
parents: 69355
diff changeset
    96
        File.copy(entry.path, session_fonts)
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    97
    }
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
    98
  }
66040
f826ba18fe08 HTML preview based on PIDE markup;
wenzelm
parents: 66038
diff changeset
    99
f826ba18fe08 HTML preview based on PIDE markup;
wenzelm
parents: 66038
diff changeset
   100
67253
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   101
  /** preview **/
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   102
67260
ecd607631bc7 uniform preview for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents: 67256
diff changeset
   103
  sealed case class Preview(title: String, content: String)
ecd607631bc7 uniform preview for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents: 67256
diff changeset
   104
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   105
  def preview(
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   106
    resources: Resources,
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   107
    snapshot: Document.Snapshot,
67262
46540a2ead4b action "isabelle.draft" for plain-text preview;
wenzelm
parents: 67260
diff changeset
   108
    plain_text: Boolean = false,
67260
ecd607631bc7 uniform preview for Isabelle/jEdit and Isabelle/VSCode;
wenzelm
parents: 67256
diff changeset
   109
    fonts_url: String => String = HTML.fonts_url()): Preview =
67253
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   110
  {
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   111
    require(!snapshot.is_outdated)
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   112
67264
16f74b7c248a PIDE markup for non-theory nodes;
wenzelm
parents: 67262
diff changeset
   113
    def output_document(title: String, body: XML.Body): String =
16f74b7c248a PIDE markup for non-theory nodes;
wenzelm
parents: 67262
diff changeset
   114
      HTML.output_document(
16f74b7c248a PIDE markup for non-theory nodes;
wenzelm
parents: 67262
diff changeset
   115
        List(
69362
77c93eaf6cb7 tuned whitespace;
wenzelm
parents: 69360
diff changeset
   116
          HTML.style(HTML.fonts_css(fonts_url) + "\n\n" + File.read(HTML.isabelle_css)),
67264
16f74b7c248a PIDE markup for non-theory nodes;
wenzelm
parents: 67262
diff changeset
   117
          HTML.title(title)),
67337
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   118
        List(HTML.source(body)), css = "", structural = false)
67264
16f74b7c248a PIDE markup for non-theory nodes;
wenzelm
parents: 67262
diff changeset
   119
67253
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   120
    val name = snapshot.node_name
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   121
67264
16f74b7c248a PIDE markup for non-theory nodes;
wenzelm
parents: 67262
diff changeset
   122
    if (plain_text) {
69366
b6dacf6eabe3 clarified signature;
wenzelm
parents: 69362
diff changeset
   123
      val title = "File " + quote(name.path.file_name)
67265
f32287c95432 store full blob source for the sake of markup_to_XML;
wenzelm
parents: 67264
diff changeset
   124
      val content = output_document(title, HTML.text(snapshot.node.source))
67264
16f74b7c248a PIDE markup for non-theory nodes;
wenzelm
parents: 67262
diff changeset
   125
      Preview(title, content)
16f74b7c248a PIDE markup for non-theory nodes;
wenzelm
parents: 67262
diff changeset
   126
    }
67253
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   127
    else {
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   128
      resources.make_preview(snapshot) match {
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   129
        case Some(preview) => preview
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   130
        case None =>
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   131
          val title =
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   132
            if (name.is_theory) "Theory " + quote(name.theory_base_name)
69366
b6dacf6eabe3 clarified signature;
wenzelm
parents: 69362
diff changeset
   133
            else "File " + quote(name.path.file_name)
69255
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   134
          val content = output_document(title, pide_document(snapshot))
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   135
          Preview(title, content)
800b1ce96fce more general support for Isabelle/PIDE file formats -- less hardwired Bibtex operations;
wenzelm
parents: 69033
diff changeset
   136
      }
67253
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   137
    }
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   138
  }
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   139
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   140
67264
16f74b7c248a PIDE markup for non-theory nodes;
wenzelm
parents: 67262
diff changeset
   141
  /* PIDE document */
66040
f826ba18fe08 HTML preview based on PIDE markup;
wenzelm
parents: 66038
diff changeset
   142
67336
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   143
  private val document_elements =
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   144
    Rendering.foreground_elements ++ Rendering.text_color_elements ++ Rendering.markdown_elements +
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   145
    Markup.NUMERAL + Markup.COMMENT + Markup.LANGUAGE
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   146
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   147
  private val div_elements =
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   148
    Set(HTML.div.name, HTML.pre.name, HTML.par.name, HTML.list.name, HTML.enum.name,
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   149
      HTML.descr.name)
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   150
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   151
  private def html_div(html: XML.Body): Boolean =
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   152
    html exists {
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   153
      case XML.Elem(markup, body) => div_elements.contains(markup.name) || html_div(body)
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   154
      case XML.Text(_) => false
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   155
    }
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   156
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   157
  private def html_class(c: String, html: XML.Body): XML.Tree =
67337
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   158
    if (html.forall(_ == HTML.no_text)) HTML.no_text
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   159
    else if (html_div(html)) HTML.div(c, html)
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   160
    else HTML.span(c, html)
66040
f826ba18fe08 HTML preview based on PIDE markup;
wenzelm
parents: 66038
diff changeset
   161
66075
408a5325379c tuned rendering;
wenzelm
parents: 66044
diff changeset
   162
  private def make_html(xml: XML.Body): XML.Body =
66040
f826ba18fe08 HTML preview based on PIDE markup;
wenzelm
parents: 66038
diff changeset
   163
    xml map {
67336
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   164
      case XML.Elem(Markup(Markup.LANGUAGE, Markup.Name(Markup.Language.DOCUMENT)), body) =>
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   165
        html_class(Markup.Language.DOCUMENT, make_html(body))
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   166
      case XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), body) => HTML.par(make_html(body))
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   167
      case XML.Elem(Markup(Markup.MARKDOWN_ITEM, _), body) => HTML.item(make_html(body))
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   168
      case XML.Elem(Markup(Markup.Markdown_Bullet.name, _), _) => HTML.no_text
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   169
      case XML.Elem(Markup.Markdown_List(kind), body) =>
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   170
        if (kind == Markup.ENUMERATE) HTML.enum(make_html(body)) else HTML.list(make_html(body))
66040
f826ba18fe08 HTML preview based on PIDE markup;
wenzelm
parents: 66038
diff changeset
   171
      case XML.Elem(markup, body) =>
66044
bd7516709051 more HTML rendering as in Isabelle/jEdit;
wenzelm
parents: 66043
diff changeset
   172
        val name = markup.name
bd7516709051 more HTML rendering as in Isabelle/jEdit;
wenzelm
parents: 66043
diff changeset
   173
        val html =
bd7516709051 more HTML rendering as in Isabelle/jEdit;
wenzelm
parents: 66043
diff changeset
   174
          markup.properties match {
bd7516709051 more HTML rendering as in Isabelle/jEdit;
wenzelm
parents: 66043
diff changeset
   175
            case Markup.Kind(kind) if kind == Markup.COMMAND || kind == Markup.KEYWORD =>
67336
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   176
              List(html_class(kind, make_html(body)))
66044
bd7516709051 more HTML rendering as in Isabelle/jEdit;
wenzelm
parents: 66043
diff changeset
   177
            case _ =>
bd7516709051 more HTML rendering as in Isabelle/jEdit;
wenzelm
parents: 66043
diff changeset
   178
              make_html(body)
bd7516709051 more HTML rendering as in Isabelle/jEdit;
wenzelm
parents: 66043
diff changeset
   179
          }
66075
408a5325379c tuned rendering;
wenzelm
parents: 66044
diff changeset
   180
        Rendering.foreground.get(name) orElse Rendering.text_color.get(name) match {
67336
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   181
          case Some(c) => html_class(c.toString, html)
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   182
          case None => html_class(name, html)
66044
bd7516709051 more HTML rendering as in Isabelle/jEdit;
wenzelm
parents: 66043
diff changeset
   183
        }
66040
f826ba18fe08 HTML preview based on PIDE markup;
wenzelm
parents: 66038
diff changeset
   184
      case XML.Text(text) =>
f826ba18fe08 HTML preview based on PIDE markup;
wenzelm
parents: 66038
diff changeset
   185
        XML.Text(Symbol.decode(text))
f826ba18fe08 HTML preview based on PIDE markup;
wenzelm
parents: 66038
diff changeset
   186
    }
f826ba18fe08 HTML preview based on PIDE markup;
wenzelm
parents: 66038
diff changeset
   187
67253
93b4333f33bb clarified modules;
wenzelm
parents: 67251
diff changeset
   188
  def pide_document(snapshot: Document.Snapshot): XML.Body =
67336
3ee6da378183 HTML output for Markdown elements;
wenzelm
parents: 67265
diff changeset
   189
    make_html(snapshot.markup_to_XML(Text.Range.full, document_elements))
67246
4cedf44f2af1 isabelle.preview presents auxiliary text files as well;
wenzelm
parents: 67204
diff changeset
   190
4cedf44f2af1 isabelle.preview presents auxiliary text files as well;
wenzelm
parents: 67204
diff changeset
   191
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   192
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   193
  /** build documents **/
72565
wenzelm
parents: 72376
diff changeset
   194
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   195
  val session_tex_path = Path.explode("session.tex")
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   196
  val session_graph_path = Path.explode("session_graph.pdf")
72565
wenzelm
parents: 72376
diff changeset
   197
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   198
  def tex_name(name: Document.Node.Name): String = name.theory_base_name + ".tex"
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   199
  def document_tex_name(name: Document.Node.Name): String = "document/" + tex_name(name)
72309
564012e31db1 discontinued obsolete DVI document format and related settings/tools;
wenzelm
parents: 71114
diff changeset
   200
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   201
  def isabelletags(tags: List[String]): String =
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   202
    Library.terminate_lines(
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   203
      tags.map(tag =>
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   204
        tag.toList match {
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   205
          case '/' :: cs => "\\isafoldtag{" + cs.mkString + "}"
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   206
          case '-' :: cs => "\\isadroptag{" + cs.mkString + "}"
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   207
          case '+' :: cs => "\\isakeeptag{" + cs.mkString + "}"
67201
4cffa4791ef7 proper \isakeeptag (amending 13b5c3ff1954);
wenzelm
parents: 67187
diff changeset
   208
          case cs => "\\isakeeptag{" + cs.mkString + "}"
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   209
        }))
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   210
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   211
  def build_documents(
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   212
    session: String,
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   213
    deps: Sessions.Deps,
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   214
    store: Sessions.Store,
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   215
    progress: Progress = new Progress,
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   216
    verbose: Boolean = false,
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   217
    verbose_latex: Boolean = false): List[(String, Bytes)] =
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   218
  {
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   219
    /* session info */
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   220
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   221
    val info = deps.sessions_structure(session)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   222
    val options = info.options
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   223
    val base = deps(session)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   224
    val graph_pdf = graphview.Graph_File.make_pdf(options, base.session_graph_display)
67177
af5b89bc065c removed obsolete option (see 74a1b722507e);
wenzelm
parents: 67176
diff changeset
   225
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   226
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   227
    /* prepare document directory */
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   228
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   229
    def prepare_dir(dir: Path, doc_name: String, doc_tags: List[String]): (Path, String) =
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   230
    {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   231
      val doc_dir = dir + Path.basic(doc_name)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   232
      Isabelle_System.make_directory(doc_dir)
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   233
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   234
      Isabelle_System.bash("isabelle latex -o sty", cwd = doc_dir.file).check
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   235
      File.write(doc_dir + Path.explode("isabelletags.sty"), isabelletags(doc_tags))
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   236
      for ((base_dir, src) <- info.document_files) File.copy_base(info.dir + base_dir, src, doc_dir)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   237
      Bytes.write(doc_dir + session_graph_path, graph_pdf)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   238
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   239
      File.write(doc_dir + session_tex_path,
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   240
        Library.terminate_lines(
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   241
          base.session_theories.map(name => "\\input{" + tex_name(name) + "}")))
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   242
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   243
      using(store.open_database(session))(db =>
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   244
        for (name <- base.session_theories) {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   245
          val tex =
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   246
            Export.read_entry(db, session, name.theory, document_tex_name(name)) match {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   247
              case Some(entry) => entry.uncompressed(cache = store.xz_cache)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   248
              case None => Bytes.empty
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   249
            }
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   250
          Bytes.write(doc_dir + Path.basic(tex_name(name)), tex)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   251
        })
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   252
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   253
      val root1 = "root_" + doc_name
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   254
      val root = if ((doc_dir + Path.explode(root1).tex).is_file) root1 else "root"
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   255
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   256
      (doc_dir, root)
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   257
    }
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   258
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   259
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   260
    /* produce documents */
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   261
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   262
    val doc_output = info.document_output
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   263
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   264
    val docs =
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   265
      for ((doc_name, doc_tags) <- info.documents)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   266
      yield {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   267
        Isabelle_System.with_tmp_dir("document")(tmp_dir =>
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   268
        {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   269
          val (doc_dir, root) = prepare_dir(tmp_dir, doc_name, doc_tags)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   270
          doc_output.foreach(prepare_dir(_, doc_name, doc_tags))
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   271
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   272
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   273
          // bash scripts
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   274
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   275
          def root_bash(ext: String): String = Bash.string(root + "." + ext)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   276
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   277
          def latex_bash(fmt: String = "pdf", ext: String = "tex"): String =
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   278
            "isabelle latex -o " + Bash.string(fmt) + " " + Bash.string(root + "." + ext)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   279
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   280
          def bash(items: String*): Process_Result =
72599
76550282267f more interrupts, notably for running latex;
wenzelm
parents: 72595
diff changeset
   281
            progress.bash(items.mkString(" && "), cwd = doc_dir.file,
76550282267f more interrupts, notably for running latex;
wenzelm
parents: 72595
diff changeset
   282
              echo = verbose_latex, watchdog = Time.seconds(0.5))
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   283
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   284
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   285
          // prepare document
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   286
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   287
          val result =
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   288
            if ((doc_dir + Path.explode("build")).is_file) {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   289
              bash("./build pdf " + Bash.string(doc_name))
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   290
            }
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   291
            else {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   292
              bash(
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   293
                latex_bash(),
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   294
                "{ [ ! -f " + root_bash("bib") + " ] || " + latex_bash("bbl") + "; }",
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   295
                "{ [ ! -f " + root_bash("idx") + " ] || " + latex_bash("idx") + "; }",
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   296
                latex_bash(),
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   297
                latex_bash())
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   298
            }
67204
849a838f7e57 purge log files -- avoid old errors;
wenzelm
parents: 67203
diff changeset
   299
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   300
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   301
          // result
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   302
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   303
          val root_pdf = Path.basic(root).pdf
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   304
          val result_path = doc_dir + root_pdf
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   305
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   306
          if (!result.ok) {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   307
            cat_error(
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   308
              Library.trim_line(result.err),
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   309
              cat_lines(Latex.latex_errors(doc_dir, root) ::: Bibtex.bibtex_errors(doc_dir, root)),
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   310
              "Failed to build document " + quote(doc_name))
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   311
          }
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   312
          else if (!result_path.is_file) {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   313
            error("Bad document result: expected to find " + root_pdf)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   314
          }
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   315
          else doc_name -> Bytes.read(result_path)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   316
        })
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   317
      }
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   318
72595
c806eeb9138c clarified messages;
wenzelm
parents: 72577
diff changeset
   319
    def output(dir: Path)
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   320
    {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   321
      Isabelle_System.make_directory(dir)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   322
      for ((name, pdf) <- docs) {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   323
        val path = dir + Path.basic(name).pdf
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   324
        Bytes.write(path, pdf)
72595
c806eeb9138c clarified messages;
wenzelm
parents: 72577
diff changeset
   325
        progress.echo_document(path)
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   326
      }
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   327
    }
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   328
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   329
    if (info.options.bool("browser_info") || doc_output.isEmpty) {
72595
c806eeb9138c clarified messages;
wenzelm
parents: 72577
diff changeset
   330
      output(store.browser_info + Path.basic(info.chapter) + Path.basic(session))
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   331
    }
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   332
72595
c806eeb9138c clarified messages;
wenzelm
parents: 72577
diff changeset
   333
    doc_output.foreach(output)
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   334
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   335
    docs
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   336
  }
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   337
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   338
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   339
  /* Isabelle tool wrapper */
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   340
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   341
  val isabelle_tool =
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   342
    Isabelle_Tool("document", "prepare session theory document", args =>
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   343
    {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   344
      var verbose_latex = false
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   345
      var dirs: List[Path] = Nil
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   346
      var no_build = false
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   347
      var options = Options.init()
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   348
      var verbose_build = false
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   349
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   350
      val getopts = Getopts(
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   351
        """
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   352
Usage: isabelle document [OPTIONS] SESSION
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   353
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   354
  Options are:
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   355
    -O           set option "document_output", relative to current directory
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   356
    -V           verbose latex
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   357
    -d DIR       include session directory
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   358
    -n           no build of session
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   359
    -o OPTION    override Isabelle system OPTION (via NAME=VAL or NAME)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   360
    -v           verbose build
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   361
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   362
  Prepare the theory document of a session.
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   363
""",
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   364
        "O:" -> (arg => options += ("document_output=" + Path.explode(arg).absolute.implode)),
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   365
        "V" -> (_ => verbose_latex = true),
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   366
        "d:" -> (arg => dirs = dirs ::: List(Path.explode(arg))),
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   367
        "n" -> (_ => no_build = true),
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   368
        "o:" -> (arg => options = options + arg),
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   369
        "v" -> (_ => verbose_build = true))
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   370
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   371
      val more_args = getopts(args)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   372
      val session =
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   373
        more_args match {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   374
          case List(a) => a
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   375
          case _ => getopts.usage()
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   376
        }
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   377
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   378
      val progress = new Console_Progress(verbose = verbose_build)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   379
      val store = Sessions.store(options)
67176
13b5c3ff1954 re-implemented "isabelle document" in Isabelle/Scala, include latex_errors here;
wenzelm
parents: 66075
diff changeset
   380
72574
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   381
      progress.interrupt_handler {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   382
        if (!no_build) {
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   383
          val res =
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   384
            Build.build(options, selection = Sessions.Selection.session(session),
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   385
              dirs = dirs, progress = progress, verbose = verbose_build)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   386
          if (!res.ok) error("Failed to build session " + quote(session))
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   387
        }
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   388
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   389
        val deps =
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   390
          Sessions.load_structure(options + "document=pdf", dirs = dirs).
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   391
            selection_deps(Sessions.Selection.session(session))
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   392
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   393
        build_documents(session, deps, store, progress = progress,
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   394
          verbose = true, verbose_latex = verbose_latex)
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   395
      }
d892f6d66402 build documents in Isabelle/Scala, based on generated tex files as session exports;
wenzelm
parents: 72565
diff changeset
   396
    })
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
   397
}