Sha256: 4286baacc600d22d6c37986d1f4771bcf804a10b57e0ded935c3b686a93e9838

Contents?: true

Size: 1.7 KB

Versions: 1

Compression:

Stored size: 1.7 KB

Contents

# frozen_string_literal: true

module Asciidoctor::Reducer
  class TreeProcessor < ::Asciidoctor::Extensions::TreeProcessor
    def process doc
      if (inc_replacements = doc.reader.include_replacements).size > 1 || !(inc_replacements[0][:drop] || []).empty?
        inc_replacements[0][:lines] = doc.source_lines.dup
        inc_replacements.reverse_each do |it|
          if (into = it[:into])
            target_lines = inc_replacements[into][:lines]
            # adds extra assurance that we're replacing the correct line
            next unless target_lines[(idx = it[:lineno] - 1)] == it[:line]
          end
          lines = it[:lines]
          unless (drop = it[:drop] || []).empty?
            drop.reverse_each do |drop_it|
              ::Array === drop_it ? (lines[drop_it[0] - 1] = drop_it[1]) : (lines.delete_at drop_it - 1)
            end
          end
          target_lines[idx] = lines if target_lines
        end
        reduced_source_lines = inc_replacements[0][:lines].flatten
        if doc.sourcemap
          logger = ::Asciidoctor::LoggerManager.logger
          opts = doc.options.merge logger: nil, parse: false, reduced: true
          if (ext_reg = opts[:extension_registry])
            opts[:extension_registry] = ::Asciidoctor::Extensions::Registry.new ext_reg.groups
          end
          includes = doc.catalog[:includes]
          doc = ::Asciidoctor.load reduced_source_lines, opts
          doc.catalog[:includes] = includes
          doc.parse
          ::Asciidoctor::LoggerManager.logger = logger
        else
          reduced_source_lines.pop while reduced_source_lines[-1] == ''
          doc.source_lines.replace reduced_source_lines
        end
      end
      doc
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
asciidoctor-reducer-1.0.2 lib/asciidoctor/reducer/tree_processor.rb