From 3344655f91ba42692e9460eaaa41d8465b185508 Mon Sep 17 00:00:00 2001 From: Tiark Rompf Date: Fri, 25 Jul 2014 18:08:48 +0200 Subject: [PATCH] checkpoint --- build.sbt | 2 +- src/main/resources/template.html | 18 +++++----- .../scala/me/grison/scalocco/Mustache.scala | 3 +- .../scala/me/grison/scalocco/Scalocco.scala | 35 ++++++++++++------- 4 files changed, 34 insertions(+), 24 deletions(-) diff --git a/build.sbt b/build.sbt index 3847eb4..5737462 100644 --- a/build.sbt +++ b/build.sbt @@ -12,7 +12,7 @@ startYear := Some(2013) version := "1.1.0" -scalaVersion := "2.10.1" +scalaVersion := "2.11.2" scalacOptions ++= Seq( "-unchecked" diff --git a/src/main/resources/template.html b/src/main/resources/template.html index 000bc0c..3f66a19 100644 --- a/src/main/resources/template.html +++ b/src/main/resources/template.html @@ -23,29 +23,29 @@ } h4, h5, h6 { color: #333; - margin: 6px 0 6px 0; + padding: 6px 0 6px 0; font-size: 13px; } h2, h3 { - margin-bottom: 0; + padding-bottom: 15px; color: #000; overflow: hidden; } h1 { - margin-top: 40px; - margin-bottom: 15px; + /*padding-top: 40px;*/ + padding-bottom: 15px; color: #000; } #container { position: relative; } - #background { + /*#background { position: fixed; top: 0; left: 525px; right: 0; bottom: 0; background: #f5f5ff; border-left: 1px solid #e5e5ee; z-index: -1; - } + }*/ #jump_to, #jump_page { background: white; -webkit-box-shadow: 0 0 25px #777; -moz-box-shadow: 0 0 25px #777; @@ -122,7 +122,7 @@ opacity: 1; } td.code, th.code { - padding: 14px 15px 16px 25px; + padding: 10px 10px 10px 50px; width: 100%; vertical-align: top; background: #f5f5ff; @@ -154,7 +154,7 @@
{{#sources}} - + {{.}} {{/sources}} @@ -179,7 +179,7 @@

{{title}}

{{{doc}}} - +
{{code}}
diff --git a/src/main/scala/me/grison/scalocco/Mustache.scala b/src/main/scala/me/grison/scalocco/Mustache.scala index 9a249ab..4739d2f 100644 --- a/src/main/scala/me/grison/scalocco/Mustache.scala +++ b/src/main/scala/me/grison/scalocco/Mustache.scala @@ -441,10 +441,9 @@ case class MustacheParseException(line:Int, msg:String) stack.headOption match { case None => None case Some(head) => { - type MapLikeString[a, b] = MapLike[String, a, b] head match { case null => None - case m:MapLikeString[_,_] => + case m:Map[String,Any] => m.get(key) match { case Some(v) => v case None => None diff --git a/src/main/scala/me/grison/scalocco/Scalocco.scala b/src/main/scala/me/grison/scalocco/Scalocco.scala index cdf73f4..cd127ee 100644 --- a/src/main/scala/me/grison/scalocco/Scalocco.scala +++ b/src/main/scala/me/grison/scalocco/Scalocco.scala @@ -68,7 +68,7 @@ class Markdown { //Scalocco //--------------- object Scalocco extends Markdown { - // This value is used to differentiate normal comments from scaladoc style. + /* This value is used to differentiate normal comments from scaladoc style. */ val scaladoc = UUID.randomUUID().toString //ScalaDoc Parsing @@ -184,20 +184,23 @@ object Scalocco extends Markdown { } else if (line.matches("[^*]*[*]/\\s*$")) { inScalaDoc = false; // Reading a comment line `//` or ` * ` if inside `Scaladoc` - } else if (line.matches("^\\s*//.*") || (inScalaDoc && line.matches("^\\s*[*].*"))) { + //} else if (line.matches("^\\s*//.*") || inScalaDoc) { + } else if (inScalaDoc) { // if we did had code, store the code and documentation in the resulting section list if (hasCode) { val documentation = scaladocIfNeeded(doc) sections ::= Section(documentation, code.toString) hasCode = false doc = new StringBuilder - if (inScalaDoc) doc.append(scaladoc) + //if (inScalaDoc) doc.append(scaladoc) code = new StringBuilder } - doc.append(line.replaceFirst("^\\s*//", "")).append("\n") + val cleaned = line.replaceFirst(if (inScalaDoc) "^\\s*[*]" else "^\\s*//", "") + doc.append(cleaned).append("\n") } else { hasCode = true - code.append(line).append("\n") + if (!code.isEmpty || !line.trim.isEmpty) + code.append(line).append("\n") inScalaDoc = false } ) @@ -213,19 +216,26 @@ object Scalocco extends Markdown { * @param path the Path of the original file. * @param destPath the Path where to write the documentation file. */ - def documentFile(source: File, path: String, destPath: String) = { + def documentFile(source: File, path: String, destPath: String, templateFile: String) = { val sections = parseSections(source) - val mustache = new Mustache(Source.fromURL(getClass.getResource("/template.html")).mkString) + val template = if (templateFile == "default") Source.fromURL(getClass.getResource("/template.html")).mkString + else Source.fromFile(templateFile).mkString + val mustache = new Mustache(template) + def outFile(destPath: String)(source: File) = { + new File(source.getCanonicalPath.replace(path, destPath).replace(".scala",".html")) + } val html = mustache.render(Map( // This is the title of the Scala source file "title" -> source.getName, // The sources are available in the right-upper box on the generated documentation - "sources" -> sources, + "destPath" -> destPath, + "sources" -> sources.map(outFile("")), // keep indexes for sections "sections" -> sections.zipWithIndex.map(t => Map("index" -> t._2, "code" -> t._1.code, "doc" -> t._1.doc.markdown)) )) - val outputFile = new File(source.getCanonicalPath.replace(path, destPath) + ".html") + val outputFile = outFile(destPath)(source) + (new File(outputFile.getParent)).mkdirs println("Generating documentation: " + outputFile.getCanonicalPath) // output the HTML rendered with Mustache into a file named `SOURCE_FILE.scala.html` val p = new PrintWriter(outputFile) @@ -237,7 +247,7 @@ object Scalocco extends Markdown { * @param path the Path were we may find some scala files. * @param destPath the Path were we should output the documentation. */ - def generateDoc(path: String, destPath: String) = { + def generateDoc(path: String, destPath: String, template: String) = { val files = scalaFiles(path) sources = files.toList @@ -249,7 +259,7 @@ object Scalocco extends Markdown { val dest = new File(destPath) if (!dest.exists()) dest.mkdirs() - files.foreach(documentFile(_, basePath, destPath)) + files.foreach(documentFile(_, basePath, destPath, template)) } /** @@ -261,7 +271,8 @@ object Scalocco extends Markdown { else { val path = args(0) val destPath = if (args.length > 1) args(1) else "./docs/" - generateDoc(path, destPath) + val template = if (args.length > 2) args(2) else "default" + generateDoc(path, destPath, template) } } }