"Fossies" - the Fresh Open Source Software Archive

Member "asciidoctor-2.0.10/lib/asciidoctor/load.rb" (1 Jun 2019, 4428 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 module Asciidoctor
    2   module_function
    3 
    4   # Public: Parse the AsciiDoc source input into a {Document}
    5   #
    6   # Accepts input as an IO (or StringIO), String or String Array object. If the
    7   # input is a File, the object is expected to be opened for reading and is not
    8   # closed afterwards by this method. Information about the file (filename,
    9   # directory name, etc) gets assigned to attributes on the Document object.
   10   #
   11   # input   - the AsciiDoc source as a IO, String or Array.
   12   # options - a String, Array or Hash of options to control processing (default: {})
   13   #           String and Array values are converted into a Hash.
   14   #           See {Document#initialize} for details about these options.
   15   #
   16   # Returns the Document
   17   def load input, options = {}
   18     options = options.merge
   19 
   20     if (timings = options[:timings])
   21       timings.start :read
   22     end
   23 
   24     if (logger = options[:logger]) && logger != LoggerManager.logger
   25       LoggerManager.logger = logger
   26     end
   27 
   28     if !(attrs = options[:attributes])
   29       attrs = {}
   30     elsif ::Hash === attrs
   31       attrs = attrs.merge
   32     elsif (defined? ::Java::JavaUtil::Map) && ::Java::JavaUtil::Map === attrs
   33       attrs = attrs.dup
   34     elsif ::Array === attrs
   35       attrs = {}.tap do |accum|
   36         attrs.each do |entry|
   37           k, _, v = entry.partition '='
   38           accum[k] = v
   39         end
   40       end
   41     elsif ::String === attrs
   42       # condense and convert non-escaped spaces to null, unescape escaped spaces, then split on null
   43       attrs = {}.tap do |accum|
   44         attrs.gsub(SpaceDelimiterRx, '\1' + NULL).gsub(EscapedSpaceRx, '\1').split(NULL).each do |entry|
   45           k, _, v = entry.partition '='
   46           accum[k] = v
   47         end
   48       end
   49     elsif (attrs.respond_to? :keys) && (attrs.respond_to? :[])
   50       # coerce attrs to a real Hash
   51       attrs = {}.tap {|accum| attrs.keys.each {|k| accum[k] = attrs[k] } }
   52     else
   53       raise ::ArgumentError, %(illegal type for attributes option: #{attrs.class.ancestors.join ' < '})
   54     end
   55 
   56     if ::File === input
   57       options[:input_mtime] = input.mtime
   58       # NOTE defer setting infile and indir until we get a better sense of their purpose
   59       # TODO cli checks if input path can be read and is file, but might want to add check to API too
   60       attrs['docfile'] = input_path = ::File.absolute_path input.path
   61       attrs['docdir'] = ::File.dirname input_path
   62       attrs['docname'] = Helpers.basename input_path, (attrs['docfilesuffix'] = Helpers.extname input_path)
   63       source = input.read
   64     elsif input.respond_to? :read
   65       # NOTE tty, pipes & sockets can't be rewound, but can't be sniffed easily either
   66       # just fail the rewind operation silently to handle all cases
   67       input.rewind rescue nil
   68       source = input.read
   69     elsif ::String === input
   70       source = input
   71     elsif ::Array === input
   72       source = input.drop 0
   73     elsif input
   74       raise ::ArgumentError, %(unsupported input type: #{input.class})
   75     end
   76 
   77     if timings
   78       timings.record :read
   79       timings.start :parse
   80     end
   81 
   82     options[:attributes] = attrs
   83     doc = options[:parse] == false ? (Document.new source, options) : (Document.new source, options).parse
   84 
   85     timings.record :parse if timings
   86     doc
   87   rescue => ex
   88     begin
   89       context = %(asciidoctor: FAILED: #{attrs['docfile'] || '<stdin>'}: Failed to load AsciiDoc document)
   90       if ex.respond_to? :exception
   91         # The original message must be explicitly preserved when wrapping a Ruby exception
   92         wrapped_ex = ex.exception %(#{context} - #{ex.message})
   93         # JRuby automatically sets backtrace; MRI did not until 2.6
   94         wrapped_ex.set_backtrace ex.backtrace
   95       else
   96         # Likely a Java exception class
   97         wrapped_ex = ex.class.new context, ex
   98         wrapped_ex.stack_trace = ex.stack_trace
   99       end
  100     rescue
  101       wrapped_ex = ex
  102     end
  103     raise wrapped_ex
  104   end
  105 
  106   # Public: Parse the contents of the AsciiDoc source file into an Asciidoctor::Document
  107   #
  108   # input   - the String AsciiDoc source filename
  109   # options - a String, Array or Hash of options to control processing (default: {})
  110   #           String and Array values are converted into a Hash.
  111   #           See Asciidoctor::Document#initialize for details about options.
  112   #
  113   # Returns the Asciidoctor::Document
  114   def load_file filename, options = {}
  115     ::File.open(filename, FILE_READ_MODE) {|file| load file, options }
  116   end
  117 end