summaryrefslogtreecommitdiff
path: root/book/src/main/scala
diff options
context:
space:
mode:
Diffstat (limited to 'book/src/main/scala')
-rw-r--r--book/src/main/scala/book/Book.scala43
-rw-r--r--book/src/main/scala/book/Main.scala36
-rw-r--r--book/src/main/scala/book/Utils.scala57
3 files changed, 136 insertions, 0 deletions
diff --git a/book/src/main/scala/book/Book.scala b/book/src/main/scala/book/Book.scala
new file mode 100644
index 0000000..e509716
--- /dev/null
+++ b/book/src/main/scala/book/Book.scala
@@ -0,0 +1,43 @@
+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
+
+ val intro = twf("book/intro.tw")
+ val contentBar = {
+ def rec(current: Node, depth: Int): Frag = {
+ div(
+ marginLeft := s"${depth * 5}px",
+ a(current.name, href:="#"+Utils.munge(current.name)),
+ current.children.map(
+ rec(_, depth + 1)
+ )
+ )
+ }
+ // @li(cls:="menu-item-divided pure-menu-selected")
+ ul(rec(Utils.structure, 0))
+ }
+ println(contentBar)
+
+ val txt = twf("book/index.tw").render
+
+ object highlight{
+ def highlight(snippet: Seq[String], lang: String) = {
+ pre(code(cls:=lang, snippet.mkString))
+ }
+
+ def javascript(code: String*) = highlight(code, "javascript")
+ def scala(code: String*) = highlight(code, "scala")
+ def bash(code: String*) = highlight(code, "bash")
+ }
+}
diff --git a/book/src/main/scala/book/Main.scala b/book/src/main/scala/book/Main.scala
new file mode 100644
index 0000000..9b06d29
--- /dev/null
+++ b/book/src/main/scala/book/Main.scala
@@ -0,0 +1,36 @@
+package book
+
+import java.io.InputStream
+import java.nio.charset.StandardCharsets
+import java.nio.file.{Paths, Files}
+
+import scala.collection.mutable
+import scalatags.Text.all._
+import scalatags.Text.tags2
+
+
+object Main {
+ def write(txt: String, dest: String) = {
+ Paths.get(dest).toFile.getParentFile.mkdirs()
+ Files.deleteIfExists(Paths.get(dest))
+ Files.write(Paths.get(dest), txt.getBytes)
+ }
+ def copy(src: InputStream, dest: String) = {
+ Paths.get(dest).toFile.getParentFile.mkdirs()
+ Files.deleteIfExists(Paths.get(dest))
+ Files.copy(src, Paths.get(dest))
+ }
+
+ def main(args: Array[String]): Unit = {
+ println("Writing Book")
+
+ write(Book.txt, "output/index.html")
+
+ for(res <- Utils.autoResources ++ Utils.manualResources) {
+ copy(getClass.getResourceAsStream("/" + res), "output/" + res)
+ }
+ println("Writing Done")
+ }
+
+
+}
diff --git a/book/src/main/scala/book/Utils.scala b/book/src/main/scala/book/Utils.scala
new file mode 100644
index 0000000..0b435a3
--- /dev/null
+++ b/book/src/main/scala/book/Utils.scala
@@ -0,0 +1,57 @@
+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",
+ "css/pure-min.css",
+ "css/layouts/side-menu.css",
+ "js/ui.js"
+ )
+
+ val manualResources = Seq(
+ "images/javascript-the-good-parts-the-definitive-guide.jpg",
+ "example-fastopt.js"
+ )
+
+ 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("")
+ }
+ println(includes)
+ var indent = 1
+ val headers = Seq(h1, h2, h3, h4, h5, h6)
+ val structure = Node("Hands-on Scala.js", mutable.Buffer.empty)
+ var current = structure
+ case class sect(name: String){
+ indent += 1
+ val newNode = Node(name, mutable.Buffer.empty)
+ current.children.append(newNode)
+ val prev = current
+ current = newNode
+ def apply(args: Frag*) = {
+ val res = Seq(
+ headers(indent-1)(cls:="content-subhead", id:=munge(name), name) +: args:_*
+ )
+ indent -= 1
+ current = prev
+ res
+ }
+ }
+ def munge(name: String) = {
+ name.replace(" ", "")
+ }
+
+} \ No newline at end of file