diff options
author | Li Haoyi <haoyi@dropbox.com> | 2014-10-26 22:54:44 -0700 |
---|---|---|
committer | Li Haoyi <haoyi@dropbox.com> | 2014-10-26 22:54:44 -0700 |
commit | 6987ac5195b0112b4bda123f0fced3ace7115932 (patch) | |
tree | 6d38c52d6a0d7876cde7f25b8ef39667bd168ef1 /book/src/main/scala | |
parent | ab668dead5c3123eb9fb26b9e94c6eccabaf6ab7 (diff) | |
download | hands-on-scala-js-6987ac5195b0112b4bda123f0fced3ace7115932.tar.gz hands-on-scala-js-6987ac5195b0112b4bda123f0fced3ace7115932.tar.bz2 hands-on-scala-js-6987ac5195b0112b4bda123f0fced3ace7115932.zip |
stuff works, paragraph-starting-tags still accidentally capturing entire paragraph
Diffstat (limited to 'book/src/main/scala')
-rw-r--r-- | book/src/main/scala/book/Book.scala | 111 | ||||
-rw-r--r-- | book/src/main/scala/book/Main.scala | 8 | ||||
-rw-r--r-- | book/src/main/scala/book/Utils.scala | 7 |
3 files changed, 119 insertions, 7 deletions
diff --git a/book/src/main/scala/book/Book.scala b/book/src/main/scala/book/Book.scala index e509716..d301824 100644 --- a/book/src/main/scala/book/Book.scala +++ b/book/src/main/scala/book/Book.scala @@ -14,6 +14,7 @@ object Book { import Utils.sect val intro = twf("book/intro.tw") + val gettingStarted = twf("book/getting-started.tw") val contentBar = { def rec(current: Node, depth: Int): Frag = { div( @@ -27,17 +28,113 @@ object Book { // @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)) + 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{ + println("LINES " + lines.toList) + println(snippet) + 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") - def scala(code: String*) = highlight(code, "scala") - def bash(code: String*) = highlight(code, "bash") + 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, identifier: String = "", indented: Boolean = true) = { + + 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 + val blob = if (identifier == ""){ + lines.mkString("\n") + }else { + val firstLine = lines.indexWhere(_.contains(identifier)) + val whitespace = lines(firstLine).indexWhere(!_.isWhitespace) + val things = + lines.drop(firstLine + 1) + .takeWhile{ x => + val firstCharIndex = x.indexWhere(!_.isWhitespace) + firstCharIndex == -1 || firstCharIndex >= whitespace + (if (indented) 1 else 0) + } + + things.foreach(println) + val stuff = + if (!indented) { + println("NOT INDENTED " + things) + things + } + else { + val last = lines(firstLine + things.length + 1) +// println("LAST: " + last) + if (last.trim.toSet subsetOf "}])".toSet) { + lines(firstLine) +: things :+ last + } else { + lines(firstLine) +: things + } + } + stuff.map(_.drop(whitespace)).mkString("\n") + } + println(blob) + + pre(code(cls:=lang + " highlight-me", blob)) + } } + + } diff --git a/book/src/main/scala/book/Main.scala b/book/src/main/scala/book/Main.scala index 9b06d29..89fb5e1 100644 --- a/book/src/main/scala/book/Main.scala +++ b/book/src/main/scala/book/Main.scala @@ -4,6 +4,8 @@ import java.io.InputStream import java.nio.charset.StandardCharsets import java.nio.file.{Paths, Files} +import org.eclipse.jgit.api.Git + import scala.collection.mutable import scalatags.Text.all._ import scalatags.Text.tags2 @@ -23,6 +25,12 @@ object Main { def main(args: Array[String]): Unit = { println("Writing Book") +// Files.deleteIfExists(Paths.get("output/temp")) +// Git.cloneRepository() +// .setURI("https://github.com/lihaoyi/workbench-example-app") +// .setDirectory(new java.io.File("output/temp")) +// .call() + write(Book.txt, "output/index.html") diff --git a/book/src/main/scala/book/Utils.scala b/book/src/main/scala/book/Utils.scala index 0b435a3..a7b36d1 100644 --- a/book/src/main/scala/book/Utils.scala +++ b/book/src/main/scala/book/Utils.scala @@ -12,13 +12,20 @@ object Utils{ "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" ) 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", "example-fastopt.js" ) |