src/Pure/Thy/html.scala
author wenzelm
Mon Dec 07 00:02:54 2009 +0100 (2009-12-07 ago)
changeset 34006 bbd146caa6b2
parent 34004 30c8746074d0
child 34046 8e743ca417b9
permissions -rw-r--r--
avoid lazy val with side-effects -- spurious null pointers!?
wenzelm@33984
     1
/*  Title:      Pure/Thy/html.scala
wenzelm@33984
     2
    Author:     Makarius
wenzelm@33984
     3
wenzelm@33984
     4
Basic HTML output.
wenzelm@33984
     5
*/
wenzelm@33984
     6
wenzelm@33984
     7
package isabelle
wenzelm@33984
     8
wenzelm@34000
     9
import scala.collection.mutable.ListBuffer
wenzelm@34000
    10
wenzelm@34000
    11
wenzelm@33984
    12
object HTML
wenzelm@33984
    13
{
wenzelm@33984
    14
  // common elements and attributes
wenzelm@33984
    15
wenzelm@33984
    16
  val BODY = "body"
wenzelm@33984
    17
  val DIV = "div"
wenzelm@33984
    18
  val SPAN = "span"
wenzelm@33984
    19
  val BR = "br"
wenzelm@34000
    20
  val PRE = "pre"
wenzelm@33984
    21
  val CLASS = "class"
wenzelm@33984
    22
wenzelm@33984
    23
wenzelm@33984
    24
  // document markup
wenzelm@33984
    25
wenzelm@34002
    26
  def span(cls: String, body: List[XML.Tree]): XML.Elem =
wenzelm@34002
    27
    XML.Elem(SPAN, List((CLASS, cls)), body)
wenzelm@34002
    28
wenzelm@34002
    29
  def hidden(txt: String): XML.Elem =
wenzelm@34002
    30
    span(Markup.HIDDEN, List(XML.Text(txt)))
wenzelm@34002
    31
wenzelm@34002
    32
  def sub(txt: String): XML.Elem = XML.elem("sub", List(XML.Text(txt)))
wenzelm@34002
    33
  def sup(txt: String): XML.Elem = XML.elem("sup", List(XML.Text(txt)))
wenzelm@34002
    34
wenzelm@33984
    35
  def spans(tree: XML.Tree): List[XML.Tree] =
wenzelm@33984
    36
    tree match {
wenzelm@34002
    37
      case XML.Elem(name, _, ts) => List(span(name, ts.flatMap(spans)))
wenzelm@34000
    38
      case XML.Text(txt) =>
wenzelm@34000
    39
        val ts = new ListBuffer[XML.Tree]
wenzelm@34000
    40
        val t = new StringBuilder
wenzelm@34002
    41
        def flush() {
wenzelm@34000
    42
          if (!t.isEmpty) {
wenzelm@34000
    43
            ts + XML.Text(t.toString)
wenzelm@34000
    44
            t.clear
wenzelm@34000
    45
          }
wenzelm@33984
    46
        }
wenzelm@34002
    47
        def add(elem: XML.Tree) {
wenzelm@34002
    48
          flush()
wenzelm@34002
    49
          ts + elem
wenzelm@34002
    50
        }
wenzelm@34002
    51
        val syms = Symbol.elements(txt)
wenzelm@34002
    52
        while (syms.hasNext) {
wenzelm@34002
    53
          val s1 = syms.next
wenzelm@34006
    54
          def s2() = if (syms.hasNext) syms.next else ""
wenzelm@34002
    55
          s1 match {
wenzelm@34002
    56
            case "\n" => add(XML.elem(BR))
wenzelm@34002
    57
            case "\\<^bsub>" => t ++ s1  // FIXME
wenzelm@34002
    58
            case "\\<^esub>" => t ++ s1  // FIXME
wenzelm@34002
    59
            case "\\<^bsup>" => t ++ s1  // FIXME
wenzelm@34002
    60
            case "\\<^esup>" => t ++ s1  // FIXME
wenzelm@34006
    61
            case "\\<^sub>" | "\\<^isub>" => add(hidden(s1)); add(sub(s2()))
wenzelm@34006
    62
            case "\\<^sup>" | "\\<^isup>" => add(hidden(s1)); add(sup(s2()))
wenzelm@34006
    63
            case "\\<^bold>" => add(hidden(s1)); add(span("bold", List(XML.Text(s2()))))
wenzelm@34006
    64
            case "\\<^loc>" => add(hidden(s1)); add(span("loc", List(XML.Text(s2()))))
wenzelm@34002
    65
            case _ => t ++ s1
wenzelm@34000
    66
          }
wenzelm@34000
    67
        }
wenzelm@34002
    68
        flush()
wenzelm@34000
    69
        ts.toList
wenzelm@33984
    70
    }
wenzelm@33984
    71
}