Sha256: d0edc8b8da03cadd4966814d704c79736f2a34ac94b4ec18a1756cdd845e858f

Contents?: true

Size: 1.68 KB

Versions: 5

Compression:

Stored size: 1.68 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
        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 source_lines, opts
          doc.catalog[:includes] = includes
          doc.parse
          ::Asciidoctor::LoggerManager.logger = logger
        else
          source_lines.pop while (source_lines[-1] || :eof).empty?
          doc.reader.source_lines = source_lines
        end
      end
      doc
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

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