From f0306328d337ae4aca45e4fe546f37d0b07d25b6 Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Sat, 20 Feb 2016 19:42:08 +0100 Subject: [PATCH] fixes #206 - adds the option to set attributes through command line and updates README --- README.adoc | 30 +++++++++++++++++++ .../asciidoctor/maven/AsciidoctorMojo.java | 11 ++++++- .../maven/test/AsciidoctorMojoTest.groovy | 28 +++++++++++++++++ 3 files changed, 68 insertions(+), 1 deletion(-) diff --git a/README.adoc b/README.adoc index 3a89b8b4..ea22309e 100644 --- a/README.adoc +++ b/README.adoc @@ -205,6 +205,36 @@ In the `` part of the Asciidoctor Maven Plugin configuration: You can find more information and many examples ready to copy-paste in the {uri-examples}[Asciidoctor Maven examples] project. +==== Command line configuration + +Configuration options can be set (but not replaced) using system properties directly in the command line as follows: + + mvn generate-resources -Dasciidoctor.sourceDirectory=src/docs -Dasciidoctor.outputDirectory=target/docs + +All options follow the naming convention _`asciidoctor.` + option_name_. + +In order to provide a higher degree of flexibility `attributes` configuration follows a different behavior. +Attributes defined through the command line are added to the ones already found in the XML configuration. +The result of it is that attributes and other configuration options can be updated if they are added to the command line as attributes. +For example, the following configuration could be modified with the command options as seen below. + +[source,xml] +---- + + html5 + coderay + + left + + +---- + + mvn generate-resources -Dasciidoctor.attributes=toc=right + + mvn generate-resources -Dasciidoctor.attributes="toc=right source-highlighter=highlightjs imagesdir=my_images" + +Note that in the second case we need to use quotes due to the spaces, and that `source-highlighter` is the asciidoctor attribute name used to update the configuration. + === Multiple outputs for the same file Maven has the ability to execute a Mojo multiple times. diff --git a/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java b/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java index 79601bd1..74f57ab4 100644 --- a/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java +++ b/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java @@ -82,9 +82,12 @@ public class AsciidoctorMojo extends AbstractMojo { @Parameter(property = AsciidoctorMaven.PREFIX + "requires") protected List requires = new ArrayList(); - @Parameter(property = AsciidoctorMaven.PREFIX + Options.ATTRIBUTES, required = false) + @Parameter(required = false) protected Map attributes = new HashMap(); + @Parameter(property = AsciidoctorMaven.PREFIX + Options.ATTRIBUTES, required = false) + protected String attributesChain = ""; + @Parameter(property = AsciidoctorMaven.PREFIX + Options.BACKEND, defaultValue = "docbook", required = true) protected String backend = ""; @@ -384,6 +387,12 @@ else if (val instanceof Boolean) { attributesBuilder.attribute(attributeEntry.getKey(), val); } } + + if (!attributesChain.isEmpty()) { + getLog().info("Attributes: " + attributesChain); + attributesBuilder.arguments(attributesChain); + } + } public File getSourceDirectory() { diff --git a/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoTest.groovy b/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoTest.groovy index 179fd582..8e59f483 100644 --- a/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoTest.groovy +++ b/src/test/groovy/org/asciidoctor/maven/test/AsciidoctorMojoTest.groovy @@ -858,4 +858,32 @@ class AsciidoctorMojoTest extends Specification { !(new File(outputDir, 'github-include.html').text.contains('modelVersion')) } + def "command line attributes replace configurations"() { + setup: + File srcDir = new File('target/test-classes/src/asciidoctor') + File outputDir = new File('target/asciidoctor-output/command-line-options') + + if (!outputDir.exists()) + outputDir.mkdirs() + when: 'set toc and sourceHighlighter as XML configuration and command line attributes' + AsciidoctorMojo mojo = new AsciidoctorMojo() + mojo.backend = 'html' + mojo.sourceDirectory = srcDir + mojo.sourceDocumentName = 'sample.asciidoc' + mojo.outputDirectory = outputDir + mojo.sourceHighlighter = 'coderay' + mojo.attributes['toc'] = 'left' + // replace some options + mojo.attributesChain = 'toc=right source-highlighter=highlightjs' + mojo.execute() + then: 'command line options are applied instead of xml configuration' + outputDir.list().toList().isEmpty() == false + outputDir.list().toList().contains('sample.html') + File sampleOutput = new File('sample.html', outputDir) + sampleOutput.length() > 0 + String text = sampleOutput.getText() + text.contains('') + text.contains('
')
+    }
+
 }