author | wenzelm |
Thu, 01 Jun 2017 11:57:04 +0200 | |
changeset 65990 | 868089ee9d60 |
parent 65988 | 8040d2563593 |
child 65991 | fa787e233214 |
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 != "\"" => |
64355 | 33 |
s ++= ("<" + elem + ">") |
65990 | 34 |
output_string(sym) |
64355 | 35 |
s ++= ("</" + elem + ">") |
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 |
{ |
|
57 |
def attrib(p: (String, String)): Unit = |
|
58 |
{ s ++= " "; s ++= p._1; s ++= "=\""; output(p._2, s); s ++= "\"" } |
|
59 |
def elem(markup: Markup): Unit = |
|
60 |
{ s ++= markup.name; markup.properties.foreach(attrib) } |
|
61 |
def tree(t: XML.Tree): Unit = |
|
62 |
t match { |
|
63 |
case XML.Elem(markup, Nil) => |
|
64 |
s ++= "<"; elem(markup); s ++= "/>" |
|
65 |
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
|
66 |
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
|
67 |
s ++= "<"; elem(markup); s ++= ">" |
64355 | 68 |
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
|
69 |
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
|
70 |
if (structural_elements(markup.name)) s += '\n' |
64355 | 71 |
case XML.Text(txt) => output(txt, s) |
72 |
} |
|
73 |
body.foreach(tree) |
|
37200
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
74 |
} |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
75 |
|
64355 | 76 |
def output(body: XML.Body): String = Library.make_string(output(body, _)) |
77 |
def output(tree: XML.Tree): String = output(List(tree)) |
|
78 |
||
79 |
||
65753 | 80 |
/* attributes */ |
81 |
||
65942 | 82 |
class Attribute(name: String, value: String) |
83 |
{ def apply(elem: XML.Elem): XML.Elem = elem + (name -> value) } |
|
84 |
||
85 |
def id(s: String) = new Attribute("id", s) |
|
65946 | 86 |
def css_class(name: String) = new Attribute("class", name) |
87 |
||
65942 | 88 |
def width(w: Int) = new Attribute("width", w.toString) |
89 |
def height(h: Int) = new Attribute("height", h.toString) |
|
65946 | 90 |
def size(w: Int, h: Int)(elem: XML.Elem): XML.Elem = width(w)(height(h)(elem)) |
65753 | 91 |
|
92 |
||
64359 | 93 |
/* structured markup operators */ |
94 |
||
95 |
def text(txt: String): XML.Body = if (txt.isEmpty) Nil else List(XML.Text(txt)) |
|
65753 | 96 |
val break: XML.Body = List(XML.elem("br")) |
64359 | 97 |
|
98 |
class Operator(name: String) |
|
99 |
{ def apply(body: XML.Body): XML.Elem = XML.elem(name, body) } |
|
64356 | 100 |
|
64359 | 101 |
class Heading(name: String) extends Operator(name) |
102 |
{ def apply(txt: String): XML.Elem = super.apply(text(txt)) } |
|
103 |
||
104 |
val div = new Operator("div") |
|
105 |
val span = new Operator("span") |
|
65753 | 106 |
val pre = new Operator("pre") |
64359 | 107 |
val par = new Operator("p") |
108 |
val emph = new Operator("em") |
|
109 |
val bold = new Operator("b") |
|
65771 | 110 |
val code = new Operator("code") |
64355 | 111 |
|
64359 | 112 |
val title = new Heading("title") |
113 |
val chapter = new Heading("h1") |
|
114 |
val section = new Heading("h2") |
|
115 |
val subsection = new Heading("h3") |
|
116 |
val subsubsection = new Heading("h4") |
|
117 |
val paragraph = new Heading("h5") |
|
118 |
val subparagraph = new Heading("h6") |
|
119 |
||
120 |
def itemize(items: List[XML.Body]): XML.Elem = |
|
121 |
XML.elem("ul", items.map(XML.elem("li", _))) |
|
122 |
||
123 |
def enumerate(items: List[XML.Body]): XML.Elem = |
|
124 |
XML.elem("ol", items.map(XML.elem("li", _))) |
|
125 |
||
126 |
def description(items: List[(XML.Body, XML.Body)]): XML.Elem = |
|
127 |
XML.elem("dl", items.flatMap({ case (x, y) => List(XML.elem("dt", x), XML.elem("dd", y)) })) |
|
128 |
||
129 |
def link(href: String, body: XML.Body = Nil): XML.Elem = |
|
130 |
XML.Elem(Markup("a", List("href" -> href)), if (body.isEmpty) text(href) else body) |
|
64355 | 131 |
|
65753 | 132 |
def image(src: String, alt: String = ""): XML.Elem = |
133 |
XML.Elem(Markup("img", List("src" -> src) ::: proper_string(alt).map("alt" -> _).toList), Nil) |
|
134 |
||
65988 | 135 |
def source(src: String): XML.Elem = css_class("source")(div(List(pre(text(src))))) |
136 |
||
65941 | 137 |
def style(s: String): XML.Elem = XML.elem("style", text(s)) |
138 |
||
37200
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
139 |
|
65892 | 140 |
/* messages */ |
141 |
||
65939 | 142 |
// background |
65943 | 143 |
val writeln_message: Attribute = css_class("writeln_message") |
144 |
val warning_message: Attribute = css_class("warning_message") |
|
145 |
val error_message: Attribute = css_class("error_message") |
|
65892 | 146 |
|
65939 | 147 |
// underline |
65943 | 148 |
val writeln: Attribute = css_class("writeln") |
149 |
val warning: Attribute = css_class("warning") |
|
150 |
val error: Attribute = css_class("error") |
|
65892 | 151 |
|
65944 | 152 |
|
153 |
/* tooltips */ |
|
154 |
||
155 |
def tooltip(item: XML.Body, tip: XML.Body): XML.Elem = |
|
156 |
span(item ::: List(css_class("tooltip")(div(tip)))) |
|
157 |
||
158 |
def tooltip_errors(item: XML.Body, msgs: List[XML.Body]): XML.Elem = |
|
159 |
HTML.error(tooltip(item, msgs.map(msg => error_message(pre(msg))))) |
|
65892 | 160 |
|
161 |
||
51399
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
162 |
/* document */ |
6ac3c29a300e
discontinued "isabelle usedir" option -r (reset session path);
wenzelm
parents:
50707
diff
changeset
|
163 |
|
64359 | 164 |
val header: String = |
165 |
"""<?xml version="1.0" encoding="utf-8" ?> |
|
166 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
167 |
<html xmlns="http://www.w3.org/1999/xhtml">""" |
|
168 |
||
169 |
val head_meta: XML.Elem = |
|
170 |
XML.Elem(Markup("meta", |
|
171 |
List("http-equiv" -> "Content-Type", "content" -> "text/html; charset=utf-8")), Nil) |
|
172 |
||
65836 | 173 |
def head_css(css: String): XML.Elem = |
174 |
XML.Elem(Markup("link", List("rel" -> "stylesheet", "type" -> "text/css", "href" -> css)), Nil) |
|
175 |
||
176 |
def output_document(head: XML.Body, body: XML.Body, css: String = "isabelle.css"): String = |
|
64359 | 177 |
cat_lines( |
178 |
List(header, |
|
65836 | 179 |
output(XML.elem("head", head_meta :: (if (css == "") Nil else List(head_css(css))) ::: head)), |
64359 | 180 |
output(XML.elem("body", body)))) |
181 |
||
65838 | 182 |
|
183 |
/* document directory */ |
|
184 |
||
65836 | 185 |
def init_dir(dir: Path) |
186 |
{ |
|
187 |
Isabelle_System.mkdirs(dir) |
|
188 |
File.copy(Path.explode("~~/etc/isabelle.css"), dir) |
|
189 |
} |
|
190 |
||
65838 | 191 |
def write_document(dir: Path, name: String, head: XML.Body, body: XML.Body, |
192 |
css: String = "isabelle.css") |
|
193 |
{ |
|
194 |
init_dir(dir) |
|
195 |
File.write(dir + Path.basic(name), output_document(head, body, css)) |
|
196 |
} |
|
33984 | 197 |
} |