Convert to System.out when reading input from System.in. Set :standalone option to true by default and to false if no-header-footer.
MODIFY
asciidoctorj-api/src/main/java/org/asciidoctor/Options.java
asciidoctorj-api/src/main/java/org/asciidoctor/Options.java
@@ -19,6 +19,7 @@ public class Options {
public static final String MKDIRS = "mkdirs";
public static final String SAFE = "safe";
public static final String SOURCEMAP = "sourcemap";
+ public static final String STANDALONE = "standalone";
public static final String ERUBY = "eruby";
public static final String CATALOG_ASSETS = "catalog_assets";
public static final String COMPACT = "compact";

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/cli/AsciidoctorCliOptions.java
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/cli/AsciidoctorCliOptions.java
@@ -248,6 +248,10 @@ public class AsciidoctorCliOptions {
return this.version;
}
+ private boolean isInputStdin() {
+ return getParameters().size() == 1 && "-".equals(getParameters().get(0));
+ }
+
private boolean isOutputStdout() {
return "-".equals(getOutFile());
}
@@ -257,21 +261,31 @@ public class AsciidoctorCliOptions {
}
public Options parse() {
- OptionsBuilder optionsBuilder = OptionsBuilder.options();
AttributesBuilder attributesBuilder = AttributesBuilder.attributes();
- optionsBuilder.backend(this.backend).safe(this.safeMode).eruby(this.eruby);
+ OptionsBuilder optionsBuilder = OptionsBuilder.options()
+ .backend(this.backend)
+ .safe(this.safeMode)
+ .eruby(this.eruby)
+ .option(Options.STANDALONE, true);
if (isDoctypeOption()) {
optionsBuilder.docType(this.doctype);
}
+ if (isInputStdin()) {
+ optionsBuilder.toStream(System.out);
+ if (outFile == null) {
+ outFile = "-";
+ }
+ }
+
if (isOutFileOption() && !isOutputStdout()) {
optionsBuilder.toFile(new File(this.outFile));
}
- if (isOutFileOption() && isOutputStdout()) {
- optionsBuilder.toFile(false);
+ if (isOutputStdout()) {
+ optionsBuilder.toStream(System.out);
}
if (this.safe) {
@@ -279,7 +293,7 @@ public class AsciidoctorCliOptions {
}
if (this.noHeaderFooter) {
- optionsBuilder.headerFooter(false);
+ optionsBuilder.option(Options.STANDALONE, false);
}
if (this.sectionNumbers) {

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/cli/AsciidoctorInvoker.java
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/cli/AsciidoctorInvoker.java
@@ -70,17 +70,13 @@ public class AsciidoctorInvoker {
setVerboseLevel(asciidoctor, asciidoctorCliOptions);
- String output = renderInput(asciidoctor, options, inputFiles);
+ renderInput(asciidoctor, options, inputFiles);
if (asciidoctorCliOptions.isTimings()) {
Map<String, Object> optionsMap = options.map();
IRubyObject timings = (IRubyObject) optionsMap.get("timings");
timings.callMethod(JRubyRuntimeContext.get(asciidoctor).getCurrentContext(), "print_report");
}
-
- if (output != null && !"".equals(output.trim())) {
- System.out.println(output);
- }
}
}
@@ -183,14 +179,7 @@ public class AsciidoctorInvoker {
}
private String readInputFromStdIn() {
- String content = IOUtils.readFull(System.in);
- try {
- System.in.close();
- } catch (IOException e) {
- // swallow
- }
-
- return content;
+ return IOUtils.readFull(System.in);
}
private List<File> getInputFiles(AsciidoctorCliOptions asciidoctorCliOptions) {

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/IOUtils.java
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/IOUtils.java
@@ -13,11 +13,15 @@ public class IOUtils {
}
public static String readFull(InputStream inputStream) {
- return new Scanner(inputStream).useDelimiter("\\A").next();
+ try (Scanner scanner = new Scanner(inputStream).useDelimiter("\\A")){
+ return scanner.next();
+ }
}
public static String readFull(Reader reader) {
- return new Scanner(reader).useDelimiter("\\A").next();
+ try (Scanner scanner = new Scanner(reader).useDelimiter("\\A")){
+ return scanner.next();
+ }
}
public static void writeFull(Writer writer, String content) throws IOException {

MODIFY
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/JRubyAsciidoctor.java
asciidoctorj-core/src/main/java/org/asciidoctor/jruby/internal/JRubyAsciidoctor.java
@@ -379,6 +379,11 @@ public class JRubyAsciidoctor implements AsciidoctorJRuby, LogHandler {
rubyRuntime.setCurrentDirectory((String) options.get(Options.BASEDIR));
}
+ final Object toFileOption = options.get(Options.TO_FILE);
+ if (toFileOption instanceof OutputStream) {
+ options.put(Options.TO_FILE, RubyOutputStreamWrapper.wrap(getRubyRuntime(), (OutputStream) toFileOption));
+ }
+
RubyHash rubyHash = RubyHashUtil.convertMapToRubyHashWithSymbols(rubyRuntime, options);
try {

Convert to System.out when reading input from System.in. Set :standalone option to true by default and to false if no-header-footer.