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;
     1 /*  Title:      Pure/Thy/html.scala
     2     Author:     Makarius
     3 
     4 Basic HTML output.
     5 */
     6 
     7 package isabelle
     8 
     9 import scala.collection.mutable.ListBuffer
    10 
    11 
    12 object HTML
    13 {
    14   // encode text
    15 
    16   def encode(text: String): String =
    17   {
    18     val s = new StringBuilder
    19     for (c <- text.iterator) c match {
    20       case '<' => s ++= "&lt;"
    21       case '>' => s ++= "&gt;"
    22       case '&' => s ++= "&amp;"
    23       case '"' => s ++= "&quot;"
    24       case '\'' => s ++= "&#39;"
    25       case '\n' => s ++= "<br/>"
    26       case _ => s += c
    27     }
    28     s.toString
    29   }
    30 
    31 
    32   // common elements and attributes
    33 
    34   val BODY = "body"
    35   val DIV = "div"
    36   val SPAN = "span"
    37   val BR = "br"
    38   val PRE = "pre"
    39   val CLASS = "class"
    40 
    41 
    42   // document markup
    43 
    44   def span(cls: String, body: XML.Body): XML.Elem =
    45     XML.Elem(Markup(SPAN, List((CLASS, cls))), body)
    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 
    53   def spans(symbols: Symbol.Interpretation,
    54     input: XML.Tree, original_data: Boolean = false): XML.Body =
    55   {
    56     def html_spans(tree: XML.Tree): XML.Body =
    57       tree match {
    58         case XML.Elem(Markup(name, _), ts) =>
    59           if (original_data)
    60             List(XML.Elem(Markup.Data, List(tree, span(name, ts.flatMap(html_spans)))))
    61           else List(span(name, ts.flatMap(html_spans)))
    62         case XML.Text(txt) =>
    63           val ts = new ListBuffer[XML.Tree]
    64           val t = new StringBuilder
    65           def flush() {
    66             if (!t.isEmpty) {
    67               ts += XML.Text(t.toString)
    68               t.clear
    69             }
    70           }
    71           def add(elem: XML.Tree) {
    72             flush()
    73             ts += elem
    74           }
    75           val syms = Symbol.iterator(txt).map(_.toString)
    76           while (syms.hasNext) {
    77             val s1 = syms.next
    78             def s2() = if (syms.hasNext) syms.next else ""
    79             if (s1 == "\n") add(XML.elem(BR))
    80             else if (symbols.is_subscript_decoded(s1)) { add(hidden(s1)); add(sub(s2())) }
    81             else if (symbols.is_superscript_decoded(s1)) { add(hidden(s1)); add(sup(s2())) }
    82             else if (s1 == "\\<^bsub>") t ++= s1  // FIXME
    83             else if (s1 == "\\<^esub>") t ++= s1  // FIXME
    84             else if (s1 == "\\<^bsup>") t ++= s1  // FIXME
    85             else if (s1 == "\\<^esup>") t ++= s1  // FIXME
    86             else if (symbols.is_bold_decoded(s1)) {
    87               add(hidden(s1)); add(span("bold", List(XML.Text(s2()))))
    88             }
    89             else if (s1 == "\\<^loc>") { add(hidden(s1)); add(span("loc", List(XML.Text(s2())))) }
    90             else t ++= s1
    91           }
    92           flush()
    93           ts.toList
    94       }
    95     html_spans(input)
    96   }
    97 }