summaryrefslogtreecommitdiff
path: root/book/src/main/scala/book/Utils.scala
blob: 01682da82cb2aceff2751777e8a6612fd280e98d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package book

import acyclic.file
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(" ", "")
  }
}