"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/cli/invoker.rb" (1 Jun 2019, 5455 Bytes) of package /linux/www/asciidoctor-2.0.10.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Ruby source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 # frozen_string_literal: true
    2 module Asciidoctor
    3   module Cli
    4     # Public Invocation class for starting Asciidoctor via CLI
    5     class Invoker
    6       include Logging
    7 
    8       attr_reader :options
    9       attr_reader :documents
   10       attr_reader :code
   11 
   12       def initialize *options
   13         @documents = []
   14         @out = nil
   15         @err = nil
   16         @code = 0
   17         options = options.flatten
   18         case (first_option = options[0])
   19         when Options
   20           @options = first_option
   21         when ::Hash
   22           @options = Options.new options
   23         else
   24           if ::Integer === (result = Options.parse! options)
   25             @code = result
   26             @options = nil
   27           else
   28             @options = result
   29           end
   30         end
   31       end
   32 
   33       def invoke!
   34         return unless @options
   35 
   36         old_logger = old_logger_level = nil
   37         old_verbose, $VERBOSE = $VERBOSE, @options[:warnings]
   38         opts = {}
   39         infiles = []
   40         outfile = nil
   41         abs_srcdir_posix = nil
   42         non_posix_env = ::File::ALT_SEPARATOR == RS
   43         err = @err || $stderr
   44         show_timings = false
   45 
   46         @options.map do |key, val|
   47           case key
   48           when :input_files
   49             infiles = val
   50           when :output_file
   51             outfile = val
   52           when :source_dir
   53             if val
   54               abs_srcdir_posix = ::File.expand_path val
   55               abs_srcdir_posix = abs_srcdir_posix.tr RS, FS if non_posix_env && (abs_srcdir_posix.include? RS)
   56             end
   57           when :destination_dir
   58             opts[:to_dir] = val if val
   59           when :attributes
   60             # NOTE processor will dup attributes internally
   61             opts[:attributes] = val
   62           when :timings
   63             show_timings = val
   64           when :trace
   65             # no assignment
   66           when :verbose
   67             case val
   68             when 0
   69               $VERBOSE = nil
   70               old_logger, LoggerManager.logger = logger, NullLogger.new
   71             when 2
   72               old_logger_level, logger.level = logger.level, ::Logger::Severity::DEBUG
   73             end
   74           else
   75             opts[key] = val unless val.nil?
   76           end
   77         end
   78 
   79         if infiles.size == 1
   80           if (infile0 = infiles[0]) == '-'
   81             outfile ||= infile0
   82             stdin = true
   83           elsif ::File.pipe? infile0
   84             outfile ||= '-'
   85           end
   86         end
   87 
   88         if outfile == '-'
   89           # NOTE set_encoding returns nil on JRuby 9.1
   90           (tofile = @out) || ((tofile = $stdout).set_encoding UTF_8)
   91         elsif outfile
   92           opts[:mkdirs] = true
   93           tofile = outfile
   94         else
   95           opts[:mkdirs] = true
   96           # automatically calculate outfile based on infile
   97         end
   98 
   99         if stdin
  100           # allows use of block to supply stdin, particularly useful for tests
  101           # NOTE set_encoding returns nil on JRuby 9.1
  102           block_given? ? (input = yield) : ((input = $stdin).set_encoding UTF_8, UTF_8)
  103           input_opts = opts.merge to_file: tofile
  104           if show_timings
  105             @documents << (::Asciidoctor.convert input, (input_opts.merge timings: (timings = Timings.new)))
  106             timings.print_report err, '-'
  107           else
  108             @documents << (::Asciidoctor.convert input, input_opts)
  109           end
  110         else
  111           infiles.each do |infile|
  112             input_opts = opts.merge to_file: tofile
  113             if abs_srcdir_posix && (input_opts.key? :to_dir)
  114               abs_indir = ::File.dirname ::File.expand_path infile
  115               if non_posix_env
  116                 abs_indir_posix = (abs_indir.include? RS) ? (abs_indir.tr RS, FS) : abs_indir
  117               else
  118                 abs_indir_posix = abs_indir
  119               end
  120               if abs_indir_posix.start_with? %(#{abs_srcdir_posix}/)
  121                 input_opts[:to_dir] += abs_indir.slice abs_srcdir_posix.length, abs_indir.length
  122               end
  123             end
  124             if show_timings
  125               @documents << (::Asciidoctor.convert_file infile, (input_opts.merge timings: (timings = Timings.new)))
  126               timings.print_report err, infile
  127             else
  128               @documents << (::Asciidoctor.convert_file infile, input_opts)
  129             end
  130           end
  131         end
  132         @code = 1 if (logger.respond_to? :max_severity) && logger.max_severity && logger.max_severity >= opts[:failure_level]
  133       rescue ::Exception => e
  134         if ::SignalException === e
  135           @code = e.signo
  136           # add extra newline if Ctrl+C is used
  137           err.puts if ::Interrupt === e
  138         else
  139           @code = (e.respond_to? :status) ? e.status : 1
  140           if @options[:trace]
  141             raise e
  142           else
  143             err.puts ::RuntimeError === e ? %(#{e.message} (#{e.class})) : e.message
  144             err.puts '  Use --trace for backtrace'
  145           end
  146         end
  147         nil
  148       ensure
  149         $VERBOSE = old_verbose
  150         if old_logger
  151           LoggerManager.logger = old_logger
  152         elsif old_logger_level
  153           logger.level = old_logger_level
  154         end
  155       end
  156 
  157       def document
  158         @documents[0]
  159       end
  160 
  161       def redirect_streams out, err = nil
  162         @out = out
  163         @err = err
  164       end
  165 
  166       def read_output
  167         @out ? @out.string : ''
  168       end
  169 
  170       def read_error
  171         @err ? @err.string : ''
  172       end
  173 
  174       def reset_streams
  175         @out = nil
  176         @err = nil
  177       end
  178     end
  179   end
  180 end