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