Skip to content

Commit a3bd98b

Browse files
authored
Reuse Asciidoctor Ruby invoker (#1249)
Reuse Asciidoctor Ruby invoker
1 parent 4315499 commit a3bd98b

File tree

15 files changed

+218
-223
lines changed

15 files changed

+218
-223
lines changed

asciidoctorj-api/src/main/java/org/asciidoctor/Attributes.java

+5-1
Original file line numberDiff line numberDiff line change
@@ -593,7 +593,7 @@ private void addAttributes(String[] allAttributes) {
593593

594594
private void extractAttributeNameAndValue(String attribute, int equalsIndex) {
595595
String attributeName = attribute.substring(0, equalsIndex);
596-
String attributeValue = attribute.substring(equalsIndex + 1, attribute.length());
596+
String attributeValue = attribute.substring(equalsIndex + 1);
597597

598598
this.attributes.put(attributeName, attributeValue);
599599
}
@@ -620,6 +620,10 @@ private static String toTime(Date time) {
620620
return TIME_FORMAT.format(time);
621621
}
622622

623+
public boolean isEmpty() {
624+
return this.attributes.isEmpty();
625+
}
626+
623627
/**
624628
* @deprecated For internal use only.
625629
*/

asciidoctorj-api/src/main/java/org/asciidoctor/Options.java

+4-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@
66
/**
77
* AsciidoctorJ conversion options.
88
* <p>
9-
* See https://docs.asciidoctor.org/asciidoctor/latest/api/options/ for further
9+
* See <a href="https://docs.asciidoctor.org/asciidoctor/latest/api/options/">https://docs.asciidoctor.org/asciidoctor/latest/api/options/</a> for further
1010
* details.
1111
*/
1212
public class Options {
1313

1414
public static final String IN_PLACE = "in_place";
15+
public static final String WARNINGS = "warnings";
16+
public static final String TIMINGS = "timings";
1517
public static final String ATTRIBUTES = "attributes";
1618
public static final String TEMPLATE_DIRS = "template_dirs";
1719
public static final String TEMPLATE_ENGINE = "template_engine";
@@ -24,10 +26,10 @@ public class Options {
2426
public static final String ERUBY = "eruby";
2527
public static final String CATALOG_ASSETS = "catalog_assets";
2628
public static final String COMPACT = "compact";
27-
public static final String SOURCE_DIR = "source_dir";
2829
public static final String BACKEND = "backend";
2930
public static final String DOCTYPE = "doctype";
3031
public static final String BASEDIR = "base_dir";
32+
public static final String TRACE = "trace";
3133
public static final String TEMPLATE_CACHE = "template_cache";
3234
public static final String SOURCE = "source";
3335
public static final String PARSE = "parse";
@@ -160,10 +162,6 @@ public void setCompact(boolean compact) {
160162
this.options.put(COMPACT, compact);
161163
}
162164

163-
public void setSourceDir(String srcDir) {
164-
this.options.put(SOURCE_DIR, srcDir);
165-
}
166-
167165
public void setBackend(String backend) {
168166
this.options.put(BACKEND, backend);
169167
}

asciidoctorj-api/src/main/java/org/asciidoctor/OptionsBuilder.java

+2-17
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
import java.io.File;
44
import java.io.OutputStream;
5-
import java.util.Map;
65

76
/**
87
* Fluent Options API for AsciidoctorJ.
@@ -160,7 +159,7 @@ public OptionsBuilder toStream(OutputStream toStream) {
160159
* @return this instance.
161160
*/
162161
public OptionsBuilder toDir(File directory) {
163-
this.options.setToDir(directory.getAbsolutePath());
162+
this.options.setToDir(directory.getPath());
164163
return this;
165164
}
166165

@@ -265,20 +264,6 @@ public OptionsBuilder parseHeaderOnly(boolean parseHeaderOnly) {
265264
return this;
266265
}
267266

268-
/**
269-
* Source directory.
270-
*
271-
* This must be used alongside {@link #toDir(File)}.
272-
*
273-
* @param srcDir
274-
* source directory.
275-
* @return this instance.
276-
*/
277-
public OptionsBuilder sourceDir(File srcDir) {
278-
this.options.setSourceDir(srcDir.getAbsolutePath());
279-
return this;
280-
}
281-
282267
/**
283268
* Sets a custom or unlisted option.
284269
*
@@ -301,7 +286,7 @@ public OptionsBuilder option(String option, Object value) {
301286
* @return this instance.
302287
*/
303288
public OptionsBuilder baseDir(File baseDir) {
304-
this.options.setBaseDir(baseDir.getAbsolutePath());
289+
this.options.setBaseDir(baseDir.getPath());
305290
return this;
306291
}
307292

asciidoctorj-cli/src/main/java/org/asciidoctor/cli/AsciidoctorCliOptions.java

+58-62
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
package org.asciidoctor.cli;
22

33
import com.beust.jcommander.Parameter;
4-
import org.asciidoctor.*;
4+
import org.asciidoctor.Attributes;
5+
import org.asciidoctor.AttributesBuilder;
6+
import org.asciidoctor.Options;
7+
import org.asciidoctor.SafeMode;
58
import org.asciidoctor.log.Severity;
9+
import org.jruby.Ruby;
10+
import org.jruby.RubyHash;
611

712
import java.io.File;
8-
import java.io.IOException;
913
import java.util.ArrayList;
1014
import java.util.Collections;
1115
import java.util.List;
@@ -16,6 +20,7 @@ public class AsciidoctorCliOptions {
1620
public static final String LOAD_PATHS = "-I";
1721
public static final String REQUIRE = "-r";
1822
public static final String QUIET = "-q";
23+
public static final String WARNINGS = "-w";
1924
public static final String ATTRIBUTE = "-a";
2025
public static final String HELP = "-h";
2126
public static final String DESTINATION_DIR = "-D";
@@ -35,7 +40,6 @@ public class AsciidoctorCliOptions {
3540
public static final String VERBOSE = "-v";
3641
public static final String TIMINGS = "-t";
3742
public static final char ATTRIBUTE_SEPARATOR = '=';
38-
public static final String TIMINGS_OPTION_NAME = "timings";
3943

4044
@Parameter(names = {VERBOSE, "--verbose"}, description = "enable verbose mode")
4145
private boolean verbose = false;
@@ -47,7 +51,7 @@ public class AsciidoctorCliOptions {
4751
private boolean version = false;
4852

4953
@Parameter(names = {BACKEND, "--backend"}, description = "set output format backend")
50-
private String backend = "html5";
54+
private String backend;
5155

5256
@Parameter(names = {DOCTYPE, "--doctype"}, description = "document type to use when rendering output: [article, book, inline] (default: article)")
5357
private String doctype;
@@ -70,8 +74,8 @@ public class AsciidoctorCliOptions {
7074
@Parameter(names = {SECTION_NUMBERS, "--section-numbers"}, description = "auto-number section titles; disabled by default")
7175
private boolean sectionNumbers = false;
7276

73-
@Parameter(names = {"--eruby"}, description = "specify eRuby implementation to render built-in templates: [erb, erubis]")
74-
private String eruby = "erb";
77+
@Parameter(names = {"--eruby"}, description = "specify eRuby implementation to render built-in templates: [erb, erubis]; default erb")
78+
private String eruby;
7579

7680
@Parameter(names = {COMPACT, "--compact"}, description = "compact the output by removing blank lines")
7781
private boolean compact = false;
@@ -103,6 +107,9 @@ public class AsciidoctorCliOptions {
103107
@Parameter(names = {QUIET, "--quiet"}, description = "suppress warnings")
104108
private boolean quiet = false;
105109

110+
@Parameter(names = {WARNINGS, "--warnings"}, description = "suppress warnings")
111+
private boolean warnings = false;
112+
106113
@Parameter(names = {"--failure-level"}, converter = SeverityConverter.class, description = "set minimum log level that yields a non-zero exit code.")
107114
private Severity failureLevel = Severity.FATAL;
108115

@@ -266,80 +273,69 @@ private boolean isInPlaceRequired() {
266273
return !isOutFileOption() && !isDestinationDirOption() && !isOutputStdout();
267274
}
268275

269-
public Options parse() throws IOException {
270-
OptionsBuilder optionsBuilder = Options.builder()
271-
.backend(this.backend)
272-
.safe(this.safeMode)
273-
.eruby(this.eruby)
274-
.option(Options.STANDALONE, true);
276+
public RubyHash parse(Ruby ruby) {
275277

276-
if (isDoctypeOption()) {
277-
optionsBuilder.docType(this.doctype);
278-
}
278+
RubyHash opts = new RubyHash(ruby);
279+
Attributes attributes = buildAttributes();
279280

280-
if (isInputStdin()) {
281-
optionsBuilder.toStream(System.out);
282-
if (outFile == null) {
283-
outFile = "-";
284-
}
285-
}
281+
opts.put(ruby.newSymbol(Options.STANDALONE), true);
282+
opts.put(ruby.newSymbol(Options.WARNINGS), false);
286283

287-
if (isOutFileOption() && !isOutputStdout()) {
288-
optionsBuilder.toFile(new File(this.outFile));
284+
if (this.backend != null) {
285+
attributes.setAttribute(Options.BACKEND, this.backend);
289286
}
290-
291-
if (isOutputStdout()) {
292-
optionsBuilder.toStream(System.out);
287+
if (this.doctype != null) {
288+
attributes.setAttribute(Options.DOCTYPE, this.doctype);
289+
}
290+
if (this.embedded) {
291+
opts.put(ruby.newSymbol(Options.STANDALONE), false);
292+
}
293+
if (this.outFile != null) {
294+
opts.put(ruby.newSymbol("output_file"), this.outFile);
293295
}
294-
295296
if (this.safe) {
296-
optionsBuilder.safe(SafeMode.SAFE);
297+
opts.put(ruby.newSymbol(Options.SAFE), SafeMode.SAFE.getLevel());
297298
}
298-
299-
if (this.embedded) {
300-
optionsBuilder.option(Options.STANDALONE, false);
299+
if (this.safeMode != null) {
300+
opts.put(ruby.newSymbol(Options.SAFE), this.safeMode.getLevel());
301301
}
302-
303302
if (this.noHeaderFooter) {
304-
optionsBuilder.option(Options.STANDALONE, false);
303+
opts.put(ruby.newSymbol(Options.STANDALONE), false);
305304
}
306-
307-
if (this.compact) {
308-
optionsBuilder.compact(this.compact);
305+
if (this.sectionNumbers) {
306+
attributes.setAttribute(Attributes.SECTION_NUMBERS, "");
309307
}
310-
311-
if (isBaseDirOption()) {
312-
optionsBuilder.baseDir(new File(this.baseDir).getCanonicalFile());
308+
if (this.eruby != null) {
309+
opts.put(ruby.newSymbol(Options.ERUBY), this.eruby);
310+
}
311+
if (isTemplateDirOption()) {
312+
opts.put(ruby.newSymbol(Options.TEMPLATE_DIRS), this.templateDir.toArray(new String[0]));
313313
}
314-
315314
if (isTemplateEngineOption()) {
316-
optionsBuilder.templateEngine(this.templateEngine);
315+
opts.put(ruby.newSymbol(Options.TEMPLATE_ENGINE), this.templateEngine);
317316
}
318-
319-
if (isTemplateDirOption()) {
320-
for (String templateDir : this.templateDir) {
321-
optionsBuilder.templateDirs(new File(templateDir).getCanonicalFile());
322-
}
317+
if (this.baseDir != null) {
318+
opts.put(ruby.newSymbol(Options.BASEDIR), this.baseDir);
323319
}
324-
325-
if (isDestinationDirOption() && !isOutputStdout()) {
326-
optionsBuilder.toDir(new File(this.destinationDir).getCanonicalFile());
327-
328-
if (isSourceDirOption()) {
329-
optionsBuilder.sourceDir(new File(this.sourceDir).getCanonicalFile());
330-
}
320+
if (this.destinationDir != null) {
321+
opts.put(ruby.newSymbol("destination_dir"), this.destinationDir);
331322
}
332-
333-
if (isInPlaceRequired()) {
334-
optionsBuilder.inPlace(true);
323+
if (this.trace) {
324+
opts.put(ruby.newSymbol(Options.TRACE), true);
335325
}
336-
337-
Attributes attributesBuilder = buildAttributes();
338-
if (this.sectionNumbers) {
339-
attributesBuilder.setSectionNumbers(this.sectionNumbers);
326+
if (this.timings) {
327+
opts.put(ruby.newSymbol(Options.TIMINGS), true);
328+
}
329+
if (this.warnings) {
330+
opts.put(ruby.newSymbol(Options.WARNINGS), true);
331+
}
332+
if (!attributes.isEmpty()) {
333+
opts.put(ruby.newSymbol(Options.ATTRIBUTES), attributes.map());
334+
}
335+
if (this.isSourceDirOption()) {
336+
opts.put(ruby.newSymbol("source_dir"), this.sourceDir);
340337
}
341-
optionsBuilder.attributes(attributesBuilder);
342-
return optionsBuilder.build();
338+
return opts;
343339
}
344340

345341
/**

0 commit comments

Comments
 (0)