1
1
package org .asciidoctor .cli ;
2
2
3
3
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 ;
5
8
import org .asciidoctor .log .Severity ;
9
+ import org .jruby .Ruby ;
10
+ import org .jruby .RubyHash ;
6
11
7
12
import java .io .File ;
8
- import java .io .IOException ;
9
13
import java .util .ArrayList ;
10
14
import java .util .Collections ;
11
15
import java .util .List ;
@@ -16,6 +20,7 @@ public class AsciidoctorCliOptions {
16
20
public static final String LOAD_PATHS = "-I" ;
17
21
public static final String REQUIRE = "-r" ;
18
22
public static final String QUIET = "-q" ;
23
+ public static final String WARNINGS = "-w" ;
19
24
public static final String ATTRIBUTE = "-a" ;
20
25
public static final String HELP = "-h" ;
21
26
public static final String DESTINATION_DIR = "-D" ;
@@ -35,7 +40,6 @@ public class AsciidoctorCliOptions {
35
40
public static final String VERBOSE = "-v" ;
36
41
public static final String TIMINGS = "-t" ;
37
42
public static final char ATTRIBUTE_SEPARATOR = '=' ;
38
- public static final String TIMINGS_OPTION_NAME = "timings" ;
39
43
40
44
@ Parameter (names = {VERBOSE , "--verbose" }, description = "enable verbose mode" )
41
45
private boolean verbose = false ;
@@ -47,7 +51,7 @@ public class AsciidoctorCliOptions {
47
51
private boolean version = false ;
48
52
49
53
@ Parameter (names = {BACKEND , "--backend" }, description = "set output format backend" )
50
- private String backend = "html5" ;
54
+ private String backend ;
51
55
52
56
@ Parameter (names = {DOCTYPE , "--doctype" }, description = "document type to use when rendering output: [article, book, inline] (default: article)" )
53
57
private String doctype ;
@@ -70,8 +74,8 @@ public class AsciidoctorCliOptions {
70
74
@ Parameter (names = {SECTION_NUMBERS , "--section-numbers" }, description = "auto-number section titles; disabled by default" )
71
75
private boolean sectionNumbers = false ;
72
76
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 ;
75
79
76
80
@ Parameter (names = {COMPACT , "--compact" }, description = "compact the output by removing blank lines" )
77
81
private boolean compact = false ;
@@ -103,6 +107,9 @@ public class AsciidoctorCliOptions {
103
107
@ Parameter (names = {QUIET , "--quiet" }, description = "suppress warnings" )
104
108
private boolean quiet = false ;
105
109
110
+ @ Parameter (names = {WARNINGS , "--warnings" }, description = "suppress warnings" )
111
+ private boolean warnings = false ;
112
+
106
113
@ Parameter (names = {"--failure-level" }, converter = SeverityConverter .class , description = "set minimum log level that yields a non-zero exit code." )
107
114
private Severity failureLevel = Severity .FATAL ;
108
115
@@ -266,80 +273,69 @@ private boolean isInPlaceRequired() {
266
273
return !isOutFileOption () && !isDestinationDirOption () && !isOutputStdout ();
267
274
}
268
275
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 ) {
275
277
276
- if (isDoctypeOption ()) {
277
- optionsBuilder .docType (this .doctype );
278
- }
278
+ RubyHash opts = new RubyHash (ruby );
279
+ Attributes attributes = buildAttributes ();
279
280
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 );
286
283
287
- if (isOutFileOption () && ! isOutputStdout () ) {
288
- optionsBuilder . toFile ( new File ( this .outFile ) );
284
+ if (this . backend != null ) {
285
+ attributes . setAttribute ( Options . BACKEND , this .backend );
289
286
}
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 );
293
295
}
294
-
295
296
if (this .safe ) {
296
- optionsBuilder . safe ( SafeMode .SAFE );
297
+ opts . put ( ruby . newSymbol ( Options . SAFE ), SafeMode .SAFE . getLevel () );
297
298
}
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 ());
301
301
}
302
-
303
302
if (this .noHeaderFooter ) {
304
- optionsBuilder . option ( Options .STANDALONE , false );
303
+ opts . put ( ruby . newSymbol ( Options .STANDALONE ) , false );
305
304
}
306
-
307
- if (this .compact ) {
308
- optionsBuilder .compact (this .compact );
305
+ if (this .sectionNumbers ) {
306
+ attributes .setAttribute (Attributes .SECTION_NUMBERS , "" );
309
307
}
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 ]));
313
313
}
314
-
315
314
if (isTemplateEngineOption ()) {
316
- optionsBuilder . templateEngine ( this .templateEngine );
315
+ opts . put ( ruby . newSymbol ( Options . TEMPLATE_ENGINE ), this .templateEngine );
317
316
}
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 );
323
319
}
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 );
331
322
}
332
-
333
- if (isInPlaceRequired ()) {
334
- optionsBuilder .inPlace (true );
323
+ if (this .trace ) {
324
+ opts .put (ruby .newSymbol (Options .TRACE ), true );
335
325
}
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 );
340
337
}
341
- optionsBuilder .attributes (attributesBuilder );
342
- return optionsBuilder .build ();
338
+ return opts ;
343
339
}
344
340
345
341
/**
0 commit comments