src/Pure/Thy/presentation.scala
author wenzelm
Thu, 04 Nov 2021 16:47:28 +0100
changeset 74693 f7525f5c84b6
parent 74688 7e31f7022c7b
child 74694 2d9d92116fac
permissions -rw-r--r--
tuned signature;
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
73718
ecb31c3bf980 clarified modules;
wenzelm
parents: 73529
diff changeset
     1
/*  Title:      Pure/Thy/presentation.scala
50707
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
73718
ecb31c3bf980 clarified modules;
wenzelm
parents: 73529
diff changeset
     4
HTML presentation of PIDE document content.
50707
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
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    10
import scala.annotation.tailrec
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
    11
import scala.collection.immutable.SortedMap
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    12
import scala.collection.mutable
51402
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
72652
07edf1952ab1 clarified modules;
wenzelm
parents: 72650
diff changeset
    15
object Presentation
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
    16
{
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
    17
  /** HTML documents **/
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
    18
73718
ecb31c3bf980 clarified modules;
wenzelm
parents: 73529
diff changeset
    19
  /* HTML context */
ecb31c3bf980 clarified modules;
wenzelm
parents: 73529
diff changeset
    20
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    21
  val fonts_path = Path.explode("fonts")
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    22
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
    23
  sealed case class HTML_Document(title: String, content: String)
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
    24
72959
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    25
  def html_context(fonts_url: String => String = HTML.fonts_url()): HTML_Context =
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    26
    new HTML_Context(fonts_url)
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    27
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    28
  final class HTML_Context private[Presentation](fonts_url: String => String)
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    29
  {
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73317
diff changeset
    30
    def init_fonts(dir: Path): Unit =
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    31
    {
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    32
      val fonts_dir = Isabelle_System.make_directory(dir + fonts_path)
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    33
      for (entry <- Isabelle_Fonts.fonts(hidden = true))
73317
df49ca5da9d0 clarified modules: more like ML;
wenzelm
parents: 73277
diff changeset
    34
        Isabelle_System.copy_file(entry.path, fonts_dir)
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    35
    }
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    36
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    37
    def head(title: String, rest: XML.Body = Nil): XML.Tree =
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    38
      HTML.div("head", HTML.chapter(title) :: rest)
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    39
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    40
    def source(body: XML.Body): XML.Tree = HTML.pre("source", body)
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    41
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    42
    def offset_ref(offset: Text.Range): String =
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    43
      "offset/" + offset.start + ".." + offset.stop
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    44
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    45
    def export_ref(entity: Entity): String =
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    46
      Export_Theory.export_kind(entity.kind) + "/" + entity.name
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    47
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    48
    def contents(heading: String, items: List[XML.Body], css_class: String = "contents")
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    49
      : List[XML.Elem] =
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    50
    {
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    51
      if (items.isEmpty) Nil
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    52
      else List(HTML.div(css_class, List(HTML.section(heading), HTML.itemize(items))))
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    53
    }
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
    54
72959
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    55
    def output_document(title: String, body: XML.Body): String =
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    56
      HTML.output_document(
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    57
        List(
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    58
          HTML.style(HTML.fonts_css(fonts_url) + "\n\n" + File.read(HTML.isabelle_css)),
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    59
          HTML.title(title)),
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    60
        List(HTML.source(body)), css = "", structural = false)
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    61
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    62
    def html_document(title: String, body: XML.Body): HTML_Document =
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    63
      HTML_Document(title, output_document(title, body))
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    64
  }
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
    65
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
    66
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    67
  /* presentation elements */
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    68
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    69
  sealed case class Elements(
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    70
    html: Markup.Elements = Markup.Elements.empty,
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
    71
    entity: Markup.Elements = Markup.Elements.empty,
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    72
    language: Markup.Elements = Markup.Elements.empty)
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
    73
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    74
  val elements1: Elements =
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    75
    Elements(
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    76
      html = Rendering.foreground_elements ++ Rendering.text_color_elements +
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
    77
        Markup.NUMERAL + Markup.COMMENT + Markup.ENTITY + Markup.LANGUAGE,
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    78
      entity = Markup.Elements(Markup.THEORY, Markup.TYPE_NAME, Markup.CONSTANT, Markup.FACT,
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    79
        Markup.CLASS, Markup.LOCALE, Markup.FREE))
73036
b028e8d22d8d clarified HTML presentation elements;
wenzelm
parents: 73028
diff changeset
    80
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    81
  val elements2: Elements =
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    82
    Elements(
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    83
      html = elements1.html ++ Rendering.markdown_elements,
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
    84
      language = Markup.Elements(Markup.Language.DOCUMENT))
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    85
74678
wenzelm
parents: 74677
diff changeset
    86
wenzelm
parents: 74677
diff changeset
    87
  /* formal entities */
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    88
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    89
  type Entity = Export_Theory.Entity[Export_Theory.No_Content]
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    90
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    91
  case class Entity_Context(node: Document.Node.Name)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    92
  {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    93
    val seen_ranges: mutable.Set[Symbol.Range] = mutable.Set.empty
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
    94
  }
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
    95
74678
wenzelm
parents: 74677
diff changeset
    96
73718
ecb31c3bf980 clarified modules;
wenzelm
parents: 73529
diff changeset
    97
  /* HTML output */
73036
b028e8d22d8d clarified HTML presentation elements;
wenzelm
parents: 73028
diff changeset
    98
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
    99
  private val div_elements =
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   100
    Set(HTML.div.name, HTML.pre.name, HTML.par.name, HTML.list.name, HTML.enum.name,
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   101
      HTML.descr.name)
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   102
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   103
  def make_html(
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   104
    name: Document.Node.Name,
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   105
    elements: Elements,
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   106
    entity_anchor: (Entity_Context, Symbol.Range, XML.Body) => Option[XML.Tree],
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   107
    entity_link: (Entity_Context, Properties.T, XML.Body) => Option[XML.Tree],
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   108
    xml: XML.Body): XML.Body =
73054
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   109
  {
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   110
    val context = Entity_Context(name)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   111
73054
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   112
    def html_div(html: XML.Body): Boolean =
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   113
      html exists {
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   114
        case XML.Elem(markup, body) => div_elements.contains(markup.name) || html_div(body)
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   115
        case XML.Text(_) => false
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   116
      }
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   117
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
   118
    def html_class(c: String, html: XML.Body): XML.Body =
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
   119
      if (c == "") html
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
   120
      else if (html_div(html)) List(HTML.div(c, html))
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
   121
      else List(HTML.span(c, html))
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   122
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   123
    def html_body(xml_body: XML.Body, end_offset: Symbol.Offset): (XML.Body, Symbol.Offset) =
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   124
      xml_body.foldRight((List.empty[XML.Tree], end_offset)) { case (tree, (res, end_offset1)) =>
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   125
        val (res1, offset) = html_body_single(tree, end_offset1)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   126
        (res1 ++ res, offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   127
      }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   128
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   129
    @tailrec
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   130
    def html_body_single(xml_tree: XML.Tree, end_offset: Symbol.Offset): (XML.Body, Symbol.Offset) =
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   131
      xml_tree match {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   132
        case XML.Wrapped_Elem(markup, _, body) => html_body_single(XML.Elem(markup, body), end_offset)
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   133
        case XML.Elem(Markup(Markup.ENTITY, props @ Markup.Kind(kind)), body) =>
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   134
          val (body1, offset) = html_body(body, end_offset)
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   135
          if (elements.entity(kind)) {
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   136
            entity_link(context, props, body1) match {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   137
              case Some(link) => (List(link), offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   138
              case None => (body1, offset)
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   139
            }
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   140
          }
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   141
          else (body1, offset)
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
   142
        case XML.Elem(Markup(Markup.LANGUAGE, Markup.Name(name)), body) =>
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   143
          val (body1, offset) = html_body(body, end_offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   144
          (html_class(if (elements.language(name)) name else "", body1), offset)
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
   145
        case XML.Elem(Markup(Markup.MARKDOWN_PARAGRAPH, _), body) =>
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   146
          val (body1, offset) = html_body(body, end_offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   147
          (List(HTML.par(body1)), offset)
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
   148
        case XML.Elem(Markup(Markup.MARKDOWN_ITEM, _), body) =>
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   149
          val (body1, offset) = html_body(body, end_offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   150
          (List(HTML.item(body1)), offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   151
        case XML.Elem(Markup(Markup.Markdown_Bullet.name, _), text) =>
74683
c8327efc7af1 clarified signature: more direct XML.symbol_length;
wenzelm
parents: 74682
diff changeset
   152
          (Nil, end_offset - XML.symbol_length(text))
73054
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   153
        case XML.Elem(Markup.Markdown_List(kind), body) =>
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   154
          val (body1, offset) = html_body(body, end_offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   155
          if (kind == Markup.ENUMERATE) (List(HTML.enum(body1)), offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   156
          else (List(HTML.list(body1)), offset)
73054
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   157
        case XML.Elem(markup, body) =>
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   158
          val name = markup.name
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   159
          val (body1, offset) = html_body(body, end_offset)
73054
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   160
          val html =
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   161
            markup.properties match {
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   162
              case Markup.Kind(kind) if kind == Markup.COMMAND || kind == Markup.KEYWORD =>
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   163
                html_class(kind, body1)
73054
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   164
              case _ =>
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   165
                body1
73054
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   166
            }
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   167
          Rendering.foreground.get(name) orElse Rendering.text_color.get(name) match {
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   168
            case Some(c) => (html_class(c.toString, html), offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   169
            case None => (html_class(name, html), offset)
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   170
          }
73054
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   171
        case XML.Text(text) =>
74682
ce4adcc85f6c more direct Symbol.length: Symbol.decode is redundant, symbol counts are invariant under it;
wenzelm
parents: 74679
diff changeset
   172
          val offset = end_offset - Symbol.length(text)
ce4adcc85f6c more direct Symbol.length: Symbol.decode is redundant, symbol counts are invariant under it;
wenzelm
parents: 74679
diff changeset
   173
          val body = HTML.text(Symbol.decode(text))
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   174
          entity_anchor(context, Text.Range(offset, end_offset), body) match {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   175
            case Some(body1) => (List(body1), offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   176
            case None => (body, offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   177
          }
73054
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   178
      }
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   179
74683
c8327efc7af1 clarified signature: more direct XML.symbol_length;
wenzelm
parents: 74682
diff changeset
   180
    html_body(xml, XML.symbol_length(xml) + 1)._1
73054
517b17e54d28 tuned signature;
wenzelm
parents: 73043
diff changeset
   181
  }
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   182
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   183
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   184
  /* PIDE HTML document */
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   185
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   186
  def html_document(
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   187
    resources: Resources,
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   188
    snapshot: Document.Snapshot,
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   189
    html_context: HTML_Context,
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
   190
    elements: Elements,
72959
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
   191
    plain_text: Boolean = false): HTML_Document =
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   192
  {
73120
c3589f2dff31 more informative errors: simplify diagnosis of spurious failures reported by users;
wenzelm
parents: 73059
diff changeset
   193
    require(!snapshot.is_outdated, "document snapshot outdated")
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   194
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   195
    val name = snapshot.node_name
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   196
    if (plain_text) {
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   197
      val title = "File " + Symbol.cartouche_decoded(name.path.file_name)
72959
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
   198
      val body = HTML.text(snapshot.node.source)
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   199
      html_context.html_document(title, body)
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   200
    }
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   201
    else {
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   202
      resources.html_document(snapshot) getOrElse {
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   203
        val title =
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   204
          if (name.is_theory) "Theory " + quote(name.theory_base_name)
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   205
          else "File " + Symbol.cartouche_decoded(name.path.file_name)
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   206
        val xml = snapshot.xml_markup(elements = elements.html)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   207
        val body = make_html(name, elements, (_, _, _) => None, (_, _, _) => None, xml)
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   208
        html_context.html_document(title, body)
72957
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   209
      }
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   210
    }
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   211
  }
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   212
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   213
75fc90edc0a8 clarified signature and module structure;
wenzelm
parents: 72956
diff changeset
   214
73718
ecb31c3bf980 clarified modules;
wenzelm
parents: 73529
diff changeset
   215
  /** HTML presentation **/
72649
4ba5b1b08dd5 clarified signature: more explicit types;
wenzelm
parents: 72648
diff changeset
   216
72648
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   217
  /* presentation context */
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   218
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   219
  object Context
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   220
  {
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   221
    val none: Context = new Context { def enabled: Boolean = false }
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   222
    val standard: Context = new Context { def enabled: Boolean = true }
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   223
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   224
    def dir(path: Path): Context =
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   225
      new Context {
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   226
        def enabled: Boolean = true
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   227
        override def dir(store: Sessions.Store): Path = path
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   228
      }
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   229
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   230
    def make(s: String): Context =
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   231
      if (s == ":") standard else dir(Path.explode(s))
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   232
  }
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   233
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   234
  abstract class Context private
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   235
  {
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   236
    def enabled: Boolean
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   237
    def enabled(info: Sessions.Info): Boolean = enabled || info.browser_info
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   238
    def dir(store: Sessions.Store): Path = store.presentation_dir
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   239
    def dir(store: Sessions.Store, info: Sessions.Info): Path =
72670
4db9411c859c tuned signature;
wenzelm
parents: 72669
diff changeset
   240
      dir(store) + Path.explode(info.chapter_session)
72648
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   241
  }
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   242
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   243
72955
942bf91545fa clarified comments: file-system access is always unsynchronized;
wenzelm
parents: 72882
diff changeset
   244
  /* maintain chapter index */
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
   245
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
   246
  private val sessions_path = Path.basic(".sessions")
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
   247
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
   248
  private def read_sessions(dir: Path): List[(String, String)] =
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
   249
  {
51416
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
   250
    val path = dir + sessions_path
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
   251
    if (path.is_file) {
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
   252
      import XML.Decode._
65344
b99283eed13c clarified YXML vs. symbol encoding: operate on whole message;
wenzelm
parents: 65089
diff changeset
   253
      list(pair(string, string))(Symbol.decode_yxml(File.read(path)))
51416
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
   254
    }
e2505a192a7c do not absorb I/O errors;
wenzelm
parents: 51406
diff changeset
   255
    else Nil
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
   256
  }
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
   257
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73317
diff changeset
   258
  private def write_sessions(dir: Path, sessions: List[(String, String)]): Unit =
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
   259
  {
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
   260
    import XML.Encode._
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
   261
    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
   262
  }
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
   263
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73317
diff changeset
   264
  def update_chapter_index(
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73317
diff changeset
   265
    browser_info: Path, chapter: String, new_sessions: List[(String, String)]): Unit =
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
   266
  {
72376
04bce3478688 clarified signature;
wenzelm
parents: 72375
diff changeset
   267
    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
   268
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
   269
    val sessions0 =
51406
950b897f95bb proper path -- I/O was hidden due to permissiveness;
wenzelm
parents: 51402
diff changeset
   270
      try { read_sessions(dir) }
51987
7d8e0e3c553b tuned signature;
wenzelm
parents: 51416
diff changeset
   271
      catch { case _: XML.Error => Nil }
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
   272
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
   273
    val sessions = (SortedMap.empty[String, String] ++ sessions0 ++ new_sessions).toList
65988
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
   274
    write_sessions(dir, sessions)
51402
b05cd411d3d3 include session description in chapter index;
wenzelm
parents: 51399
diff changeset
   275
65988
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
   276
    val title = "Isabelle/" + chapter + " sessions"
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
   277
    HTML.write_document(dir, "index.html",
73523
2cd23d587db9 further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents: 73474
diff changeset
   278
      List(HTML.title(title + Isabelle_System.isabelle_heading())),
65988
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
   279
      HTML.chapter(title) ::
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
   280
       (if (sessions.isEmpty) Nil
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
   281
        else
65992
50daca61efd6 tuned signature;
wenzelm
parents: 65989
diff changeset
   282
          List(HTML.div("sessions",
50daca61efd6 tuned signature;
wenzelm
parents: 65989
diff changeset
   283
            List(HTML.description(
65988
8040d2563593 modernized generated HTML;
wenzelm
parents: 65404
diff changeset
   284
              sessions.map({ case (name, description) =>
69318
f3351bb4390e clarified presentation;
wenzelm
parents: 69255
diff changeset
   285
                val descr = Symbol.trim_blank_lines(description)
65989
68cd15585f46 tuned layout;
wenzelm
parents: 65988
diff changeset
   286
                (List(HTML.link(name + "/index.html", HTML.text(name))),
69318
f3351bb4390e clarified presentation;
wenzelm
parents: 69255
diff changeset
   287
                  if (descr == "") Nil
f3351bb4390e clarified presentation;
wenzelm
parents: 69255
diff changeset
   288
                  else HTML.break ::: List(HTML.pre(HTML.text(descr)))) })))))))
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
   289
  }
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   290
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73317
diff changeset
   291
  def make_global_index(browser_info: Path): Unit =
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   292
  {
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   293
    if (!(browser_info + Path.explode("index.html")).is_file) {
72375
e48d93811ed7 clarified signature;
wenzelm
parents: 72309
diff changeset
   294
      Isabelle_System.make_directory(browser_info)
73317
df49ca5da9d0 clarified modules: more like ML;
wenzelm
parents: 73277
diff changeset
   295
      Isabelle_System.copy_file(Path.explode("~~/lib/logo/isabelle.gif"),
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   296
        browser_info + Path.explode("isabelle.gif"))
73528
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   297
      val title = "The " + XML.text(Isabelle_System.isabelle_name()) + " Library"
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   298
      File.write(browser_info + Path.explode("index.html"),
73529
cf1a1e92bf34 more standard header, with utf-8 encoding;
wenzelm
parents: 73528
diff changeset
   299
        HTML.header +
cf1a1e92bf34 more standard header, with utf-8 encoding;
wenzelm
parents: 73528
diff changeset
   300
"""
73528
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   301
<head>
73529
cf1a1e92bf34 more standard header, with utf-8 encoding;
wenzelm
parents: 73528
diff changeset
   302
  """ + HTML.head_meta + """
73528
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   303
  <title>""" + title + """</title>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   304
</head>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   305
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   306
<body text="#000000" bgcolor="#FFFFFF" link="#0000FF" vlink="#000099" alink="#404040">
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   307
  <center>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   308
    <table width="100%" border="0" cellspacing="10" cellpadding="0">
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   309
      <tr>
74676
d37b204e1f89 proper HTTPS;
wenzelm
parents: 74670
diff changeset
   310
        <td width="20%" valign="middle" align="center"><a href="https://isabelle.in.tum.de/"><img align="bottom" src="isabelle.gif" width="100" height="86" alt="[Isabelle]" border="0" /></a></td>
73528
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   311
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   312
        <td width="80%" valign="middle" align="center">
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   313
          <table width="90%" border="0" cellspacing="0" cellpadding="20">
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   314
            <tr>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   315
              <td valign="middle" align="center" bgcolor="#AACCCC"><font face="Helvetica,Arial" size="+2">""" + title + """</font></td>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   316
            </tr>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   317
          </table>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   318
        </td>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   319
      </tr>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   320
    </table>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   321
  </center>
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   322
  <hr />
74670
b2604cd4d131 recover library_index_content.template from c337c798f64c: required for website/build/main;
wenzelm
parents: 73718
diff changeset
   323
""" + File.read(Path.explode("~~/lib/html/library_index_content.template")) +
b2604cd4d131 recover library_index_content.template from c337c798f64c: required for website/build/main;
wenzelm
parents: 73718
diff changeset
   324
"""
73528
c337c798f64c clarified HTML template (see also 04cb7e02ca38): avoid odd patching of sources;
wenzelm
parents: 73523
diff changeset
   325
</body>
73529
cf1a1e92bf34 more standard header, with utf-8 encoding;
wenzelm
parents: 73528
diff changeset
   326
""" + HTML.footer)
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   327
    }
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   328
  }
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   329
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   330
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   331
  /* cache */
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   332
74693
f7525f5c84b6 tuned signature;
wenzelm
parents: 74688
diff changeset
   333
  class Entity_Cache private(cache: mutable.Map[Document.Node.Name, List[Entity]])
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   334
  {
74693
f7525f5c84b6 tuned signature;
wenzelm
parents: 74688
diff changeset
   335
    def get_or_update(node: Document.Node.Name, e: => List[Entity]): List[Entity] =
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   336
      cache.getOrElseUpdate(node, e)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   337
  }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   338
  object Entity_Cache
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   339
  {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   340
    def empty: Entity_Cache = new Entity_Cache(mutable.Map.empty)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   341
  }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   342
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   343
  /* present session */
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   344
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   345
  val session_graph_path = Path.explode("session_graph.pdf")
72959
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
   346
  val readme_path = Path.explode("README.html")
a093b8fc9e21 tuned signature: more explicit types;
wenzelm
parents: 72957
diff changeset
   347
  val files_path = Path.explode("files")
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   348
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   349
  def html_name(name: Document.Node.Name): String = Path.explode(name.theory_base_name).html.implode
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   350
  def html_path(path: Path): String = (files_path + path.squash.html).implode
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   351
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   352
  def session_relative(deps: Sessions.Deps, session0: String, session1: String): Option[String] =
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   353
  {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   354
    for {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   355
      info0 <- deps.sessions_structure.get(session0)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   356
      info1 <- deps.sessions_structure.get(session1)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   357
    } yield info0.relative_path(info1)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   358
  }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   359
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   360
  def node_relative(
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   361
    deps: Sessions.Deps,
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   362
    session0: String,
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   363
    node_name: Document.Node.Name): Option[String] =
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   364
  {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   365
    val session1 = deps(session0).theory_qualifier(node_name)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   366
    session_relative(deps, session0, session1)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   367
  }
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   368
73042
22f5a6283477 tuned signature;
wenzelm
parents: 73041
diff changeset
   369
  def theory_link(deps: Sessions.Deps, session0: String,
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   370
    name: Document.Node.Name, body: XML.Body, anchor: Option[String] = None): Option[XML.Tree] =
73042
22f5a6283477 tuned signature;
wenzelm
parents: 73041
diff changeset
   371
  {
22f5a6283477 tuned signature;
wenzelm
parents: 73041
diff changeset
   372
    val session1 = deps(session0).theory_qualifier(name)
73057
45a34cc581b8 more robust and permissive;
wenzelm
parents: 73056
diff changeset
   373
    val info0 = deps.sessions_structure.get(session0)
45a34cc581b8 more robust and permissive;
wenzelm
parents: 73056
diff changeset
   374
    val info1 = deps.sessions_structure.get(session1)
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   375
    val fragment = if (anchor.isDefined) "#" + anchor.get else ""
73057
45a34cc581b8 more robust and permissive;
wenzelm
parents: 73056
diff changeset
   376
    if (info0.isDefined && info1.isDefined) {
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   377
      Some(HTML.link(info0.get.relative_path(info1.get) + html_name(name) + fragment, body))
73057
45a34cc581b8 more robust and permissive;
wenzelm
parents: 73056
diff changeset
   378
    }
45a34cc581b8 more robust and permissive;
wenzelm
parents: 73056
diff changeset
   379
    else None
73042
22f5a6283477 tuned signature;
wenzelm
parents: 73041
diff changeset
   380
  }
22f5a6283477 tuned signature;
wenzelm
parents: 73041
diff changeset
   381
72648
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   382
  def session_html(
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   383
    resources: Resources,
72648
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   384
    session: String,
1cbac4ae934d more explicit presentation directory;
wenzelm
parents: 72634
diff changeset
   385
    deps: Sessions.Deps,
72683
b5e6f0d137a7 clarified signature: prefer Database_Context;
wenzelm
parents: 72682
diff changeset
   386
    db_context: Sessions.Database_Context,
72992
bcba32fd89de more interrupts;
wenzelm
parents: 72989
diff changeset
   387
    progress: Progress = new Progress,
73022
38528017e4c8 more verbosity for potentially bulky presentation;
wenzelm
parents: 72992
diff changeset
   388
    verbose: Boolean = false,
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   389
    html_context: HTML_Context,
73055
3e4df2e689ff clarified signature;
wenzelm
parents: 73054
diff changeset
   390
    elements: Elements,
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   391
    presentation: Context,
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   392
    seen_nodes_cache: Entity_Cache = Entity_Cache.empty): Unit =
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   393
  {
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   394
    val info = deps.sessions_structure(session)
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   395
    val options = info.options
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   396
    val base = deps(session)
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   397
72683
b5e6f0d137a7 clarified signature: prefer Database_Context;
wenzelm
parents: 72682
diff changeset
   398
    val session_dir = presentation.dir(db_context.store, info)
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   399
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   400
    html_context.init_fonts(session_dir)
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   401
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   402
    Bytes.write(session_dir + session_graph_path,
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   403
      graphview.Graph_File.make_pdf(options, base.session_graph_display))
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   404
72673
8ff7a0e394f9 clarified PDF/HTML presentation, based on pdf blobs from session database (e.g. from earlier builds);
wenzelm
parents: 72670
diff changeset
   405
    val documents =
72683
b5e6f0d137a7 clarified signature: prefer Database_Context;
wenzelm
parents: 72682
diff changeset
   406
      for {
b5e6f0d137a7 clarified signature: prefer Database_Context;
wenzelm
parents: 72682
diff changeset
   407
        doc <- info.document_variants
73718
ecb31c3bf980 clarified modules;
wenzelm
parents: 73529
diff changeset
   408
        document <- db_context.input_database(session)(Document_Build.read_document(_, _, doc.name))
73022
38528017e4c8 more verbosity for potentially bulky presentation;
wenzelm
parents: 72992
diff changeset
   409
      } yield {
38528017e4c8 more verbosity for potentially bulky presentation;
wenzelm
parents: 72992
diff changeset
   410
        if (verbose) progress.echo("Presenting document " + session + "/" + doc.name)
38528017e4c8 more verbosity for potentially bulky presentation;
wenzelm
parents: 72992
diff changeset
   411
        Bytes.write(session_dir + doc.path.pdf, document.pdf)
38528017e4c8 more verbosity for potentially bulky presentation;
wenzelm
parents: 72992
diff changeset
   412
        doc
38528017e4c8 more verbosity for potentially bulky presentation;
wenzelm
parents: 72992
diff changeset
   413
      }
72673
8ff7a0e394f9 clarified PDF/HTML presentation, based on pdf blobs from session database (e.g. from earlier builds);
wenzelm
parents: 72670
diff changeset
   414
73041
wenzelm
parents: 73036
diff changeset
   415
    val view_links =
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   416
    {
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   417
      val deps_link =
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   418
        HTML.link(session_graph_path, HTML.text("theory dependencies"))
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   419
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   420
      val readme_links =
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   421
        if ((info.dir + readme_path).is_file) {
73317
df49ca5da9d0 clarified modules: more like ML;
wenzelm
parents: 73277
diff changeset
   422
          Isabelle_System.copy_file(info.dir + readme_path, session_dir + readme_path)
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   423
          List(HTML.link(readme_path, HTML.text("README")))
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   424
        }
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   425
        else Nil
62971
087e36ce0593 back to exact copy of non-text file (amending dcc8e1d34b18);
wenzelm
parents: 62631
diff changeset
   426
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   427
      val document_links =
72673
8ff7a0e394f9 clarified PDF/HTML presentation, based on pdf blobs from session database (e.g. from earlier builds);
wenzelm
parents: 72670
diff changeset
   428
        documents.map(doc => HTML.link(doc.path.pdf, HTML.text(doc.name)))
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   429
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   430
      Library.separate(HTML.break ::: HTML.nl,
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   431
        (deps_link :: readme_links ::: document_links).
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   432
          map(link => HTML.text("View ") ::: List(link))).flatten
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   433
    }
62971
087e36ce0593 back to exact copy of non-text file (amending dcc8e1d34b18);
wenzelm
parents: 62631
diff changeset
   434
74693
f7525f5c84b6 tuned signature;
wenzelm
parents: 74688
diff changeset
   435
    def read_exports(name: Document.Node.Name): List[Entity] =
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   436
    {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   437
      seen_nodes_cache.get_or_update(name, {
74693
f7525f5c84b6 tuned signature;
wenzelm
parents: 74688
diff changeset
   438
        if (Sessions.is_pure(name.theory_base_name)) Nil
74678
wenzelm
parents: 74677
diff changeset
   439
        else {
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   440
          val session1 = deps(session).theory_qualifier(name)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   441
          val provider = Export.Provider.database_context(db_context, List(session1), name.theory)
74688
7e31f7022c7b clarified signature;
wenzelm
parents: 74684
diff changeset
   442
          if (Export_Theory.read_theory_parents(provider, name.theory).isDefined) {
7e31f7022c7b clarified signature;
wenzelm
parents: 74684
diff changeset
   443
            val theory = Export_Theory.read_theory(provider, session1, name.theory)
74693
f7525f5c84b6 tuned signature;
wenzelm
parents: 74688
diff changeset
   444
            theory.entity_iterator.toList
74688
7e31f7022c7b clarified signature;
wenzelm
parents: 74684
diff changeset
   445
          }
7e31f7022c7b clarified signature;
wenzelm
parents: 74684
diff changeset
   446
          else {
7e31f7022c7b clarified signature;
wenzelm
parents: 74684
diff changeset
   447
            progress.echo_warning("No theory exports for " + name)
74693
f7525f5c84b6 tuned signature;
wenzelm
parents: 74688
diff changeset
   448
            Nil
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   449
          }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   450
        }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   451
      })
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   452
    }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   453
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   454
    val exports = base.known_theories.map(_._2.name).map(node => node -> read_exports(node)).toMap
74678
wenzelm
parents: 74677
diff changeset
   455
    val export_ranges =
wenzelm
parents: 74677
diff changeset
   456
      exports.view.mapValues(_.groupBy(entity =>
wenzelm
parents: 74677
diff changeset
   457
        Text.Range(Position.Offset.get(entity.pos), Position.End_Offset.get(entity.pos)))).toMap
wenzelm
parents: 74677
diff changeset
   458
    val export_names =
wenzelm
parents: 74677
diff changeset
   459
      exports.map {
wenzelm
parents: 74677
diff changeset
   460
        case (node, entities) => node.theory -> entities.map(entity => entity.name -> entity).toMap
wenzelm
parents: 74677
diff changeset
   461
      }
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   462
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   463
    val theories: List[XML.Body] =
72962
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   464
    {
af2d0e07493b present auxiliary files with PIDE markup;
wenzelm
parents: 72959
diff changeset
   465
      var seen_files = List.empty[(Path, String, Document.Node.Name)]
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   466
74678
wenzelm
parents: 74677
diff changeset
   467
      def node_file(node: Document.Node.Name): String =
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   468
        if (node.node.endsWith(".thy")) html_name(node) else html_path(Path.explode(node.node))
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   469
74678
wenzelm
parents: 74677
diff changeset
   470
      def entity_anchor(
wenzelm
parents: 74677
diff changeset
   471
        context: Entity_Context, range: Symbol.Range, body: XML.Body): Option[XML.Elem] =
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   472
      {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   473
        body match {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   474
          case List(XML.Elem(Markup("span", List("id" -> _)), _)) => None
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   475
          case _ =>
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   476
            Some {
74678
wenzelm
parents: 74677
diff changeset
   477
              val body1 =
wenzelm
parents: 74677
diff changeset
   478
                if (context.seen_ranges.contains(range)) {
74679
0efa6a8b6e20 clarified link style: similar to Isabelle/jEdit;
wenzelm
parents: 74678
diff changeset
   479
                  HTML.entity_def(HTML.span(HTML.id(html_context.offset_ref(range)), body))
74678
wenzelm
parents: 74677
diff changeset
   480
                }
wenzelm
parents: 74677
diff changeset
   481
                else HTML.span(body)
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   482
              export_ranges.get(context.node).flatMap(_.get(range)) match {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   483
                case Some(entities) =>
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   484
                  entities.map(html_context.export_ref).foldLeft(body1) {
74679
0efa6a8b6e20 clarified link style: similar to Isabelle/jEdit;
wenzelm
parents: 74678
diff changeset
   485
                    case (elem, id) =>
0efa6a8b6e20 clarified link style: similar to Isabelle/jEdit;
wenzelm
parents: 74678
diff changeset
   486
                      HTML.entity_def(HTML.span(HTML.id(id), List(elem)))
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   487
                  }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   488
                case None => body1
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   489
              }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   490
            }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   491
        }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   492
      }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   493
74678
wenzelm
parents: 74677
diff changeset
   494
      def entity_ref(theory: String, name: String): Option[String] =
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   495
        export_names.get(theory).flatMap(_.get(name)).map(html_context.export_ref)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   496
74678
wenzelm
parents: 74677
diff changeset
   497
      def offset_ref(context: Entity_Context, theory: String, props: Properties.T): Option[String] =
wenzelm
parents: 74677
diff changeset
   498
      {
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   499
        if (theory == context.node.theory) {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   500
          for {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   501
            offset <- Position.Def_Offset.unapply(props)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   502
            end_offset <- Position.Def_End_Offset.unapply(props)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   503
            range = Text.Range(offset, end_offset)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   504
          } yield {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   505
            context.seen_ranges += range
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   506
            html_context.offset_ref(range)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   507
          }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   508
        } else None
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   509
      }
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   510
74678
wenzelm
parents: 74677
diff changeset
   511
      def entity_link(
wenzelm
parents: 74677
diff changeset
   512
        context: Entity_Context, props: Properties.T, body: XML.Body): Option[XML.Elem] =
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   513
      {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   514
        (props, props, props, props) match {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   515
          case (Markup.Kind(Markup.THEORY), Markup.Name(theory), Position.Def_File(thy_file), _) =>
73057
45a34cc581b8 more robust and permissive;
wenzelm
parents: 73056
diff changeset
   516
            val node_name = resources.file_node(Path.explode(thy_file), theory = theory)
74678
wenzelm
parents: 74677
diff changeset
   517
            node_relative(deps, session, node_name).map(html_dir =>
wenzelm
parents: 74677
diff changeset
   518
              HTML.link(html_dir + html_name(node_name), body))
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   519
          case (Markup.Ref(_), Position.Def_File(thy_file), Position.Def_Theory(def_theory),
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   520
              Markup.Name(name)) =>
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   521
            val theory = if (def_theory.nonEmpty) def_theory else context.node.theory
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   522
            val node_name = resources.file_node(Path.explode(thy_file), theory = theory)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   523
            for {
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   524
              html_dir <- node_relative(deps, session, node_name)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   525
              html_file = node_file(node_name)
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   526
              html_ref <- entity_ref(theory, name).orElse(offset_ref(context, theory, props))
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   527
            } yield {
74679
0efa6a8b6e20 clarified link style: similar to Isabelle/jEdit;
wenzelm
parents: 74678
diff changeset
   528
              HTML.entity_ref(HTML.link(html_dir + html_file + "#" + html_ref, body))
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   529
            }
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   530
          case _ => None
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   531
        }
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   532
      }
73056
696819fe2424 present theory using PIDE markup;
wenzelm
parents: 73055
diff changeset
   533
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   534
      sealed case class Theory(
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   535
        name: Document.Node.Name,
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   536
        command: Command,
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   537
        files_html: List[(Path, XML.Tree)],
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   538
        html: XML.Tree)
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   539
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   540
      def read_theory(name: Document.Node.Name): Option[Theory] =
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   541
      {
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   542
        progress.expose_interrupt()
73216
60c32e2c5577 clarified messages;
wenzelm
parents: 73215
diff changeset
   543
        if (verbose) progress.echo("Presenting theory " + name)
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   544
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   545
        for (command <- Build_Job.read_theory(db_context, resources, session, name.theory))
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   546
        yield {
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   547
          val snapshot = Document.State.init.snippet(command)
73214
5de4a6ae6065 more parallel;
wenzelm
parents: 73120
diff changeset
   548
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   549
          val files_html =
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   550
            for {
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   551
              (src_path, xml) <- snapshot.xml_markup_blobs(elements = elements.html)
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   552
              if xml.nonEmpty
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   553
            }
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   554
            yield {
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   555
              progress.expose_interrupt()
73216
60c32e2c5577 clarified messages;
wenzelm
parents: 73215
diff changeset
   556
              if (verbose) progress.echo("Presenting file " + src_path)
60c32e2c5577 clarified messages;
wenzelm
parents: 73215
diff changeset
   557
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   558
              (src_path, html_context.source(
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   559
                make_html(name, elements, entity_anchor, entity_link, xml)))
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   560
            }
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   561
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   562
          val html =
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   563
            html_context.source(
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   564
              make_html(name, elements, entity_anchor, entity_link,
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   565
                snapshot.xml_markup(elements = elements.html)))
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   566
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   567
          Theory(name, command, files_html, html)
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   568
        }
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   569
      }
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   570
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   571
      for (thy <- Par_List.map(read_theory, base.session_theories).flatten)
72669
5e7916535860 generate theory HTML in Isabelle/Scala;
wenzelm
parents: 72665
diff changeset
   572
      yield {
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   573
        val files =
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   574
          for { (src_path, file_html) <- thy.files_html }
73043
759b6869377d misc tuning and clarification;
wenzelm
parents: 73042
diff changeset
   575
          yield {
74677
0d30ea76756c improved HTML presentation by Fabian Huch;
wenzelm
parents: 74676
diff changeset
   576
            val file_name = html_path(src_path)
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   577
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   578
            seen_files.find(p => p._1 == src_path || p._2 == file_name) match {
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   579
              case None => seen_files ::= (src_path, file_name, thy.name)
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   580
              case Some((_, _, thy_name1)) =>
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   581
                error("Incoherent use of file name " + src_path + " as " + quote(file_name) +
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   582
                  " in theory " + thy_name1 + " vs. " + thy.name)
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   583
            }
72669
5e7916535860 generate theory HTML in Isabelle/Scala;
wenzelm
parents: 72665
diff changeset
   584
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   585
            val file_path = session_dir + Path.explode(file_name)
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   586
            html_context.init_fonts(file_path.dir)
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   587
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   588
            val file_title = "File " + Symbol.cartouche_decoded(src_path.implode_short)
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   589
            HTML.write_document(file_path.dir, file_path.file_name,
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   590
              List(HTML.title(file_title)), List(html_context.head(file_title), file_html))
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   591
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   592
            List(HTML.link(file_name, HTML.text(file_title)))
73043
759b6869377d misc tuning and clarification;
wenzelm
parents: 73042
diff changeset
   593
          }
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   594
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   595
        val thy_title = "Theory " + thy.name.theory_base_name
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   596
73215
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   597
        HTML.write_document(session_dir, html_name(thy.name),
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   598
          List(HTML.title(thy_title)), List(html_context.head(thy_title), thy.html))
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   599
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   600
        List(HTML.link(html_name(thy.name),
a81ec42bac45 more parallel;
wenzelm
parents: 73214
diff changeset
   601
          HTML.text(thy.name.theory_base_name) :::
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   602
            (if (files.isEmpty) Nil else List(HTML.itemize(files)))))
72669
5e7916535860 generate theory HTML in Isabelle/Scala;
wenzelm
parents: 72665
diff changeset
   603
      }
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   604
    }
61500
56a167b31a7f Isabelle fonts via external component;
wenzelm
parents: 61484
diff changeset
   605
72622
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   606
    val title = "Session " + session
830222403681 HTML presentation in Isabelle/Scala, based on theory html exports from Isabelle/ML;
wenzelm
parents: 72600
diff changeset
   607
    HTML.write_document(session_dir, "index.html",
73523
2cd23d587db9 further clarification of Isabelle distribution identification -- avoid odd patching of sources;
wenzelm
parents: 73474
diff changeset
   608
      List(HTML.title(title + Isabelle_System.isabelle_heading())),
73041
wenzelm
parents: 73036
diff changeset
   609
      html_context.head(title, List(HTML.par(view_links))) ::
72989
5906162c8dd4 clarified presentation of files for each theory;
wenzelm
parents: 72963
diff changeset
   610
        html_context.contents("Theories", theories))
61372
cf40b6b1de54 more Present operations on Scala side;
wenzelm
parents: 60077
diff changeset
   611
  }
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents:
diff changeset
   612
}