src/Pure/Thy/html.scala
author wenzelm
Sun Aug 22 13:52:24 2010 +0200 (2010-08-22 ago)
changeset 38573 d163f0f28e8c
parent 38444 796904799f4d
child 43455 4b4b93672f15
permissions -rw-r--r--
tuned signatures;
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@37200
    14
  // encode text
wenzelm@37200
    15
wenzelm@37200
    16
  def encode(text: String): String =
wenzelm@37200
    17
  {
wenzelm@37200
    18
    val s = new StringBuilder
wenzelm@37200
    19
    for (c <- text.iterator) c match {
wenzelm@37200
    20
      case '<' => s ++= "&lt;"
wenzelm@37200
    21
      case '>' => s ++= "&gt;"
wenzelm@37200
    22
      case '&' => s ++= "&amp;"
wenzelm@37200
    23
      case '"' => s ++= "&quot;"
wenzelm@37200
    24
      case '\'' => s ++= "&#39;"
wenzelm@37200
    25
      case '\n' => s ++= "<br/>"
wenzelm@37200
    26
      case _ => s += c
wenzelm@37200
    27
    }
wenzelm@37200
    28
    s.toString
wenzelm@37200
    29
  }
wenzelm@37200
    30
wenzelm@37200
    31
wenzelm@33984
    32
  // common elements and attributes
wenzelm@33984
    33
wenzelm@33984
    34
  val BODY = "body"
wenzelm@33984
    35
  val DIV = "div"
wenzelm@33984
    36
  val SPAN = "span"
wenzelm@33984
    37
  val BR = "br"
wenzelm@34000
    38
  val PRE = "pre"
wenzelm@33984
    39
  val CLASS = "class"
wenzelm@33984
    40
wenzelm@33984
    41
wenzelm@33984
    42
  // document markup
wenzelm@33984
    43
wenzelm@38573
    44
  def span(cls: String, body: XML.Body): XML.Elem =
wenzelm@38230
    45
    XML.Elem(Markup(SPAN, List((CLASS, cls))), body)
wenzelm@34002
    46
wenzelm@34002
    47
  def hidden(txt: String): XML.Elem =
wenzelm@34002
    48
    span(Markup.HIDDEN, List(XML.Text(txt)))
wenzelm@34002
    49
wenzelm@34002
    50
  def sub(txt: String): XML.Elem = XML.elem("sub", List(XML.Text(txt)))
wenzelm@34002
    51
  def sup(txt: String): XML.Elem = XML.elem("sup", List(XML.Text(txt)))
wenzelm@34002
    52
wenzelm@38573
    53
  def spans(input: XML.Tree, original_data: Boolean = false): XML.Body =
wenzelm@38444
    54
  {
wenzelm@38573
    55
    def html_spans(tree: XML.Tree): XML.Body =
wenzelm@38444
    56
      tree match {
wenzelm@38444
    57
        case XML.Elem(Markup(name, _), ts) =>
wenzelm@38444
    58
          if (original_data)
wenzelm@38444
    59
            List(XML.Elem(Markup.Data, List(tree, span(name, ts.flatMap(html_spans)))))
wenzelm@38444
    60
          else List(span(name, ts.flatMap(html_spans)))
wenzelm@38444
    61
        case XML.Text(txt) =>
wenzelm@38444
    62
          val ts = new ListBuffer[XML.Tree]
wenzelm@38444
    63
          val t = new StringBuilder
wenzelm@38444
    64
          def flush() {
wenzelm@38444
    65
            if (!t.isEmpty) {
wenzelm@38444
    66
              ts += XML.Text(t.toString)
wenzelm@38444
    67
              t.clear
wenzelm@38444
    68
            }
wenzelm@34000
    69
          }
wenzelm@38444
    70
          def add(elem: XML.Tree) {
wenzelm@38444
    71
            flush()
wenzelm@38444
    72
            ts += elem
wenzelm@38444
    73
          }
wenzelm@38444
    74
          val syms = Symbol.iterator(txt).map(_.toString)
wenzelm@38444
    75
          while (syms.hasNext) {
wenzelm@38444
    76
            val s1 = syms.next
wenzelm@38444
    77
            def s2() = if (syms.hasNext) syms.next else ""
wenzelm@38444
    78
            s1 match {
wenzelm@38444
    79
              case "\n" => add(XML.elem(BR))
wenzelm@38444
    80
              case "\\<^bsub>" => t ++= s1  // FIXME
wenzelm@38444
    81
              case "\\<^esub>" => t ++= s1  // FIXME
wenzelm@38444
    82
              case "\\<^bsup>" => t ++= s1  // FIXME
wenzelm@38444
    83
              case "\\<^esup>" => t ++= s1  // FIXME
wenzelm@38444
    84
              case "\\<^sub>" | "\\<^isub>" => add(hidden(s1)); add(sub(s2()))
wenzelm@38444
    85
              case "\\<^sup>" | "\\<^isup>" => add(hidden(s1)); add(sup(s2()))
wenzelm@38444
    86
              case "\\<^bold>" => add(hidden(s1)); add(span("bold", List(XML.Text(s2()))))
wenzelm@38444
    87
              case "\\<^loc>" => add(hidden(s1)); add(span("loc", List(XML.Text(s2()))))
wenzelm@38444
    88
              case _ => t ++= s1
wenzelm@38444
    89
            }
wenzelm@38444
    90
          }
wenzelm@34002
    91
          flush()
wenzelm@38444
    92
          ts.toList
wenzelm@38444
    93
      }
wenzelm@38444
    94
    html_spans(input)
wenzelm@38444
    95
  }
wenzelm@33984
    96
}