Skip to content

Commit

Permalink
Merge pull request #9 from dwijnand/leading-breadcrumbs
Browse files Browse the repository at this point in the history
Introducing: Leady McBreadcrumbs
  • Loading branch information
pvlugter authored Aug 18, 2016
2 parents a37b944 + c1827af commit 9def7ae
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
* Process all mappings to build the site.
*/
def process(mappings: Seq[(File, String)],
leadingBreadcrumbs: List[(String, String)],
outputDirectory: File,
sourceSuffix: String,
targetSuffix: String,
Expand All @@ -50,7 +51,7 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
val writerContext = Writer.Context(loc, paths, sourceSuffix, targetSuffix, properties)
val pageToc = new TableOfContents(pages = true, headers = false, ordered = false, maxDepth = navigationDepth)
val headerToc = new TableOfContents(pages = false, headers = true, ordered = false, maxDepth = navigationDepth)
val pageContext = PageContents(loc, writer, writerContext, pageToc, headerToc)
val pageContext = PageContents(leadingBreadcrumbs, loc, writer, writerContext, pageToc, headerToc)
val outputFile = new File(outputDirectory, page.path)
outputFile.getParentFile.mkdirs
template.write(pageContext, outputFile, errorListener)
Expand All @@ -63,7 +64,7 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
/**
* Default template contents for a markdown page at a particular location.
*/
case class PageContents(loc: Location[Page], writer: Writer, context: Writer.Context, pageToc: TableOfContents, headerToc: TableOfContents) extends PageTemplate.Contents {
case class PageContents(leadingBreadcrumbs: List[(String, String)], loc: Location[Page], writer: Writer, context: Writer.Context, pageToc: TableOfContents, headerToc: TableOfContents) extends PageTemplate.Contents {
import scala.collection.JavaConverters._

private val page = loc.tree.label
Expand All @@ -75,7 +76,7 @@ class ParadoxProcessor(reader: Reader = new Reader, writer: Writer = new Writer)
lazy val getHome = link(Some(loc.root))
lazy val getPrev = link(loc.prev)
lazy val getNext = link(loc.next)
lazy val getBreadcrumbs = writer.writeFragment(Breadcrumbs.markdown(loc.path), context)
lazy val getBreadcrumbs = writer.writeFragment(Breadcrumbs.markdown(leadingBreadcrumbs, loc.path), context)
lazy val getNavigation = writer.writeFragment(pageToc.root(loc), context)
lazy val hasSubheaders = page.headers.nonEmpty
lazy val getToc = writer.writeFragment(headerToc.headers(loc), context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

package com.lightbend.paradox.markdown

import scala.collection.JavaConverters._

import com.lightbend.paradox.tree.Tree.Location
import org.pegdown.ast._

Expand All @@ -28,32 +30,35 @@ object Breadcrumbs {
* Convert a location path into a markdown list.
* Note: locations are ordered from current location up to root.
*/
def markdown(locations: List[Location[Page]]): BulletListNode = locations match {
case current :: parents => crumbs(current.tree.label.base, current.tree.label.path, locations.reverse)
case _ => list(Nil)
}
def markdown(leadingBreadcrumbs: List[(String, String)], locations: List[Location[Page]]): BulletListNode =
locations match {
case current :: parents => crumbs(current.tree.label.base, current.tree.label.path, leadingBreadcrumbs, locations.reverse)
case _ => list(Nil)
}

private def crumbs(base: String, active: String, locations: List[Location[Page]]): BulletListNode = {
list(locations map item(base, active))
private def crumbs(base: String, active: String, leadingBreadcrumbs: List[(String, String)], locations: List[Location[Page]]): BulletListNode = {
val lead = leadingBreadcrumbs map { case (label, url) => item(url, "", labelNode(label), active) }
list(lead ++ (locations map pageItem(base, active)))
}

private def list(items: List[ListItemNode]): BulletListNode = {
val parent = new SuperNode
items.foreach(parent.getChildren.add)
new BulletListNode(parent)
new BulletListNode(new SuperNode(items.map(n => n: Node).asJava))
}

private def item(base: String, active: String)(location: Location[Page]): ListItemNode = {
private def pageItem(base: String, active: String)(location: Location[Page]): ListItemNode = {
val page = location.tree.label
val label = link(base, page.path, page.label, active)
val parent = new SuperNode
parent.getChildren.add(label)
new ListItemNode(parent)
item(base + page.path, page.path, page.label, active)
}

private def link(base: String, path: String, label: Node, active: String): Node = {
private def item(url: String, path: String, label: Node, active: String): ListItemNode = {
new ListItemNode(new SuperNode(link(url, path, label, active)))
}

private def link(url: String, path: String, label: Node, active: String): Node = {
if (path == active) label // no link for current location
else new ExpLinkNode("", base + path, label)
else new ExpLinkNode("", url, label)
}

private def labelNode(label: String): Node = new SuperNode(new TextNode(label))

}
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ trait ParadoxKeys {
val paradox = taskKey[File]("Build the paradox site.")
val paradoxMarkdownToHtml = taskKey[Seq[(File, String)]]("Convert markdown files to HTML.")
val paradoxNavigationDepth = settingKey[Int]("Determines depth of TOC for page navigation.")
val paradoxLeadingBreadcrumbs = settingKey[List[(String, String)]]("Any leading breadcrumbs (label -> url)")
val paradoxOrganization = settingKey[String]("Paradox dependency organization (for theme dependencies).")
val paradoxProcessor = taskKey[ParadoxProcessor]("ParadoxProcessor to use when generating the site.")
val paradoxProperties = taskKey[Map[String, String]]("Property map passed to paradox.")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ object ParadoxPlugin extends AutoPlugin {
paradoxNavigationDepth := 2,
paradoxProperties := Map.empty,
paradoxTheme := None,
paradoxLeadingBreadcrumbs := Nil,
libraryDependencies ++= paradoxTheme.value.toSeq
)

Expand Down Expand Up @@ -108,6 +109,7 @@ object ParadoxPlugin extends AutoPlugin {
IO.delete((target in paradoxMarkdownToHtml).value)
paradoxProcessor.value.process(
(mappings in paradoxMarkdownToHtml).value,
paradoxLeadingBreadcrumbs.value,
(target in paradoxMarkdownToHtml).value,
paradoxSourceSuffix.value,
paradoxTargetSuffix.value,
Expand Down
3 changes: 2 additions & 1 deletion plugin/src/sbt-test/paradox/site/build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,6 @@ lazy val docs = project
.in(file("."))
.enablePlugins(ParadoxPlugin)
.settings(
name := "Paradox Test"
name := "Paradox Test",
paradoxLeadingBreadcrumbs := List("Alphabet" -> "https://abc.xyz/", "Google" -> "https://www.google.com")
)
2 changes: 2 additions & 0 deletions plugin/src/sbt-test/paradox/site/expected/a.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<a href="a.html" class="active">A</a>
0.1-SNAPSHOT
<ul>
<li><a href="https://abc.xyz/">Alphabet</a></li>
<li><a href="https://www.google.com">Google</a></li>
<li>A</li>
</ul>
<ul>
Expand Down
2 changes: 2 additions & 0 deletions plugin/src/sbt-test/paradox/site/expected/a/a.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<a href="../a.html">A</a>
0.1-SNAPSHOT
<ul>
<li><a href="https://abc.xyz/">Alphabet</a></li>
<li><a href="https://www.google.com">Google</a></li>
<li><a href="../a.html">A</a></li>
<li>AA</li>
</ul>
Expand Down
2 changes: 2 additions & 0 deletions plugin/src/sbt-test/paradox/site/expected/a/b.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<a href="../a.html">A</a>
0.1-SNAPSHOT
<ul>
<li><a href="https://abc.xyz/">Alphabet</a></li>
<li><a href="https://www.google.com">Google</a></li>
<li><a href="../a.html">A</a></li>
<li><a href="../a/a.html">AA</a></li>
<li>AB</li>
Expand Down
2 changes: 2 additions & 0 deletions plugin/src/sbt-test/paradox/site/expected/a/c.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<a href="../a.html">A</a>
0.1-SNAPSHOT
<ul>
<li><a href="https://abc.xyz/">Alphabet</a></li>
<li><a href="https://www.google.com">Google</a></li>
<li><a href="../a.html">A</a></li>
<li><a href="../a/a.html">AA</a></li>
<li>AC</li>
Expand Down
2 changes: 2 additions & 0 deletions plugin/src/sbt-test/paradox/site/expected/b/a.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<a href="../a.html">A</a>
0.1-SNAPSHOT
<ul>
<li><a href="https://abc.xyz/">Alphabet</a></li>
<li><a href="https://www.google.com">Google</a></li>
<li><a href="../a.html">A</a></li>
<li>BA</li>
</ul>
Expand Down
2 changes: 2 additions & 0 deletions plugin/src/sbt-test/paradox/site/expected/b/a/a.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<a href="../../a.html">A</a>
0.1-SNAPSHOT
<ul>
<li><a href="https://abc.xyz/">Alphabet</a></li>
<li><a href="https://www.google.com">Google</a></li>
<li><a href="../../a.html">A</a></li>
<li><a href="../../b/a.html">BA</a></li>
<li>BAA</li>
Expand Down
2 changes: 2 additions & 0 deletions plugin/src/sbt-test/paradox/site/expected/b/b.html
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<a href="../a.html">A</a>
0.1-SNAPSHOT
<ul>
<li><a href="https://abc.xyz/">Alphabet</a></li>
<li><a href="https://www.google.com">Google</a></li>
<li><a href="../a.html">A</a></li>
<li><a href="../b/a.html">BA</a></li>
<li>BB</li>
Expand Down

0 comments on commit 9def7ae

Please sign in to comment.