author | wenzelm |
Wed, 12 Apr 2017 17:48:19 +0200 | |
changeset 65468 | c41791ad75c3 |
parent 64362 | 8a0fe5469ba0 |
child 65753 | 787e5ee6ef53 |
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 |
||
61 |
def output(body: XML.Body, s: StringBuilder) |
|
62 |
{ |
|
63 |
def attrib(p: (String, String)): Unit = |
|
64 |
{ s ++= " "; s ++= p._1; s ++= "=\""; output(p._2, s); s ++= "\"" } |
|
65 |
def elem(markup: Markup): Unit = |
|
66 |
{ s ++= markup.name; markup.properties.foreach(attrib) } |
|
67 |
def tree(t: XML.Tree): Unit = |
|
68 |
t match { |
|
69 |
case XML.Elem(markup, Nil) => |
|
70 |
s ++= "<"; elem(markup); s ++= "/>" |
|
71 |
case XML.Elem(markup, ts) => |
|
64362
8a0fe5469ba0
more readable output: whitespace is insignificant in HTML;
wenzelm
parents:
64359
diff
changeset
|
72 |
s ++= "\n<"; elem(markup); s ++= ">" |
64355 | 73 |
ts.foreach(tree) |
64362
8a0fe5469ba0
more readable output: whitespace is insignificant in HTML;
wenzelm
parents:
64359
diff
changeset
|
74 |
s ++= "</"; s ++= markup.name; s ++= ">\n" |
64355 | 75 |
case XML.Text(txt) => output(txt, s) |
76 |
} |
|
77 |
body.foreach(tree) |
|
37200
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
78 |
} |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
79 |
|
64355 | 80 |
def output(body: XML.Body): String = Library.make_string(output(body, _)) |
81 |
def output(tree: XML.Tree): String = output(List(tree)) |
|
82 |
||
83 |
||
64359 | 84 |
/* structured markup operators */ |
85 |
||
86 |
def text(txt: String): XML.Body = if (txt.isEmpty) Nil else List(XML.Text(txt)) |
|
87 |
||
88 |
class Operator(name: String) |
|
89 |
{ def apply(body: XML.Body): XML.Elem = XML.elem(name, body) } |
|
64356 | 90 |
|
64359 | 91 |
class Heading(name: String) extends Operator(name) |
92 |
{ def apply(txt: String): XML.Elem = super.apply(text(txt)) } |
|
93 |
||
94 |
val div = new Operator("div") |
|
95 |
val span = new Operator("span") |
|
96 |
val par = new Operator("p") |
|
97 |
val emph = new Operator("em") |
|
98 |
val bold = new Operator("b") |
|
64355 | 99 |
|
64359 | 100 |
val title = new Heading("title") |
101 |
val chapter = new Heading("h1") |
|
102 |
val section = new Heading("h2") |
|
103 |
val subsection = new Heading("h3") |
|
104 |
val subsubsection = new Heading("h4") |
|
105 |
val paragraph = new Heading("h5") |
|
106 |
val subparagraph = new Heading("h6") |
|
107 |
||
108 |
def itemize(items: List[XML.Body]): XML.Elem = |
|
109 |
XML.elem("ul", items.map(XML.elem("li", _))) |
|
110 |
||
111 |
def enumerate(items: List[XML.Body]): XML.Elem = |
|
112 |
XML.elem("ol", items.map(XML.elem("li", _))) |
|
113 |
||
114 |
def description(items: List[(XML.Body, XML.Body)]): XML.Elem = |
|
115 |
XML.elem("dl", items.flatMap({ case (x, y) => List(XML.elem("dt", x), XML.elem("dd", y)) })) |
|
116 |
||
117 |
def link(href: String, body: XML.Body = Nil): XML.Elem = |
|
118 |
XML.Elem(Markup("a", List("href" -> href)), if (body.isEmpty) text(href) else body) |
|
64355 | 119 |
|
37200
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
120 |
|
51399
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
121 |
/* document */ |
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
122 |
|
64359 | 123 |
val header: String = |
124 |
"""<?xml version="1.0" encoding="utf-8" ?> |
|
125 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
126 |
<html xmlns="http://www.w3.org/1999/xhtml">""" |
|
127 |
||
128 |
val head_meta: XML.Elem = |
|
129 |
XML.Elem(Markup("meta", |
|
130 |
List("http-equiv" -> "Content-Type", "content" -> "text/html; charset=utf-8")), Nil) |
|
131 |
||
132 |
def output_document(head: XML.Body, body: XML.Body): String = |
|
133 |
cat_lines( |
|
134 |
List(header, |
|
135 |
output(XML.elem("head", head_meta :: head)), |
|
136 |
output(XML.elem("body", body)))) |
|
137 |
||
138 |
||
139 |
/* Isabelle document */ |
|
140 |
||
51399
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
141 |
def begin_document(title: String): String = |
64359 | 142 |
header + "\n" + |
143 |
"<head>\n" + output(head_meta) + "\n" + |
|
62113 | 144 |
"<title>" + output(title + " (" + Distribution.version + ")") + "</title>\n" + |
51399
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
145 |
"<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
|
146 |
"</head>\n" + |
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
147 |
"\n" + |
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
148 |
"<body>\n" + |
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
149 |
"<div class=\"head\">" + |
62113 | 150 |
"<h1>" + output(title) + "</h1>\n" |
151 |
||
152 |
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
|
153 |
|
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
154 |
|
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
155 |
/* common markup elements */ |
33984 | 156 |
|
51402 | 157 |
private def session_entry(entry: (String, String)): String = |
158 |
{ |
|
159 |
val (name, description) = entry |
|
160 |
val descr = |
|
161 |
if (description == "") Nil |
|
162 |
else List(XML.elem("br"), XML.elem("pre", List(XML.Text(description)))) |
|
50707
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
50201
diff
changeset
|
163 |
XML.string_of_tree( |
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
50201
diff
changeset
|
164 |
XML.elem("li", |
5b2bf7611662
maintain session index on Scala side, for more determistic results;
wenzelm
parents:
50201
diff
changeset
|
165 |
List(XML.Elem(Markup("a", List(("href", name + "/index.html"))), |
51402 | 166 |
List(XML.Text(name)))) ::: descr)) + "\n" |
167 |
} |
|
34002 | 168 |
|
51402 | 169 |
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
|
170 |
{ |
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
171 |
begin_document("Isabelle/" + chapter + " sessions") + |
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
172 |
(if (sessions.isEmpty) "" |
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
173 |
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
|
174 |
end_document |
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
175 |
} |
33984 | 176 |
} |