src/Pure/Thy/html.scala
author wenzelm
Sat, 20 Nov 2021 20:42:41 +0100
changeset 74826 0e4d8aa61ad7
parent 74794 c606fddc5b05
child 74921 74655fd58f8e
permissions -rw-r--r--
more symbolic latex_output via XML (using YXML within text);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
33984
c54498f88a77 Basic HTML output.
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/Thy/html.scala
c54498f88a77 Basic HTML output.
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
c54498f88a77 Basic HTML output.
wenzelm
parents:
diff changeset
     3
50707
5b2bf7611662 maintain session index on Scala side, for more determistic results;
wenzelm
parents: 50201
diff changeset
     4
HTML presentation elements.
33984
c54498f88a77 Basic HTML output.
wenzelm
parents:
diff changeset
     5
*/
c54498f88a77 Basic HTML output.
wenzelm
parents:
diff changeset
     6
c54498f88a77 Basic HTML output.
wenzelm
parents:
diff changeset
     7
package isabelle
c54498f88a77 Basic HTML output.
wenzelm
parents:
diff changeset
     8
55618
995162143ef4 tuned imports;
wenzelm
parents: 54379
diff changeset
     9
33984
c54498f88a77 Basic HTML output.
wenzelm
parents:
diff changeset
    10
object HTML
c54498f88a77 Basic HTML output.
wenzelm
parents:
diff changeset
    11
{
73207
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    12
  /* attributes */
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    13
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    14
  class Attribute(val name: String, value: String)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    15
  {
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    16
    def xml: XML.Attribute = name -> value
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    17
    def apply(elem: XML.Elem): XML.Elem = elem + xml
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    18
  }
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    19
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    20
  def id(s: String): Attribute = new Attribute("id", s)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    21
  def class_(name: String): Attribute = new Attribute("class", name)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    22
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    23
  def width(w: Int): Attribute = new Attribute("width", w.toString)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    24
  def height(h: Int): Attribute = new Attribute("height", h.toString)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    25
  def size(w: Int, h: Int)(elem: XML.Elem): XML.Elem = width(w)(height(h)(elem))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    26
74679
0efa6a8b6e20 clarified link style: similar to Isabelle/jEdit;
wenzelm
parents: 74657
diff changeset
    27
  val entity_def: Attribute = class_("entity_def")
0efa6a8b6e20 clarified link style: similar to Isabelle/jEdit;
wenzelm
parents: 74657
diff changeset
    28
  val entity_ref: Attribute = class_("entity_ref")
0efa6a8b6e20 clarified link style: similar to Isabelle/jEdit;
wenzelm
parents: 74657
diff changeset
    29
73207
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    30
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    31
  /* structured markup operators */
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    32
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    33
  def text(txt: String): XML.Body = if (txt.isEmpty) Nil else List(XML.Text(txt))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    34
  val break: XML.Body = List(XML.elem("br"))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    35
  val nl: XML.Body = List(XML.Text("\n"))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    36
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    37
  class Operator(val name: String)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    38
  {
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    39
    def apply(body: XML.Body): XML.Elem = XML.elem(name, body)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    40
    def apply(att: Attribute, body: XML.Body): XML.Elem = att(apply(body))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    41
    def apply(c: String, body: XML.Body): XML.Elem = apply(class_(c), body)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    42
  }
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    43
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    44
  class Heading(name: String) extends Operator(name)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    45
  {
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    46
    def apply(txt: String): XML.Elem = super.apply(text(txt))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    47
    def apply(att: Attribute, txt: String): XML.Elem = super.apply(att, text(txt))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    48
    def apply(c: String, txt: String): XML.Elem = super.apply(c, text(txt))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    49
  }
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    50
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    51
  val div = new Operator("div")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    52
  val span = new Operator("span")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    53
  val pre = new Operator("pre")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    54
  val par = new Operator("p")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    55
  val sub = new Operator("sub")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    56
  val sup = new Operator("sup")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    57
  val emph = new Operator("em")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    58
  val bold = new Operator("b")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    59
  val code = new Operator("code")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    60
  val item = new Operator("li")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    61
  val list = new Operator("ul")
74657
9fcf80ceb863 updated to scala-2.13.7 --- problems with jline disappear after purging $HOME/.inputrc;
wenzelm
parents: 73340
diff changeset
    62
  val `enum` = new Operator("ol")
73207
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    63
  val descr = new Operator("dl")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    64
  val dt = new Operator("dt")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    65
  val dd = new Operator("dd")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    66
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    67
  val title = new Heading("title")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    68
  val chapter = new Heading("h1")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    69
  val section = new Heading("h2")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    70
  val subsection = new Heading("h3")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    71
  val subsubsection = new Heading("h4")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    72
  val paragraph = new Heading("h5")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    73
  val subparagraph = new Heading("h6")
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    74
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    75
  def itemize(items: List[XML.Body]): XML.Elem = list(items.map(item(_)))
74657
9fcf80ceb863 updated to scala-2.13.7 --- problems with jline disappear after purging $HOME/.inputrc;
wenzelm
parents: 73340
diff changeset
    76
  def enumerate(items: List[XML.Body]): XML.Elem = `enum`(items.map(item(_)))
73207
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    77
  def description(items: List[(XML.Body, XML.Body)]): XML.Elem =
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    78
    descr(items.flatMap({ case (x, y) => List(dt(x), dd(y)) }))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    79
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    80
  def link(href: String, body: XML.Body): XML.Elem =
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    81
    XML.Elem(Markup("a", List("href" -> href)), if (body.isEmpty) text(href) else body)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    82
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    83
  def link(path: Path, body: XML.Body): XML.Elem = link(path.implode, body)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    84
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    85
  def image(src: String, alt: String = ""): XML.Elem =
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    86
    XML.Elem(Markup("img", List("src" -> src) ::: proper_string(alt).map("alt" -> _).toList), Nil)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    87
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    88
  def source(body: XML.Body): XML.Elem = pre("source", body)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    89
  def source(src: String): XML.Elem = source(text(src))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    90
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    91
  def style(s: String): XML.Elem = XML.elem("style", text(s))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    92
  def style_file(href: String): XML.Elem =
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    93
    XML.Elem(Markup("link", List("rel" -> "stylesheet", "type" -> "text/css", "href" -> href)), Nil)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    94
  def style_file(path: Path): XML.Elem = style_file(Url.print_file(path.file))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    95
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    96
  def script(s: String): XML.Elem = XML.elem("script", text(s))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    97
  def script_file(href: String): XML.Elem = XML.Elem(Markup("script", List("src" -> href)), Nil)
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    98
  def script_file(path: Path): XML.Elem = script_file(Url.print_file(path.file))
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
    99
1ab0e1159e7c clarified signature: proper order;
wenzelm
parents: 73206
diff changeset
   100
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   101
  /* output text with control symbols */
59063
b3c45d0e4fe1 encode text with control symbols;
wenzelm
parents: 56748
diff changeset
   102
73208
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73207
diff changeset
   103
  private val control: Map[Symbol.Symbol, Operator] =
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   104
    Map(
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   105
      Symbol.sub -> sub, Symbol.sub_decoded -> sub,
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   106
      Symbol.sup -> sup, Symbol.sup_decoded -> sup,
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   107
      Symbol.bold -> bold, Symbol.bold_decoded -> bold)
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   108
73208
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73207
diff changeset
   109
  private val control_block_begin: Map[Symbol.Symbol, Operator] =
62104
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 60215
diff changeset
   110
    Map(
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   111
      Symbol.bsub -> sub, Symbol.bsub_decoded -> sub,
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   112
      Symbol.bsup -> sup, Symbol.bsup_decoded -> sup)
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   113
73208
e53f4c5927a1 tuned signature: more types;
wenzelm
parents: 73207
diff changeset
   114
  private val control_block_end: Map[Symbol.Symbol, Operator] =
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   115
    Map(
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   116
      Symbol.esub -> sub, Symbol.esub_decoded -> sub,
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   117
      Symbol.esup -> sup, Symbol.esup_decoded -> sup)
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   118
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   119
  def is_control(sym: Symbol.Symbol): Boolean = control.isDefinedAt(sym)
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   120
73209
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   121
  def is_control_block_begin(sym: Symbol.Symbol): Boolean = control_block_begin.isDefinedAt(sym)
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   122
  def is_control_block_end(sym: Symbol.Symbol): Boolean = control_block_end.isDefinedAt(sym)
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   123
  def is_control_block_pair(bg: Symbol.Symbol, en: Symbol.Symbol): Boolean =
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   124
  {
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   125
    val bg_decoded = Symbol.decode(bg)
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   126
    val en_decoded = Symbol.decode(en)
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   127
    bg_decoded == Symbol.bsub_decoded && en_decoded == Symbol.esub_decoded ||
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   128
    bg_decoded == Symbol.bsup_decoded && en_decoded == Symbol.esup_decoded
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   129
  }
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   130
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   131
  def check_control_blocks(body: XML.Body): Boolean =
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   132
  {
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   133
    var ok = true
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   134
    var open = List.empty[Symbol.Symbol]
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   135
    for { XML.Text(text) <- body; sym <- Symbol.iterator(text) } {
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   136
      if (is_control_block_begin(sym)) open ::= sym
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   137
      else if (is_control_block_end(sym)) {
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   138
        open match {
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   139
          case bg :: rest if is_control_block_pair(bg, sym) => open = rest
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   140
          case _ => ok = false
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   141
        }
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   142
      }
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   143
    }
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   144
    ok && open.isEmpty
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   145
  }
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   146
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   147
  def output(s: StringBuilder, text: String,
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73209
diff changeset
   148
    control_blocks: Boolean, hidden: Boolean, permissive: Boolean): Unit =
66018
9ce3720976dc permissive output of XML.Text, e.g. relevant for embedded <style>;
wenzelm
parents: 66006
diff changeset
   149
  {
9ce3720976dc permissive output of XML.Text, e.g. relevant for embedded <style>;
wenzelm
parents: 66006
diff changeset
   150
    def output_string(str: String): Unit =
73204
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   151
      XML.output_string(s, str, permissive = permissive)
66018
9ce3720976dc permissive output of XML.Text, e.g. relevant for embedded <style>;
wenzelm
parents: 66006
diff changeset
   152
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   153
    def output_hidden(body: => Unit): Unit =
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   154
      if (hidden) { s ++= "<span class=\"hidden\">"; body; s ++= "</span>" }
37200
0f3edc64356a added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents: 36016
diff changeset
   155
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   156
    def output_symbol(sym: Symbol.Symbol): Unit =
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   157
      if (sym != "") {
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   158
        control_block_begin.get(sym) match {
73209
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   159
          case Some(op) if control_blocks =>
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   160
            output_hidden(output_string(sym))
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   161
            XML.output_elem(s, Markup(op.name, Nil))
73209
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   162
          case _ =>
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   163
            control_block_end.get(sym) match {
73209
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   164
              case Some(op) if control_blocks =>
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   165
                XML.output_elem_end(s, op.name)
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   166
                output_hidden(output_string(sym))
73209
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   167
              case _ =>
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   168
                if (hidden && Symbol.is_control_encoded(sym)) {
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   169
                  output_hidden(output_string(Symbol.control_prefix))
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   170
                  s ++= "<span class=\"control\">"
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   171
                  output_string(Symbol.control_name(sym).get)
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   172
                  s ++= "</span>"
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   173
                  output_hidden(output_string(Symbol.control_suffix))
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   174
                }
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   175
                else output_string(sym)
67255
f1f983484878 HTML rendering of \<^control> as in Isabelle/jEdit;
wenzelm
parents: 66212
diff changeset
   176
            }
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   177
        }
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   178
      }
59063
b3c45d0e4fe1 encode text with control symbols;
wenzelm
parents: 56748
diff changeset
   179
62104
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 60215
diff changeset
   180
    var ctrl = ""
59063
b3c45d0e4fe1 encode text with control symbols;
wenzelm
parents: 56748
diff changeset
   181
    for (sym <- Symbol.iterator(text)) {
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   182
      if (is_control(sym)) { output_symbol(ctrl); ctrl = sym }
59063
b3c45d0e4fe1 encode text with control symbols;
wenzelm
parents: 56748
diff changeset
   183
      else {
62104
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 60215
diff changeset
   184
        control.get(ctrl) match {
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   185
          case Some(op) if Symbol.is_controllable(sym) =>
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   186
            output_hidden(output_symbol(ctrl))
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   187
            XML.output_elem(s, Markup(op.name, Nil))
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   188
            output_symbol(sym)
73206
3d881c1531f3 clarified signature: more explicit types;
wenzelm
parents: 73205
diff changeset
   189
            XML.output_elem_end(s, op.name)
59063
b3c45d0e4fe1 encode text with control symbols;
wenzelm
parents: 56748
diff changeset
   190
          case _ =>
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   191
            output_symbol(ctrl)
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   192
            output_symbol(sym)
59063
b3c45d0e4fe1 encode text with control symbols;
wenzelm
parents: 56748
diff changeset
   193
        }
62104
fb73c0d7bb37 clarified symbol insertion, depending on buffer encoding;
wenzelm
parents: 60215
diff changeset
   194
        ctrl = ""
59063
b3c45d0e4fe1 encode text with control symbols;
wenzelm
parents: 56748
diff changeset
   195
      }
37200
0f3edc64356a added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents: 36016
diff changeset
   196
    }
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   197
    output_symbol(ctrl)
64355
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   198
  }
59063
b3c45d0e4fe1 encode text with control symbols;
wenzelm
parents: 56748
diff changeset
   199
66018
9ce3720976dc permissive output of XML.Text, e.g. relevant for embedded <style>;
wenzelm
parents: 66006
diff changeset
   200
  def output(text: String): String =
73209
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   201
  {
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   202
    val control_blocks = check_control_blocks(List(XML.Text(text)))
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   203
    Library.make_string(output(_, text,
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   204
      control_blocks = control_blocks, hidden = false, permissive = true))
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   205
  }
64355
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   206
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   207
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   208
  /* output XML as HTML */
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   209
65834
67a6e0f166c2 extra space only for some structual elements, but not <a>, <b>, <em> etc. (amending 8a0fe5469ba0);
wenzelm
parents: 65773
diff changeset
   210
  private val structural_elements =
67a6e0f166c2 extra space only for some structual elements, but not <a>, <b>, <em> etc. (amending 8a0fe5469ba0);
wenzelm
parents: 65773
diff changeset
   211
    Set("head", "body", "meta", "div", "pre", "p", "title", "h1", "h2", "h3", "h4", "h5", "h6",
67a6e0f166c2 extra space only for some structual elements, but not <a>, <b>, <em> etc. (amending 8a0fe5469ba0);
wenzelm
parents: 65773
diff changeset
   212
      "ul", "ol", "dl", "li", "dt", "dd")
67a6e0f166c2 extra space only for some structual elements, but not <a>, <b>, <em> etc. (amending 8a0fe5469ba0);
wenzelm
parents: 65773
diff changeset
   213
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73209
diff changeset
   214
  def output(s: StringBuilder, xml: XML.Body, hidden: Boolean, structural: Boolean): Unit =
64355
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   215
  {
73205
wenzelm
parents: 73204
diff changeset
   216
    def output_body(body: XML.Body): Unit =
wenzelm
parents: 73204
diff changeset
   217
    {
73209
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   218
      val control_blocks = check_control_blocks(body)
73205
wenzelm
parents: 73204
diff changeset
   219
      body foreach {
64355
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   220
        case XML.Elem(markup, Nil) =>
73204
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   221
          XML.output_elem(s, markup, end = true)
64355
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   222
        case XML.Elem(markup, ts) =>
67337
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   223
          if (structural && structural_elements(markup.name)) s += '\n'
73204
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   224
          XML.output_elem(s, markup)
73205
wenzelm
parents: 73204
diff changeset
   225
          output_body(ts)
73204
aa3d4cf7825a clarified signature: no symbol markup within XML attributes;
wenzelm
parents: 73203
diff changeset
   226
          XML.output_elem_end(s, markup.name)
67337
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   227
          if (structural && structural_elements(markup.name)) s += '\n'
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   228
        case XML.Text(txt) =>
73209
de16d797adbe more robust HTML output: non-balanced bsub/esub are shown verbatim, e.g. within mixfix declarations (due to extra markup);
wenzelm
parents: 73208
diff changeset
   229
          output(s, txt, control_blocks = control_blocks, hidden = hidden, permissive = true)
64355
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   230
      }
73205
wenzelm
parents: 73204
diff changeset
   231
    }
wenzelm
parents: 73204
diff changeset
   232
    output_body(xml)
37200
0f3edc64356a added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents: 36016
diff changeset
   233
  }
0f3edc64356a added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents: 36016
diff changeset
   234
67337
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   235
  def output(body: XML.Body, hidden: Boolean, structural: Boolean): String =
74794
c606fddc5b05 slightly faster XML output: avoid too much regrowing of StringBuilder;
wenzelm
parents: 74754
diff changeset
   236
    Library.make_string(output(_, body, hidden, structural), capacity = XML.text_length(body) * 2)
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   237
67337
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   238
  def output(tree: XML.Tree, hidden: Boolean, structural: Boolean): String =
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   239
    output(List(tree), hidden, structural)
64355
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   240
c6a1031cf925 support for XML as HTML;
wenzelm
parents: 62113
diff changeset
   241
65892
bbad8162d678 clarified signature;
wenzelm
parents: 65891
diff changeset
   242
  /* messages */
bbad8162d678 clarified signature;
wenzelm
parents: 65891
diff changeset
   243
65939
9fb044904a4d support for message underline and tooltips;
wenzelm
parents: 65892
diff changeset
   244
  // background
65993
75590c9a585f tuned signature;
wenzelm
parents: 65992
diff changeset
   245
  val writeln_message: Attribute = class_("writeln_message")
75590c9a585f tuned signature;
wenzelm
parents: 65992
diff changeset
   246
  val warning_message: Attribute = class_("warning_message")
75590c9a585f tuned signature;
wenzelm
parents: 65992
diff changeset
   247
  val error_message: Attribute = class_("error_message")
65892
bbad8162d678 clarified signature;
wenzelm
parents: 65891
diff changeset
   248
65939
9fb044904a4d support for message underline and tooltips;
wenzelm
parents: 65892
diff changeset
   249
  // underline
65993
75590c9a585f tuned signature;
wenzelm
parents: 65992
diff changeset
   250
  val writeln: Attribute = class_("writeln")
75590c9a585f tuned signature;
wenzelm
parents: 65992
diff changeset
   251
  val warning: Attribute = class_("warning")
75590c9a585f tuned signature;
wenzelm
parents: 65992
diff changeset
   252
  val error: Attribute = class_("error")
65892
bbad8162d678 clarified signature;
wenzelm
parents: 65891
diff changeset
   253
65944
79e4d94aa9ad tuned signature;
wenzelm
parents: 65943
diff changeset
   254
79e4d94aa9ad tuned signature;
wenzelm
parents: 65943
diff changeset
   255
  /* tooltips */
79e4d94aa9ad tuned signature;
wenzelm
parents: 65943
diff changeset
   256
79e4d94aa9ad tuned signature;
wenzelm
parents: 65943
diff changeset
   257
  def tooltip(item: XML.Body, tip: XML.Body): XML.Elem =
65992
50daca61efd6 tuned signature;
wenzelm
parents: 65991
diff changeset
   258
    span(item ::: List(div("tooltip", tip)))
65944
79e4d94aa9ad tuned signature;
wenzelm
parents: 65943
diff changeset
   259
79e4d94aa9ad tuned signature;
wenzelm
parents: 65943
diff changeset
   260
  def tooltip_errors(item: XML.Body, msgs: List[XML.Body]): XML.Elem =
79e4d94aa9ad tuned signature;
wenzelm
parents: 65943
diff changeset
   261
    HTML.error(tooltip(item, msgs.map(msg => error_message(pre(msg)))))
65892
bbad8162d678 clarified signature;
wenzelm
parents: 65891
diff changeset
   262
bbad8162d678 clarified signature;
wenzelm
parents: 65891
diff changeset
   263
66196
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   264
  /* GUI elements */
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   265
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   266
  object GUI
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   267
  {
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   268
    private def optional_value(text: String): XML.Attributes =
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   269
      proper_string(text).map(a => "value" -> a).toList
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   270
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   271
    private def optional_name(name: String): XML.Attributes =
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   272
      proper_string(name).map(a => "name" -> a).toList
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   273
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   274
    private def optional_title(tooltip: String): XML.Attributes =
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   275
      proper_string(tooltip).map(a => "title" -> a).toList
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   276
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   277
    private def optional_submit(submit: Boolean): XML.Attributes =
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   278
      if (submit) List("onChange" -> "this.form.submit()") else Nil
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   279
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   280
    private def optional_checked(selected: Boolean): XML.Attributes =
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   281
      if (selected) List("checked" -> "") else Nil
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   282
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   283
    private def optional_action(action: String): XML.Attributes =
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   284
      proper_string(action).map(a => "action" -> a).toList
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   285
66210
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   286
    private def optional_onclick(script: String): XML.Attributes =
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   287
      proper_string(script).map(a => "onclick" -> a).toList
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   288
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   289
    private def optional_onchange(script: String): XML.Attributes =
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   290
      proper_string(script).map(a => "onchange" -> a).toList
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   291
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   292
    def button(body: XML.Body, name: String = "", tooltip: String = "", submit: Boolean = false,
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   293
        script: String = ""): XML.Elem =
66196
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   294
      XML.Elem(
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   295
        Markup("button",
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   296
          List("type" -> (if (submit) "submit" else "button"), "value" -> "true") :::
66210
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   297
          optional_name(name) ::: optional_title(tooltip) ::: optional_onclick(script)), body)
66196
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   298
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   299
    def checkbox(body: XML.Body, name: String = "", tooltip: String = "", submit: Boolean = false,
66210
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   300
        selected: Boolean = false, script: String = ""): XML.Elem =
66196
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   301
      XML.elem("label",
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   302
        XML.elem(
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   303
          Markup("input",
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   304
            List("type" -> "checkbox", "value" -> "true") ::: optional_name(name) :::
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   305
              optional_title(tooltip) ::: optional_submit(submit) :::
66210
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   306
              optional_checked(selected) ::: optional_onchange(script))) :: body)
66196
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   307
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   308
    def text_field(columns: Int = 0, text: String = "", name: String = "", tooltip: String = "",
66210
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   309
        submit: Boolean = false, script: String = ""): XML.Elem =
66196
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   310
      XML.elem(Markup("input",
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   311
        List("type" -> "text") :::
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   312
          (if (columns > 0) List("size" -> columns.toString) else Nil) :::
66210
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   313
          optional_value(text) ::: optional_name(name) ::: optional_title(tooltip) :::
a8b936749300 more operations/options;
wenzelm
parents: 66209
diff changeset
   314
          optional_submit(submit) ::: optional_onchange(script)))
66196
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   315
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   316
    def parameter(text: String = "", name: String = ""): XML.Elem =
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   317
      XML.elem(
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   318
        Markup("input", List("type" -> "hidden") ::: optional_value(text) ::: optional_name(name)))
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   319
66209
3a4dfe10ab1a clarified signature;
wenzelm
parents: 66206
diff changeset
   320
    def form(body: XML.Body, name: String = "", action: String = "", http_post: Boolean = false)
3a4dfe10ab1a clarified signature;
wenzelm
parents: 66206
diff changeset
   321
        : XML.Elem =
3a4dfe10ab1a clarified signature;
wenzelm
parents: 66206
diff changeset
   322
      XML.Elem(
3a4dfe10ab1a clarified signature;
wenzelm
parents: 66206
diff changeset
   323
        Markup("form", optional_name(name) ::: optional_action(action) :::
3a4dfe10ab1a clarified signature;
wenzelm
parents: 66206
diff changeset
   324
          (if (http_post) List("method" -> "post") else Nil)), body)
66196
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   325
  }
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   326
31c9b09cc1d4 some HTML GUI elements;
wenzelm
parents: 66040
diff changeset
   327
66200
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   328
  /* GUI layout */
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   329
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   330
  object Wrap_Panel
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   331
  {
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   332
    object Alignment extends Enumeration
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   333
    {
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   334
      val left, right, center = Value
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   335
    }
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   336
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   337
    def apply(contents: List[XML.Elem], name: String = "", action: String = "",
66206
2d2082db735a clarified defaults;
wenzelm
parents: 66200
diff changeset
   338
      alignment: Alignment.Value = Alignment.right): XML.Elem =
66200
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   339
    {
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   340
      val body = Library.separate(XML.Text(" "), contents)
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   341
      GUI.form(List(div(body) + ("style" -> ("text-align: " + alignment))),
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   342
        name = name, action = action)
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   343
    }
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   344
  }
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   345
02c66b71c013 GUI layout similar to Pure/GUI/wrap_panel.scala;
wenzelm
parents: 66196
diff changeset
   346
51399
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
   347
  /* document */
6ac3c29a300e discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents: 50707
diff changeset
   348
64359
27739e1d7978 more operations;
wenzelm
parents: 64356
diff changeset
   349
  val header: String =
69804
9efccbad7d42 uniform XML header;
wenzelm
parents: 69374
diff changeset
   350
    XML.header +
9efccbad7d42 uniform XML header;
wenzelm
parents: 69374
diff changeset
   351
    """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
64359
27739e1d7978 more operations;
wenzelm
parents: 64356
diff changeset
   352
<html xmlns="http://www.w3.org/1999/xhtml">"""
27739e1d7978 more operations;
wenzelm
parents: 64356
diff changeset
   353
73129
ff9cd62d2d20 proper </html>;
wenzelm
parents: 73028
diff changeset
   354
  val footer: String = """</html>"""
ff9cd62d2d20 proper </html>;
wenzelm
parents: 73028
diff changeset
   355
64359
27739e1d7978 more operations;
wenzelm
parents: 64356
diff changeset
   356
  val head_meta: XML.Elem =
27739e1d7978 more operations;
wenzelm
parents: 64356
diff changeset
   357
    XML.Elem(Markup("meta",
27739e1d7978 more operations;
wenzelm
parents: 64356
diff changeset
   358
      List("http-equiv" -> "Content-Type", "content" -> "text/html; charset=utf-8")), Nil)
27739e1d7978 more operations;
wenzelm
parents: 64356
diff changeset
   359
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   360
  def output_document(head: XML.Body, body: XML.Body,
67337
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   361
    css: String = "isabelle.css",
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   362
    hidden: Boolean = true,
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   363
    structural: Boolean = true): String =
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   364
  {
64359
27739e1d7978 more operations;
wenzelm
parents: 64356
diff changeset
   365
    cat_lines(
73129
ff9cd62d2d20 proper </html>;
wenzelm
parents: 73028
diff changeset
   366
      List(
ff9cd62d2d20 proper </html>;
wenzelm
parents: 73028
diff changeset
   367
        header,
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   368
        output(
66000
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   369
          XML.elem("head", head_meta :: (if (css == "") Nil else List(style_file(css))) ::: head),
67337
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   370
          hidden = hidden, structural = structural),
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   371
        output(XML.elem("body", body),
73129
ff9cd62d2d20 proper </html>;
wenzelm
parents: 73028
diff changeset
   372
          hidden = hidden, structural = structural),
ff9cd62d2d20 proper </html>;
wenzelm
parents: 73028
diff changeset
   373
        footer))
65997
e3dc9ea67a62 output control symbols like ML version, with optionally hidden source;
wenzelm
parents: 65993
diff changeset
   374
  }
64359
27739e1d7978 more operations;
wenzelm
parents: 64356
diff changeset
   375
65838
30c2d78b5d38 tuned signature;
wenzelm
parents: 65836
diff changeset
   376
66000
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   377
  /* fonts */
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   378
74709
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   379
  val fonts_path: Path = Path.explode("fonts")
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   380
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   381
  def init_fonts(dir: Path): Unit =
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   382
  {
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   383
    val fonts_dir = Isabelle_System.make_directory(dir + HTML.fonts_path)
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   384
    for (entry <- Isabelle_Fonts.fonts(hidden = true))
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   385
      Isabelle_System.copy_file(entry.path, fonts_dir)
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   386
  }
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   387
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   388
  def fonts_dir(prefix: String)(ttf_name: String): String = prefix + "/" + ttf_name
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   389
66000
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   390
  def fonts_url(): String => String =
69374
ab66951166f3 clarified "hidden" terminology;
wenzelm
parents: 69366
diff changeset
   391
    (for (entry <- Isabelle_Fonts.fonts(hidden = true))
70743
342b0a1fc86d proper file name instead of font name (amending dc9a39c3f75d);
wenzelm
parents: 69804
diff changeset
   392
     yield (entry.path.file_name -> Url.print_file(entry.path.file))).toMap
66000
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   393
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   394
  def fonts_css(make_url: String => String = fonts_url()): String =
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   395
  {
69360
dc9a39c3f75d more explicit Isabelle_Fonts.Entry;
wenzelm
parents: 69356
diff changeset
   396
    def font_face(entry: Isabelle_Fonts.Entry): String =
66000
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   397
      cat_lines(
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   398
        List(
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   399
          "@font-face {",
69360
dc9a39c3f75d more explicit Isabelle_Fonts.Entry;
wenzelm
parents: 69356
diff changeset
   400
          "  font-family: '" + entry.family + "';",
69366
b6dacf6eabe3 clarified signature;
wenzelm
parents: 69362
diff changeset
   401
          "  src: url('" + make_url(entry.path.file_name) + "') format('truetype');") :::
69360
dc9a39c3f75d more explicit Isabelle_Fonts.Entry;
wenzelm
parents: 69356
diff changeset
   402
        (if (entry.is_bold) List("  font-weight: bold;") else Nil) :::
dc9a39c3f75d more explicit Isabelle_Fonts.Entry;
wenzelm
parents: 69356
diff changeset
   403
        (if (entry.is_italic) List("  font-style: italic;") else Nil) :::
66000
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   404
        List("}"))
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   405
71601
97ccf48c2f0c misc tuning based on hints by IntelliJ IDEA;
wenzelm
parents: 70743
diff changeset
   406
    ("/* Isabelle fonts */" :: Isabelle_Fonts.fonts(hidden = true).map(font_face))
69362
77c93eaf6cb7 tuned whitespace;
wenzelm
parents: 69360
diff changeset
   407
      .mkString("", "\n\n", "\n")
66000
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   408
  }
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   409
74709
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   410
  def fonts_css_dir(prefix: String = ""): String =
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   411
  {
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   412
    val prefix1 = if (prefix.isEmpty || prefix.endsWith("/")) prefix else prefix + "/"
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   413
    fonts_css(fonts_dir(prefix1 + fonts_path.implode))
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   414
  }
66000
58aa6749ff36 generate CSS for Isabelle fonts;
wenzelm
parents: 65999
diff changeset
   415
74709
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   416
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   417
  /* document directory context (fonts + css) */
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   418
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   419
  def relative_prefix(dir: Path, base: Option[Path]): String =
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   420
    base match {
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   421
      case None => ""
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   422
      case Some(base_dir) =>
74754
eaeab1358ced more robust;
wenzelm
parents: 74709
diff changeset
   423
        val path = File.path(dir.absolute.java_path.relativize(base_dir.absolute.java_path).toFile)
eaeab1358ced more robust;
wenzelm
parents: 74709
diff changeset
   424
        if (path.is_current) "" else path.implode + "/"
74709
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   425
    }
65838
30c2d78b5d38 tuned signature;
wenzelm
parents: 65836
diff changeset
   426
65998
d07300e8a14d tuned signature;
wenzelm
parents: 65997
diff changeset
   427
  def isabelle_css: Path = Path.explode("~~/etc/isabelle.css")
d07300e8a14d tuned signature;
wenzelm
parents: 65997
diff changeset
   428
65838
30c2d78b5d38 tuned signature;
wenzelm
parents: 65836
diff changeset
   429
  def write_document(dir: Path, name: String, head: XML.Body, body: XML.Body,
74709
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   430
    base: Option[Path] = None,
69366
b6dacf6eabe3 clarified signature;
wenzelm
parents: 69362
diff changeset
   431
    css: String = isabelle_css.file_name,
67337
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   432
    hidden: Boolean = true,
73340
0ffcad1f6130 tuned --- fewer warnings;
wenzelm
parents: 73209
diff changeset
   433
    structural: Boolean = true): Unit =
65838
30c2d78b5d38 tuned signature;
wenzelm
parents: 65836
diff changeset
   434
  {
74709
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   435
    Isabelle_System.make_directory(dir)
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   436
    val prefix = relative_prefix(dir, base)
d73a7e3c618c avoid multiple copies of fonts;
wenzelm
parents: 74679
diff changeset
   437
    File.write(dir + isabelle_css.base, fonts_css_dir(prefix) + "\n\n" + File.read(isabelle_css))
67337
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   438
    File.write(dir + Path.basic(name),
4254cfd15b00 more tight HTML output: avoid extra lines within <pre>;
wenzelm
parents: 67336
diff changeset
   439
      output_document(head, body, css = css, hidden = hidden, structural = structural))
65838
30c2d78b5d38 tuned signature;
wenzelm
parents: 65836
diff changeset
   440
  }
33984
c54498f88a77 Basic HTML output.
wenzelm
parents:
diff changeset
   441
}