aboutsummaryrefslogtreecommitdiff
path: root/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala
diff options
context:
space:
mode:
authorFelix Mulder <felix.mulder@gmail.com>2017-01-09 15:27:08 +0100
committerFelix Mulder <felix.mulder@gmail.com>2017-01-31 14:31:05 +0100
commit51a22ba302184937f4b7930cc7db60b2ab353e71 (patch)
tree340af479d8998eed925689d569d61c24f9ff54d9 /doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala
parentce4ba7f7718fb1b5f698e784fa7f89902f5bf098 (diff)
downloaddotty-51a22ba302184937f4b7930cc7db60b2ab353e71.tar.gz
dotty-51a22ba302184937f4b7930cc7db60b2ab353e71.tar.bz2
dotty-51a22ba302184937f4b7930cc7db60b2ab353e71.zip
Implement `{% include 'template' %}` feature
Diffstat (limited to 'doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala')
-rw-r--r--doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala46
1 files changed, 25 insertions, 21 deletions
diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala
index ac609e6b6..b1962735f 100644
--- a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala
+++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala
@@ -7,12 +7,7 @@ import dotc.config.Printers.dottydoc
import dotc.core.Contexts.Context
import scala.io.Source
-class Site(val root: JFile) {
-
- /** If, for some reason, the supplied default files cannot be found - this
- * exception will be thrown in `layouts`.
- */
- final case class ResourceNotFoundException(message: String) extends Exception(message)
+class Site(val root: JFile) extends ResourceFinder {
/** Files that define a layout then referred to by `layout: filename-no-ext`
* in yaml front-matter.
@@ -24,19 +19,10 @@ class Site(val root: JFile) {
* defaults, the user-defined one will take precedence.
*/
val layouts: Map[String, String] = {
- def collectLayouts(dir: JFile): Map[String, String] =
- dir
- .listFiles
- .filter(f => f.getName.endsWith(".md") || f.getName.endsWith(".html"))
- .map { f =>
- (f.getName.substring(0, f.getName.lastIndexOf('.')), Source.fromFile(f).mkString)
- }
- .toMap
-
val userDefinedLayouts =
root
.listFiles.find(d => d.getName == "_layouts" && d.isDirectory)
- .map(collectLayouts)
+ .map(collectFiles(_, f => f.endsWith(".md") || f.endsWith(".html")))
.getOrElse(Map.empty)
val defaultLayouts: Map[String, String] = Map(
@@ -47,10 +33,28 @@ class Site(val root: JFile) {
defaultLayouts ++ userDefinedLayouts
}
- private def getResource(r: String): String =
- Option(getClass.getResourceAsStream(r)).map(scala.io.Source.fromInputStream)
- .map(_.mkString)
- .getOrElse(throw ResourceNotFoundException(r))
+ val includes: Map[String, String] = {
+ val userDefinedIncludes =
+ root
+ .listFiles.find(d => d.getName == "_includes" && d.isDirectory)
+ .map(collectFiles(_, f => f.endsWith(".md") || f.endsWith(".html")))
+ .getOrElse(Map.empty)
+
+ val defaultIncludes: Map[String, String] = Map(
+ "header.html" -> "/_includes/header.html"
+ ).mapValues(getResource)
+
+ defaultIncludes ++ userDefinedIncludes
+ }
+
+ private def collectFiles(dir: JFile, includes: String => Boolean): Map[String, String] =
+ dir
+ .listFiles
+ .filter(f => includes(f.getName))
+ .map { f =>
+ (f.getName.substring(0, f.getName.lastIndexOf('.')), Source.fromFile(f).mkString)
+ }
+ .toMap
def render(page: Page, params: Map[String, AnyRef])(implicit ctx: Context): String = {
page.yaml.get("layout").flatMap(layouts.get(_)) match {
@@ -58,7 +62,7 @@ class Site(val root: JFile) {
page.html
case Some(layout) =>
val newParams = Map("content" -> page.html) ++ params ++ Map("page" -> page.yaml)
- val expandedTemplate = new HtmlPage(layout, newParams)
+ val expandedTemplate = new HtmlPage(layout, newParams, includes)
render(expandedTemplate, params)
}
}