Skip to content

Commit

Permalink
Fixes asciidoctor#1135 on v2.5.x. Target file name should be relative…
Browse files Browse the repository at this point in the history
… to destination dir if source dir is given.
  • Loading branch information
robertpanzer committed Nov 19, 2023
1 parent 616e74a commit a88a8cd
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 26 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 {url-repo}/commits/main[co

== Unreleased

Bug Fixes::

* Fix CLI target file location for source files relative to source dir (#1135,#1241) (@AlexCzar,@mojavelinux)

Improvement::

* Upgrade to JRuby 9.4.3.0 (#1235) (@headius)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.asciidoctor.log.Severity;

import java.io.File;
import java.io.IOException;
import java.util.*;

public class AsciidoctorCliOptions {
Expand Down Expand Up @@ -257,7 +258,7 @@ private boolean isInPlaceRequired() {
return !isOutFileOption() && !isDestinationDirOption() && !isOutputStdout();
}

public Options parse() {
public Options parse() throws IOException {
AttributesBuilder attributesBuilder = Attributes.builder();

OptionsBuilder optionsBuilder = Options.builder()
Expand Down Expand Up @@ -302,7 +303,7 @@ public Options parse() {
}

if (isBaseDirOption()) {
optionsBuilder.baseDir(new File(this.baseDir));
optionsBuilder.baseDir(new File(this.baseDir).getCanonicalFile());
}

if (isTemplateEngineOption()) {
Expand All @@ -311,15 +312,15 @@ public Options parse() {

if (isTemplateDirOption()) {
for (String templateDir : this.templateDir) {
optionsBuilder.templateDirs(new File(templateDir));
optionsBuilder.templateDirs(new File(templateDir).getCanonicalFile());
}
}

if (isDestinationDirOption() && !isOutputStdout()) {
optionsBuilder.toDir(new File(this.destinationDir));
optionsBuilder.toDir(new File(this.destinationDir).getCanonicalFile());

if (isSourceDirOption()) {
optionsBuilder.sourceDir(new File(this.sourceDir));
optionsBuilder.sourceDir(new File(this.sourceDir).getCanonicalFile());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,7 @@

public class AsciidoctorInvoker {

private static final String LINE_SEPARATOR = System.getProperty("line.separator");

public int invoke(String... parameters) {
public int invoke(String... parameters) throws IOException {

AsciidoctorCliOptions asciidoctorCliOptions = new AsciidoctorCliOptions();
JCommander jCommander = new JCommander(asciidoctorCliOptions);
Expand Down Expand Up @@ -160,6 +158,8 @@ private void convertInput(Asciidoctor asciidoctor, Options options, List<File> i
return;
}

options.setMkDirs(true);

findInvalidInputFile(inputFiles)
.ifPresent(inputFile -> {
System.err.println("asciidoctor: FAILED: input file(s) '"
Expand All @@ -171,7 +171,26 @@ private void convertInput(Asciidoctor asciidoctor, Options options, List<File> i
+ "' missing or cannot be read");
});

inputFiles.forEach(inputFile -> asciidoctor.convertFile(inputFile, options));
final Optional<File> toDir = getAbsolutePathFromOption(options, Options.TO_DIR);
final Optional<File> srcDir = getAbsolutePathFromOption(options, Options.SOURCE_DIR);

inputFiles.forEach(inputFile -> {
if (toDir.isPresent() && srcDir.isPresent()) {
if (inputFile.getAbsolutePath().startsWith(srcDir.get().getAbsolutePath())) {
String relativePath = srcDir.get().toURI().relativize(inputFile.getParentFile().getAbsoluteFile().toURI()).getPath();
String absolutePath = new File(toDir.get(), relativePath).getAbsolutePath();
options.setToDir(absolutePath);
}
}
asciidoctor.convertFile(inputFile, options);
});
}

private Optional<File> getAbsolutePathFromOption(Options options, String name) {
return Optional.ofNullable(options.map().get(name))
.filter(String.class::isInstance)
.map(String.class::cast)
.map(File::new);
}

private Optional<File> findInvalidInputFile(List<File> inputFiles) {
Expand Down Expand Up @@ -199,7 +218,7 @@ private List<File> getInputFiles(AsciidoctorCliOptions asciidoctorCliOptions) {
.collect(Collectors.toList());
}

public static void main(String args[]) {
public static void main(String args[]) throws IOException {

// Process .jrubyrc file
Main.processDotfile();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.core.StringStartsWith.startsWith;
import static org.junit.Assert.assertTrue;

@RunWith(Arquillian.class)
public class WhenAsciidoctorIsCalledUsingCli {
Expand All @@ -33,7 +34,7 @@ public class WhenAsciidoctorIsCalledUsingCli {
public String pwd = new File("").getAbsolutePath();

@Test
public void with_no_options_file_should_be_rendered_in_place_and_in_html5_format() {
public void with_no_options_file_should_be_rendered_in_place_and_in_html5_format() throws IOException {

File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
Expand Down Expand Up @@ -61,7 +62,7 @@ public void should_honor_doctype_defined_in_document_by_default() throws IOExcep
}

@Test
public void file_should_be_rendered_to_docbook_with_docbook_backend() {
public void file_should_be_rendered_to_docbook_with_docbook_backend() throws IOException {

File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
Expand Down Expand Up @@ -108,7 +109,7 @@ public void composed_attributes_should_be_built_as_attributes_map() throws IOExc
}

@Test
public void destination_dir_should_render_files_to_ouput_directory() {
public void destination_dir_should_render_files_to_ouput_directory() throws IOException {
File outputDirectory = temporaryFolder.getRoot();

File inputFile = classpath.getResource("rendersample.asciidoc");
Expand All @@ -122,12 +123,12 @@ public void destination_dir_should_render_files_to_ouput_directory() {


@Test(expected = IllegalArgumentException.class)
public void empty_input_file_name_should_throw_an_exception() {
public void empty_input_file_name_should_throw_an_exception() throws IOException {
new AsciidoctorInvoker().invoke("");
}

@Test
public void version_flag_should_print_version_and_exit() {
public void version_flag_should_print_version_and_exit() throws IOException {
PrintStream oldOs = System.out;
ByteArrayOutputStream os = new ByteArrayOutputStream();
System.setOut(new PrintStream(os));
Expand All @@ -140,14 +141,14 @@ public void version_flag_should_print_version_and_exit() {
}

@Test(expected = IllegalArgumentException.class)
public void invalid_input_file_should_throw_an_exception() {
public void invalid_input_file_should_throw_an_exception() throws IOException {

new AsciidoctorInvoker().invoke("myunknown.adoc");

}

@Test
public void more_than_one_input_file_should_throw_an_exception() {
public void more_than_one_input_file_should_throw_an_exception() throws IOException {

File inputFile1 = classpath.getResource("rendersample.asciidoc");
String inputPath1 = inputFile1.getPath().substring(pwd.length() + 1);
Expand All @@ -167,7 +168,7 @@ public void more_than_one_input_file_should_throw_an_exception() {
}

@Test
public void glob_expression_can_be_used_to_render_AsciiDoc_files() {
public void glob_expression_can_be_used_to_render_AsciiDoc_files() throws IOException {

File inputFile = classpath.getResource("toc2sample.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
Expand All @@ -183,7 +184,7 @@ public void glob_expression_can_be_used_to_render_AsciiDoc_files() {
}

@Test
public void help_option_should_show_usage_information() {
public void help_option_should_show_usage_information() throws IOException {
ByteArrayOutputStream output = redirectStdout();

new AsciidoctorInvoker().invoke("--help");
Expand All @@ -194,7 +195,7 @@ public void help_option_should_show_usage_information() {
}

@Test
public void no_parameters_should_show_usage_information() {
public void no_parameters_should_show_usage_information() throws IOException {
ByteArrayOutputStream output = redirectStdout();

new AsciidoctorInvoker().invoke();
Expand All @@ -205,7 +206,7 @@ public void no_parameters_should_show_usage_information() {
}

@Test
public void output_file_hyphen_symbol_should_render_output_to_stdout() {
public void output_file_hyphen_symbol_should_render_output_to_stdout() throws IOException {

ByteArrayOutputStream output = redirectStdout();

Expand All @@ -223,7 +224,7 @@ public void output_file_hyphen_symbol_should_render_output_to_stdout() {
}

@Test
public void verbose_option_should_fill_monitor_map() {
public void verbose_option_should_fill_monitor_map() throws IOException {

ByteArrayOutputStream output = redirectStdout();

Expand All @@ -237,7 +238,7 @@ public void verbose_option_should_fill_monitor_map() {
}

@Test
public void quiet_option_should_not_write_to_console() {
public void quiet_option_should_not_write_to_console() throws IOException {

ByteArrayOutputStream output = redirectStdout();

Expand All @@ -250,23 +251,23 @@ public void quiet_option_should_not_write_to_console() {
}

@Test
public void should_exit_with_zero_status_even_if_errors_were_logged() {
public void should_exit_with_zero_status_even_if_errors_were_logged() throws IOException {
File inputFile = classpath.getResource("brokeninclude.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
int status = new AsciidoctorInvoker().invoke(inputPath);
assertThat(status, is(0));
}

@Test
public void should_exit_with_nonzero_status_if_logged_severity_was_at_least_failure_level() {
public void should_exit_with_nonzero_status_if_logged_severity_was_at_least_failure_level() throws IOException {
File inputFile = classpath.getResource("brokeninclude.asciidoc");
String inputPath = inputFile.getPath().substring(pwd.length() + 1);
int status = new AsciidoctorInvoker().invoke("--failure-level", "warn", inputPath);
assertThat(status, is(1));
}

@Test
public void with_absolute_path_file_should_be_rendered() {
public void with_absolute_path_file_should_be_rendered() throws IOException {

File inputFile = classpath.getResource("rendersample.asciidoc");
String inputPath = inputFile.getAbsolutePath();
Expand All @@ -277,6 +278,19 @@ public void with_absolute_path_file_should_be_rendered() {
expectedFile.delete();
}

@Test
public void should_convert_to_subdirectories() throws IOException {

File inputFile = classpath.getResource("relative/sub/test.adoc");
File srcDir = inputFile.getParentFile().getParentFile();
File toDir = new File(srcDir, "target");
new AsciidoctorInvoker().invoke("-R", srcDir.getPath(), "-D", toDir.getPath(), srcDir.getAbsolutePath() + "/**/*.adoc");
File expectedFile = new File(toDir, "sub/test.html");

assertTrue(expectedFile.exists());
expectedFile.delete();
}

private ByteArrayOutputStream redirectStdout() {
ByteArrayOutputStream output = new ByteArrayOutputStream();
System.setOut(new PrintStream(output));
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions asciidoctorj-core/src/test/resources/relative/sub/test.adoc
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
= Test

== Test

Test

0 comments on commit a88a8cd

Please sign in to comment.