diff options
author | Felix Mulder <felix.mulder@gmail.com> | 2017-01-12 18:37:49 +0100 |
---|---|---|
committer | Felix Mulder <felix.mulder@gmail.com> | 2017-01-31 14:31:07 +0100 |
commit | be9c3799aad4c5712f85948e0e4c29321bdcb9a4 (patch) | |
tree | 4e9d78f0e50d6c1af0ff478d08ba47d0d53c6f1e /doc-tool/src/dotty/tools/dottydoc/staticsite | |
parent | 3e16bfd9c32d965820121abc0083e8db94537821 (diff) | |
download | dotty-be9c3799aad4c5712f85948e0e4c29321bdcb9a4.tar.gz dotty-be9c3799aad4c5712f85948e0e4c29321bdcb9a4.tar.bz2 dotty-be9c3799aad4c5712f85948e0e4c29321bdcb9a4.zip |
Add basic API generation from templates
Diffstat (limited to 'doc-tool/src/dotty/tools/dottydoc/staticsite')
-rw-r--r-- | doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala | 15 | ||||
-rw-r--r-- | doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala | 105 |
2 files changed, 87 insertions, 33 deletions
diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala index d0f49100c..ce57dc805 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala @@ -3,24 +3,31 @@ package dottydoc package staticsite import java.util.{ List => JList } +import model.{ Entity, NonEntity } case class DefaultParams( docs: JList[_], page: PageInfo, - site: SiteInfo + site: SiteInfo, + entity: Entity = NonEntity ) { - + import model.java._ import scala.collection.JavaConverters._ + def toMap: Map[String, AnyRef] = Map( "docs" -> docs, + "page" -> Map( "url" -> page.url, "path" -> page.path ), + "site" -> Map( "baseurl" -> site.baseurl, "posts" -> site.posts.map(_.toMap) - ).asJava + ).asJava, + + "entity" -> entity.asJava() ) def withPosts(posts: Array[BlogPost]): DefaultParams = @@ -28,6 +35,8 @@ case class DefaultParams( def withUrl(url: String): DefaultParams = copy(page = PageInfo(url)) + + def withEntity(e: model.Entity) = copy(entity = e) } case class PageInfo(url: String) { diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala index 7c9dc00fe..ecc076ba3 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala @@ -18,10 +18,17 @@ import com.vladsch.flexmark.util.options.{ DataHolder, MutableDataSet } import dotc.config.Printers.dottydoc import dotc.core.Contexts.Context +import model.Package import scala.io.Source import scala.collection.mutable.ArrayBuffer -case class Site(val root: JFile, val docs: JList[_]) extends ResourceFinder { +case class Site(val root: JFile, val documentation: Map[String, Package]) extends ResourceFinder { + /** Documentation serialized to java maps */ + private val docs: JList[_] = { + import model.java._ + documentation.toJavaList + } + /** All files that are considered static in this context, this can be * anything from CSS, JS to images and other files. * @@ -87,6 +94,7 @@ case class Site(val root: JFile, val docs: JList[_]) extends ResourceFinder { // Copy statics included in resources Map( + "css/api-page.css" -> "/css/api-page.css", "css/dottydoc.css" -> "/css/dottydoc.css", "css/color-brewer.css" -> "/css/color-brewer.css", "js/highlight.pack.js" -> "/js/highlight.pack.js" @@ -113,41 +121,73 @@ case class Site(val root: JFile, val docs: JList[_]) extends ResourceFinder { DefaultParams(docs, PageInfo(pathFromRoot), SiteInfo(baseUrl, Array())) } - /** Generate HTML files from markdown and .html sources */ - def generateHtmlFiles(outDir: JFile = new JFile(root.getAbsolutePath + "/_site"))(implicit ctx: Context): this.type = { + private def createOutput(outDir: JFile)(op: => Unit): this.type = { if (!outDir.isDirectory) outDir.mkdirs() if (!outDir.isDirectory) /*dottydoc.*/println(s"couldn't create output folder: $outDir") - else compilableFiles.foreach { asset => - val pathFromRoot = stripRoot(asset) - val fileContents = Source.fromFile(asset).mkString - val params = defaultParams(asset).withPosts(blogInfo).toMap - val page = - if (asset.getName.endsWith(".md")) new MarkdownPage(fileContents, params, includes) - else new HtmlPage(fileContents, params, includes) - - val renderedPage = render(page) - val source = new ByteArrayInputStream(renderedPage.getBytes(StandardCharsets.UTF_8)) - val target = pathFromRoot.splitAt(pathFromRoot.lastIndexOf('.'))._1 + ".html" - val htmlTarget = mkdirs(fs.getPath(outDir.getAbsolutePath, target)) - Files.copy(source, htmlTarget, REPLACE_EXISTING) - } + else op this } - def generateBlog(outDir: JFile = new JFile(root.getAbsolutePath + "/_site"))(implicit ctx: Context): Unit = { - blogposts.foreach { file => - val BlogPost.extract(year, month, day, name, ext) = file.getName - val fileContents = Source.fromFile(file).mkString - val params = defaultParams(file, 2).withPosts(blogInfo).toMap - val page = - if (ext == "md") new MarkdownPage(fileContents, params, includes) - else new HtmlPage(fileContents, params, includes) + /** Generate HTML for the API documentation */ + def generateApiDocs(outDir: JFile = new JFile(root.getAbsolutePath + "/_site"))(implicit ctx: Context): this.type = + createOutput(outDir) { + def genDoc(e: model.Entity) = { + // Suffix is index.html for packages and therefore the additional depth + // is increased by 1 + val (suffix, offset) = + if (e.kind == "package") ("/index.html", -1) + else (".html", 0) + + val target = mkdirs(fs.getPath(outDir.getAbsolutePath + "/api/" + e.path.mkString("/") + suffix)) + val params = defaultParams(target.toFile, -1).withPosts(blogInfo).withEntity(e) + val page = new HtmlPage(layouts("api-page"), params.toMap, includes) + + val rendered = render(page) + val source = new ByteArrayInputStream(rendered.getBytes(StandardCharsets.UTF_8)) - val source = new ByteArrayInputStream(render(page).getBytes(StandardCharsets.UTF_8)) - val target = mkdirs(fs.getPath(outDir.getAbsolutePath, "blog", year, month, day, name + ".html")) - Files.copy(source, target, REPLACE_EXISTING) + Files.copy(source, target, REPLACE_EXISTING) + } + + documentation.values.foreach { pkg => + genDoc(pkg) + pkg.members.filterNot(_.kind == "package").map(genDoc) + } + } + + /** Generate HTML files from markdown and .html sources */ + def generateHtmlFiles(outDir: JFile = new JFile(root.getAbsolutePath + "/_site"))(implicit ctx: Context): this.type = + createOutput(outDir) { + compilableFiles.foreach { asset => + val pathFromRoot = stripRoot(asset) + val fileContents = Source.fromFile(asset).mkString + val params = defaultParams(asset).withPosts(blogInfo).toMap + val page = + if (asset.getName.endsWith(".md")) new MarkdownPage(fileContents, params, includes) + else new HtmlPage(fileContents, params, includes) + + val renderedPage = render(page) + val source = new ByteArrayInputStream(renderedPage.getBytes(StandardCharsets.UTF_8)) + val target = pathFromRoot.splitAt(pathFromRoot.lastIndexOf('.'))._1 + ".html" + val htmlTarget = mkdirs(fs.getPath(outDir.getAbsolutePath, target)) + Files.copy(source, htmlTarget, REPLACE_EXISTING) + } + } + + def generateBlog(outDir: JFile = new JFile(root.getAbsolutePath + "/_site"))(implicit ctx: Context): this.type = + createOutput(outDir) { + blogposts.foreach { file => + val BlogPost.extract(year, month, day, name, ext) = file.getName + val fileContents = Source.fromFile(file).mkString + val params = defaultParams(file, 2).withPosts(blogInfo).toMap + val page = + if (ext == "md") new MarkdownPage(fileContents, params, includes) + else new HtmlPage(fileContents, params, includes) + + val source = new ByteArrayInputStream(render(page).getBytes(StandardCharsets.UTF_8)) + val target = mkdirs(fs.getPath(outDir.getAbsolutePath, "blog", year, month, day, name + ".html")) + Files.copy(source, target, REPLACE_EXISTING) + } } - } private def mkdirs(path: Path): path.type = { val parent = path.getParent.toFile @@ -181,7 +221,11 @@ case class Site(val root: JFile, val docs: JList[_]) extends ResourceFinder { def splitFiles(f: JFile, assets: ArrayBuffer[JFile], comp: ArrayBuffer[JFile]): Unit = { val name = f.getName if (f.isDirectory) { - if (!f.getName.startsWith("_")) f.listFiles.foreach(splitFiles(_, assets, comp)) + val name = f.getName + if (!name.startsWith("_") && name != "api") f.listFiles.foreach(splitFiles(_, assets, comp)) + if (f.getName == "api") dottydoc.println { + "the specified `/api` directory will not be used since it is needed for the api documentation" + } } else if (name.endsWith(".md") || name.endsWith(".html")) comp.append(f) else assets.append(f) @@ -230,6 +274,7 @@ case class Site(val root: JFile, val docs: JList[_]) extends ResourceFinder { "main" -> "/_layouts/main.html", "doc" -> "/_layouts/doc.html", "doc-page" -> "/_layouts/doc-page.html", + "api-page" -> "/_layouts/api-page.html", "blog" -> "/_layouts/blog.html", "index" -> "/_layouts/index.html" ).mapValues(getResource) |