Skip to content

Commit

Permalink
Try to fix CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
robertpanzer committed Dec 2, 2023
1 parent e6298f0 commit 28fc7b5
Show file tree
Hide file tree
Showing 8 changed files with 152 additions and 119 deletions.
9 changes: 3 additions & 6 deletions asciidoctorj-api/src/main/java/org/asciidoctor/Options.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,13 @@
/**
* AsciidoctorJ conversion options.
* <p>
* See https://docs.asciidoctor.org/asciidoctor/latest/api/options/ for further
* See <a href="https://docs.asciidoctor.org/asciidoctor/latest/api/options/">https://docs.asciidoctor.org/asciidoctor/latest/api/options/</a> for further
* details.
*/
public class Options {

public static final String IN_PLACE = "in_place";
public static final String WARNINGS = "warnings";
public static final String ATTRIBUTES = "attributes";
public static final String TEMPLATE_DIRS = "template_dirs";
public static final String TEMPLATE_ENGINE = "template_engine";
Expand All @@ -24,10 +25,10 @@ public class Options {
public static final String ERUBY = "eruby";
public static final String CATALOG_ASSETS = "catalog_assets";
public static final String COMPACT = "compact";
public static final String SOURCE_DIR = "source_dir";
public static final String BACKEND = "backend";
public static final String DOCTYPE = "doctype";
public static final String BASEDIR = "base_dir";
public static final String TRACE = "trace";
public static final String TEMPLATE_CACHE = "template_cache";
public static final String SOURCE = "source";
public static final String PARSE = "parse";
Expand Down Expand Up @@ -160,10 +161,6 @@ public void setCompact(boolean compact) {
this.options.put(COMPACT, compact);
}

public void setSourceDir(String srcDir) {
this.options.put(SOURCE_DIR, srcDir);
}

public void setBackend(String backend) {
this.options.put(BACKEND, backend);
}
Expand Down
19 changes: 2 additions & 17 deletions asciidoctorj-api/src/main/java/org/asciidoctor/OptionsBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.io.File;
import java.io.OutputStream;
import java.util.Map;

/**
* Fluent Options API for AsciidoctorJ.
Expand Down Expand Up @@ -160,7 +159,7 @@ public OptionsBuilder toStream(OutputStream toStream) {
* @return this instance.
*/
public OptionsBuilder toDir(File directory) {
this.options.setToDir(directory.getAbsolutePath());
this.options.setToDir(directory.getPath());
return this;
}

Expand Down Expand Up @@ -265,20 +264,6 @@ public OptionsBuilder parseHeaderOnly(boolean parseHeaderOnly) {
return this;
}

/**
* Source directory.
*
* This must be used alongside {@link #toDir(File)}.
*
* @param srcDir
* source directory.
* @return this instance.
*/
public OptionsBuilder sourceDir(File srcDir) {
this.options.setSourceDir(srcDir.getAbsolutePath());
return this;
}

/**
* Sets a custom or unlisted option.
*
Expand All @@ -301,7 +286,7 @@ public OptionsBuilder option(String option, Object value) {
* @return this instance.
*/
public OptionsBuilder baseDir(File baseDir) {
this.options.setBaseDir(baseDir.getAbsolutePath());
this.options.setBaseDir(baseDir.getPath());
return this;
}

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

import com.beust.jcommander.Parameter;
import org.asciidoctor.*;
import org.asciidoctor.Attributes;
import org.asciidoctor.AttributesBuilder;
import org.asciidoctor.Options;
import org.asciidoctor.SafeMode;
import org.asciidoctor.log.Severity;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.StringTokenizer;

public class AsciidoctorCliOptions {

public static final String LOAD_PATHS = "-I";
public static final String REQUIRE = "-r";
public static final String QUIET = "-q";
public static final String WARNINGS = "-w";
public static final String ATTRIBUTE = "-a";
public static final String HELP = "-h";
public static final String DESTINATION_DIR = "-D";
Expand Down Expand Up @@ -47,7 +52,7 @@ public class AsciidoctorCliOptions {
private boolean version = false;

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

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

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

@Parameter(names = {COMPACT, "--compact"}, description = "compact the output by removing blank lines")
private boolean compact = false;
Expand Down Expand Up @@ -103,6 +108,9 @@ public class AsciidoctorCliOptions {
@Parameter(names = {QUIET, "--quiet"}, description = "suppress warnings")
private boolean quiet = false;

@Parameter(names = {WARNINGS, "--warnings"}, description = "suppress warnings")
private boolean warnings = false;

@Parameter(names = {"--failure-level"}, converter = SeverityConverter.class, description = "set minimum log level that yields a non-zero exit code.")
private Severity failureLevel = Severity.FATAL;

Expand Down Expand Up @@ -266,80 +274,64 @@ private boolean isInPlaceRequired() {
return !isOutFileOption() && !isDestinationDirOption() && !isOutputStdout();
}

public Options parse() throws IOException {
OptionsBuilder optionsBuilder = Options.builder()
.backend(this.backend)
.safe(this.safeMode)
.eruby(this.eruby)
.option(Options.STANDALONE, true);
public Map<String, Object> parse() throws IOException {

if (isDoctypeOption()) {
optionsBuilder.docType(this.doctype);
}
Options opts = Options.builder().build();
Attributes attributes = buildAttributes();

if (isInputStdin()) {
optionsBuilder.toStream(System.out);
if (outFile == null) {
outFile = "-";
}
}
opts.setOption(Options.STANDALONE, true);
opts.setOption(Options.WARNINGS, false);

if (isOutFileOption() && !isOutputStdout()) {
optionsBuilder.toFile(new File(this.outFile));
}

if (isOutputStdout()) {
optionsBuilder.toStream(System.out);
if (this.backend != null) {
opts.setBackend(this.backend);
}

if (this.safe) {
optionsBuilder.safe(SafeMode.SAFE);
if (this.doctype != null) {
opts.setDocType(this.doctype);
}

if (this.embedded) {
optionsBuilder.option(Options.STANDALONE, false);
opts.setOption(Options.STANDALONE, false);
}
if (this.outFile != null) {
opts.setToFile(this.outFile);
}
if (this.safe) {
opts.setSafe(SafeMode.SAFE);
}
if (this.safeMode != null) {
opts.setSafe(this.safeMode);
}

if (this.noHeaderFooter) {
optionsBuilder.option(Options.STANDALONE, false);
opts.setOption(Options.STANDALONE, false);
}

if (this.compact) {
optionsBuilder.compact(this.compact);
if (this.sectionNumbers) {
attributes.setAttribute("sectnums", "");
}

if (isBaseDirOption()) {
optionsBuilder.baseDir(new File(this.baseDir).getCanonicalFile());
if (this.eruby != null) {
opts.setEruby(this.eruby);
}
if (isTemplateDirOption()) {
opts.setTemplateDirs(this.templateDir.toArray(new String[0]));
}

if (isTemplateEngineOption()) {
optionsBuilder.templateEngine(this.templateEngine);
opts.setTemplateEngine(this.templateEngine);
}

if (isTemplateDirOption()) {
for (String templateDir : this.templateDir) {
optionsBuilder.templateDirs(new File(templateDir).getCanonicalFile());
}
if (this.baseDir != null) {
opts.setBaseDir(this.baseDir);
}

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

if (isSourceDirOption()) {
optionsBuilder.sourceDir(new File(this.sourceDir).getCanonicalFile());
}
if (this.destinationDir != null) {
opts.setOption(Options.TO_DIR, this.destinationDir);
}

if (isInPlaceRequired()) {
optionsBuilder.inPlace(true);
if (this.trace) {
opts.setOption(Options.TRACE, true);
}

Attributes attributesBuilder = buildAttributes();
if (this.sectionNumbers) {
attributesBuilder.setSectionNumbers(this.sectionNumbers);
if (this.warnings) {
opts.setOption(Options.WARNINGS, true);
}
if (!attributes.map().isEmpty()) {
opts.setAttributes(attributes);
}
optionsBuilder.attributes(attributesBuilder);
return optionsBuilder.build();
return opts.map();
}

/**
Expand Down
Loading

0 comments on commit 28fc7b5

Please sign in to comment.