author | wenzelm |
Mon, 29 Mar 2010 22:43:56 +0200 | |
changeset 36011 | 3ff725ac13a4 |
parent 34137 | 6cc9a0cbaf55 |
child 36012 | 0614676f14d4 |
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 |
{ |
|
14 |
// common elements and attributes |
|
15 |
||
16 |
val BODY = "body" |
|
17 |
val DIV = "div" |
|
18 |
val SPAN = "span" |
|
19 |
val BR = "br" |
|
34000
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
20 |
val PRE = "pre" |
33984 | 21 |
val CLASS = "class" |
22 |
||
23 |
||
24 |
// document markup |
|
25 |
||
34002 | 26 |
def span(cls: String, body: List[XML.Tree]): XML.Elem = |
27 |
XML.Elem(SPAN, List((CLASS, cls)), body) |
|
28 |
||
29 |
def hidden(txt: String): XML.Elem = |
|
30 |
span(Markup.HIDDEN, List(XML.Text(txt))) |
|
31 |
||
32 |
def sub(txt: String): XML.Elem = XML.elem("sub", List(XML.Text(txt))) |
|
33 |
def sup(txt: String): XML.Elem = XML.elem("sup", List(XML.Text(txt))) |
|
34 |
||
33984 | 35 |
def spans(tree: XML.Tree): List[XML.Tree] = |
36 |
tree match { |
|
34046 | 37 |
case XML.Elem(name, _, ts) => |
38 |
List(XML.elem(Markup.DATA, List(tree, span(name, ts.flatMap(spans))))) |
|
34000
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
39 |
case XML.Text(txt) => |
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
40 |
val ts = new ListBuffer[XML.Tree] |
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
41 |
val t = new StringBuilder |
34002 | 42 |
def flush() { |
34000
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
43 |
if (!t.isEmpty) { |
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
44 |
ts + XML.Text(t.toString) |
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
45 |
t.clear |
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
46 |
} |
33984 | 47 |
} |
34002 | 48 |
def add(elem: XML.Tree) { |
49 |
flush() |
|
50 |
ts + elem |
|
51 |
} |
|
36011
3ff725ac13a4
adapted to Scala 2.8.0 Beta1 -- with notable changes to scala.collection;
wenzelm
parents:
34137
diff
changeset
|
52 |
val syms = Symbol.iterator(txt).map(_.toString) |
34002 | 53 |
while (syms.hasNext) { |
54 |
val s1 = syms.next |
|
34006
bbd146caa6b2
avoid lazy val with side-effects -- spurious null pointers!?
wenzelm
parents:
34004
diff
changeset
|
55 |
def s2() = if (syms.hasNext) syms.next else "" |
34002 | 56 |
s1 match { |
57 |
case "\n" => add(XML.elem(BR)) |
|
58 |
case "\\<^bsub>" => t ++ s1 // FIXME |
|
59 |
case "\\<^esub>" => t ++ s1 // FIXME |
|
60 |
case "\\<^bsup>" => t ++ s1 // FIXME |
|
61 |
case "\\<^esup>" => t ++ s1 // FIXME |
|
34006
bbd146caa6b2
avoid lazy val with side-effects -- spurious null pointers!?
wenzelm
parents:
34004
diff
changeset
|
62 |
case "\\<^sub>" | "\\<^isub>" => add(hidden(s1)); add(sub(s2())) |
bbd146caa6b2
avoid lazy val with side-effects -- spurious null pointers!?
wenzelm
parents:
34004
diff
changeset
|
63 |
case "\\<^sup>" | "\\<^isup>" => add(hidden(s1)); add(sup(s2())) |
bbd146caa6b2
avoid lazy val with side-effects -- spurious null pointers!?
wenzelm
parents:
34004
diff
changeset
|
64 |
case "\\<^bold>" => add(hidden(s1)); add(span("bold", List(XML.Text(s2())))) |
bbd146caa6b2
avoid lazy val with side-effects -- spurious null pointers!?
wenzelm
parents:
34004
diff
changeset
|
65 |
case "\\<^loc>" => add(hidden(s1)); add(span("loc", List(XML.Text(s2())))) |
34002 | 66 |
case _ => t ++ s1 |
34000
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
67 |
} |
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
68 |
} |
34002 | 69 |
flush() |
34000
1fecda948697
more robust treatment of line breaks -- Java "split" has off semantics;
wenzelm
parents:
33984
diff
changeset
|
70 |
ts.toList |
33984 | 71 |
} |
72 |
} |