src/Pure/Thy/html.scala
author wenzelm
Sun Jun 19 14:11:06 2011 +0200 (2011-06-19 ago)
changeset 43455 4b4b93672f15
parent 38573 d163f0f28e8c
child 43458 b55a273ede18
permissions -rw-r--r--
some unicode chars for special control symbols;
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@43455
    53
  def spans(symbols: Symbol.Interpretation,
wenzelm@43455
    54
    input: XML.Tree, original_data: Boolean = false): XML.Body =
wenzelm@38444
    55
  {
wenzelm@38573
    56
    def html_spans(tree: XML.Tree): XML.Body =
wenzelm@38444
    57
      tree match {
wenzelm@38444
    58
        case XML.Elem(Markup(name, _), ts) =>
wenzelm@38444
    59
          if (original_data)
wenzelm@38444
    60
            List(XML.Elem(Markup.Data, List(tree, span(name, ts.flatMap(html_spans)))))
wenzelm@38444
    61
          else List(span(name, ts.flatMap(html_spans)))
wenzelm@38444
    62
        case XML.Text(txt) =>
wenzelm@38444
    63
          val ts = new ListBuffer[XML.Tree]
wenzelm@38444
    64
          val t = new StringBuilder
wenzelm@38444
    65
          def flush() {
wenzelm@38444
    66
            if (!t.isEmpty) {
wenzelm@38444
    67
              ts += XML.Text(t.toString)
wenzelm@38444
    68
              t.clear
wenzelm@38444
    69
            }
wenzelm@34000
    70
          }
wenzelm@38444
    71
          def add(elem: XML.Tree) {
wenzelm@38444
    72
            flush()
wenzelm@38444
    73
            ts += elem
wenzelm@38444
    74
          }
wenzelm@38444
    75
          val syms = Symbol.iterator(txt).map(_.toString)
wenzelm@38444
    76
          while (syms.hasNext) {
wenzelm@38444
    77
            val s1 = syms.next
wenzelm@38444
    78
            def s2() = if (syms.hasNext) syms.next else ""
wenzelm@43455
    79
            if (s1 == "\n") add(XML.elem(BR))
wenzelm@43455
    80
            else if (symbols.is_subscript_decoded(s1)) { add(hidden(s1)); add(sub(s2())) }
wenzelm@43455
    81
            else if (symbols.is_superscript_decoded(s1)) { add(hidden(s1)); add(sup(s2())) }
wenzelm@43455
    82
            else if (s1 == "\\<^bsub>") t ++= s1  // FIXME
wenzelm@43455
    83
            else if (s1 == "\\<^esub>") t ++= s1  // FIXME
wenzelm@43455
    84
            else if (s1 == "\\<^bsup>") t ++= s1  // FIXME
wenzelm@43455
    85
            else if (s1 == "\\<^esup>") t ++= s1  // FIXME
wenzelm@43455
    86
            else if (symbols.is_bold_decoded(s1)) {
wenzelm@43455
    87
              add(hidden(s1)); add(span("bold", List(XML.Text(s2()))))
wenzelm@38444
    88
            }
wenzelm@43455
    89
            else if (s1 == "\\<^loc>") { add(hidden(s1)); add(span("loc", List(XML.Text(s2())))) }
wenzelm@43455
    90
            else t ++= s1
wenzelm@38444
    91
          }
wenzelm@34002
    92
          flush()
wenzelm@38444
    93
          ts.toList
wenzelm@38444
    94
      }
wenzelm@38444
    95
    html_spans(input)
wenzelm@38444
    96
  }
wenzelm@33984
    97
}