class Asciidoctor::Cli::Invoker

Public Invocation class for starting Asciidoctor via CLI

Attributes

code[R]
document[R]
options[R]

Public Class Methods

new(*options) click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 9
def initialize(*options)
  @document = nil
  @out = nil
  @err = nil
  @code = 0
  options = options.flatten
  if !options.empty? && options.first.is_a?(Cli::Options)
    @options = options.first
  elsif options.first.is_a? Hash
    @options = Cli::Options.new(options)
  else
    @options = Cli::Options.parse!(options)
    # hmmm
    if @options.is_a?(Integer)
      @code = @options
      @options = nil
    end
  end
end

Public Instance Methods

invoke!() { |: STDIN| ... } click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 29
def invoke!
  return if @options.nil?

  begin
    opts = {}
    monitor = {}
    infile = nil
    outfile = nil
    @options.map {|k, v|
      case k
      when :input_file
        infile = v
      when :output_file
        outfile = v
      when :destination_dir
        #opts[:to_dir] = File.expand_path(v) unless v.nil?
        opts[:to_dir] = v unless v.nil?
      when :attributes
        opts[:attributes] = v.dup
      when :verbose
        opts[:monitor] = monitor if v
      when :trace
        # currently, nothing
      else
        opts[k] = v unless v.nil?
      end
    }

    if infile == '-'
      # allow use of block to supply stdin, particularly useful for tests
      input = block_given? ? yield : STDIN
    else
      input = File.new(infile)
    end

    if outfile == '-' || (infile == '-' && (outfile.to_s.empty? || outfile != '/dev/null'))
      opts[:to_file] = (@out || $stdout)
    elsif !outfile.nil?
      opts[:to_file] = outfile
    else
      opts[:in_place] = true unless opts.has_key? :to_dir
    end

    @document = Asciidoctor.render(input, opts)

    # FIXME this should be :monitor, :profile or :timings rather than :verbose
    if @options[:verbose]
      puts "Time to read and parse source: #{'%05.5f' % monitor[:parse]}"
      puts "Time to render document: #{'%05.5f' % monitor[:render]}"
      puts "Total time to read, parse and render: #{'%05.5f' % monitor[:load_render]}"
    end
  rescue Exception => e
    raise e if @options[:trace] || SystemExit === e
    err = (@err || $stderr)
    err.print "#{e.class}: " if e.class != RuntimeError
    err.puts e.message
    err.puts '  Use --trace for backtrace'
    @code = 1
  end
end
read_error() click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 99
def read_error
  !@err.nil? ? @err.string : ''
end
read_output() click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 95
def read_output
  !@out.nil? ? @out.string : ''
end
redirect_streams(out, err = nil) click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 90
def redirect_streams(out, err = nil)
  @out = out
  @err = err
end
reset_streams() click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 103
def reset_streams
  @out = nil
  @err = nil
end