author | wenzelm |
Thu, 03 Jan 2013 14:12:12 +0100 | |
changeset 50700 | e1df173b12a1 |
parent 50201 | c26369c9eda6 |
child 50707 | 5b2bf7611662 |
permissions | -rw-r--r-- |
33984 | 1 |
/* Title: Pure/Thy/html.scala |
2 |
Author: Makarius |
|
3 |
||
4 |
Basic HTML output. |
|
5 |
*/ |
|
6 |
||
7 |
package isabelle |
|
8 |
||
34000
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
9 |
import scala.collection.mutable.ListBuffer |
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
10 |
|
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
11 |
|
33984 | 12 |
object HTML |
13 |
{ |
|
37200
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
14 |
// encode text |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
15 |
|
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
16 |
def encode(text: String): String = |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
17 |
{ |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
18 |
val s = new StringBuilder |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
19 |
for (c <- text.iterator) c match { |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
20 |
case '<' => s ++= "<" |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
21 |
case '>' => s ++= ">" |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
22 |
case '&' => s ++= "&" |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
23 |
case '"' => s ++= """ |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
24 |
case '\'' => s ++= "'" |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
25 |
case '\n' => s ++= "<br/>" |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
26 |
case _ => s += c |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
27 |
} |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
28 |
s.toString |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
29 |
} |
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
30 |
|
0f3edc64356a
added HTML.encode (in Scala), similar to HTML.output in ML;
wenzelm
parents:
36016
diff
changeset
|
31 |
|
49613 | 32 |
/// FIXME unused stuff |
33 |
||
33984 | 34 |
// common elements and attributes |
35 |
||
36 |
val BODY = "body" |
|
37 |
val DIV = "div" |
|
38 |
val SPAN = "span" |
|
39 |
val BR = "br" |
|
34000
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
40 |
val PRE = "pre" |
33984 | 41 |
val CLASS = "class" |
43490 | 42 |
val STYLE = "style" |
33984 | 43 |
|
44 |
||
45 |
// document markup |
|
46 |
||
38573 | 47 |
def span(cls: String, body: XML.Body): XML.Elem = |
38230
ed147003de4b
simplified type XML.Tree: embed Markup directly, avoid slightly odd triple;
wenzelm
parents:
37200
diff
changeset
|
48 |
XML.Elem(Markup(SPAN, List((CLASS, cls))), body) |
34002 | 49 |
|
43490 | 50 |
def user_font(font: String, txt: String): XML.Elem = |
51 |
XML.Elem(Markup(SPAN, List((STYLE, "font-family: " + font))), List(XML.Text(txt))) |
|
52 |
||
34002 | 53 |
def hidden(txt: String): XML.Elem = |
50201
c26369c9eda6
Isabelle-specific implementation of quasi-abstract markup elements -- back to module arrangement before d83797ef0d2d;
wenzelm
parents:
49650
diff
changeset
|
54 |
span(Markup.HIDDEN, List(XML.Text(txt))) |
34002 | 55 |
|
56 |
def sub(txt: String): XML.Elem = XML.elem("sub", List(XML.Text(txt))) |
|
57 |
def sup(txt: String): XML.Elem = XML.elem("sup", List(XML.Text(txt))) |
|
43459 | 58 |
def bold(txt: String): XML.Elem = span("bold", List(XML.Text(txt))) |
34002 | 59 |
|
49613 | 60 |
def spans(input: XML.Tree): XML.Body = |
38444
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
61 |
{ |
38573 | 62 |
def html_spans(tree: XML.Tree): XML.Body = |
38444
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
63 |
tree match { |
49650
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
64 |
case XML.Wrapped_Elem(markup, _, ts) => |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
65 |
List(span(markup.name, ts.flatMap(html_spans))) |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
66 |
case XML.Elem(markup, ts) => |
9fad6480300d
support for wrapped XML elements, which allows to preserve full markup tree information in to_XML/from_XML conversion;
wenzelm
parents:
49613
diff
changeset
|
67 |
List(span(markup.name, ts.flatMap(html_spans))) |
38444
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
68 |
case XML.Text(txt) => |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
69 |
val ts = new ListBuffer[XML.Tree] |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
70 |
val t = new StringBuilder |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
71 |
def flush() { |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
72 |
if (!t.isEmpty) { |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
73 |
ts += XML.Text(t.toString) |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
74 |
t.clear |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
75 |
} |
34000
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
76 |
} |
38444
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
77 |
def add(elem: XML.Tree) { |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
78 |
flush() |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
79 |
ts += elem |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
80 |
} |
43675
8252d51d70e2
simplified Symbol.iterator: produce strings, which are mostly preallocated;
wenzelm
parents:
43661
diff
changeset
|
81 |
val syms = Symbol.iterator(txt) |
38444
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
82 |
while (syms.hasNext) { |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
83 |
val s1 = syms.next |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
84 |
def s2() = if (syms.hasNext) syms.next else "" |
43455 | 85 |
if (s1 == "\n") add(XML.elem(BR)) |
44238
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
43695
diff
changeset
|
86 |
else if (s1 == Symbol.sub_decoded || s1 == Symbol.isub_decoded) |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
43695
diff
changeset
|
87 |
{ add(hidden(s1)); add(sub(s2())) } |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
43695
diff
changeset
|
88 |
else if (s1 == Symbol.sup_decoded || s1 == Symbol.isup_decoded) |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
43695
diff
changeset
|
89 |
{ add(hidden(s1)); add(sup(s2())) } |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
43695
diff
changeset
|
90 |
else if (s1 == Symbol.bsub_decoded) t ++= s1 // FIXME |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
43695
diff
changeset
|
91 |
else if (s1 == Symbol.esub_decoded) t ++= s1 // FIXME |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
43695
diff
changeset
|
92 |
else if (s1 == Symbol.bsup_decoded) t ++= s1 // FIXME |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
43695
diff
changeset
|
93 |
else if (s1 == Symbol.esup_decoded) t ++= s1 // FIXME |
36120feb70ed
some convenience actions/shortcuts for control symbols;
wenzelm
parents:
43695
diff
changeset
|
94 |
else if (s1 == Symbol.bold_decoded) { add(hidden(s1)); add(bold(s2())) } |
43695
5130dfe1b7be
simplified Symbol based on lazy Symbol.Interpretation -- reduced odd "functorial style";
wenzelm
parents:
43675
diff
changeset
|
95 |
else if (Symbol.fonts.isDefinedAt(s1)) { add(user_font(Symbol.fonts(s1), s1)) } |
43455 | 96 |
else t ++= s1 |
38444
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
97 |
} |
34002 | 98 |
flush() |
38444
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
99 |
ts.toList |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
100 |
} |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
101 |
html_spans(input) |
796904799f4d
HTML.spans: explicit flag for preservation of original data (which would be turned into org.w3c.dom user data in XML.document_node);
wenzelm
parents:
38231
diff
changeset
|
102 |
} |
33984 | 103 |
} |