src/Pure/Thy/html.scala
changeset 65992 50daca61efd6
parent 65991 fa787e233214
child 65993 75590c9a585f
equal deleted inserted replaced
65991:fa787e233214 65992:50daca61efd6
    80   def output(tree: XML.Tree): String = output(List(tree))
    80   def output(tree: XML.Tree): String = output(List(tree))
    81 
    81 
    82 
    82 
    83   /* attributes */
    83   /* attributes */
    84 
    84 
    85   class Attribute(name: String, value: String)
    85   class Attribute(val name: String, value: String)
    86   { def apply(elem: XML.Elem): XML.Elem = elem + (name -> value) }
    86   {
    87 
    87     def xml: XML.Attribute = name -> value
    88   def id(s: String) = new Attribute("id", s)
    88     def apply(elem: XML.Elem): XML.Elem = elem + xml
    89   def css_class(name: String) = new Attribute("class", name)
    89   }
    90 
    90 
    91   def width(w: Int) = new Attribute("width", w.toString)
    91   def id(s: String): Attribute = new Attribute("id", s)
    92   def height(h: Int) = new Attribute("height", h.toString)
    92   def css_class(name: String): Attribute = new Attribute("class", name)
       
    93 
       
    94   def width(w: Int): Attribute = new Attribute("width", w.toString)
       
    95   def height(h: Int): Attribute = new Attribute("height", h.toString)
    93   def size(w: Int, h: Int)(elem: XML.Elem): XML.Elem = width(w)(height(h)(elem))
    96   def size(w: Int, h: Int)(elem: XML.Elem): XML.Elem = width(w)(height(h)(elem))
    94 
    97 
    95 
    98 
    96   /* structured markup operators */
    99   /* structured markup operators */
    97 
   100 
    98   def text(txt: String): XML.Body = if (txt.isEmpty) Nil else List(XML.Text(txt))
   101   def text(txt: String): XML.Body = if (txt.isEmpty) Nil else List(XML.Text(txt))
    99   val break: XML.Body = List(XML.elem("br"))
   102   val break: XML.Body = List(XML.elem("br"))
   100 
   103 
   101   class Operator(name: String)
   104   class Operator(val name: String)
   102   { def apply(body: XML.Body): XML.Elem = XML.elem(name, body) }
   105   {
       
   106     def apply(body: XML.Body): XML.Elem = XML.elem(name, body)
       
   107     def apply(att: Attribute, body: XML.Body): XML.Elem = att(apply(body))
       
   108     def apply(c: String, body: XML.Body): XML.Elem = apply(css_class(c), body)
       
   109   }
   103 
   110 
   104   class Heading(name: String) extends Operator(name)
   111   class Heading(name: String) extends Operator(name)
   105   { def apply(txt: String): XML.Elem = super.apply(text(txt)) }
   112   {
       
   113     def apply(txt: String): XML.Elem = super.apply(text(txt))
       
   114     def apply(att: Attribute, txt: String): XML.Elem = super.apply(att, text(txt))
       
   115     def apply(c: String, txt: String): XML.Elem = super.apply(c, text(txt))
       
   116   }
   106 
   117 
   107   val div = new Operator("div")
   118   val div = new Operator("div")
   108   val span = new Operator("span")
   119   val span = new Operator("span")
   109   val pre = new Operator("pre")
   120   val pre = new Operator("pre")
   110   val par = new Operator("p")
   121   val par = new Operator("p")
   133     XML.Elem(Markup("a", List("href" -> href)), if (body.isEmpty) text(href) else body)
   144     XML.Elem(Markup("a", List("href" -> href)), if (body.isEmpty) text(href) else body)
   134 
   145 
   135   def image(src: String, alt: String = ""): XML.Elem =
   146   def image(src: String, alt: String = ""): XML.Elem =
   136     XML.Elem(Markup("img", List("src" -> src) ::: proper_string(alt).map("alt" -> _).toList), Nil)
   147     XML.Elem(Markup("img", List("src" -> src) ::: proper_string(alt).map("alt" -> _).toList), Nil)
   137 
   148 
   138   def source(src: String): XML.Elem = css_class("source")(div(List(pre(text(src)))))
   149   def source(src: String): XML.Elem = div("source", List(pre(text(src))))
   139 
   150 
   140   def style(s: String): XML.Elem = XML.elem("style", text(s))
   151   def style(s: String): XML.Elem = XML.elem("style", text(s))
   141 
   152 
   142 
   153 
   143   /* messages */
   154   /* messages */
   154 
   165 
   155 
   166 
   156   /* tooltips */
   167   /* tooltips */
   157 
   168 
   158   def tooltip(item: XML.Body, tip: XML.Body): XML.Elem =
   169   def tooltip(item: XML.Body, tip: XML.Body): XML.Elem =
   159     span(item ::: List(css_class("tooltip")(div(tip))))
   170     span(item ::: List(div("tooltip", tip)))
   160 
   171 
   161   def tooltip_errors(item: XML.Body, msgs: List[XML.Body]): XML.Elem =
   172   def tooltip_errors(item: XML.Body, msgs: List[XML.Body]): XML.Elem =
   162     HTML.error(tooltip(item, msgs.map(msg => error_message(pre(msg)))))
   173     HTML.error(tooltip(item, msgs.map(msg => error_message(pre(msg)))))
   163 
   174 
   164 
   175