author | haftmann |
Thu, 16 Sep 2010 17:52:00 +0200 | |
changeset 39481 | f15514acc942 |
parent 38573 | d163f0f28e8c |
child 43455 | 4b4b93672f15 |
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 |
|
33984 | 32 |
// common elements and attributes |
33 |
||
34 |
val BODY = "body" |
|
35 |
val DIV = "div" |
|
36 |
val SPAN = "span" |
|
37 |
val BR = "br" |
|
34000
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
38 |
val PRE = "pre" |
33984 | 39 |
val CLASS = "class" |
40 |
||
41 |
||
42 |
// document markup |
|
43 |
||
38573 | 44 |
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
|
45 |
XML.Elem(Markup(SPAN, List((CLASS, cls))), body) |
34002 | 46 |
|
47 |
def hidden(txt: String): XML.Elem = |
|
48 |
span(Markup.HIDDEN, List(XML.Text(txt))) |
|
49 |
||
50 |
def sub(txt: String): XML.Elem = XML.elem("sub", List(XML.Text(txt))) |
|
51 |
def sup(txt: String): XML.Elem = XML.elem("sup", List(XML.Text(txt))) |
|
52 |
||
38573 | 53 |
def spans(input: XML.Tree, original_data: Boolean = false): 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
|
54 |
{ |
38573 | 55 |
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
|
56 |
tree match { |
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
|
57 |
case XML.Elem(Markup(name, _), ts) => |
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
|
58 |
if (original_data) |
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
|
59 |
List(XML.Elem(Markup.Data, List(tree, span(name, ts.flatMap(html_spans))))) |
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
|
60 |
else List(span(name, ts.flatMap(html_spans))) |
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 |
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
|
62 |
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
|
63 |
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
|
64 |
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
|
65 |
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
|
66 |
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
|
67 |
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
|
68 |
} |
34000
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
69 |
} |
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
|
70 |
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
|
71 |
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 |
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
|
73 |
} |
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 |
val syms = Symbol.iterator(txt).map(_.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
|
75 |
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
|
76 |
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
|
77 |
def s2() = if (syms.hasNext) syms.next else "" |
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 |
s1 match { |
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 |
case "\n" => add(XML.elem(BR)) |
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 |
case "\\<^bsub>" => t ++= s1 // FIXME |
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
|
81 |
case "\\<^esub>" => t ++= s1 // FIXME |
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 |
case "\\<^bsup>" => t ++= s1 // FIXME |
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 |
case "\\<^esup>" => t ++= s1 // FIXME |
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 |
case "\\<^sub>" | "\\<^isub>" => add(hidden(s1)); add(sub(s2())) |
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
|
85 |
case "\\<^sup>" | "\\<^isup>" => add(hidden(s1)); add(sup(s2())) |
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
|
86 |
case "\\<^bold>" => add(hidden(s1)); add(span("bold", List(XML.Text(s2())))) |
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
|
87 |
case "\\<^loc>" => add(hidden(s1)); add(span("loc", List(XML.Text(s2())))) |
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
|
88 |
case _ => t ++= s1 |
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
|
89 |
} |
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
|
90 |
} |
34002 | 91 |
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
|
92 |
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
|
93 |
} |
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
|
94 |
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
|
95 |
} |
33984 | 96 |
} |