diff --git a/build.sbt b/build.sbt index de96f3f..8455c34 100644 --- a/build.sbt +++ b/build.sbt @@ -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") diff --git a/src/main/paradox/generators/asciidoctor.md b/src/main/paradox/generators/asciidoctor.md index 11efd0b..3ccf4f4 100644 --- a/src/main/paradox/generators/asciidoctor.md +++ b/src/main/paradox/generators/asciidoctor.md @@ -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 diff --git a/src/main/scala/com/typesafe/sbt/site/asciidoctor/AsciidoctorKeys.scala b/src/main/scala/com/typesafe/sbt/site/asciidoctor/AsciidoctorKeys.scala new file mode 100644 index 0000000..fe60538 --- /dev/null +++ b/src/main/scala/com/typesafe/sbt/site/asciidoctor/AsciidoctorKeys.scala @@ -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") +} diff --git a/src/main/scala/com/typesafe/sbt/site/asciidoctor/AsciidoctorPlugin.scala b/src/main/scala/com/typesafe/sbt/site/asciidoctor/AsciidoctorPlugin.scala index 2cda173..5720340 100644 --- a/src/main/scala/com/typesafe/sbt/site/asciidoctor/AsciidoctorPlugin.scala +++ b/src/main/scala/com/typesafe/sbt/site/asciidoctor/AsciidoctorPlugin.scala @@ -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._ @@ -14,11 +17,13 @@ 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. */ @@ -26,7 +31,12 @@ object AsciidoctorPlugin extends AutoPlugin { 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 := "" ) ) ++ @@ -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() @@ -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" diff --git a/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/build.sbt b/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/build.sbt new file mode 100644 index 0000000..0108987 --- /dev/null +++ b/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/build.sbt @@ -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")}") +} diff --git a/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/project/plugins.sbt b/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/project/plugins.sbt new file mode 100644 index 0000000..6246999 --- /dev/null +++ b/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/project/plugins.sbt @@ -0,0 +1 @@ +addSbtPlugin("com.typesafe.sbt" % "sbt-site" % sys.props("project.version")) \ No newline at end of file diff --git a/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/src/asciidoctor/index.adoc b/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/src/asciidoctor/index.adoc new file mode 100644 index 0000000..ee7b1e5 --- /dev/null +++ b/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/src/asciidoctor/index.adoc @@ -0,0 +1,11 @@ +--- +title: "Document Title" +--- + +== Hello, AsciiDoc! +Doc Writer +:project-version: + +This tests should show that `attributes` are taken into account while rendering. + +This is passed from the sbt build: {project-version} diff --git a/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/test b/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/test new file mode 100644 index 0000000..9f9a269 --- /dev/null +++ b/src/sbt-test/asciidoctor/asciidoctor-skips-frontmatter/test @@ -0,0 +1,2 @@ +> makeSite +> checkContent