Skip to content

Commit

Permalink
Fixes conversion failure when using erb template (#610)
Browse files Browse the repository at this point in the history
Internally erubi option was set as null which caused Asciidoctor to fail
with 'Unknown ERB implementation'.
The fix sets the option only when a non empty value is present, delegating
to AsciidoctorJ default which is 'erb' (consistent with current docs).
  • Loading branch information
abelsromero authored Jan 23, 2023
1 parent d7cb395 commit 319848d
Show file tree
Hide file tree
Showing 6 changed files with 68 additions and 7 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ For a detailed view of what has changed, refer to the {uri-repo}/commits/main[co

== Unreleased

Bug Fixes::

* Fixed default value for eruby which caused a fail when using erb templates (#610)

Improvements::

* Split plugin and site integration in sub-modules: asciidoctor-maven-plugin and asciidoctor-doxia-module (#595)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.asciidoctor.jruby.AsciidoctorJRuby;
import org.asciidoctor.jruby.internal.JRubyRuntimeContext;
import org.asciidoctor.maven.commons.AsciidoctorHelper;
import org.asciidoctor.maven.commons.StringUtils;
import org.asciidoctor.maven.extensions.AsciidoctorJExtensionRegistry;
import org.asciidoctor.maven.extensions.ExtensionConfiguration;
import org.asciidoctor.maven.extensions.ExtensionRegistry;
Expand All @@ -32,6 +33,7 @@
import java.util.logging.Logger;
import java.util.stream.Collectors;

import static org.asciidoctor.maven.commons.StringUtils.isBlank;
import static org.asciidoctor.maven.process.SourceDirectoryFinder.DEFAULT_SOURCE_DIR;


Expand Down Expand Up @@ -81,13 +83,13 @@ public class AsciidoctorMojo extends AbstractMojo {
protected String attributesChain = "";

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

@Parameter(property = AsciidoctorMaven.PREFIX + Options.DOCTYPE)
protected String doctype;

@Parameter(property = AsciidoctorMaven.PREFIX + Options.ERUBY)
protected String eruby = "";
protected String eruby;

@Parameter(property = AsciidoctorMaven.PREFIX + "headerFooter")
protected boolean headerFooter = true;
Expand Down Expand Up @@ -116,14 +118,14 @@ public class AsciidoctorMojo extends AbstractMojo {
@Parameter
protected List<ExtensionConfiguration> extensions = new ArrayList<>();

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

// List of resources to copy to the output directory (e.g., images, css). By default everything is copied
@Parameter
protected List<Resource> resources;

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

@Parameter
Expand Down Expand Up @@ -388,9 +390,11 @@ protected OptionsBuilder createOptionsBuilder(AsciidoctorMojo configuration, Att
.backend(configuration.getBackend())
.safe(SafeMode.UNSAFE)
.headerFooter(configuration.isHeaderFooter())
.eruby(configuration.getEruby())
.mkDirs(true);

if (!isBlank(configuration.getEruby()))
optionsBuilder.eruby(configuration.getEruby());

if (configuration.isSourcemap())
optionsBuilder.option(Options.SOURCEMAP, true);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,9 @@ public void should_convert_to_html_with_attributes() throws MojoFailureException
}

@Test
public void should_convert_to_html_with_a_custom_template() throws MojoFailureException, MojoExecutionException {
public void should_convert_to_html_with_a_custom_slim_template() throws MojoFailureException, MojoExecutionException {
// given
final String templatesPath = "target/test-classes/templates/";
final String templatesPath = "target/test-classes/templates/slim/";
File srcDir = new File(DEFAULT_SOURCE_DIRECTORY);
File outputDir = newOutputTestDirectory();

Expand All @@ -203,6 +203,31 @@ public void should_convert_to_html_with_a_custom_template() throws MojoFailureEx
.contains("custom-block-style");
}

@Test
public void should_convert_to_html_with_a_custom_erb_template() throws MojoFailureException, MojoExecutionException {
// given
final String templatesPath = "target/test-classes/templates/";
File srcDir = new File(DEFAULT_SOURCE_DIRECTORY);
File outputDir = newOutputTestDirectory();

// when
AsciidoctorMojo mojo = mockAsciidoctorMojo();
mojo.backend = "html";
mojo.sourceDirectory = srcDir;
mojo.sourceDocumentName = "sample.asciidoc";
mojo.resources = excludeAll();
mojo.outputDirectory = outputDir;
mojo.templateDirs = Arrays.asList(
new File(templatesPath, "erb")
);
mojo.execute();

// then
assertThat(outputDir, "sample.html")
.isNotEmpty()
.contains("custom-style");
}

@Test
public void should_set_output_file() throws MojoFailureException, MojoExecutionException {
// given
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<%#encoding:UTF-8%><%
slevel = @level.zero? && @special ? 1 : @level
anchor = link_start = link_end = nil
if @id
if @document.attr? :sectanchors
anchor = %(<a class="anchor" href="##{@id}"></a>)
elsif @document.attr? :sectlinks
link_start = %(<a class="link" href="##{@id}">)
link_end = '</a>'
end
end
if slevel.zero?
%><h1<%= @id && %( id="#{@id}") %> class="sect0"><%= %(#{anchor}#{link_start}#{title}#{link_end}) %></h1>
<%= content %><%
else
snum = @numbered && @caption.nil? && slevel <= (@document.attr 'sectnumlevels', 3).to_i ? %(#{sectnum} ) : nil
hlevel = slevel + 1
%><div class="<%= [%(sect#{slevel}),role].compact * ' ' %>">
<h<%= hlevel %><%= @id && %( id="#{@id}") %>><%= %(#{anchor}#{link_start}#{snum}#{captioned_title}#{link_end}) %></h<%= hlevel %>><%
if slevel == 1 %>
<div class="sectionbody custom-style">
<%= content %>
</div><%
else %>
<%= content %><%
end %>
</div><%
end %>

0 comments on commit 319848d

Please sign in to comment.