summaryrefslogtreecommitdiff
path: root/book/src/main/scala/book/Book.scala
blob: c41ab15dd990fe07f958778bfa526d42868aed4c (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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
package book

import twist._

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

/**
 * Created by haoyi on 10/26/14.
 */
object Book {
  import Utils.sect

  lazy val intro = sect("Intro to Scala.js")(twf("book/intro.tw"))
  lazy val gettingStarted = sect("Getting Started")(twf("book/getting-started.tw"))
  lazy val canvasApp = sect("Canvas App")(twf("book/canvas-app.tw"))
  val txt = twf("book/index.tw")
  val contentBar = {
    def rec(current: Node, depth: Int): Seq[Frag] = {
      println("\t"*depth + current.name)
      Seq(
        li(
          a(
            current.name,
            href:="#"+Utils.munge(current.name),
            paddingLeft := s"${depth * 10 + 10}px",
            cls := "menu-item" + (if (depth == 1) " menu-item-divided " else "")
          )
        )
      ) ++ current.children.flatMap(rec(_, depth + 1))
    }

    println("TABLE OF CONTENTS")
    rec(Utils.structure, 0)
  }
  val site = Seq(
    raw("<!doctype html>"),
    html(
      head(
        meta(charset:="utf-8"),
        meta(name:="viewport", content:="width=device-width, initial-scale=1.0"),
        tags2.title("Hands-on Scala.js"),
        Utils.includes
      ),

      div(id:="layout")(
        a(href:="#menu", id:="menuLink", cls:="menu-link")(
          span
        ),

        div(id:="menu")(
          div(cls:="pure-menu pure-menu-open")(
            a(cls:="pure-menu-heading", href:="#")(
              "Contents"
            ),
            ul(cls:="menu-item-list")(
              contentBar
            )
          )
        )
      ),
      div(id:="main",
        div(id:="main-box")(
          txt
        )
      )
    )
  ).render

  object hli{
    def javascript(code: String*) = hl.highlight(code, "javascript", inline=true)
    def scala(code: String*) = hl.highlight(code, "scala", inline=true)
    def bash(code: String*) = hl.highlight(code, "bash", inline=true)
    def diff(code: String*) = hl.highlight(code, "diff", inline=true)
    def html(code: String*) = hl.highlight(code, "xml", inline=true)
  }

  object hl{
    def highlight(snippet: Seq[String], lang: String, inline: Boolean) = {
      val string = snippet.mkString
      val lines = string.split("\n", -1)
      if (inline){
        code(cls:=lang + " highlight-me", lines(0), padding:=0, display:="inline")
      }else{
        val minIndent = lines.map(_.takeWhile(_ == ' ').length)
          .filter(_ > 0)
          .min
        val stripped = lines.map(_.drop(minIndent))
          .dropWhile(_ == "")
          .mkString("\n")

        pre(code(cls:=lang + " highlight-me", stripped))
      }
    }

    def javascript(code: String*) = highlight(code, "javascript", inline=false)
    def scala(code: String*) = highlight(code, "scala", inline=false)
    def bash(code: String*) = highlight(code, "bash", inline=false)
    def diff(code: String*) = highlight(code, "diff", inline=false)
    def html(code: String*) = highlight(code, "xml", inline=false)

    /**
     * Kinds of refs:
     *
     * Rule: Starting from a line, keep consuming until
     * the identation drops below the start
     *
     * def main = {
     *   /*example*/
     *   i am a cow
     *   hear me moo
     * }
     *
     * Rule: Starting from a line, keep consuming until
     * the indentation becomes equivalent to the current. If
     * it's a cosing brace, keep it.
     * val x = omg
     * val y = zzz
     *
     * class C{
     *
     * }
     */

    def ref(filepath: String, start: String = "", end: String = "\n") = {

      val lang = filepath.split('.').last match {
        case "js" => "javascript"
        case "scala" => "scala"
        case "sbt" => "scala"
        case "sh" => "bash"
        case "html" => "xml"
        case x =>
          println("??? " + x)
          ???
      }

      val lines = io.Source.fromFile(filepath).getLines().toVector

      def indent(line: String) = line.takeWhile(_.isWhitespace).length
      println(lines)
      println(start)

      val startLine = lines.indexWhere(_.contains(start))
      if (startLine == -1){
        throw new Exception("Can't find marker: " + start)
      }
      val whitespace = indent(lines(startLine))
      val endLine = lines.indexWhere(
        line => line.contains(end) || (indent(line) < whitespace && line.trim != ""),
        startLine
      )
      val sliced =
        if (endLine == -1) lines.drop(startLine)
        else lines.slice(startLine, endLine)
      val blob = sliced.map(_.drop(whitespace)).mkString("\n")


      pre(code(cls:=lang + " highlight-me", blob))
    }
  }
}