From 21d64335e1ca4ae8dc2f8f9cedc8c3201d46d858 Mon Sep 17 00:00:00 2001 From: Abel Salgado Romero Date: Tue, 13 Sep 2016 21:33:43 +0200 Subject: [PATCH] fixes #264 - adding sourcemap, catalog_assets and template_cache options --- README.adoc | 7 +- .../asciidoctor/maven/AsciidoctorMojo.java | 71 ++++++++++++------- 2 files changed, 51 insertions(+), 27 deletions(-) diff --git a/README.adoc b/README.adoc index d5688a92..af7c14ff 100644 --- a/README.adoc +++ b/README.adoc @@ -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` 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 diff --git a/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java b/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java index baee3b0e..2e67e263 100644 --- a/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java +++ b/src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java @@ -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; @@ -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 @@ -119,6 +122,12 @@ public class AsciidoctorMojo extends AbstractMojo { @Parameter(property = AsciidoctorMaven.PREFIX + "sourceDocumentExtensions") protected List sourceDocumentExtensions = new ArrayList(); + @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 synchronizations = new ArrayList(); @@ -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); @@ -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 @@ -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") @@ -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]; @@ -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 { @@ -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); } @@ -684,8 +705,8 @@ public void setPreserveDirertories(boolean preserveDirertories) { public void setRelativeBaseDir(boolean relativeBaseDir) { this.relativeBaseDir = relativeBaseDir; - } - + } + public List getExtensions() { return extensions; }