Skip to content

Commit

Permalink
fixes #206 - adds the option to set attributes through command line a…
Browse files Browse the repository at this point in the history
…nd updates README
  • Loading branch information
abelsromero committed Feb 20, 2016
1 parent 28992e4 commit f030632
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 1 deletion.
30 changes: 30 additions & 0 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,36 @@ In the `<attributes>` 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]
----
<configuration>
<backend>html5</backend>
<sourceHighlighter>coderay</sourceHighlighter>
<attributes>
<toc>left</toc>
</attributes>
</configuration>
----

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.
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,12 @@ public class AsciidoctorMojo extends AbstractMojo {
@Parameter(property = AsciidoctorMaven.PREFIX + "requires")
protected List<String> requires = new ArrayList<String>();

@Parameter(property = AsciidoctorMaven.PREFIX + Options.ATTRIBUTES, required = false)
@Parameter(required = false)
protected Map<String, Object> attributes = new HashMap<String, Object>();

@Parameter(property = AsciidoctorMaven.PREFIX + Options.ATTRIBUTES, required = false)
protected String attributesChain = "";

@Parameter(property = AsciidoctorMaven.PREFIX + Options.BACKEND, defaultValue = "docbook", required = true)
protected String backend = "";

Expand Down Expand Up @@ -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() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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('<body class="article toc2 toc-right">')
text.contains('<pre class="highlightjs highlight">')
}

}

0 comments on commit f030632

Please sign in to comment.