diff --git a/core/src/main/scala/com/lightbend/paradox/markdown/Writer.scala b/core/src/main/scala/com/lightbend/paradox/markdown/Writer.scala index aad4e13b..1d1b3f84 100644 --- a/core/src/main/scala/com/lightbend/paradox/markdown/Writer.scala +++ b/core/src/main/scala/com/lightbend/paradox/markdown/Writer.scala @@ -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. @@ -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 { diff --git a/plugin/src/main/scala/com/lightbend/paradox/sbt/ParadoxKeys.scala b/plugin/src/main/scala/com/lightbend/paradox/sbt/ParadoxKeys.scala index c957efad..8cbb8d21 100644 --- a/plugin/src/main/scala/com/lightbend/paradox/sbt/ParadoxKeys.scala +++ b/plugin/src/main/scala/com/lightbend/paradox/sbt/ParadoxKeys.scala @@ -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 { @@ -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\"].") diff --git a/plugin/src/main/scala/com/lightbend/paradox/sbt/ParadoxPlugin.scala b/plugin/src/main/scala/com/lightbend/paradox/sbt/ParadoxPlugin.scala index 543c9f3d..95d00d12 100644 --- a/plugin/src/main/scala/com/lightbend/paradox/sbt/ParadoxPlugin.scala +++ b/plugin/src/main/scala/com/lightbend/paradox/sbt/ParadoxPlugin.scala @@ -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 @@ -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", @@ -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) diff --git a/plugin/src/sbt-test/paradox/custom-directive/build.sbt b/plugin/src/sbt-test/paradox/custom-directive/build.sbt new file mode 100644 index 00000000..fd0e2c71 --- /dev/null +++ b/plugin/src/sbt-test/paradox/custom-directive/build.sbt @@ -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" + ) diff --git a/plugin/src/sbt-test/paradox/custom-directive/expected/test.html b/plugin/src/sbt-test/paradox/custom-directive/expected/test.html new file mode 100644 index 00000000..ecac7e80 --- /dev/null +++ b/plugin/src/sbt-test/paradox/custom-directive/expected/test.html @@ -0,0 +1 @@ +directive diff --git a/plugin/src/sbt-test/paradox/custom-directive/project/CustomDirective.scala b/plugin/src/sbt-test/paradox/custom-directive/project/CustomDirective.scala new file mode 100644 index 00000000..499aed4d --- /dev/null +++ b/plugin/src/sbt-test/paradox/custom-directive/project/CustomDirective.scala @@ -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", "")) + } +} diff --git a/plugin/src/sbt-test/paradox/custom-directive/project/plugins.sbt b/plugin/src/sbt-test/paradox/custom-directive/project/plugins.sbt new file mode 100644 index 00000000..be8b011a --- /dev/null +++ b/plugin/src/sbt-test/paradox/custom-directive/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % sys.props("project.version")) diff --git a/plugin/src/sbt-test/paradox/custom-directive/src/main/paradox/_template/page.st b/plugin/src/sbt-test/paradox/custom-directive/src/main/paradox/_template/page.st new file mode 100644 index 00000000..fa871c5b --- /dev/null +++ b/plugin/src/sbt-test/paradox/custom-directive/src/main/paradox/_template/page.st @@ -0,0 +1 @@ +$page.content$ diff --git a/plugin/src/sbt-test/paradox/custom-directive/src/main/paradox/test.md b/plugin/src/sbt-test/paradox/custom-directive/src/main/paradox/test.md new file mode 100644 index 00000000..1788d6ff --- /dev/null +++ b/plugin/src/sbt-test/paradox/custom-directive/src/main/paradox/test.md @@ -0,0 +1 @@ +@@custom diff --git a/plugin/src/sbt-test/paradox/custom-directive/test b/plugin/src/sbt-test/paradox/custom-directive/test new file mode 100644 index 00000000..99950786 --- /dev/null +++ b/plugin/src/sbt-test/paradox/custom-directive/test @@ -0,0 +1,3 @@ +> paradox + +$ must-mirror target/paradox/site/main/test.html expected/test.html