Skip to content

Commit

Permalink
fixes #264 - adding sourcemap, catalog_assets and template_cache opti…
Browse files Browse the repository at this point in the history
…ons (#267)
  • Loading branch information
abelsromero committed Sep 16, 2016
1 parent ac30064 commit de98965
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 27 deletions.
7 changes: 5 additions & 2 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,12 @@ backend:: defaults to `docbook`
doctype:: defaults to `null` (which trigger's Asciidoctor's default of `article`)
eruby:: defaults to erb, the version used in JRuby
headerFooter:: defaults to `true`
templateDir:: disabled by default, defaults to `null`
templateEngine:: disabled by default
templateDir:: directory of Tilt-compatible templates to be used instead of the default built-in templates, disabled by default (`null`)
templateEngine:: template engine to use for the custom converter templates, disabled by default (`null`)
templateCache:: enables the built-in cache used by the template converter when reading the source of template files. Only relevant if the :template_dir option is specified, defaults to `true`
sourceHighlighter:: enables and sets the source highlighter (currently `coderay` or `highlight.js` are supported)
sourcemap:: adds file and line number information to each parsed block (`lineno` and `source_location` attributes), defaults to `false`
catalogAssets:: tells the parser to capture images and links in the reference table available via the `references property on the document AST object (experimental), defaults to `false`
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
71 changes: 46 additions & 25 deletions src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,10 @@ public class AsciidoctorMojo extends AbstractMojo {

@Parameter(property = AsciidoctorMaven.PREFIX + "preserveDirectories", defaultValue = "false", required = false)
protected boolean preserveDirectories = false;

@Parameter(property = AsciidoctorMaven.PREFIX + "relativeBaseDir", defaultValue = "false", required = false)
protected boolean relativeBaseDir = false;

@Parameter(property = AsciidoctorMaven.PREFIX + "projectDirectory", defaultValue = "${basedir}", required = false, readonly = false)
protected File projectDirectory;

Expand Down Expand Up @@ -104,6 +104,9 @@ public class AsciidoctorMojo extends AbstractMojo {
@Parameter(property = AsciidoctorMaven.PREFIX + "templateEngine", required = false)
protected String templateEngine;

@Parameter(property = AsciidoctorMaven.PREFIX + "templateCache")
protected boolean templateCache = true;

@Parameter(property = AsciidoctorMaven.PREFIX + "imagesDir", required = false)
protected String imagesDir = "images"; // use a string because otherwise html doc uses absolute path

Expand All @@ -119,6 +122,12 @@ public class AsciidoctorMojo extends AbstractMojo {
@Parameter(property = AsciidoctorMaven.PREFIX + "sourceDocumentExtensions")
protected List<String> sourceDocumentExtensions = new ArrayList<String>();

@Parameter(property = AsciidoctorMaven.PREFIX + "sourcemap")
protected boolean sourcemap = false;

@Parameter(property = AsciidoctorMaven.PREFIX + "catalogAssets")
protected boolean catalogAssets = false;

@Parameter(property = AsciidoctorMaven.PREFIX + "synchronizations", required = false)
protected List<Synchronization> synchronizations = new ArrayList<Synchronization>();

Expand Down Expand Up @@ -181,17 +190,10 @@ public void execute() throws MojoExecutionException, MojoFailureException {

asciidoctor.requireLibraries(requires);

final OptionsBuilder optionsBuilder = OptionsBuilder.options()
.backend(backend)
.safe(SafeMode.UNSAFE)
.headerFooter(headerFooter)
.eruby(eruby)
.mkDirs(true);

setOptions(optionsBuilder);
final OptionsBuilder optionsBuilder = OptionsBuilder.options();
setOptionsOnBuilder(optionsBuilder);

final AttributesBuilder attributesBuilder = AttributesBuilder.attributes();

setAttributesOnBuilder(attributesBuilder);

optionsBuilder.attributes(attributesBuilder);
Expand Down Expand Up @@ -286,7 +288,7 @@ private void copyResources() throws MojoExecutionException {

/**
* Updates optionsBuilder object's baseDir and destination(s) accordingly to the options.
*
*
* @param optionsBuilder
* AsciidoctorJ options to be updated.
* @param sourceFile
Expand Down Expand Up @@ -327,7 +329,7 @@ protected Asciidoctor getAsciidoctorInstance(String gemPath) throws MojoExecutio
String normalizedGemPath = (File.separatorChar == '\\') ? gemPath.replaceAll("\\\\", "/") : gemPath;
asciidoctor = Asciidoctor.Factory.create(normalizedGemPath);
}

Ruby rubyInstance = null;
try {
rubyInstance = (Ruby) JRubyRuntimeContext.class.getMethod("get")
Expand All @@ -348,7 +350,7 @@ protected Asciidoctor getAsciidoctorInstance(String gemPath) throws MojoExecutio
throw new MojoExecutionException(
"Failed to invoke get for JRubyRuntimeContext", e);
}

String gemHome = rubyInstance.evalScriptlet("ENV['GEM_HOME']").toString();
String gemHomeExpected = (gemPath == null || "".equals(gemPath)) ? "" : gemPath.split(java.io.File.pathSeparator)[0];

Expand Down Expand Up @@ -425,18 +427,38 @@ protected void ensureOutputExists() {
}
}

protected void setOptions(OptionsBuilder optionsBuilder) {
if (doctype != null) {
/**
* Updates and OptionsBuilder instance with the options defined in the configuration.
*
* @param optionsBuilder
* AsciidoctorJ options to be updated.
*/
protected void setOptionsOnBuilder(OptionsBuilder optionsBuilder) {
optionsBuilder
.backend(backend)
.safe(SafeMode.UNSAFE)
.headerFooter(headerFooter)
.eruby(eruby)
.mkDirs(true);

// Following options are only set when the value is different than the default
if (sourcemap)
optionsBuilder.option("sourcemap", true);

if (catalogAssets)
optionsBuilder.option("catalog_assets", true);

if (!templateCache)
optionsBuilder.option("template_cache", false);

if (doctype != null)
optionsBuilder.docType(doctype);
}

if (templateEngine != null) {
if (templateEngine != null)
optionsBuilder.templateEngine(templateEngine);
}

if (templateDir != null) {
if (templateDir != null)
optionsBuilder.templateDir(templateDir);
}
}

protected void setAttributesOnBuilder(AttributesBuilder attributesBuilder) throws MojoExecutionException {
Expand Down Expand Up @@ -480,8 +502,7 @@ else if ("false".equals(val)) {
// NOTE Maven can't assign a Boolean value from the XML-based configuration, but a client may
else if (val instanceof Boolean) {
attributesBuilder.attribute(attributeEntry.getKey(), Attributes.toAsciidoctorFlag((Boolean) val));
}
else {
} else {
// Can't do anything about dates and times because all that logic is private in Attributes
attributesBuilder.attribute(attributeEntry.getKey(), val);
}
Expand Down Expand Up @@ -684,8 +705,8 @@ public void setPreserveDirertories(boolean preserveDirertories) {

public void setRelativeBaseDir(boolean relativeBaseDir) {
this.relativeBaseDir = relativeBaseDir;
}
}

public List<ExtensionConfiguration> getExtensions() {
return extensions;
}
Expand Down

0 comments on commit de98965

Please sign in to comment.