Sha256: 72ed5671c1e977d4241a0c81b757acf848f377446d838a5f7e42a3f57f490793

Contents?: true

Size: 1.32 KB

Versions: 3

Compression:

Stored size: 1.32 KB

Contents

# frozen_string_literal: true

autoload :Pathname, 'pathname'
require_relative 'extensions'

module Asciidoctor::Reducer
  autoload :VERSION, (::File.join __dir__, 'version.rb')

  class << self
    def reduce input, opts = {}
      opts = opts&.merge || {}
      if (extension_registry = Extensions.prepare_registry opts[:extension_registry] || opts[:extensions])
        opts[:extension_registry] = extension_registry
      end
      opts[:safe] ||= :safe
      to = opts.delete :to
      case input
      when ::File
        doc = ::Asciidoctor.load_file input, opts
      when ::Pathname
        doc = ::Asciidoctor.load_file input.to_path, opts
      else
        doc = ::Asciidoctor.load input, opts
      end
      write doc, to
    end

    def reduce_file input_file, opts = {}
      reduce (::Pathname.new input_file), opts
    end

    private

    def write doc, to
      return doc unless to && to != '/dev/null'
      output = doc.source
      return output if to == ::String
      output += LF unless output.empty?
      if ::Pathname === to || (!(to.respond_to? :write) && (to = ::Pathname.new to.to_s))
        to.dirname.mkpath
        to.write output, encoding: UTF_8, newline: :universal
      else
        to.write output
      end
      doc
    end
  end

  LF = ?\n
  UTF_8 = ::Encoding::UTF_8

  private_constant :LF, :UTF_8
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
asciidoctor-reducer-1.0.0-beta.1 lib/asciidoctor/reducer/api.rb
asciidoctor-reducer-1.0.0.alpha.10 lib/asciidoctor/reducer/api.rb
asciidoctor-reducer-1.0.0.alpha.9 lib/asciidoctor/reducer/api.rb