Thy.html.scala Maven / Gradle / Ivy
/* Title: Pure/Thy/html.scala
Author: Makarius
HTML presentation elements.
*/
package isabelle
object HTML
{
/* encode text with control symbols */
val control_decoded =
Map(Symbol.sub_decoded -> "sub",
Symbol.sup_decoded -> "sup",
Symbol.bold_decoded -> "b")
def encode(text: String): String =
{
val result = new StringBuilder
def encode_char(c: Char) =
c match {
case '<' => result ++= "<"
case '>' => result ++= ">"
case '&' => result ++= "&"
case '"' => result ++= """
case '\'' => result ++= "'"
case '\n' => result ++= "
"
case _ => result += c
}
def encode_chars(s: String) = s.iterator.foreach(encode_char(_))
var control = ""
for (sym <- Symbol.iterator(text)) {
if (control_decoded.isDefinedAt(sym)) control = sym
else {
control_decoded.get(control) match {
case Some(elem) if Symbol.is_controllable(sym) && sym != "\"" =>
result ++= ("<" + elem + ">")
encode_chars(sym)
result ++= ("" + elem + ">")
case _ =>
encode_chars(control)
encode_chars(sym)
}
control = ""
}
}
encode_chars(control)
result.toString
}
/* document */
val end_document = "\n