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