From a84c09efcb2d843ee04ee3299ceb592a00a42267 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sun, 28 Dec 2014 09:09:39 +0100 Subject: wip --- book/src/main/scala/book/Utils.scala | 149 ++--------------------------------- 1 file changed, 7 insertions(+), 142 deletions(-) (limited to 'book/src/main/scala/book/Utils.scala') diff --git a/book/src/main/scala/book/Utils.scala b/book/src/main/scala/book/Utils.scala index 2c861d0..e551621 100644 --- a/book/src/main/scala/book/Utils.scala +++ b/book/src/main/scala/book/Utils.scala @@ -13,69 +13,18 @@ case class pureTable(header: Frag*){ ) } } -object sect{ - +object sect extends scalatex.site.Section{ var indent = 0 - val headers = Seq[((String, String, Frag) => scalatags.Text.Tag, Option[Frag => Frag])]( - ((h, s, l) => div(cls:="header")( - h1(h, l), - h2(s) - ), Some(f => div(cls:="content", f))), - ((h, s, l) => div(cls:="header")( - h1(id:=munge(h), h, l), - br - ), None), - (h1(_, _, _), None), - (h2(_, _, _), None), - (h3(_, _, _), None), - (h4(_, _, _), None), - (h5(_, _, _), None), - (h6(_, _, _), None) - ) - - var structure = Tree[String]("root", mutable.Buffer.empty) - - val usedRefs = mutable.Set.empty[String] - def ref(s: String, txt: String = "") = { - usedRefs += s - a(if (txt == "") s else txt, href:=s"#${munge(s)}") - } - - def munge(name: String) = { - name.replace(" ", "") - } + override val headers: Seq[Header] = Seq( + Header((h, s, l) => div(cls:="header")(h1(h, l), h2(s)), f => div(cls:="content", f)), + Header((h, s, l) => div(cls:="header")(h1(id:=munge(h), h, l), br)), + h1, h2, h3, h4, h5, h6 + ) } -case class sect(name: String, subname: String = ""){ - sect.indent += 1 - val newNode = Tree[String](name, mutable.Buffer.empty) - val (headerWrap, contentWrap) = sect.headers(sect.indent-1) - sect.structure.children.append(newNode) - val prev = sect.structure - sect.structure = newNode - def apply(args: Frag*) = { - val wrappedContents = contentWrap.getOrElse((x: Frag) => x)(args) - val headingAnchor = a( - cls:="header-link", - href:=s"#${sect.munge(name)}", - " ", - i(cls:="fa fa-link") - ) - val res = Seq[Frag]( - headerWrap(name, subname, headingAnchor)( - cls:="content-subhead", - id:=sect.munge(name) - ), - wrappedContents - ) - sect.indent -= 1 - sect.structure = prev - res - } -} -case class Tree[T](value: T, children: mutable.Buffer[Tree[T]]) + object lnk{ val usedLinks = mutable.Set.empty[String] def apply(name: String, url: String) = { @@ -122,87 +71,3 @@ object lnk{ val scalaPickling = lnk("scala-pickling", "https://github.com/scala/pickling") } } - -class Highlighter(mappings: Seq[(String, String)]){ - 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") - - pre(code(cls:=lang + " highlight-me hljs", stripped)) - } - } - - 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 - - def indent(line: String) = line.takeWhile(_.isWhitespace).length - - 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 + 1 - ) - val sliced = - if (endLine == -1) lines.drop(startLine) - else lines.slice(startLine, endLine) - - val blob = sliced.map(_.drop(whitespace)).mkString("\n") - - val (prefix, url) = - mappings.iterator - .find{case (prefix, path) => filepath.startsWith(prefix)} - .get - - val hash = - if (endLine == -1) "" - else s"#L$startLine-L$endLine" - - val linkUrl = - s"$url/tree/master/${filepath.drop(prefix.length)}$hash" - pre( - code(cls:=lang + " highlight-me hljs", blob), - a( - cls:="header-link", - i(cls:="fa fa-link "), - position.absolute, - right:="0.5em", - bottom:="0.5em", - display.block, - fontSize:="24px", - href:=linkUrl, - target:="_blank" - ) - ) - } -} \ No newline at end of file -- cgit v1.2.3