Skip to content

Commit

Permalink
Fixes conversion failure when using erb template
Browse files Browse the repository at this point in the history
Internally eruby 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 committed Mar 18, 2023
1 parent 8980edb commit b1042d2
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 8 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,12 @@ 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)

Build / Infrastructure::

* Bump Doxia to v1.11.1 and maven-site-plugin in IT to 3.12.0 (#579)
* Bump netty-codec-http to v4.1.77.Final (fix CVE-2021-21290) (#582)
* Upgrade Asciidoctorj to v2.5.4 and jRuby to v9.3.4.0 (#584)
Expand Down
12 changes: 7 additions & 5 deletions src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,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 @@ -123,14 +123,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 @@ -476,9 +476,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
29 changes: 27 additions & 2 deletions src/test/java/org/asciidoctor/maven/AsciidoctorMojoTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,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 @@ -205,6 +205,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
28 changes: 28 additions & 0 deletions src/test/resources/templates/erb/section.html.erb
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 b1042d2

Please sign in to comment.