Skip to content

Commit

Permalink
Merge pull request #115 from pvlugter/directives
Browse files Browse the repository at this point in the history
Allow custom directives to be added to sbt setting
  • Loading branch information
eed3si9n authored May 15, 2017
2 parents 45cf0c3 + 2804888 commit a1a8fa1
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 23 deletions.
44 changes: 22 additions & 22 deletions core/src/main/scala/com/lightbend/paradox/markdown/Writer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ class Writer(serializer: Writer.Context => ToHtmlSerializer) {
def this(
linkRenderer: Writer.Context => LinkRenderer = Writer.defaultLinks,
verbatimSerializers: Map[String, VerbatimSerializer] = Writer.defaultVerbatims,
serializerPlugins: Writer.Context => Seq[ToHtmlSerializerPlugin] = Writer.defaultPlugins) =
serializerPlugins: Seq[Writer.Context => ToHtmlSerializerPlugin] = Writer.defaultPlugins(Writer.defaultDirectives)) =
this((context: Writer.Context) => new ToHtmlSerializer(
linkRenderer(context),
verbatimSerializers.asJava,
serializerPlugins(context).asJava))
serializerPlugins.map(p => p(context)).asJava))

/**
* Write main content.
Expand Down Expand Up @@ -108,28 +108,28 @@ object Writer {
Map(VerbatimSerializer.DEFAULT -> PrettifyVerbatimSerializer)
}

def defaultPlugins(context: Context): Seq[ToHtmlSerializerPlugin] = Seq(
new ClassyLinkSerializer,
new AnchorLinkSerializer,
new DirectiveSerializer(defaultDirectives(context))
def defaultPlugins(directives: Seq[Context => Directive]): Seq[Context => ToHtmlSerializerPlugin] = Seq(
context => new ClassyLinkSerializer,
context => new AnchorLinkSerializer,
context => new DirectiveSerializer(directives.map(d => d(context)))
)

def defaultDirectives(context: Context): Seq[Directive] = Seq(
RefDirective(context.location.tree.label, context.paths, context.pageMappings),
ExtRefDirective(context.location.tree.label, context.properties),
ScaladocDirective(context.location.tree.label, context.properties),
JavadocDirective(context.location.tree.label, context.properties),
GitHubDirective(context.location.tree.label, context.properties),
SnipDirective(context.location.tree.label, context.properties),
FiddleDirective(context.location.tree.label),
TocDirective(context.location),
VarDirective(context.properties),
VarsDirective(context.properties),
CalloutDirective("note", "Note"),
CalloutDirective("warning", "Warning"),
WrapDirective("div"),
InlineWrapDirective("span"),
InlineGroupDirective(context.groups.values.flatten.map(_.toLowerCase).toSeq)
def defaultDirectives: Seq[Context => Directive] = Seq(
context => RefDirective(context.location.tree.label, context.paths, context.pageMappings),
context => ExtRefDirective(context.location.tree.label, context.properties),
context => ScaladocDirective(context.location.tree.label, context.properties),
context => JavadocDirective(context.location.tree.label, context.properties),
context => GitHubDirective(context.location.tree.label, context.properties),
context => SnipDirective(context.location.tree.label, context.properties),
context => FiddleDirective(context.location.tree.label),
context => TocDirective(context.location),
context => VarDirective(context.properties),
context => VarsDirective(context.properties),
context => CalloutDirective("note", "Note"),
context => CalloutDirective("warning", "Warning"),
context => WrapDirective("div"),
context => InlineWrapDirective("span"),
context => InlineGroupDirective(context.groups.values.flatten.map(_.toLowerCase).toSeq)
)

class DefaultLinkRenderer(context: Context) extends LinkRenderer {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package com.lightbend.paradox.sbt

import sbt._
import com.lightbend.paradox.ParadoxProcessor
import com.lightbend.paradox.markdown.{ Directive, Writer }
import com.lightbend.paradox.template.PageTemplate

trait ParadoxKeys {
Expand All @@ -28,6 +29,7 @@ trait ParadoxKeys {
val paradoxNavigationIncludeHeaders = settingKey[Boolean]("Whether to include headers in the navigation.")
val paradoxLeadingBreadcrumbs = settingKey[List[(String, String)]]("Any leading breadcrumbs (label -> url)")
val paradoxOrganization = settingKey[String]("Paradox dependency organization (for theme dependencies).")
val paradoxDirectives = taskKey[Seq[Writer.Context => Directive]]("Enabled paradox directives.")
val paradoxProcessor = taskKey[ParadoxProcessor]("ParadoxProcessor to use when generating the site.")
val paradoxProperties = taskKey[Map[String, String]]("Property map passed to paradox.")
val paradoxSourceSuffix = settingKey[String]("Source file suffix for markdown files [default = \".md\"].")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import sbt._
import sbt.Keys._

import com.lightbend.paradox.ParadoxProcessor
import com.lightbend.paradox.markdown.Writer
import com.lightbend.paradox.template.PageTemplate
import com.typesafe.sbt.web.Import.{ Assets, WebKeys }
import com.typesafe.sbt.web.SbtWeb
Expand All @@ -45,6 +46,7 @@ object ParadoxPlugin extends AutoPlugin {
paradoxNavigationDepth := 2,
paradoxNavigationExpandDepth := None,
paradoxNavigationIncludeHeaders := false,
paradoxDirectives := Writer.defaultDirectives,
paradoxProperties := Map.empty,
paradoxTheme := Some(builtinParadoxTheme("generic")),
paradoxDefaultTemplateName := "page",
Expand All @@ -54,7 +56,7 @@ object ParadoxPlugin extends AutoPlugin {
)

def paradoxSettings(config: Configuration): Seq[Setting[_]] = paradoxGlobalSettings ++ inConfig(config)(Seq(
paradoxProcessor := new ParadoxProcessor,
paradoxProcessor := new ParadoxProcessor(writer = new Writer(serializerPlugins = Writer.defaultPlugins(paradoxDirectives.value))),

sourceDirectory := {
if (config.name != Compile.name)
Expand Down
9 changes: 9 additions & 0 deletions plugin/src/sbt-test/paradox/custom-directive/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
lazy val docs = project
.in(file("."))
.enablePlugins(ParadoxPlugin)
.settings(
name := "Paradox Directives Test",
paradoxTheme := None,
paradoxDirectives += CustomDirective,
paradoxProperties += "custom.content" -> "directive"
)
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
directive
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import com.lightbend.paradox.markdown.{ Directive, LeafBlockDirective, Writer }
import org.pegdown.Printer
import org.pegdown.ast.{ DirectiveNode, Visitor }

object CustomDirective extends (Writer.Context => CustomDirective) {
def apply(context: Writer.Context): CustomDirective = CustomDirective(context.properties)
}

case class CustomDirective(properties: Map[String, String]) extends LeafBlockDirective("custom") {
def render(node: DirectiveNode, visitor: Visitor, printer: Printer): Unit = {
printer.println.print(properties.getOrElse("custom.content", ""))
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
$page.content$
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@@custom
3 changes: 3 additions & 0 deletions plugin/src/sbt-test/paradox/custom-directive/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
> paradox

$ must-mirror target/paradox/site/main/test.html expected/test.html

0 comments on commit a1a8fa1

Please sign in to comment.