Skip to content

Commit

Permalink
resolves #566 add CLI option to specify a config file containing API …
Browse files Browse the repository at this point in the history
…options
  • Loading branch information
mojavelinux committed Oct 10, 2023
1 parent d26821f commit 79f9ffb
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ Enhancements::
* Honor `level` and `formatter` keyword arguments passed to Logger constructor (#4250)
* Set logdev to $stderr if no arguments are passed to Logger constructor (#4250)
* Add support for skipping TOML front matter (Hugo) when `skip-front-matter` attribute is set (#4300) (*@abhinav*)
* Add `-c, --config-file` option to CLI to specify a config file containing API options (#566)

Compliance::

Expand Down
19 changes: 19 additions & 0 deletions lib/asciidoctor/cli/invoker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,25 @@ def invoke!
# NOTE in Ruby 2.7, RubyGems sets SOURCE_DATE_EPOCH if it's not set
::ENV.delete 'SOURCE_DATE_EPOCH' if (::ENV.key? 'IGNORE_SOURCE_DATE_EPOCH') && (::Gem.respond_to? :source_date_epoch)

if (config_file = @options[:config_file])
require 'yaml'
if ::Hash === (config_data = ::YAML.load_file config_file)
config_data = config_data['asciidoc'] if ::Hash === config_data['asciidoc']
config_data.each do |opt_name, opt_val|
case (opt_name = opt_name.to_sym)
when :attributes
if ::Hash === opt_val
opt_val.each_with_object(@options[:attributes] ||= {}) {|(n, v), accum| accum[n] = v }
end
when :input_file, :output_file, :source_dir, :destination_dir
# ignore
else
@options[opt_name] = opt_val
end
end
end
end

@options.map do |key, val|
case key
when :input_files
Expand Down
4 changes: 4 additions & 0 deletions lib/asciidoctor/cli/options.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module Cli
class Options < ::Hash
def initialize options = {}
self[:attributes] = options[:attributes] || {}
self[:config_file] = options[:config_file]
self[:input_files] = options[:input_files]
self[:output_file] = options[:output_file]
self[:safe] = options[:safe] || SafeMode::UNSAFE
Expand Down Expand Up @@ -99,6 +100,9 @@ def parse! args
name, _, val = attr.partition '='
self[:attributes][name] = val
end
opts.on '-c', '--config-file FILE', 'a YAML file that contains API options defined as key-value pairs' do |config_file|
self[:config_file] = config_file
end
opts.on '-T', '--template-dir DIR', 'a directory containing custom converter templates that override the built-in converter (requires tilt gem)',
'may be specified multiple times' do |template_dir|
if self[:template_dirs].nil?
Expand Down
3 changes: 3 additions & 0 deletions test/fixtures/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
asciidoc:
attributes:
toc: ''
14 changes: 14 additions & 0 deletions test/invoker_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,20 @@
assert_equal 'asciidoctor: INFO: possible invalid reference: install', output.chomp
end

test 'should allow API options to be passed from config file' do
input_path = fixture_path 'sample.adoc'
config_path = fixture_path 'config.yml'
output = run_command(asciidoctor_cmd, '-c', config_path, '-o', '-', input_path) {|out| out.read }
assert_includes output, '<div id="toc" class="toc">'
end

test 'should merge attributes from config file with attributes passed as CLI options' do
input_path = fixture_path 'sample.adoc'
config_path = fixture_path 'config.yml'
output = run_command(asciidoctor_cmd, '-c', config_path, '-a', 'toc-class=interactive-toc', '-o', '-', input_path) {|out| out.read }
assert_includes output, '<div id="toc" class="interactive-toc">'
end

test 'should not log when --log-level and -q are both specified' do
input = <<~'EOS'
skip to <<install>>
Expand Down

0 comments on commit 79f9ffb

Please sign in to comment.