Fixes Target file name should be relative to destination dir if source dir is given.
MODIFY
CHANGELOG.adoc
CHANGELOG.adoc
@@ -27,6 +27,7 @@ Improvement::
Bug Fixes::
+* Fix CLI target file location for source files relative to source dir (#1135) (@AlexCzar)
* Cell nodes do not inherit from StructuralNode (#1086) (@rahmanusta)
* Avoid throwing an exception when using AsciidoctorJ CLI and reading input from stdin (#1105) (@AlexCzar)

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/cli/AsciidoctorInvoker.java
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/cli/AsciidoctorInvoker.java
@@ -160,6 +160,8 @@ public class AsciidoctorInvoker {
return;
}
+ options.setMkDirs(true);
+
findInvalidInputFile(inputFiles)
.ifPresent(inputFile -> {
System.err.println("asciidoctor: FAILED: input file(s) '"
@@ -171,7 +173,26 @@ public class AsciidoctorInvoker {
+ "' 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((String) 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).map(File::getAbsoluteFile);
}
private Optional<File> findInvalidInputFile(List<File> inputFiles) {

Fixes #1135. Target file name should be relative to destination dir if source dir is given.