diff options
author | Li Haoyi <haoyi@dropbox.com> | 2014-11-10 21:10:07 -0800 |
---|---|---|
committer | Li Haoyi <haoyi@dropbox.com> | 2014-11-10 21:10:07 -0800 |
commit | 8ced367e0d736b429f0b39ae7fde2b76b1d64ed5 (patch) | |
tree | e1154648219f8367c2537580f207e8f100e75590 /book/src/main/scala/book/Book.scala | |
parent | 2846d28c95d183792d6d809d1c3884b619b6f937 (diff) | |
download | hands-on-scala-js-8ced367e0d736b429f0b39ae7fde2b76b1d64ed5.tar.gz hands-on-scala-js-8ced367e0d736b429f0b39ae7fde2b76b1d64ed5.tar.bz2 hands-on-scala-js-8ced367e0d736b429f0b39ae7fde2b76b1d64ed5.zip |
More refactoring
Diffstat (limited to 'book/src/main/scala/book/Book.scala')
-rw-r--r-- | book/src/main/scala/book/Book.scala | 167 |
1 files changed, 79 insertions, 88 deletions
diff --git a/book/src/main/scala/book/Book.scala b/book/src/main/scala/book/Book.scala index e11c512..9473698 100644 --- a/book/src/main/scala/book/Book.scala +++ b/book/src/main/scala/book/Book.scala @@ -1,110 +1,101 @@ package book import acyclic.file -import scalatex._ 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 myTable = Seq( - ("Most of java.lang.*", "j.l.Thread, j.l.Runtime, ..."), - ("Almost all of scala.*", "s.c.parallel, s.tools.nsc"), - ("Some of java.util.*", "org.omg.CORBA, sun.misc.*"), - ("Scala Macros: upickle, scala-async, scalaxy, etc", "Reflection: scala-pickling, scala-reflect"), - ("Pure-Scala ecosystem: shapeless, scalaz, scalatags, utest", "Java-dependent: Scalatest, Scalate"), - ("JS stuff: XmlHttpRequest, Websockets. Localstorage", " JVM stuff: Netty, akka, spray, file IO, JNI"), - ("HTML DOM, Canvas, WebGL", "AWT, Swing, SWT, OpenGL"), - ("JavaScript libraries: chipmunk.js, hand.js, react.js, jquery", "Java ecosystem: guice, junit, apache-commons, log4j"), - ("IntelliJ, Eclipse, SBT, Chrome console, firebug", "Scala REPL, Yourkit, VisualVM, JProfiler") + 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" + ) - object hl{ - def highlight(snippet: Seq[String], lang: String) = { - val string = snippet.mkString - val lines = string.split("\n", -1) - if (lines.length == 1){ - 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") + val includes = for(res <- autoResources) yield { + if (res.endsWith(".js")) + script(src:=res) + else if (res.endsWith(".css")) + link(rel:="stylesheet", href:=res) + else + raw("") + } - pre(code(cls:=lang + " highlight-me", stripped)) - } + val txt = Index() + val contentBar = { + def rec(current: Node, depth: Int): Seq[Frag] = { + println("\t"*depth + current.name) + Seq( + li( + a( + current.name, + href:="#"+sect.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)) } - def javascript(code: String*) = highlight(code, "javascript") - def scala(code: String*) = highlight(code, "scala") - def bash(code: String*) = highlight(code, "bash") - def diff(code: String*) = highlight(code, "diff") - def html(code: String*) = highlight(code, "xml") - - 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 + println("TABLE OF CONTENTS") + rec(sect.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"), + includes + ), - def indent(line: String) = line.takeWhile(_.isWhitespace).length + div(id:="layout")( + a(href:="#menu", id:="menuLink", cls:="menu-link")( + span + ), - 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 + 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 + ) ) - val sliced = - if (endLine == -1) lines.drop(startLine) - else lines.slice(startLine, endLine) - val blob = sliced.map(_.drop(whitespace)).mkString("\n") + ) + ).render + - pre(code(cls:=lang + " highlight-me", blob)) - } - } - import java.io.File - def recursiveListFiles(f: File): Array[File] = { - val these = f.listFiles - these ++ these.filter(_.isDirectory).flatMap(recursiveListFiles) - } - lazy val javaAPIs = { - val roots = Seq( - "output/scala-js/javalanglib/src/main/scala", - "output/scala-js/javalib/src/main/scala" - ) - for{ - root <- roots - file <- recursiveListFiles(new File(root)) - if file != null - if file.isFile - } yield{ - val path = file.getPath - .drop(root.length + 1) - .dropRight(".scala".length) - val filename = path.replace('/', '.') - val docpath = s"https://docs.oracle.com/javase/7/docs/api/$path.html" - filename -> docpath - } - } } |