summaryrefslogtreecommitdiff
path: root/book/src/main/scala/book/Book.scala
diff options
context:
space:
mode:
authorLi Haoyi <haoyi@dropbox.com>2014-10-29 23:07:55 -0700
committerLi Haoyi <haoyi@dropbox.com>2014-10-29 23:07:55 -0700
commite8b38f242876f99966c3d13cefae2f5863c5bb9e (patch)
tree01989c3899a57cbbc37c5479a7489aa6ac8c753f /book/src/main/scala/book/Book.scala
parent42394b5fa4bc0a76585d77f587a79b11c1b7c32f (diff)
downloadhands-on-scala-js-e8b38f242876f99966c3d13cefae2f5863c5bb9e.tar.gz
hands-on-scala-js-e8b38f242876f99966c3d13cefae2f5863c5bb9e.tar.bz2
hands-on-scala-js-e8b38f242876f99966c3d13cefae2f5863c5bb9e.zip
lots of refactoring
Diffstat (limited to 'book/src/main/scala/book/Book.scala')
-rw-r--r--book/src/main/scala/book/Book.scala50
1 files changed, 20 insertions, 30 deletions
diff --git a/book/src/main/scala/book/Book.scala b/book/src/main/scala/book/Book.scala
index 909c46e..c41ab15 100644
--- a/book/src/main/scala/book/Book.scala
+++ b/book/src/main/scala/book/Book.scala
@@ -123,9 +123,9 @@ object Book {
* }
*/
- def ref(filepath: String, identifier: String = "", end: String = "", indented: Boolean = true) = {
+ def ref(filepath: String, start: String = "", end: String = "\n") = {
- val lang = filepath.split('.').last match{
+ val lang = filepath.split('.').last match {
case "js" => "javascript"
case "scala" => "scala"
case "sbt" => "scala"
@@ -138,35 +138,25 @@ object Book {
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 =>
- if (end == "") {
- val firstCharIndex = x.indexWhere(!_.isWhitespace)
- firstCharIndex == -1 || firstCharIndex >= whitespace + (if (indented) 1 else 0)
- }else{
- !x.contains(end)
- }
- }
-
- val stuff =
- if (!indented) {
- things
- } else {
- val last = lines(firstLine + things.length + 1)
- if (last.trim.toSet subsetOf "}])".toSet) {
- lines(firstLine) +: things :+ last
- } else {
- lines(firstLine) +: things
- }
- }
- stuff.map(_.drop(whitespace)).mkString("\n")
+ 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))
}
}