Sha256: 2752f6e7707f80524533c859b9a5762ba715c008e98bfb80b4a8f774c99be483

Contents?: true

Size: 1.31 KB

Versions: 2

Compression:

Stored size: 1.31 KB

Contents

# frozen_string_literal: true

module Asciidoctor::Reducer
  class TreeProcessor < ::Asciidoctor::Extensions::TreeProcessor
    def process doc
      return if doc.options[:reduced]
      inc_replacements = doc.reader.x_include_replacements
      unless inc_replacements.length == 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[(index = it[:index])] == it[:replace]
          end
          lines = it[:lines]
          unless (drop = it[:drop]).empty?
            drop.reverse_each {|idx| ::Array === idx ? (lines[idx[0]] = idx[1]) : (lines.delete_at idx) }
          end
          target_lines[index] = lines if target_lines
        end
        source_lines = inc_replacements[0][:lines].flatten
        if doc.sourcemap
          # WARNING: if include directives remain that can still be resolved, the sourcemap won't match the source lines
          doc = ::Asciidoctor.load source_lines, (doc.options.merge reduced: true)
        else
          doc.reader.source_lines = source_lines
        end
      end
      doc
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
asciidoctor-reducer-1.0.0.alpha.4 lib/asciidoctor/reducer/tree_processor.rb
asciidoctor-reducer-1.0.0.alpha.3 lib/asciidoctor/reducer/tree_processor.rb