class Asciidoctor::Cli::Invoker

Public Invocation class for starting Asciidoctor via CLI

Attributes

code[R]
documents[R]
options[R]

Public Class Methods

new(*options) click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 9
def initialize(*options)
  @documents = []
  @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

document() click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 107
def document
  @documents.size > 0 ? @documents.first : nil
end
invoke!() { |: STDIN]| ... } click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 29
def invoke!
  return if @options.nil?

  begin
    opts = {}
    profile = false
    infiles = []
    outfile = nil
    tofile = nil
    @options.map {|k, v|
      case k
      when :input_files
        infiles = 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
        profile = true if v
      when :trace
        # currently, nothing
      else
        opts[k] = v unless v.nil?
      end
    }

    if infiles.size == 1 && infiles.first == '-'
       # allows use of block to supply stdin, particularly useful for tests
       inputs = [block_given? ? yield : STDIN]
    else
       inputs = infiles.map {|infile| File.new infile}
    end

    # NOTE: if infile is stdin, default to outfile as stout
    if outfile == '-' || (infiles.size == 1 && infiles.first == '-' && outfile.to_s.empty?)
      tofile = (@out || $stdout)
    elsif !outfile.nil?
      tofile = outfile
      opts[:mkdirs] = true
    else
      tofile = nil
      # automatically calculate outfile based on infile
      opts[:in_place] = true unless opts.has_key? :to_dir
      opts[:mkdirs] = true
    end

    original_opts = opts
    inputs.each do |input|
      
      opts = Helpers.clone_options(original_opts) if inputs.size > 1
      opts[:to_file] = tofile unless tofile.nil?
      opts[:monitor] = {} if profile

      @documents ||= []
      @documents.push Asciidoctor.render(input, opts)

      if profile
        monitor = opts[:monitor]
        err = (@err || $stderr)
        err.puts "Input file: #{input.respond_to?(:path) ? input.path : '-'}"
        err.puts "  Time to read and parse source: #{'%05.5f' % monitor[:parse]}"
        err.puts "  Time to render document: #{monitor.has_key?(:render) ? '%05.5f' % monitor[:render] : 'n/a'}"
        err.puts "  Total time to read, parse and render: #{'%05.5f' % (monitor[:load_render] || monitor[:parse])}"
      end
    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 120
def read_error
  !@err.nil? ? @err.string : ''
end
read_output() click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 116
def read_output
  !@out.nil? ? @out.string : ''
end
redirect_streams(out, err = nil) click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 111
def redirect_streams(out, err = nil)
  @out = out
  @err = err
end
reset_streams() click to toggle source
# File lib/asciidoctor/cli/invoker.rb, line 124
def reset_streams
  @out = nil
  @err = nil
end