author | wenzelm |
Thu, 01 Jun 2017 21:24:33 +0200 | |
changeset 65998 | d07300e8a14d |
parent 65997 | e3dc9ea67a62 |
child 65999 | ee4cf96a9406 |
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 |
{ |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
12 |
/* output text with control symbols */ |
59063 | 13 |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
14 |
private val control = |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
15 |
Map( |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
16 |
Symbol.sub -> "sub", Symbol.sub_decoded -> "sub", |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
17 |
Symbol.sup -> "sup", Symbol.sup_decoded -> "sup", |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
18 |
Symbol.bold -> "b", Symbol.bold_decoded -> "b") |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
19 |
|
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
20 |
private val control_block = |
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
60215
diff
changeset
|
21 |
Map( |
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
22 |
Symbol.bsub -> "<sub>", Symbol.bsub_decoded -> "<sub>", |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
23 |
Symbol.esub -> "</sub>", Symbol.esub_decoded -> "</sub>", |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
24 |
Symbol.bsup -> "<sup>", Symbol.bsup_decoded -> "<sup>", |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
25 |
Symbol.esup -> "</sup>", Symbol.esup_decoded -> "</sup>") |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
26 |
|
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
27 |
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
|
28 |
|
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
29 |
def output(text: String, s: StringBuilder, hidden: Boolean) |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
30 |
{ |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
31 |
def output_hidden(body: => Unit): Unit = |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
32 |
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
|
33 |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
34 |
def output_symbol(sym: Symbol.Symbol): Unit = |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
35 |
if (sym != "") { |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
36 |
control_block.get(sym) match { |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
37 |
case Some(html) if html.startsWith("</") => |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
38 |
s ++= html; output_hidden(XML.output_string(sym, s)) |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
39 |
case Some(html) => |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
40 |
output_hidden(XML.output_string(sym, s)); s ++= html |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
41 |
case None => |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
42 |
XML.output_string(sym, s) |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
43 |
} |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
44 |
} |
59063 | 45 |
|
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
60215
diff
changeset
|
46 |
var ctrl = "" |
59063 | 47 |
for (sym <- Symbol.iterator(text)) { |
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
48 |
if (is_control(sym)) { output_symbol(ctrl); ctrl = sym } |
59063 | 49 |
else { |
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
60215
diff
changeset
|
50 |
control.get(ctrl) match { |
59063 | 51 |
case Some(elem) if Symbol.is_controllable(sym) && sym != "\"" => |
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
52 |
output_hidden(output_symbol(ctrl)) |
65991 | 53 |
s += '<'; s ++= elem; s += '>' |
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
54 |
output_symbol(sym) |
65991 | 55 |
s ++= "</"; s ++= elem; s += '>' |
59063 | 56 |
case _ => |
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
57 |
output_symbol(ctrl) |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
58 |
output_symbol(sym) |
59063 | 59 |
} |
62104
fb73c0d7bb37
clarified symbol insertion, depending on buffer encoding;
wenzelm
parents:
60215
diff
changeset
|
60 |
ctrl = "" |
59063 | 61 |
} |
37200
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
62 |
} |
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
63 |
output_symbol(ctrl) |
64355 | 64 |
} |
59063 | 65 |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
66 |
def output(text: String): String = Library.make_string(output(text, _, hidden = false)) |
64355 | 67 |
|
68 |
||
69 |
/* output XML as HTML */ |
|
70 |
||
65834
67a6e0f166c2
extra space only for some structual elements, but not <a>, <b>, <em> etc. (amending 8a0fe5469ba0);
wenzelm
parents:
65773
diff
changeset
|
71 |
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
|
72 |
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
|
73 |
"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
|
74 |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
75 |
def output(body: XML.Body, s: StringBuilder, hidden: Boolean) |
64355 | 76 |
{ |
65991 | 77 |
def elem(markup: Markup) |
78 |
{ |
|
79 |
s ++= markup.name |
|
80 |
for ((a, b) <- markup.properties) { |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
81 |
s += ' '; s ++= a; s += '='; s += '"'; output(b, s, hidden); s += '"' |
65991 | 82 |
} |
83 |
} |
|
64355 | 84 |
def tree(t: XML.Tree): Unit = |
85 |
t match { |
|
86 |
case XML.Elem(markup, Nil) => |
|
65991 | 87 |
s += '<'; elem(markup); s ++= "/>" |
64355 | 88 |
case XML.Elem(markup, ts) => |
65834
67a6e0f166c2
extra space only for some structual elements, but not <a>, <b>, <em> etc. (amending 8a0fe5469ba0);
wenzelm
parents:
65773
diff
changeset
|
89 |
if (structural_elements(markup.name)) s += '\n' |
65991 | 90 |
s += '<'; elem(markup); s += '>' |
64355 | 91 |
ts.foreach(tree) |
65991 | 92 |
s ++= "</"; s ++= markup.name; s += '>' |
65834
67a6e0f166c2
extra space only for some structual elements, but not <a>, <b>, <em> etc. (amending 8a0fe5469ba0);
wenzelm
parents:
65773
diff
changeset
|
93 |
if (structural_elements(markup.name)) s += '\n' |
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
94 |
case XML.Text(txt) => |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
95 |
output(txt, s, hidden) |
64355 | 96 |
} |
97 |
body.foreach(tree) |
|
37200
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
98 |
} |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
99 |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
100 |
def output(body: XML.Body, hidden: Boolean): String = |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
101 |
Library.make_string(output(body, _, hidden)) |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
102 |
|
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
103 |
def output(tree: XML.Tree, hidden: Boolean): String = |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
104 |
output(List(tree), hidden) |
64355 | 105 |
|
106 |
||
65753 | 107 |
/* attributes */ |
108 |
||
65992 | 109 |
class Attribute(val name: String, value: String) |
110 |
{ |
|
111 |
def xml: XML.Attribute = name -> value |
|
112 |
def apply(elem: XML.Elem): XML.Elem = elem + xml |
|
113 |
} |
|
65942 | 114 |
|
65992 | 115 |
def id(s: String): Attribute = new Attribute("id", s) |
65993 | 116 |
def class_(name: String): Attribute = new Attribute("class", name) |
65946 | 117 |
|
65992 | 118 |
def width(w: Int): Attribute = new Attribute("width", w.toString) |
119 |
def height(h: Int): Attribute = new Attribute("height", h.toString) |
|
65946 | 120 |
def size(w: Int, h: Int)(elem: XML.Elem): XML.Elem = width(w)(height(h)(elem)) |
65753 | 121 |
|
122 |
||
64359 | 123 |
/* structured markup operators */ |
124 |
||
125 |
def text(txt: String): XML.Body = if (txt.isEmpty) Nil else List(XML.Text(txt)) |
|
65753 | 126 |
val break: XML.Body = List(XML.elem("br")) |
64359 | 127 |
|
65992 | 128 |
class Operator(val name: String) |
129 |
{ |
|
130 |
def apply(body: XML.Body): XML.Elem = XML.elem(name, body) |
|
131 |
def apply(att: Attribute, body: XML.Body): XML.Elem = att(apply(body)) |
|
65993 | 132 |
def apply(c: String, body: XML.Body): XML.Elem = apply(class_(c), body) |
65992 | 133 |
} |
64356 | 134 |
|
64359 | 135 |
class Heading(name: String) extends Operator(name) |
65992 | 136 |
{ |
137 |
def apply(txt: String): XML.Elem = super.apply(text(txt)) |
|
138 |
def apply(att: Attribute, txt: String): XML.Elem = super.apply(att, text(txt)) |
|
139 |
def apply(c: String, txt: String): XML.Elem = super.apply(c, text(txt)) |
|
140 |
} |
|
64359 | 141 |
|
142 |
val div = new Operator("div") |
|
143 |
val span = new Operator("span") |
|
65753 | 144 |
val pre = new Operator("pre") |
64359 | 145 |
val par = new Operator("p") |
146 |
val emph = new Operator("em") |
|
147 |
val bold = new Operator("b") |
|
65771 | 148 |
val code = new Operator("code") |
64355 | 149 |
|
64359 | 150 |
val title = new Heading("title") |
151 |
val chapter = new Heading("h1") |
|
152 |
val section = new Heading("h2") |
|
153 |
val subsection = new Heading("h3") |
|
154 |
val subsubsection = new Heading("h4") |
|
155 |
val paragraph = new Heading("h5") |
|
156 |
val subparagraph = new Heading("h6") |
|
157 |
||
158 |
def itemize(items: List[XML.Body]): XML.Elem = |
|
159 |
XML.elem("ul", items.map(XML.elem("li", _))) |
|
160 |
||
161 |
def enumerate(items: List[XML.Body]): XML.Elem = |
|
162 |
XML.elem("ol", items.map(XML.elem("li", _))) |
|
163 |
||
164 |
def description(items: List[(XML.Body, XML.Body)]): XML.Elem = |
|
165 |
XML.elem("dl", items.flatMap({ case (x, y) => List(XML.elem("dt", x), XML.elem("dd", y)) })) |
|
166 |
||
167 |
def link(href: String, body: XML.Body = Nil): XML.Elem = |
|
168 |
XML.Elem(Markup("a", List("href" -> href)), if (body.isEmpty) text(href) else body) |
|
64355 | 169 |
|
65753 | 170 |
def image(src: String, alt: String = ""): XML.Elem = |
171 |
XML.Elem(Markup("img", List("src" -> src) ::: proper_string(alt).map("alt" -> _).toList), Nil) |
|
172 |
||
65992 | 173 |
def source(src: String): XML.Elem = div("source", List(pre(text(src)))) |
65988 | 174 |
|
65941 | 175 |
def style(s: String): XML.Elem = XML.elem("style", text(s)) |
176 |
||
37200
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
177 |
|
65892 | 178 |
/* messages */ |
179 |
||
65939 | 180 |
// background |
65993 | 181 |
val writeln_message: Attribute = class_("writeln_message") |
182 |
val warning_message: Attribute = class_("warning_message") |
|
183 |
val error_message: Attribute = class_("error_message") |
|
65892 | 184 |
|
65939 | 185 |
// underline |
65993 | 186 |
val writeln: Attribute = class_("writeln") |
187 |
val warning: Attribute = class_("warning") |
|
188 |
val error: Attribute = class_("error") |
|
65892 | 189 |
|
65944 | 190 |
|
191 |
/* tooltips */ |
|
192 |
||
193 |
def tooltip(item: XML.Body, tip: XML.Body): XML.Elem = |
|
65992 | 194 |
span(item ::: List(div("tooltip", tip))) |
65944 | 195 |
|
196 |
def tooltip_errors(item: XML.Body, msgs: List[XML.Body]): XML.Elem = |
|
197 |
HTML.error(tooltip(item, msgs.map(msg => error_message(pre(msg))))) |
|
65892 | 198 |
|
199 |
||
51399
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
200 |
/* document */ |
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
201 |
|
64359 | 202 |
val header: String = |
203 |
"""<?xml version="1.0" encoding="utf-8" ?> |
|
204 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
205 |
<html xmlns="http://www.w3.org/1999/xhtml">""" |
|
206 |
||
207 |
val head_meta: XML.Elem = |
|
208 |
XML.Elem(Markup("meta", |
|
209 |
List("http-equiv" -> "Content-Type", "content" -> "text/html; charset=utf-8")), Nil) |
|
210 |
||
65836 | 211 |
def head_css(css: String): XML.Elem = |
212 |
XML.Elem(Markup("link", List("rel" -> "stylesheet", "type" -> "text/css", "href" -> css)), Nil) |
|
213 |
||
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
214 |
def output_document(head: XML.Body, body: XML.Body, |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
215 |
css: String = "isabelle.css", hidden: Boolean = true): String = |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
216 |
{ |
64359 | 217 |
cat_lines( |
218 |
List(header, |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
219 |
output( |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
220 |
XML.elem("head", head_meta :: (if (css == "") Nil else List(head_css(css))) ::: head), |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
221 |
hidden = hidden), |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
222 |
output(XML.elem("body", body), hidden = hidden))) |
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
223 |
} |
64359 | 224 |
|
65838 | 225 |
|
226 |
/* document directory */ |
|
227 |
||
65998 | 228 |
def isabelle_css: Path = Path.explode("~~/etc/isabelle.css") |
229 |
||
65836 | 230 |
def init_dir(dir: Path) |
231 |
{ |
|
232 |
Isabelle_System.mkdirs(dir) |
|
65998 | 233 |
File.copy(isabelle_css, dir) |
65836 | 234 |
} |
235 |
||
65838 | 236 |
def write_document(dir: Path, name: String, head: XML.Body, body: XML.Body, |
65998 | 237 |
css: String = isabelle_css.base.implode, hidden: Boolean = true) |
65838 | 238 |
{ |
239 |
init_dir(dir) |
|
65997
e3dc9ea67a62
output control symbols like ML version, with optionally hidden source;
wenzelm
parents:
65993
diff
changeset
|
240 |
File.write(dir + Path.basic(name), output_document(head, body, css = css, hidden = hidden)) |
65838 | 241 |
} |
33984 | 242 |
} |