| author | wenzelm | 
| Tue, 23 Jan 2024 19:56:52 +0100 | |
| changeset 79521 | db2b5c04075d | 
| parent 79498 | 752188c16c22 | 
| child 80000 | 11a1f4d7af51 | 
| 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) | 
| 64355 | 242 | case XML.Elem(markup, ts) => | 
| 67337 
4254cfd15b00
more tight HTML output: avoid extra lines within <pre>;
 wenzelm parents: 
67336diff
changeset | 243 | if (structural && structural_elements(markup.name)) s += '\n' | 
| 73204 
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
 wenzelm parents: 
73203diff
changeset | 244 | XML.output_elem(s, markup) | 
| 73205 | 245 | output_body(ts) | 
| 73204 
aa3d4cf7825a
clarified signature: no symbol markup within XML attributes;
 wenzelm parents: 
73203diff
changeset | 246 | XML.output_elem_end(s, markup.name) | 
| 67337 
4254cfd15b00
more tight HTML output: avoid extra lines within <pre>;
 wenzelm parents: 
67336diff
changeset | 247 | if (structural && structural_elements(markup.name)) s += '\n' | 
| 65997 
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
 wenzelm parents: 
65993diff
changeset | 248 | 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 | 249 | output(s, txt, control_blocks = control_blocks, hidden = hidden, permissive = true) | 
| 64355 | 250 | } | 
| 73205 | 251 | } | 
| 252 | output_body(xml) | |
| 37200 
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
 wenzelm parents: 
36016diff
changeset | 253 | } | 
| 
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
 wenzelm parents: 
36016diff
changeset | 254 | |
| 67337 
4254cfd15b00
more tight HTML output: avoid extra lines within <pre>;
 wenzelm parents: 
67336diff
changeset | 255 | 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 | 256 | 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 | 257 | |
| 67337 
4254cfd15b00
more tight HTML output: avoid extra lines within <pre>;
 wenzelm parents: 
67336diff
changeset | 258 | def output(tree: XML.Tree, hidden: Boolean, structural: Boolean): String = | 
| 
4254cfd15b00
more tight HTML output: avoid extra lines within <pre>;
 wenzelm parents: 
67336diff
changeset | 259 | output(List(tree), hidden, structural) | 
| 64355 | 260 | |
| 261 | ||
| 77620 | 262 | /* input */ | 
| 74921 | 263 | |
| 77620 | 264 | def input(text: String): String = Jsoup_Entities.unescape(text) | 
| 265 | ||
| 266 | def parse_document(html: String): Jsoup_Document = Jsoup.parse(html) | |
| 267 | def get_document(url: String): Jsoup_Document = Jsoup.connect(url).get() | |
| 74921 | 268 | |
| 269 | ||
| 65892 | 270 | /* messages */ | 
| 271 | ||
| 65939 | 272 | // background | 
| 65993 | 273 |   val writeln_message: Attribute = class_("writeln_message")
 | 
| 274 |   val warning_message: Attribute = class_("warning_message")
 | |
| 275 |   val error_message: Attribute = class_("error_message")
 | |
| 65892 | 276 | |
| 65939 | 277 | // underline | 
| 65993 | 278 |   val writeln: Attribute = class_("writeln")
 | 
| 279 |   val warning: Attribute = class_("warning")
 | |
| 280 |   val error: Attribute = class_("error")
 | |
| 65892 | 281 | |
| 65944 | 282 | |
| 283 | /* tooltips */ | |
| 284 | ||
| 285 | def tooltip(item: XML.Body, tip: XML.Body): XML.Elem = | |
| 65992 | 286 |     span(item ::: List(div("tooltip", tip)))
 | 
| 65944 | 287 | |
| 288 | def tooltip_errors(item: XML.Body, msgs: List[XML.Body]): XML.Elem = | |
| 289 | HTML.error(tooltip(item, msgs.map(msg => error_message(pre(msg))))) | |
| 65892 | 290 | |
| 291 | ||
| 66196 | 292 | /* GUI elements */ | 
| 293 | ||
| 75393 | 294 |   object GUI {
 | 
| 66196 | 295 | private def optional_value(text: String): XML.Attributes = | 
| 296 | proper_string(text).map(a => "value" -> a).toList | |
| 297 | ||
| 298 | private def optional_name(name: String): XML.Attributes = | |
| 299 | proper_string(name).map(a => "name" -> a).toList | |
| 300 | ||
| 301 | private def optional_title(tooltip: String): XML.Attributes = | |
| 302 | proper_string(tooltip).map(a => "title" -> a).toList | |
| 303 | ||
| 304 | private def optional_submit(submit: Boolean): XML.Attributes = | |
| 305 |       if (submit) List("onChange" -> "this.form.submit()") else Nil
 | |
| 306 | ||
| 307 | private def optional_checked(selected: Boolean): XML.Attributes = | |
| 308 |       if (selected) List("checked" -> "") else Nil
 | |
| 309 | ||
| 310 | private def optional_action(action: String): XML.Attributes = | |
| 311 | proper_string(action).map(a => "action" -> a).toList | |
| 312 | ||
| 66210 | 313 | private def optional_onclick(script: String): XML.Attributes = | 
| 314 | proper_string(script).map(a => "onclick" -> a).toList | |
| 315 | ||
| 316 | private def optional_onchange(script: String): XML.Attributes = | |
| 317 | proper_string(script).map(a => "onchange" -> a).toList | |
| 318 | ||
| 319 | def button(body: XML.Body, name: String = "", tooltip: String = "", submit: Boolean = false, | |
| 320 | script: String = ""): XML.Elem = | |
| 66196 | 321 | XML.Elem( | 
| 322 |         Markup("button",
 | |
| 323 |           List("type" -> (if (submit) "submit" else "button"), "value" -> "true") :::
 | |
| 66210 | 324 | optional_name(name) ::: optional_title(tooltip) ::: optional_onclick(script)), body) | 
| 66196 | 325 | |
| 326 | def checkbox(body: XML.Body, name: String = "", tooltip: String = "", submit: Boolean = false, | |
| 66210 | 327 | selected: Boolean = false, script: String = ""): XML.Elem = | 
| 66196 | 328 |       XML.elem("label",
 | 
| 329 | XML.elem( | |
| 330 |           Markup("input",
 | |
| 331 |             List("type" -> "checkbox", "value" -> "true") ::: optional_name(name) :::
 | |
| 332 | optional_title(tooltip) ::: optional_submit(submit) ::: | |
| 66210 | 333 | optional_checked(selected) ::: optional_onchange(script))) :: body) | 
| 66196 | 334 | |
| 335 | def text_field(columns: Int = 0, text: String = "", name: String = "", tooltip: String = "", | |
| 66210 | 336 | submit: Boolean = false, script: String = ""): XML.Elem = | 
| 66196 | 337 |       XML.elem(Markup("input",
 | 
| 338 |         List("type" -> "text") :::
 | |
| 339 |           (if (columns > 0) List("size" -> columns.toString) else Nil) :::
 | |
| 66210 | 340 | optional_value(text) ::: optional_name(name) ::: optional_title(tooltip) ::: | 
| 341 | optional_submit(submit) ::: optional_onchange(script))) | |
| 66196 | 342 | |
| 343 | def parameter(text: String = "", name: String = ""): XML.Elem = | |
| 344 | XML.elem( | |
| 345 |         Markup("input", List("type" -> "hidden") ::: optional_value(text) ::: optional_name(name)))
 | |
| 346 | ||
| 66209 | 347 | def form(body: XML.Body, name: String = "", action: String = "", http_post: Boolean = false) | 
| 348 | : XML.Elem = | |
| 349 | XML.Elem( | |
| 350 |         Markup("form", optional_name(name) ::: optional_action(action) :::
 | |
| 351 |           (if (http_post) List("method" -> "post") else Nil)), body)
 | |
| 66196 | 352 | } | 
| 353 | ||
| 354 | ||
| 66200 | 355 | /* GUI layout */ | 
| 356 | ||
| 75393 | 357 |   object Wrap_Panel {
 | 
| 78603 | 358 |     enum Alignment { case left, right, center }
 | 
| 66200 | 359 | |
| 75393 | 360 | def apply( | 
| 361 | contents: List[XML.Elem], | |
| 362 | name: String = "", | |
| 363 | action: String = "", | |
| 78603 | 364 | alignment: Alignment = Alignment.right | 
| 75393 | 365 |     ): XML.Elem = {
 | 
| 66200 | 366 |       val body = Library.separate(XML.Text(" "), contents)
 | 
| 367 |       GUI.form(List(div(body) + ("style" -> ("text-align: " + alignment))),
 | |
| 368 | name = name, action = action) | |
| 369 | } | |
| 370 | } | |
| 371 | ||
| 372 | ||
| 51399 
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
 wenzelm parents: 
50707diff
changeset | 373 | /* document */ | 
| 
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
 wenzelm parents: 
50707diff
changeset | 374 | |
| 64359 | 375 | val header: String = | 
| 69804 | 376 | XML.header + | 
| 377 | """<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> | |
| 64359 | 378 | <html xmlns="http://www.w3.org/1999/xhtml">""" | 
| 379 | ||
| 73129 | 380 | val footer: String = """</html>""" | 
| 381 | ||
| 64359 | 382 | val head_meta: XML.Elem = | 
| 383 |     XML.Elem(Markup("meta",
 | |
| 384 |       List("http-equiv" -> "Content-Type", "content" -> "text/html; charset=utf-8")), Nil)
 | |
| 385 | ||
| 75393 | 386 | def output_document( | 
| 387 | head: XML.Body, | |
| 388 | body: XML.Body, | |
| 67337 
4254cfd15b00
more tight HTML output: avoid extra lines within <pre>;
 wenzelm parents: 
67336diff
changeset | 389 | css: String = "isabelle.css", | 
| 
4254cfd15b00
more tight HTML output: avoid extra lines within <pre>;
 wenzelm parents: 
67336diff
changeset | 390 | hidden: Boolean = true, | 
| 75393 | 391 | structural: Boolean = true | 
| 392 |   ): String = {
 | |
| 64359 | 393 | cat_lines( | 
| 73129 | 394 | List( | 
| 395 | header, | |
| 65997 
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
 wenzelm parents: 
65993diff
changeset | 396 | output( | 
| 66000 | 397 |           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 | 398 | hidden = hidden, structural = structural), | 
| 
4254cfd15b00
more tight HTML output: avoid extra lines within <pre>;
 wenzelm parents: 
67336diff
changeset | 399 |         output(XML.elem("body", body),
 | 
| 73129 | 400 | hidden = hidden, structural = structural), | 
| 401 | footer)) | |
| 65997 
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
 wenzelm parents: 
65993diff
changeset | 402 | } | 
| 64359 | 403 | |
| 65838 | 404 | |
| 66000 | 405 | /* fonts */ | 
| 406 | ||
| 74709 | 407 |   val fonts_path: Path = Path.explode("fonts")
 | 
| 408 | ||
| 75393 | 409 |   def init_fonts(dir: Path): Unit = {
 | 
| 74709 | 410 | val fonts_dir = Isabelle_System.make_directory(dir + HTML.fonts_path) | 
| 411 | for (entry <- Isabelle_Fonts.fonts(hidden = true)) | |
| 412 | Isabelle_System.copy_file(entry.path, fonts_dir) | |
| 413 | } | |
| 414 | ||
| 415 | def fonts_dir(prefix: String)(ttf_name: String): String = prefix + "/" + ttf_name | |
| 416 | ||
| 66000 | 417 | def fonts_url(): String => String = | 
| 69374 | 418 | (for (entry <- Isabelle_Fonts.fonts(hidden = true)) | 
| 70743 
342b0a1fc86d
proper file name instead of font name (amending dc9a39c3f75d);
 wenzelm parents: 
69804diff
changeset | 419 | yield (entry.path.file_name -> Url.print_file(entry.path.file))).toMap | 
| 66000 | 420 | |
| 75393 | 421 |   def fonts_css(make_url: String => String = fonts_url()): String = {
 | 
| 69360 | 422 | def font_face(entry: Isabelle_Fonts.Entry): String = | 
| 66000 | 423 | cat_lines( | 
| 424 | List( | |
| 425 |           "@font-face {",
 | |
| 69360 | 426 | " font-family: '" + entry.family + "';", | 
| 69366 | 427 |           "  src: url('" + make_url(entry.path.file_name) + "') format('truetype');") :::
 | 
| 69360 | 428 |         (if (entry.is_bold) List("  font-weight: bold;") else Nil) :::
 | 
| 429 |         (if (entry.is_italic) List("  font-style: italic;") else Nil) :::
 | |
| 66000 | 430 |         List("}"))
 | 
| 431 | ||
| 71601 | 432 |     ("/* Isabelle fonts */" :: Isabelle_Fonts.fonts(hidden = true).map(font_face))
 | 
| 69362 | 433 |       .mkString("", "\n\n", "\n")
 | 
| 66000 | 434 | } | 
| 435 | ||
| 76618 | 436 | def fonts_css_dir(prefix: String = ""): String = | 
| 437 | fonts_css(fonts_dir(Url.append_path(prefix, fonts_path.implode))) | |
| 66000 | 438 | |
| 74709 | 439 | |
| 440 | /* document directory context (fonts + css) */ | |
| 441 | ||
| 65998 | 442 |   def isabelle_css: Path = Path.explode("~~/etc/isabelle.css")
 | 
| 443 | ||
| 75940 
c6edbc025fae
clarified signature: terminology of "base" (here) vs. "root" (there);
 wenzelm parents: 
75939diff
changeset | 444 | 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 | 445 | root: Option[Path] = None, | 
| 69366 | 446 | css: String = isabelle_css.file_name, | 
| 67337 
4254cfd15b00
more tight HTML output: avoid extra lines within <pre>;
 wenzelm parents: 
67336diff
changeset | 447 | hidden: Boolean = true, | 
| 75393 | 448 | structural: Boolean = true | 
| 449 |   ): Unit = {
 | |
| 75940 
c6edbc025fae
clarified signature: terminology of "base" (here) vs. "root" (there);
 wenzelm parents: 
75939diff
changeset | 450 | Isabelle_System.make_directory(base_dir) | 
| 
c6edbc025fae
clarified signature: terminology of "base" (here) vs. "root" (there);
 wenzelm parents: 
75939diff
changeset | 451 | 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 | 452 | val fonts = fonts_css_dir(fonts_prefix) | 
| 75940 
c6edbc025fae
clarified signature: terminology of "base" (here) vs. "root" (there);
 wenzelm parents: 
75939diff
changeset | 453 | 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 | 454 | File.write(base_dir + Path.basic(name), | 
| 67337 
4254cfd15b00
more tight HTML output: avoid extra lines within <pre>;
 wenzelm parents: 
67336diff
changeset | 455 | output_document(head, body, css = css, hidden = hidden, structural = structural)) | 
| 65838 | 456 | } | 
| 33984 | 457 | } |