summaryrefslogblamecommitdiff
path: root/book/src/main/scala/book/Utils.scala
blob: 473832475bccf4f5c594eee7d8eaa40f473432f3 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
11
12
13
14













                                                                               

                                                                         
                       
                                   
                                
               

                         



                                                                



                                     

                                    









                                                        
 


                

                                                                                    
            
           
                                          
                                  


                                





                     


                            
                                                      

                                                  



                                                            
                              

                                                                       

                                                                                
                       

                 
                                        





                             
 
package book


import scala.collection.mutable
import scalatags.Text.all._

case class Node(name: String, children: mutable.Buffer[Node])
object Utils{
  val autoResources = Seq(
    "META-INF/resources/webjars/highlightjs/8.2-1/highlight.min.js",
    "META-INF/resources/webjars/highlightjs/8.2-1/styles/idea.min.css",
    "META-INF/resources/webjars/highlightjs/8.2-1/languages/scala.min.js",
    "META-INF/resources/webjars/highlightjs/8.2-1/languages/javascript.min.js",
    "META-INF/resources/webjars/highlightjs/8.2-1/languages/bash.min.js",
    "META-INF/resources/webjars/highlightjs/8.2-1/languages/diff.min.js",
    "META-INF/resources/webjars/highlightjs/8.2-1/languages/xml.min.js",
    "css/pure-min.css",
    "css/grids-responsive-min.css",
    "css/layouts/side-menu.css",
    "js/ui.js",
    "example-fastopt.js",
    "webpage/weather.js"
  )
  
  val manualResources = Seq(
    "images/javascript-the-good-parts-the-definitive-guide.jpg",
    "images/Hello World.png",
    "images/Hello World White.png",
    "images/Hello World Console.png",
    "images/IntelliJ Hello.png",
    "images/Dropdown.png",
    "images/Scalatags Downloads.png"
  )
  
  val includes = for(res <- Utils.autoResources) yield {
    if (res.endsWith(".js"))
      script(src:=res)
    else if (res.endsWith(".css"))
      link(rel:="stylesheet", href:=res)
    else
      raw("")
  }

  var indent = 0


  val headers = Seq[((String, String) => scalatags.Text.Tag, Option[Frag => Frag])](
    ((h, s) => div(cls:="header")(
      h1(h),
      h2(s)
    ), Some(f => div(cls:="content", f))),
    ((h, s) => div(cls:="header")(
      h1(id:=Utils.munge(h), h),
      br
    ), None),
    (h1(_, _), None),
    (h2(_, _), None),
    (h3(_, _), None),
    (h4(_, _), None),
    (h5(_, _), None),
    (h6(_, _), None)
  )

  var structure: Node = null
  case class sect(name: String, subname: String = ""){
    indent += 1
    val newNode = Node(name, mutable.Buffer.empty)
    val (headerWrap, contentWrap) = headers(indent-1)
    if (structure!= null) structure.children.append(newNode)
    val prev = structure
    structure = newNode
    def apply(args: Frag*) = {
      val wrappedContents = contentWrap.getOrElse((x: Frag) => x)(args)
      val res = Seq[Frag](
        if (name == "") ""
        else headerWrap(name, subname)(cls:="content-subhead", id:=munge(name)),
        wrappedContents
      )
      indent -= 1
      if (prev != null) structure = prev
      res
    }
  }
  def munge(name: String) = {
    name.replace(" ", "")
  }
}