Skip to content

Commit

Permalink
Merge pull request asciidoctor#1170 from abelsromero/issue-1169-remov…
Browse files Browse the repository at this point in the history
…e-AsciidoctorUtils-class

Remove AsciidoctorUtils class
  • Loading branch information
robertpanzer authored Apr 9, 2023
2 parents 5083729 + 28654c6 commit 0240f02
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 202 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ Improvement::
* Create `asciidoctorj-cli` module to prevent unnecessary dependencies to asciidoctorj jar consumers (#1149)
* Add required `--add-opens` to cli launch script to remove Jdk warnings (#1155) (@abelsromero)
* Rename deprecated `headerFooter` option to the new `standalone` with same functionality (#1155) (@abelsromero)
* Remove class `AsciidoctorUtils` to remove complexity and unused logging (#1169) (@abelsromero)

Bug Fixes::

Expand Down
Original file line number Diff line number Diff line change
@@ -1,58 +1,41 @@
package org.asciidoctor.cli;

import com.beust.jcommander.JCommander;
import org.asciidoctor.Attributes;
import org.asciidoctor.Options;
import org.asciidoctor.OptionsBuilder;
import org.asciidoctor.SafeMode;
import org.asciidoctor.jruby.internal.AsciidoctorUtils;
import org.junit.Test;

import java.io.File;
import java.util.List;
import java.nio.file.Path;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.collection.IsIterableContainingInAnyOrder.containsInAnyOrder;
import static org.hamcrest.collection.IsMapContaining.hasEntry;
import static org.hamcrest.collection.IsMapContaining.hasKey;
import static org.assertj.core.api.Assertions.assertThat;

public class WhenAsciidoctorAPICallIsCalled {

@Test
public void api_parameters_should_be_transformed_to_cli_command() {

Attributes attributes = Attributes.builder()
.attribute("myAtribute", "myValue")
.sectionNumbers(true)
.copyCss(false)
.build();

OptionsBuilder optionsBuilder = Options.builder()
.backend("docbook")
.templateDirs(new File("a"), new File("b"))
.safe(SafeMode.UNSAFE)
.attributes(attributes);

List<String> command = AsciidoctorUtils.toAsciidoctorCommand(
optionsBuilder.asMap(), "file.adoc");

String currentDirectory = new File("").getAbsolutePath() + File.separator;

String[] parameters = command.subList(1, command.size()).toArray(new String[0]);
final String currentDirectory = new File("").getAbsolutePath() + File.separator;
var parameters = new String[]{
"-T", Path.of(currentDirectory, "a").toAbsolutePath().toString(),
"-T", Path.of(currentDirectory, "b").toAbsolutePath().toString(),
"-S", "UNSAFE",
"-b", "docbook",
"-a", "myAttribute=myValue",
"-a", "sectnums",
"-a", "copycss!",
"file.adoc"
};

final AsciidoctorCliOptions asciidoctorCliOptions = new AsciidoctorCliOptions();
final JCommander jCommander = new JCommander(asciidoctorCliOptions);
jCommander.parse(parameters);

assertThat(asciidoctorCliOptions.getTemplateDir(), containsInAnyOrder(currentDirectory + "a", currentDirectory + "b"));
assertThat(asciidoctorCliOptions.getSafeMode(), is(SafeMode.UNSAFE));
assertThat(asciidoctorCliOptions.getBackend(), is("docbook"));
assertThat(asciidoctorCliOptions.getParameters(), containsInAnyOrder("file.adoc"));

assertThat(asciidoctorCliOptions.getAttributes(), hasEntry("myAtribute", "myValue"));
assertThat(asciidoctorCliOptions.getAttributes(), hasKey("sectnums"));
assertThat(asciidoctorCliOptions.getAttributes(), hasKey("copycss!"));
assertThat(asciidoctorCliOptions.getTemplateDir()).containsExactlyInAnyOrder(currentDirectory + "a", currentDirectory + "b");
assertThat(asciidoctorCliOptions.getSafeMode()).isEqualTo(SafeMode.UNSAFE);
assertThat(asciidoctorCliOptions.getBackend()).isEqualTo("docbook");
assertThat(asciidoctorCliOptions.getParameters()).containsExactly("file.adoc");
assertThat(asciidoctorCliOptions.getAttributes())
.containsEntry("myAttribute", "myValue")
.containsEntry("sectnums", "")
.containsEntry("copycss!", "");
}

}
1 change: 0 additions & 1 deletion asciidoctorj-core/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ dependencies {
testImplementation "com.google.guava:guava:$guavaVersion"
testImplementation "org.jsoup:jsoup:$jsoupVersion"
testImplementation "io.netty:netty-all:$nettyVersion"
testImplementation "org.assertj:assertj-core:$assertjVersion"
testImplementation project(':asciidoctorj-arquillian-extension')
compileOnly "org.osgi:osgi.annotation:$osgiVersion"
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -286,9 +286,7 @@ public <T> T convert(String content, Map<String, Object> options, Class<T> expec

this.rubyGemsPreloader.preloadRequiredLibraries(options);

logger.fine(String.join(" ", AsciidoctorUtils.toAsciidoctorCommand(options, "-")));

if (AsciidoctorUtils.isOptionWithAttribute(options, Attributes.SOURCE_HIGHLIGHTER, "pygments")) {
if (containsAttributeWithValue(options, Attributes.SOURCE_HIGHLIGHTER, "pygments")) {
logger.fine("In order to use Pygments with Asciidoctor, you need to install Pygments (and Python, if you don't have it yet). Read https://docs.asciidoctor.org/asciidoctor/latest/syntax-highlighting/pygments.");
}

Expand Down Expand Up @@ -324,6 +322,12 @@ public <T> T convert(String content, Map<String, Object> options, Class<T> expec

}

private boolean containsAttributeWithValue(Map<String, Object> options, String attributeName, String attributeValue) {
return Optional.ofNullable((Map<String, String>) options.get(Options.ATTRIBUTES))
.map(attributes -> attributes.get(attributeName))
.map(value -> value.equals(attributeValue))
.orElse(Boolean.FALSE);
}

@Override
public String convert(String content, Options options) {
Expand Down Expand Up @@ -372,8 +376,6 @@ public <T> T convertFile(File file, Map<String, Object> options, Class<T> expect

this.rubyGemsPreloader.preloadRequiredLibraries(options);

logger.fine(String.join(" ", AsciidoctorUtils.toAsciidoctorCommand(options, file.getAbsolutePath())));

String currentDirectory = rubyRuntime.getCurrentDirectory();

if (options.containsKey(Options.BASEDIR)) {
Expand Down
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ subprojects {

dependencies {
testImplementation "junit:junit:$junitVersion"
testImplementation "org.assertj:assertj-core:$assertjVersion"
testImplementation "org.hamcrest:hamcrest-library:$hamcrestVersion"
testImplementation("org.spockframework:spock-core:$spockVersion") {
exclude group: 'org.hamcrest', module: 'hamcrest-core'
Expand Down

0 comments on commit 0240f02

Please sign in to comment.