progs/display/display.scala
changeset 738 084e2843f478
child 873 a25da86f7c8c
equal deleted inserted replaced
737:14a348d050b3 738:084e2843f478
       
     1 
       
     2 import scalatags.Text.all._
       
     3 import scalatags.Text._
       
     4 import scala.util.Random
       
     5 
       
     6 
       
     7 
       
     8 object MinimalApplication extends cask.MainRoutes{
       
     9 
       
    10   abstract class Rexp 
       
    11   case object ZERO extends Rexp
       
    12   case object ONE extends Rexp
       
    13   case class CHAR(c: Char) extends Rexp
       
    14   case class ALT(r1: Rexp, r2: Rexp) extends Rexp 
       
    15   case class SEQ(r1: Rexp, r2: Rexp) extends Rexp 
       
    16   case class STAR(r: Rexp) extends Rexp 
       
    17 
       
    18   val evil2 = ALT(SEQ(STAR(STAR(CHAR('a'))), CHAR('b')), ONE)
       
    19 
       
    20   def pp(r: Rexp) : TypedTag[String] = r match {
       
    21     case CHAR(c) => li(code(c.toString))
       
    22     case ALT(r1, r2) => li(code("+"), ul(pp(r1), pp(r2)))
       
    23     case SEQ(r1, r2) => li(code("o"), ul(pp(r1), pp(r2)))
       
    24     case STAR(r1) => li(code("*"), ul(pp(r1)))
       
    25     case ZERO => li(code("0"))
       
    26     case ONE => li(code(div(style :="line-height:50%")
       
    27                            ("1",br, 
       
    28                             div(color:="blue", fontSize:=6.pt)("0101010101"))))
       
    29   } 
       
    30 
       
    31   abstract class ARexp 
       
    32   case object AZERO extends ARexp
       
    33   case class AONE(bs: String) extends ARexp
       
    34   case class ACHAR(bs:String, c: Char) extends ARexp
       
    35   case class AALT(bs:String, r1: ARexp, r2: ARexp) extends ARexp 
       
    36   case class ASEQ(bs: String,r1: ARexp, r2: ARexp) extends ARexp 
       
    37   case class ASTAR(bs:String,r: ARexp) extends ARexp 
       
    38 
       
    39 
       
    40   def node(s: String, bs: String) = {
       
    41     if (bs == "")
       
    42       code(div(div(style :="line-height:75%")(s, br, div(color:="blue", fontSize:=6.pt)(raw("&nbsp")))))
       
    43     else
       
    44       code(div(div(style :="line-height:75%")(s, br, div(color:="blue", fontSize:=6.pt)(bs))))
       
    45   }
       
    46   
       
    47 
       
    48   val aevil2 = AALT("", ASEQ(" ",ASTAR("111",ASTAR("0",ACHAR("00",'a'))), ACHAR("00",'b')), AONE("01"))
       
    49 
       
    50   def ppa(r: ARexp) : TypedTag[String] = r match {
       
    51     case ACHAR(bs, c) => li(node(c.toString, bs))
       
    52     case AALT(bs, r1, r2) => li(node("+", bs), ul(ppa(r1), ppa(r2)))
       
    53     case ASEQ(bs, r1, r2) => li(node("o", bs), ul(ppa(r1), ppa(r2)))
       
    54     case ASTAR(bs, r1) => li(node("*", bs), ul(ppa(r1)))
       
    55     case AZERO => li(node("0", ""))
       
    56     case AONE(bs) => li(node("1", bs))
       
    57   } 
       
    58 
       
    59 
       
    60 
       
    61 
       
    62 
       
    63   val bootstrap = "https://stackpath.bootstrapcdn.com/bootstrap/4.5.0/css/bootstrap.css"
       
    64 
       
    65   var cnt = 0
       
    66 
       
    67   @cask.get("/")
       
    68   def hello() = 
       
    69       doctype("html")(
       
    70       html(
       
    71         head(link(rel := "stylesheet", href := bootstrap),
       
    72              link(rel := "stylesheet", href := "static/style.css")), 
       
    73         body(
       
    74           div(cls := "container")( 
       
    75             h1(s"Hello"),
       
    76             i(color.green)(s"$cnt"),
       
    77             form(action := "/", method := "post")(
       
    78               button(`type` := "submit", name := "name", value := 1)(s"1"),
       
    79             ),
       
    80             ul(cls := "tree")(pp(evil2)),
       
    81 	    ul(cls := "tree")(ppa(aevil2))
       
    82           )
       
    83         )
       
    84       )
       
    85     )
       
    86 
       
    87   @cask.postForm("/")
       
    88   def answer(name: String) = {
       
    89     cnt = cnt + 1
       
    90     hello()
       
    91   }
       
    92 
       
    93   //@cask.staticFiles("/static/")
       
    94   //def staticFileRoutes() = "static"
       
    95 
       
    96   @cask.staticResources("/static")
       
    97   def staticResourceRoutes() = "static"
       
    98 
       
    99   initialize()
       
   100 }