src/Pure/Thy/html.scala
author wenzelm
Mon Jul 04 22:11:32 2011 +0200 (2011-07-04 ago)
changeset 43661 39fdbd814c7f
parent 43548 f231a7594e54
child 43675 8252d51d70e2
permissions -rw-r--r--
quasi-static Isabelle_System -- reduced tendency towards "functorial style";
     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   val STYLE = "style"
    41 
    42 
    43   // document markup
    44 
    45   def span(cls: String, body: XML.Body): XML.Elem =
    46     XML.Elem(Markup(SPAN, List((CLASS, cls))), body)
    47 
    48   def user_font(font: String, txt: String): XML.Elem =
    49     XML.Elem(Markup(SPAN, List((STYLE, "font-family: " + font))), List(XML.Text(txt)))
    50 
    51   def hidden(txt: String): XML.Elem =
    52     span(Markup.HIDDEN, List(XML.Text(txt)))
    53 
    54   def sub(txt: String): XML.Elem = XML.elem("sub", List(XML.Text(txt)))
    55   def sup(txt: String): XML.Elem = XML.elem("sup", List(XML.Text(txt)))
    56   def bold(txt: String): XML.Elem = span("bold", List(XML.Text(txt)))
    57 
    58   def spans(input: XML.Tree, original_data: Boolean = false): XML.Body =
    59   {
    60     val symbols = Isabelle_System.symbols
    61 
    62     def html_spans(tree: XML.Tree): XML.Body =
    63       tree match {
    64         case XML.Elem(m @ Markup(name, props), ts) =>
    65           val span_class =
    66             m match { case Markup.Entity(kind, _) => name + "_" + kind case _ => name }
    67           val html_span = span(span_class, ts.flatMap(html_spans))
    68           if (original_data) List(XML.Elem(Markup.Data, List(tree, html_span)))
    69           else List(html_span)
    70         case XML.Text(txt) =>
    71           val ts = new ListBuffer[XML.Tree]
    72           val t = new StringBuilder
    73           def flush() {
    74             if (!t.isEmpty) {
    75               ts += XML.Text(t.toString)
    76               t.clear
    77             }
    78           }
    79           def add(elem: XML.Tree) {
    80             flush()
    81             ts += elem
    82           }
    83           val syms = Symbol.iterator_string(txt)
    84           while (syms.hasNext) {
    85             val s1 = syms.next
    86             def s2() = if (syms.hasNext) syms.next else ""
    87             if (s1 == "\n") add(XML.elem(BR))
    88             else if (s1 == symbols.bsub_decoded) t ++= s1  // FIXME
    89             else if (s1 == symbols.esub_decoded) t ++= s1  // FIXME
    90             else if (s1 == symbols.bsup_decoded) t ++= s1  // FIXME
    91             else if (s1 == symbols.esup_decoded) t ++= s1  // FIXME
    92             else if (symbols.is_subscript_decoded(s1)) { add(hidden(s1)); add(sub(s2())) }
    93             else if (symbols.is_superscript_decoded(s1)) { add(hidden(s1)); add(sup(s2())) }
    94             else if (s1 == symbols.bold_decoded) { add(hidden(s1)); add(bold(s2())) }
    95             else if (symbols.fonts.isDefinedAt(s1)) { add(user_font(symbols.fonts(s1), s1)) }
    96             else t ++= s1
    97           }
    98           flush()
    99           ts.toList
   100       }
   101     html_spans(input)
   102   }
   103 }