From 7b3588fbff791679fc82c4fe21085eb97a97cf0a Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Mon, 9 Jan 2017 19:04:16 +0100 Subject: Add static site gen to dottydoc main --- doc-tool/src/dotty/tools/dottydoc/DocDriver.scala | 21 ++++++++ .../src/dotty/tools/dottydoc/staticsite/Site.scala | 59 ++++++++++++++++++++-- .../tools/dottydoc/staticsite/SiteTests.scala | 9 ++-- 3 files changed, 82 insertions(+), 7 deletions(-) (limited to 'doc-tool') diff --git a/doc-tool/src/dotty/tools/dottydoc/DocDriver.scala b/doc-tool/src/dotty/tools/dottydoc/DocDriver.scala index 430fb8083..18480c94f 100644 --- a/doc-tool/src/dotty/tools/dottydoc/DocDriver.scala +++ b/doc-tool/src/dotty/tools/dottydoc/DocDriver.scala @@ -9,6 +9,7 @@ import model.Package import model.json._ import dotc.config._ import dotc.core.Comments.ContextDoc +import staticsite.Site /** `DocDriver` implements the main entry point to the Dotty documentation * tool. It's methods are used by the external scala and java APIs. @@ -47,4 +48,24 @@ class DocDriver extends Driver { def indexToJsonJava(index: JMap[String, Package]): String = indexToJson(index.asScala) + + override def main(args: Array[String]): Unit = { + implicit val (filesToDocument, ctx) = setup(args, initCtx.fresh) + //doCompile(newCompiler(ctx), fileNames)(ctx) + + val siteRoot = new java.io.File(ctx.settings.siteRoot.value) + + if (!siteRoot.exists || !siteRoot.isDirectory) + ctx.error(s"Site root does not exist: $siteRoot") + else { + Site(siteRoot) + .copyStaticFiles() + .generateHtmlFiles() + + + // FIXME: liqp templates are compiled by threadpools, for some reason it + // is not shutting down :-( + System.exit(0) + } + } } diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala index b97803579..3bc4db76d 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala @@ -2,13 +2,19 @@ package dotty.tools package dottydoc package staticsite +import _root_.java.nio.file.{ Files, FileSystems } +import _root_.java.nio.file.StandardCopyOption.REPLACE_EXISTING import _root_.java.io.{ File => JFile } +import _root_.java.nio.file.Path +import _root_.java.io.ByteArrayInputStream +import _root_.java.nio.charset.StandardCharsets + import dotc.config.Printers.dottydoc import dotc.core.Contexts.Context import scala.io.Source import scala.collection.mutable.ArrayBuffer -class Site(val root: JFile) extends ResourceFinder { +case class Site(val root: JFile) extends ResourceFinder { /** All files that are considered static in this context, this can be * anything from CSS, JS to images and other files. * @@ -31,6 +37,51 @@ class Site(val root: JFile) extends ResourceFinder { _compilableFiles } + // FileSystem getter + private[this] val fs = FileSystems.getDefault + + def copyStaticFiles(outDir: JFile = new JFile(root.getAbsolutePath + "/_site")): this.type = { + if (!outDir.isDirectory) outDir.mkdirs() + if (!outDir.isDirectory) /*dottydoc.*/println(s"couldn't create output folder: $outDir") + else staticAssets.foreach { asset => + val target = mkdirs(fs.getPath(outDir.getAbsolutePath, stripRoot(asset))) + val source = mkdirs(fs.getPath(asset.getAbsolutePath)) + Files.copy(source, target, REPLACE_EXISTING) + } + this + } + + /** Generate HTML files from markdown and .html sources */ + def generateHtmlFiles(outDir: JFile = new JFile(root.getAbsolutePath + "/_site"))(implicit ctx: Context): this.type = { + if (!outDir.isDirectory) outDir.mkdirs() + if (!outDir.isDirectory) /*dottydoc.*/println(s"couldn't create output folder: $outDir") + else compilableFiles.foreach { asset => + val fileContents = Source.fromFile(asset).mkString + val page = + if (asset.getName.endsWith(".md")) new MarkdownPage(fileContents, Map.empty, includes) + else new HtmlPage(fileContents, Map.empty, includes) + + val renderedPage = render(page) + val source = new ByteArrayInputStream(renderedPage.getBytes(StandardCharsets.UTF_8)) + val target = { + val tgt = stripRoot(asset) + tgt.splitAt(tgt.lastIndexOf('.'))._1 + ".html" + } + val htmlTarget = mkdirs(fs.getPath(outDir.getAbsolutePath, target)) + Files.copy(source, htmlTarget, REPLACE_EXISTING) + } + this + } + + private def mkdirs(path: Path): path.type = { + val parent = path.getParent.toFile + + if (!parent.isDirectory && !parent.mkdirs()) + dottydoc.println(s"couldn't create directory: $parent") + + path + } + /** This function allows the stripping of the path that leads up to root. * * ```scala @@ -51,7 +102,9 @@ class Site(val root: JFile) extends ResourceFinder { // Split files between compilable and static assets def splitFiles(f: JFile, assets: ArrayBuffer[JFile], comp: ArrayBuffer[JFile]): Unit = { val name = f.getName - if (f.isDirectory) f.listFiles.foreach(splitFiles(_, assets, comp)) + if (f.isDirectory) { + if (!f.getName.startsWith("_")) f.listFiles.foreach(splitFiles(_, assets, comp)) + } else if (name.endsWith(".md") || name.endsWith(".html")) comp.append(f) else assets.append(f) } @@ -126,7 +179,7 @@ class Site(val root: JFile) extends ResourceFinder { /** Render a page to html, the resulting string is the result of the complete * expansion of the template with all its layouts and includes. */ - def render(page: Page, params: Map[String, AnyRef])(implicit ctx: Context): String = { + def render(page: Page, params: Map[String, AnyRef] = Map.empty)(implicit ctx: Context): String = { page.yaml.get("layout").flatMap(layouts.get(_)) match { case None => page.html diff --git a/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala b/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala index 91dd86321..5ad1dd9eb 100644 --- a/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala +++ b/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala @@ -93,10 +93,11 @@ class SiteTests extends DottyDocTest { "css/dottydoc.css" ) val expectedCompd = Set( - "index.md", - "_includes/header.html", - "_layouts/index.html", - "_layouts/main.html" + "index.md" + // Directories starting in `_` are not included in compilable files + //"_includes/header.html", + //"_layouts/index.html", + //"_layouts/main.html" ) assert(expectedAssets == assets, -- cgit v1.2.3