author | wenzelm |
Sat, 20 Nov 2021 20:42:41 +0100 | |
changeset 74826 | 0e4d8aa61ad7 |
parent 74794 | c606fddc5b05 |
child 74921 | 74655fd58f8e |
permissions | -rw-r--r-- |
33984 | 1 |
/* Title: Pure/Thy/html.scala |
2 |
Author: Makarius |
|
3 |
||
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
50201
diff
changeset
|
4 |
HTML presentation elements. |
33984 | 5 |
*/ |
6 |
||
7 |
package isabelle |
|
8 |
||
55618 | 9 |
|
33984 | 10 |
object HTML |
11 |
{ |
|
73207 | 12 |
/* attributes */ |
13 |
||
14 |
class Attribute(val name: String, value: String) |
|
15 |
{ |
|
16 |
def xml: XML.Attribute = name -> value |
|
17 |
def apply(elem: XML.Elem): XML.Elem = elem + xml |
|
18 |
} |
|
19 |
||
20 |
def id(s: String): Attribute = new Attribute("id", s) |
|
21 |
def class_(name: String): Attribute = new Attribute("class", name) |
|
22 |
||
23 |
def width(w: Int): Attribute = new Attribute("width", w.toString) |
|
24 |
def height(h: Int): Attribute = new Attribute("height", h.toString) |
|
25 |
def size(w: Int, h: Int)(elem: XML.Elem): XML.Elem = width(w)(height(h)(elem)) |
|
26 |
||
74679 | 27 |
val entity_def: Attribute = class_("entity_def") |
28 |
val entity_ref: Attribute = class_("entity_ref") |
|
29 |
||
73207 | 30 |
|
31 |
/* structured markup operators */ |
|
32 |
||
33 |
def text(txt: String): XML.Body = if (txt.isEmpty) Nil else List(XML.Text(txt)) |
|
34 |
val break: XML.Body = List(XML.elem("br")) |
|
35 |
val nl: XML.Body = List(XML.Text("\n")) |
|
36 |
||
37 |
class Operator(val name: String) |
|
38 |
{ |
|
39 |
def apply(body: XML.Body): XML.Elem = XML.elem(name, body) |
|
40 |
def apply(att: Attribute, body: XML.Body): XML.Elem = att(apply(body)) |
|
41 |
def apply(c: String, body: XML.Body): XML.Elem = apply(class_(c), body) |
|
42 |
} |
|
43 |
||
44 |
class Heading(name: String) extends Operator(name) |
|
45 |
{ |
|
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:
73340
diff
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:
73340
diff
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 |
||
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
101 |
/* output text with control symbols */ |
59063 | 102 |
|
73208 | 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 | 105 |
Symbol.sub -> sub, Symbol.sub_decoded -> sub, |
106 |
Symbol.sup -> sup, Symbol.sup_decoded -> sup, |
|
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 | 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 | 111 |
Symbol.bsub -> sub, Symbol.bsub_decoded -> sub, |
112 |
Symbol.bsup -> sup, Symbol.bsup_decoded -> sup) |
|
113 |
||
73208 | 114 |
private val control_block_end: Map[Symbol.Symbol, Operator] = |
73206 | 115 |
Map( |
116 |
Symbol.esub -> sub, Symbol.esub_decoded -> sub, |
|
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 | 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 | 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 | 160 |
output_hidden(output_string(sym)) |
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 | 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 | 165 |
XML.output_elem_end(s, op.name) |
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 | 168 |
if (hidden && Symbol.is_control_encoded(sym)) { |
169 |
output_hidden(output_string(Symbol.control_prefix)) |
|
170 |
s ++= "<span class=\"control\">" |
|
171 |
output_string(Symbol.control_name(sym).get) |
|
172 |
s ++= "</span>" |
|
173 |
output_hidden(output_string(Symbol.control_suffix)) |
|
174 |
} |
|
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 | 179 |
|
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
60215
diff
changeset
|
180 |
var ctrl = "" |
59063 | 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 | 183 |
else { |
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
60215
diff
changeset
|
184 |
control.get(ctrl) match { |
73206 | 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 | 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 | 189 |
XML.output_elem_end(s, op.name) |
59063 | 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 | 193 |
} |
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
60215
diff
changeset
|
194 |
ctrl = "" |
59063 | 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 | 198 |
} |
59063 | 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 | 206 |
|
207 |
||
208 |
/* output XML as HTML */ |
|
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 | 214 |
def output(s: StringBuilder, xml: XML.Body, hidden: Boolean, structural: Boolean): Unit = |
64355 | 215 |
{ |
73205 | 216 |
def output_body(body: XML.Body): Unit = |
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 | 219 |
body foreach { |
64355 | 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 | 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 | 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 | 230 |
} |
73205 | 231 |
} |
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 | 240 |
|
241 |
||
65892 | 242 |
/* messages */ |
243 |
||
65939 | 244 |
// background |
65993 | 245 |
val writeln_message: Attribute = class_("writeln_message") |
246 |
val warning_message: Attribute = class_("warning_message") |
|
247 |
val error_message: Attribute = class_("error_message") |
|
65892 | 248 |
|
65939 | 249 |
// underline |
65993 | 250 |
val writeln: Attribute = class_("writeln") |
251 |
val warning: Attribute = class_("warning") |
|
252 |
val error: Attribute = class_("error") |
|
65892 | 253 |
|
65944 | 254 |
|
255 |
/* tooltips */ |
|
256 |
||
257 |
def tooltip(item: XML.Body, tip: XML.Body): XML.Elem = |
|
65992 | 258 |
span(item ::: List(div("tooltip", tip))) |
65944 | 259 |
|
260 |
def tooltip_errors(item: XML.Body, msgs: List[XML.Body]): XML.Elem = |
|
261 |
HTML.error(tooltip(item, msgs.map(msg => error_message(pre(msg))))) |
|
65892 | 262 |
|
263 |
||
66196 | 264 |
/* GUI elements */ |
265 |
||
266 |
object GUI |
|
267 |
{ |
|
268 |
private def optional_value(text: String): XML.Attributes = |
|
269 |
proper_string(text).map(a => "value" -> a).toList |
|
270 |
||
271 |
private def optional_name(name: String): XML.Attributes = |
|
272 |
proper_string(name).map(a => "name" -> a).toList |
|
273 |
||
274 |
private def optional_title(tooltip: String): XML.Attributes = |
|
275 |
proper_string(tooltip).map(a => "title" -> a).toList |
|
276 |
||
277 |
private def optional_submit(submit: Boolean): XML.Attributes = |
|
278 |
if (submit) List("onChange" -> "this.form.submit()") else Nil |
|
279 |
||
280 |
private def optional_checked(selected: Boolean): XML.Attributes = |
|
281 |
if (selected) List("checked" -> "") else Nil |
|
282 |
||
283 |
private def optional_action(action: String): XML.Attributes = |
|
284 |
proper_string(action).map(a => "action" -> a).toList |
|
285 |
||
66210 | 286 |
private def optional_onclick(script: String): XML.Attributes = |
287 |
proper_string(script).map(a => "onclick" -> a).toList |
|
288 |
||
289 |
private def optional_onchange(script: String): XML.Attributes = |
|
290 |
proper_string(script).map(a => "onchange" -> a).toList |
|
291 |
||
292 |
def button(body: XML.Body, name: String = "", tooltip: String = "", submit: Boolean = false, |
|
293 |
script: String = ""): XML.Elem = |
|
66196 | 294 |
XML.Elem( |
295 |
Markup("button", |
|
296 |
List("type" -> (if (submit) "submit" else "button"), "value" -> "true") ::: |
|
66210 | 297 |
optional_name(name) ::: optional_title(tooltip) ::: optional_onclick(script)), body) |
66196 | 298 |
|
299 |
def checkbox(body: XML.Body, name: String = "", tooltip: String = "", submit: Boolean = false, |
|
66210 | 300 |
selected: Boolean = false, script: String = ""): XML.Elem = |
66196 | 301 |
XML.elem("label", |
302 |
XML.elem( |
|
303 |
Markup("input", |
|
304 |
List("type" -> "checkbox", "value" -> "true") ::: optional_name(name) ::: |
|
305 |
optional_title(tooltip) ::: optional_submit(submit) ::: |
|
66210 | 306 |
optional_checked(selected) ::: optional_onchange(script))) :: body) |
66196 | 307 |
|
308 |
def text_field(columns: Int = 0, text: String = "", name: String = "", tooltip: String = "", |
|
66210 | 309 |
submit: Boolean = false, script: String = ""): XML.Elem = |
66196 | 310 |
XML.elem(Markup("input", |
311 |
List("type" -> "text") ::: |
|
312 |
(if (columns > 0) List("size" -> columns.toString) else Nil) ::: |
|
66210 | 313 |
optional_value(text) ::: optional_name(name) ::: optional_title(tooltip) ::: |
314 |
optional_submit(submit) ::: optional_onchange(script))) |
|
66196 | 315 |
|
316 |
def parameter(text: String = "", name: String = ""): XML.Elem = |
|
317 |
XML.elem( |
|
318 |
Markup("input", List("type" -> "hidden") ::: optional_value(text) ::: optional_name(name))) |
|
319 |
||
66209 | 320 |
def form(body: XML.Body, name: String = "", action: String = "", http_post: Boolean = false) |
321 |
: XML.Elem = |
|
322 |
XML.Elem( |
|
323 |
Markup("form", optional_name(name) ::: optional_action(action) ::: |
|
324 |
(if (http_post) List("method" -> "post") else Nil)), body) |
|
66196 | 325 |
} |
326 |
||
327 |
||
66200 | 328 |
/* GUI layout */ |
329 |
||
330 |
object Wrap_Panel |
|
331 |
{ |
|
332 |
object Alignment extends Enumeration |
|
333 |
{ |
|
334 |
val left, right, center = Value |
|
335 |
} |
|
336 |
||
337 |
def apply(contents: List[XML.Elem], name: String = "", action: String = "", |
|
66206 | 338 |
alignment: Alignment.Value = Alignment.right): XML.Elem = |
66200 | 339 |
{ |
340 |
val body = Library.separate(XML.Text(" "), contents) |
|
341 |
GUI.form(List(div(body) + ("style" -> ("text-align: " + alignment))), |
|
342 |
name = name, action = action) |
|
343 |
} |
|
344 |
} |
|
345 |
||
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 | 349 |
val header: String = |
69804 | 350 |
XML.header + |
351 |
"""<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
64359 | 352 |
<html xmlns="http://www.w3.org/1999/xhtml">""" |
353 |
||
73129 | 354 |
val footer: String = """</html>""" |
355 |
||
64359 | 356 |
val head_meta: XML.Elem = |
357 |
XML.Elem(Markup("meta", |
|
358 |
List("http-equiv" -> "Content-Type", "content" -> "text/html; charset=utf-8")), Nil) |
|
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 | 365 |
cat_lines( |
73129 | 366 |
List( |
367 |
header, |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
368 |
output( |
66000 | 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 | 372 |
hidden = hidden, structural = structural), |
373 |
footer)) |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
374 |
} |
64359 | 375 |
|
65838 | 376 |
|
66000 | 377 |
/* fonts */ |
378 |
||
74709 | 379 |
val fonts_path: Path = Path.explode("fonts") |
380 |
||
381 |
def init_fonts(dir: Path): Unit = |
|
382 |
{ |
|
383 |
val fonts_dir = Isabelle_System.make_directory(dir + HTML.fonts_path) |
|
384 |
for (entry <- Isabelle_Fonts.fonts(hidden = true)) |
|
385 |
Isabelle_System.copy_file(entry.path, fonts_dir) |
|
386 |
} |
|
387 |
||
388 |
def fonts_dir(prefix: String)(ttf_name: String): String = prefix + "/" + ttf_name |
|
389 |
||
66000 | 390 |
def fonts_url(): String => String = |
69374 | 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 | 393 |
|
394 |
def fonts_css(make_url: String => String = fonts_url()): String = |
|
395 |
{ |
|
69360 | 396 |
def font_face(entry: Isabelle_Fonts.Entry): String = |
66000 | 397 |
cat_lines( |
398 |
List( |
|
399 |
"@font-face {", |
|
69360 | 400 |
" font-family: '" + entry.family + "';", |
69366 | 401 |
" src: url('" + make_url(entry.path.file_name) + "') format('truetype');") ::: |
69360 | 402 |
(if (entry.is_bold) List(" font-weight: bold;") else Nil) ::: |
403 |
(if (entry.is_italic) List(" font-style: italic;") else Nil) ::: |
|
66000 | 404 |
List("}")) |
405 |
||
71601 | 406 |
("/* Isabelle fonts */" :: Isabelle_Fonts.fonts(hidden = true).map(font_face)) |
69362 | 407 |
.mkString("", "\n\n", "\n") |
66000 | 408 |
} |
409 |
||
74709 | 410 |
def fonts_css_dir(prefix: String = ""): String = |
411 |
{ |
|
412 |
val prefix1 = if (prefix.isEmpty || prefix.endsWith("/")) prefix else prefix + "/" |
|
413 |
fonts_css(fonts_dir(prefix1 + fonts_path.implode)) |
|
414 |
} |
|
66000 | 415 |
|
74709 | 416 |
|
417 |
/* document directory context (fonts + css) */ |
|
418 |
||
419 |
def relative_prefix(dir: Path, base: Option[Path]): String = |
|
420 |
base match { |
|
421 |
case None => "" |
|
422 |
case Some(base_dir) => |
|
74754 | 423 |
val path = File.path(dir.absolute.java_path.relativize(base_dir.absolute.java_path).toFile) |
424 |
if (path.is_current) "" else path.implode + "/" |
|
74709 | 425 |
} |
65838 | 426 |
|
65998 | 427 |
def isabelle_css: Path = Path.explode("~~/etc/isabelle.css") |
428 |
||
65838 | 429 |
def write_document(dir: Path, name: String, head: XML.Body, body: XML.Body, |
74709 | 430 |
base: Option[Path] = None, |
69366 | 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 | 433 |
structural: Boolean = true): Unit = |
65838 | 434 |
{ |
74709 | 435 |
Isabelle_System.make_directory(dir) |
436 |
val prefix = relative_prefix(dir, base) |
|
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 | 440 |
} |
33984 | 441 |
} |