From c4757c39f238f3c3f9a75e78d54e5a42d9934142 Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Mon, 9 Jan 2017 16:59:17 +0100 Subject: Implement site structure discovery --- doc-tool/resources/css/dottydoc.css | 0 .../src/dotty/tools/dottydoc/staticsite/Site.scala | 70 ++++++++++++++++++++++ .../tools/dottydoc/staticsite/SiteTests.scala | 32 ++++++---- 3 files changed, 92 insertions(+), 10 deletions(-) create mode 100644 doc-tool/resources/css/dottydoc.css (limited to 'doc-tool') diff --git a/doc-tool/resources/css/dottydoc.css b/doc-tool/resources/css/dottydoc.css new file mode 100644 index 000000000..e69de29bb diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala index b1962735f..b97803579 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala @@ -6,8 +6,62 @@ import _root_.java.io.{ File => JFile } 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 { + /** All files that are considered static in this context, this can be + * anything from CSS, JS to images and other files. + * + * @note files that are *not* considered static are files ending in a compilable + * extension. + */ + def staticAssets: Array[JFile] = { + if (_staticAssets eq null) initFiles() + _staticAssets + } + + /** All files that are considered compilable assets in this context. This + * is mainly markdown and html files, but could include other files in the + * future. + * + * @note files that are considered compilable end in `.md` or `.html` + */ + def compilableFiles: Array[JFile] = { + if (_compilableFiles eq null) initFiles() + _compilableFiles + } + + /** This function allows the stripping of the path that leads up to root. + * + * ```scala + * stripRoot(new JFile("/some/root/dir/css/index.css")) + * // returns: dir/css/index.css + * // given that root is: /some/root + * ``` + */ + def stripRoot(f: JFile): String = { + val rootLen = root.getAbsolutePath.length + 1 + f.getAbsolutePath.drop(rootLen) + } + + // Initialization of `staticAssets` and `compilableAssets`: + private[this] var _staticAssets: Array[JFile] = _ + private[this] var _compilableFiles: Array[JFile] = _ + private[this] def initFiles() = { + // 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)) + else if (name.endsWith(".md") || name.endsWith(".html")) comp.append(f) + else assets.append(f) + } + + val assets = new ArrayBuffer[JFile] + val comp = new ArrayBuffer[JFile] + splitFiles(root, assets, comp) + _staticAssets = assets.toArray + _compilableFiles = comp.toArray + } /** Files that define a layout then referred to by `layout: filename-no-ext` * in yaml front-matter. @@ -33,6 +87,19 @@ class Site(val root: JFile) extends ResourceFinder { defaultLayouts ++ userDefinedLayouts } + /** Include files are allowed under the directory `_includes`. These files + * have to be compilable files and can be used with liquid includes: + * + * ``` + * {% include "some-file" %} + * ``` + * + * You can also use the `with` statement: + * + * ``` + * {% include "some-file" with { key: value } %} + * ``` + */ val includes: Map[String, String] = { val userDefinedIncludes = root @@ -56,6 +123,9 @@ class Site(val root: JFile) extends ResourceFinder { } .toMap + /** 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 = { page.yaml.get("layout").flatMap(layouts.get(_)) match { case None => diff --git a/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala b/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala index 0c6d232f5..91dd86321 100644 --- a/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala +++ b/doc-tool/test/dotty/tools/dottydoc/staticsite/SiteTests.scala @@ -6,6 +6,8 @@ import org.junit.Test import org.junit.Assert._ class SiteTests extends DottyDocTest { + val site = new Site(new java.io.File("../doc-tool/resources/")) + private def html( str: String, params: Map[String, AnyRef] = Map.empty, @@ -13,8 +15,6 @@ class SiteTests extends DottyDocTest { ) = new HtmlPage(str, params, includes) @Test def hasCorrectLayoutFiles = { - val site = new Site(new java.io.File("../doc-tool/resources/")) - assert(site.root.exists && site.root.isDirectory, s"'${site.root.getName}' is not a directory") @@ -24,8 +24,6 @@ class SiteTests extends DottyDocTest { } @Test def renderHelloInMainLayout = { - val site = new Site(new java.io.File("../doc-tool/resources/")) - val renderedPage = site.render(html( """|--- |layout: main @@ -43,8 +41,6 @@ class SiteTests extends DottyDocTest { } @Test def renderMultipleTemplates = { - val site = new Site(new java.io.File("../doc-tool/resources/")) - val renderedPage = site.render(html( """|--- |layout: index @@ -62,8 +58,6 @@ class SiteTests extends DottyDocTest { } @Test def preservesPageYaml = { - val site = new Site(new java.io.File("../doc-tool/resources/")) - val renderedPage = site.render(html( """|--- |title: Hello, world @@ -83,8 +77,6 @@ class SiteTests extends DottyDocTest { } @Test def include = { - val site = new Site(new java.io.File("../doc-tool/resources/")) - val renderedInclude = site.render( html("""{% include "header.html" %}""", includes = site.includes), Map.empty @@ -92,4 +84,24 @@ class SiteTests extends DottyDocTest { assertEquals("

Some header

\n", renderedInclude) } + + @Test def siteStructure = { + val assets = site.staticAssets.map(site.stripRoot).toSet + val compd = site.compilableFiles.map(site.stripRoot).toSet + + val expectedAssets = Set( + "css/dottydoc.css" + ) + val expectedCompd = Set( + "index.md", + "_includes/header.html", + "_layouts/index.html", + "_layouts/main.html" + ) + + assert(expectedAssets == assets, + s"assets incorrect, found: $assets - expected $expectedAssets") + assert(expectedCompd == compd, + s"compilable files incorrect, found: $compd - expected $expectedCompd") + } } -- cgit v1.2.3