From 7b2c84b51850a4e64107a99f9780d0ee578a1c4a Mon Sep 17 00:00:00 2001 From: Felix Mulder Date: Mon, 23 Jan 2017 16:04:20 +0100 Subject: Generalize table of contents for dottydoc --- doc-tool/resources/_includes/toc.html | 52 ++-------------------- doc-tool/resources/css/dottydoc.css | 9 ++-- .../tools/dottydoc/staticsite/DefaultParams.scala | 41 ++++++++++++++++- .../tools/dottydoc/staticsite/LiquidTemplate.scala | 1 + .../src/dotty/tools/dottydoc/staticsite/Site.scala | 12 ++++- .../src/dotty/tools/dottydoc/staticsite/Yaml.scala | 23 ++++++++++ .../src/dotty/tools/dottydoc/staticsite/tags.scala | 37 +++++++++++++++ 7 files changed, 121 insertions(+), 54 deletions(-) create mode 100644 doc-tool/src/dotty/tools/dottydoc/staticsite/Yaml.scala (limited to 'doc-tool') diff --git a/doc-tool/resources/_includes/toc.html b/doc-tool/resources/_includes/toc.html index 0ff3f9586..15952ef78 100644 --- a/doc-tool/resources/_includes/toc.html +++ b/doc-tool/resources/_includes/toc.html @@ -1,55 +1,11 @@ -{% assign parent = page.path | first %} - - diff --git a/doc-tool/resources/css/dottydoc.css b/doc-tool/resources/css/dottydoc.css index bb0d6506b..aa197de07 100644 --- a/doc-tool/resources/css/dottydoc.css +++ b/doc-tool/resources/css/dottydoc.css @@ -85,7 +85,7 @@ ul.toc > li > a#home-button svg g#logo-background { fill: rgba(202, 68, 94, 0.45); } -ul.toc > li > a.toggle-children { +ul.toc > li > a { width: 100%; user-select: none; } @@ -182,13 +182,16 @@ ul.index-entities > li.index-title > span { padding: 0 24px; } +ul.index-entities > li.index-title:hover { + background-color: transparent; +} + li.index-entity > a:focus { text-decoration: none; } ul.index-entities > li:hover, -ul.toc > li.toc-title:hover, -ul.toc > li > a.toggle-children:hover { +ul.toc > li > a:hover { background-color: rgba(0, 0, 0, 0.2); } diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala index d64ebcd90..5bed79869 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/DefaultParams.scala @@ -2,17 +2,19 @@ package dotty.tools package dottydoc package staticsite -import java.util.{ List => JList } import model.{ Entity, NonEntity } +import java.util.{ HashMap, List => JList, Map => JMap } +import scala.collection.JavaConverters._ + case class DefaultParams( docs: JList[_], page: PageInfo, site: SiteInfo, + sidebar: Sidebar, entity: Entity = NonEntity ) { import model.java._ - import scala.collection.JavaConverters._ def toMap: Map[String, AnyRef] = Map( "docs" -> docs, @@ -29,6 +31,8 @@ case class DefaultParams( "project" -> site.projectTitle ).asJava, + "sidebar" -> sidebar.titles.asJava, + "entity" -> entity.asJava() ) @@ -48,3 +52,36 @@ case class PageInfo(url: String, date: String = "") { } case class SiteInfo(baseurl: String, projectTitle: String, posts: Array[BlogPost]) + +case class Sidebar(titles: List[Title]) + +object Sidebar { + def apply(map: HashMap[String, AnyRef]): Option[Sidebar] = Option(map.get("sidebar")).map { + case list: JList[JMap[String, AnyRef]] @unchecked if !list.isEmpty => + new Sidebar(list.asScala.map(Title.apply).flatMap(x => x).toList) + case _ => Sidebar.empty + } + + def empty: Sidebar = Sidebar(Nil) +} + +case class Title(title: String, url: Option[String], subsection: List[Title]) + +object Title { + def apply(map: JMap[String, AnyRef]): Option[Title] = { + val title = Option(map.get("title")).collect { + case s: String => s + } + val url = Option(map.get("url")).collect { + case s: String => s + } + val subsection = Option(map.get("subsection")).collect { + case xs: JList[JMap[String, AnyRef]] @unchecked => + xs.asScala.map(Title.apply).toList.flatMap(x => x) + }.getOrElse(Nil) + + title.map { + case title: String => Title(title, url, subsection) + } + } +} diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/LiquidTemplate.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/LiquidTemplate.scala index 87209b47a..ef7a59f1c 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/LiquidTemplate.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/LiquidTemplate.scala @@ -28,5 +28,6 @@ case class LiquidTemplate(contents: String) extends ResourceFinder { Template.parse(contents, JEKYLL) .`with`(ResourceInclude(params, includes)) .`with`(RenderReference(params)) + .`with`(RenderTitle(params)) .render(toJavaMap(params)) } diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala index d522c597c..3fb5dad24 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/Site.scala @@ -68,6 +68,16 @@ case class Site(val root: JFile, val projectTitle: String, val documentation: Ma _blogposts } + + val sidebar: Sidebar = + root + .listFiles + .find(_.getName == "sidebar.yml") + .map("---\n" + Source.fromFile(_).mkString + "\n---") + .map(Yaml.apply) + .flatMap(Sidebar.apply) + .getOrElse(Sidebar.empty) + protected lazy val blogInfo: Array[BlogPost] = blogposts .map { file => @@ -122,7 +132,7 @@ case class Site(val root: JFile, val projectTitle: String, val documentation: Ma "../" * (assetLen - rootLen - 1 + additionalDepth) + "." } - DefaultParams(docs, PageInfo(pathFromRoot), SiteInfo(baseUrl, projectTitle, Array())) + DefaultParams(docs, PageInfo(pathFromRoot), SiteInfo(baseUrl, projectTitle, Array()), sidebar) } private def createOutput(outDir: JFile)(op: => Unit): this.type = { diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/Yaml.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/Yaml.scala new file mode 100644 index 000000000..07bc27562 --- /dev/null +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/Yaml.scala @@ -0,0 +1,23 @@ +package dotty.tools +package dottydoc +package staticsite + +import com.fasterxml.jackson.dataformat.yaml.YAMLFactory +import com.fasterxml.jackson.databind.ObjectMapper +import com.fasterxml.jackson.core.`type`.TypeReference + +object Yaml { + import scala.collection.JavaConverters._ + import java.util.HashMap + import java.io.ByteArrayInputStream + + def apply(input: String): HashMap[String, AnyRef] = { + val is = new ByteArrayInputStream(input.getBytes("UTF-8")) + val mapper = new ObjectMapper(new YAMLFactory()) + + val typeRef: TypeReference[HashMap[String, AnyRef]] = + new TypeReference[HashMap[String, AnyRef]] {} + + mapper.readValue(is, typeRef) + } +} diff --git a/doc-tool/src/dotty/tools/dottydoc/staticsite/tags.scala b/doc-tool/src/dotty/tools/dottydoc/staticsite/tags.scala index 868e28bce..fe6a05bbc 100644 --- a/doc-tool/src/dotty/tools/dottydoc/staticsite/tags.scala +++ b/doc-tool/src/dotty/tools/dottydoc/staticsite/tags.scala @@ -131,4 +131,41 @@ object tags { } } } + + /** Can be used to render the `sidebar.yml` entries, represented here as + * `Title`. + * + * ```html + * {% renderTitle title, parent %} + * ``` + * + * The rendering currently works on depths up to 2. This means that each + * title can have a subsection with its own titles. + */ + case class RenderTitle(params: Map[String, AnyRef]) extends Tag("renderTitle") with ParamConverter { + private def renderTitle(t: Title, parent: String): String = { + if (!t.url.isDefined && t.subsection.nonEmpty) { + val onclickFunction = + s"""(function(){var child=document.getElementById("${t.title}");child.classList.toggle("show");child.classList.toggle("hide");})();""" + s"""|${t.title} + |""".stripMargin + } + else if (t.url.isDefined) { + val url = t.url.get + s"""${t.title}""" + } + else /*if (t.subsection.nonEmpty)*/ { + /*dottydoc.*/println(s"url was defined for subsection with title: ${t.title}, remove url to get toggleable entries") + t.title + } + } + + override def render(ctx: TemplateContext, nodes: LNode*): AnyRef = + (nodes(0).render(ctx), nodes(1).render(ctx)) match { + case (t: Title, parent: String) => renderTitle(t, parent) + case _ => null + } + } } -- cgit v1.2.3