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

fixes #206 - adds the option to set attributes through command line an… #209

Merged
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
32 changes: 31 additions & 1 deletion README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ eruby:: defaults to erb, the version used in JRuby
headerFooter:: defaults to `true`
templateDir:: disabled by default, defaults to `null`
templateEngine:: disabled by default
sourceHighlighter:: enables and sets the source highlighter (currently `coderay` or `highlightjs` are supported)
sourceHighlighter:: enables and sets the source highlighter (currently `coderay` or `highlight.js` are supported)
attributes:: a `Map<String,Object>` of attributes to pass to Asciidoctor, defaults to `null`
embedAssets:: Embedd the CSS file, etc into the output, defaults to `false`
gemPaths:: enables to specify the location to one or more gem installation directories (same as GEM_PATH environment var), `empty` by default
Expand Down 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=highlight.js 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 @@ -466,7 +466,7 @@ class AsciidoctorMojoTest extends Specification {
/**
* Tests Highlight.js source code highlighting options.
*/
def 'code highlighting - highlightjs'() {
def 'code highlighting - highlight.js'() {
setup:
File srcDir = new File('src/test/resources/src/asciidoctor')
File outputDir = new File('target/asciidoctor-output-sourceHighlighting/highlightjs')
Expand All @@ -475,7 +475,7 @@ class AsciidoctorMojoTest extends Specification {
AsciidoctorMojo mojo = new AsciidoctorMojo()
mojo.sourceDirectory = srcDir
mojo.outputDirectory = outputDir
mojo.sourceHighlighter = 'highlightjs'
mojo.sourceHighlighter = 'highlight.js'
mojo.sourceDocumentName = new File('main-document.adoc')
mojo.backend = 'html'
mojo.execute()
Expand Down 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=highlight.js'
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">')
}

}