Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Asciidoctor attribute support #157

Merged
merged 1 commit into from
Sep 24, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@ libraryDependencies ++= Seq(
"org.foundweekends" %% "pamflet-library" % "0.8.0",
"org.yaml" % "snakeyaml" % "1.24",
"com.typesafe" % "config" % "1.3.4",
"org.asciidoctor" % "asciidoctorj" % "1.6.2",
"org.asciidoctor" % "asciidoctorj-diagram" % "1.5.16"
"org.asciidoctor" % "asciidoctorj" % "2.1.0",
"org.asciidoctor" % "asciidoctorj-diagram" % "1.5.18"
)

addSbtPlugin("com.lightbend.paradox" % "sbt-paradox" % "0.5.5")
Expand Down
13 changes: 13 additions & 0 deletions src/main/paradox/generators/asciidoctor.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,17 @@ Similarly, the output can be redirected to a subdirectory of `target/site` via t

@@ snip[siteSubdirName](/src/sbt-test/asciidoctor/can-use-asciidoctor/build.sbt) { #siteSubdirName }

## Attributes

Asciidoctor allows the rendering of documents to be inflenced by [attributes](https://asciidoctor.org/docs/user-manual/#attributes).

You can configure attributes by setting `asciidoctorAttributes`.

```sbt
asciidoctorAttributes := Map(
"skip-front-matter" -> "", // attribute without value can be set to empty string
"lang" -> "nl" // attribute with value
)
```

[Asciidoctor]: http://asciidoctor.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package com.typesafe.sbt.site.asciidoctor

import sbt.SettingKey

trait AsciidoctorKeys {
val asciidoctorAttributes = SettingKey[Map[String, String]](
"asciidoctor-attributes",
"Asciidoctor allows the rendering process to be influenced by so-called attributes. " +
"You can read more about attributes here: https://asciidoctor.org/docs/user-manual/#attributes")
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package com.typesafe.sbt.site.asciidoctor

import java.util
import com.typesafe.sbt.site.SitePlugin.autoImport.siteSubdirName

import com.typesafe.sbt.site.SitePlugin
import com.typesafe.sbt.site.SitePlugin.autoImport.siteSubdirName
import com.typesafe.sbt.site.util.SiteHelpers
import org.asciidoctor.Asciidoctor.Factory
import org.asciidoctor.{AsciiDocDirectoryWalker, Options, SafeMode}
import org.asciidoctor.Options
import org.asciidoctor.SafeMode
import org.asciidoctor.jruby.AsciiDocDirectoryWalker
import sbt.Keys._
import sbt._

Expand All @@ -14,19 +17,26 @@ object AsciidoctorPlugin extends AutoPlugin {
override def requires = SitePlugin
override def trigger = noTrigger

object autoImport {
object autoImport extends AsciidoctorKeys {
val Asciidoctor = config("asciidoctor")
}
import autoImport._

override lazy val globalSettings = Seq(
asciidoctorAttributes := Map()
)
override def projectSettings = asciidoctorSettings(Asciidoctor)

/** Creates settings necessary for running Asciidoctor in the given configuration. */
def asciidoctorSettings(config: Configuration): Seq[Setting[_]] =
inConfig(config)(
Seq(
includeFilter := AllPassFilter,
mappings := generate(sourceDirectory.value, target.value, includeFilter.value, version.value),
mappings := generate(
sourceDirectory.value,
target.value,
includeFilter.value,
version.value,
asciidoctorAttributes.value),
siteSubdirName := ""
)
) ++
Expand All @@ -36,10 +46,11 @@ object AsciidoctorPlugin extends AutoPlugin {

/** Run asciidoctor in new ClassLoader. */
private[sbt] def generate(
input: File,
output: File,
includeFilter: FileFilter,
version: String): Seq[(File, String)] = {
input: File,
output: File,
includeFilter: FileFilter,
version: String,
userSetAsciidoctorAttributes: Map[String, String]): Seq[(File, String)] = {
val oldContextClassLoader = Thread.currentThread().getContextClassLoader
Thread.currentThread().setContextClassLoader(this.getClass.getClassLoader)
val asciidoctor = Factory.create()
Expand All @@ -49,10 +60,17 @@ object AsciidoctorPlugin extends AutoPlugin {
options.setToDir(output.getAbsolutePath)
options.setDestinationDir(output.getAbsolutePath)
options.setSafe(SafeMode.UNSAFE)

//pass project.version to asciidoctor as attribute project-version
//need to do this explicitly through HashMap because otherwise JRuby complains
val attributes = new util.HashMap[String, AnyRef]()
attributes.put("project-version", version)

// Add user configured attributes into the mix
userSetAsciidoctorAttributes.foreach { case (key, value) =>
attributes.put(key, value)
}

options.setAttributes(attributes)
asciidoctor.convertDirectory(new AsciiDocDirectoryWalker(input.getAbsolutePath), options)
val inputImages = input / "images"
Expand Down
23 changes: 23 additions & 0 deletions src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name := "test"

//#enablePlugin
enablePlugins(AsciidoctorPlugin)
//#enablePlugin

//#siteSubdirName
// Puts output in `target/site/asciimd`
siteSubdirName in Asciidoctor := "asciimd"
asciidoctorAttributes in Asciidoctor := Map(
"skip-front-matter" -> "", // skip-front-matter is a flag that just has to be present.
"lang" -> "nl")
//#siteSubdirName

TaskKey[Unit]("checkContent") := {
val dest = (target in makeSite).value / (siteSubdirName in Asciidoctor).value
val index = dest / "index.html"
assert(index.exists, s"${index.getAbsolutePath} did not exist")
val content = IO.readLines(index)

assert(content.exists(_.contains("lang=\"nl\"")), s"Did not find expected content in:\n${content.mkString("\n")}")
assert(content.forall(!_.contains("title: \"Document Title\"")), s"Expected front-matter metadata to be removed from the output in:\n${content.mkString("\n")}")
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
addSbtPlugin("com.typesafe.sbt" % "sbt-site" % sys.props("project.version"))
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---
title: "Document Title"
---

== Hello, AsciiDoc!
Doc Writer <doc@example.com>
:project-version:

This tests should show that `attributes` are taken into account while rendering.

This is passed from the sbt build: {project-version}
2 changes: 2 additions & 0 deletions src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
> makeSite
> checkContent