Skip to content

Commit

Permalink
reused attributes treatment for site goal (#334)
Browse files Browse the repository at this point in the history
  • Loading branch information
abelsromero committed Jan 25, 2018
1 parent 2734494 commit abbce69
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 21 deletions.
59 changes: 59 additions & 0 deletions src/main/java/org/asciidoctor/maven/AsciidoctorHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.asciidoctor.maven;

import org.asciidoctor.Attributes;
import org.asciidoctor.AttributesBuilder;

import java.util.Map;

/**
* Utility class for re-usable logic.
*/
public class AsciidoctorHelper {

/**
* Adds attributes from a {@link Map} into a {@link AttributesBuilder} taking care of Maven's XML parsing special
* cases like toggles, nulls, etc.
*/
public static void addAttributes(final Map<String, Object> attributes, AttributesBuilder attributesBuilder) {
// TODO Figure out how to reliably set other values (like boolean values, dates, times, etc)
for (Map.Entry<String, Object> attributeEntry : attributes.entrySet()) {
addAttribute(attributeEntry.getKey(), attributeEntry.getValue(), attributesBuilder);
}
}

/**
* Adds an attribute into a {@link AttributesBuilder} taking care of Maven's XML parsing special cases like
* toggles toggles, nulls, etc.
*/
public static void addAttribute(String attribute, Object value, AttributesBuilder attributesBuilder) {
// NOTE Maven interprets an empty value as null, so we need to explicitly convert it to empty string (see #36)
// NOTE In Asciidoctor, an empty string represents a true value
if (value == null || "true".equals(value)) {
attributesBuilder.attribute(attribute, "");
}
// NOTE a value of false is effectively the same as a null value, so recommend the use of the string "false"
else if ("false".equals(value)) {
attributesBuilder.attribute(attribute, null);
}
// NOTE Maven can't assign a Boolean value from the XML-based configuration, but a client may
else if (value instanceof Boolean) {
attributesBuilder.attribute(attribute, Attributes.toAsciidoctorFlag((Boolean) value));
} else {
// Can't do anything about dates and times because all that logic is private in Attributes
attributesBuilder.attribute(attribute, value);
}
}

}
21 changes: 1 addition & 20 deletions src/main/java/org/asciidoctor/maven/AsciidoctorMojo.java
Original file line number Diff line number Diff line change
Expand Up @@ -500,26 +500,7 @@ protected void setAttributesOnBuilder(AttributesBuilder attributesBuilder) throw
throw new MojoExecutionException(attributeUndefined + " is not valid. Must be one of 'drop' or 'drop-line'");
}

// TODO Figure out how to reliably set other values (like boolean values, dates, times, etc)
for (Map.Entry<String, Object> attributeEntry : attributes.entrySet()) {
Object val = attributeEntry.getValue();
// NOTE Maven interprets an empty value as null, so we need to explicitly convert it to empty string (see #36)
// NOTE In Asciidoctor, an empty string represents a true value
if (val == null || "true".equals(val)) {
attributesBuilder.attribute(attributeEntry.getKey(), "");
}
// NOTE a value of false is effectively the same as a null value, so recommend the use of the string "false"
else if ("false".equals(val)) {
attributesBuilder.attribute(attributeEntry.getKey(), null);
}
// 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 {
// Can't do anything about dates and times because all that logic is private in Attributes
attributesBuilder.attribute(attributeEntry.getKey(), val);
}
}
AsciidoctorHelper.addAttributes(attributes, attributesBuilder);

if (!attributesChain.isEmpty()) {
getLog().info("Attributes: " + attributesChain);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.asciidoctor.AttributesBuilder;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;
import org.asciidoctor.maven.AsciidoctorHelper;
import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
import org.codehaus.plexus.util.IOUtil;
Expand Down Expand Up @@ -132,7 +133,7 @@ protected OptionsBuilder processAsciiDocConfig(Xpp3Dom siteConfig, OptionsBuilde
String optName = asciidocOpt.getName();
if ("attributes".equals(optName)) {
for (Xpp3Dom asciidocAttr : asciidocOpt.getChildren()) {
attributes.attribute(asciidocAttr.getName(), asciidocAttr.getValue());
AsciidoctorHelper.addAttribute(asciidocAttr.getName(), asciidocAttr.getValue(), attributes);
}
}
else if ("requires".equals(optName)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,53 @@ class AsciidoctorDoxiaParserTest extends Specification {
outputText.contains '<i class="fa icon-tip" title="Tip"></i>'
}

def "should process empty self-closing XML attributes"() {
given:
final File srcAsciidoc = new File("$TEST_DOCS_PATH/sample.asciidoc")
final Sink sink = createSinkMock()

AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser()
parser.@project = createMavenProjectMock("""
<configuration>
<asciidoc>
<attributes>
<sectnums/>
</attributes>
</asciidoc>
</configuration>""")

when:
parser.parse(new FileReader(srcAsciidoc), sink)

then:
String outputText = sink.sinkedText
outputText.contains '<h2 id="id_section_a">1. Section A</h2>'
outputText.contains '<h3 id="id_section_a_subsection">1.1. Section A Subsection</h3>'
}

def "should process empty value XML attributes"() {
given:
final File srcAsciidoc = new File("$TEST_DOCS_PATH/sample.asciidoc")
final Sink sink = createSinkMock()

AsciidoctorDoxiaParser parser = new AsciidoctorDoxiaParser()
parser.@project = createMavenProjectMock("""
<configuration>
<asciidoc>
<attributes>
<sectnums></sectnums>
</attributes>
</asciidoc>
</configuration>""")

when:
parser.parse(new FileReader(srcAsciidoc), sink)

then:
String outputText = sink.sinkedText
outputText.contains '<h2 id="id_section_a">1. Section A</h2>'
outputText.contains '<h3 id="id_section_a_subsection">1.1. Section A Subsection</h3>'
}

private MavenProject createMavenProjectMock(final String configuration = null) {
[getGoalConfiguration: { pluginGroupId, pluginArtifactId, executionId, goalId ->
Expand Down

0 comments on commit abbce69

Please sign in to comment.