Sha256: eb180be604985832f6e5c0b73b22d2302faab8f81e51f58a8b25f3d59a44dd19

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 bit of 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.1 lib/asciidoctor/reducer/tree_processor.rb