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