Class: Asciidoctor::IncludeExt::IncludeProcessor

Inherits:
Extensions::IncludeProcessor
  • Object
show all
Defined in:
lib/asciidoctor/include_ext/include_processor.rb

Overview

Asciidoctor preprocessor for processing `include::<target>[]` directives in the source document.

Instance Method Summary collapse

Constructor Details

#initialize(selectors: [LinenoLinesSelector, TagLinesSelector], logger: Logging.default_logger) ⇒ IncludeProcessor

Returns a new instance of IncludeProcessor.

Parameters:

  • selectors (Array<Class>) (defaults to: [LinenoLinesSelector, TagLinesSelector])

    an array of selectors that can filter specified portions of the document to include (see <asciidoctor.org/docs/user-manual#include-partial>).

  • logger (Logger) (defaults to: Logging.default_logger)

    the logger to use for logging warning and errors from this object and selectors.



26
27
28
29
30
31
# File 'lib/asciidoctor/include_ext/include_processor.rb', line 26

def initialize(selectors: [LinenoLinesSelector, TagLinesSelector],
               logger: Logging.default_logger, **)
  super
  @selectors = selectors.dup.freeze
  @logger = logger
end

Instance Method Details

#process(_, reader, target, attributes) ⇒ Object

Parameters:

  • reader (Asciidoctor::Reader)
  • target (String)

    name of the source file to include as specified in the target slot of the `include::[]` directive.

  • attributes (Hash<String, String>)

    parsed attributes of the `include::[]` directive.



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/asciidoctor/include_ext/include_processor.rb', line 38

def process(_, reader, target, attributes)
  unless include_allowed? target, reader
    reader.unshift_line("link:#{target}[]")
    return
  end

  if (max_depth = reader.exceeded_max_depth?)
    logger.error "#{reader.line_info}: maximum include depth of #{max_depth} exceeded"
    return
  end

  unless (path = resolve_target_path(target, reader))
    if attributes.key? 'optional-option'
      reader.shift
    else
      logger.error "#{reader.line_info}: include target not found: #{target}"
      unresolved_include!(target, reader)
    end
    return
  end

  selector = lines_selector_for(target, attributes)
  begin
    lines = read_lines(path, selector)
  rescue => e  # rubocop:disable RescueWithoutErrorClass
    logger.error "#{reader.line_info}: failed to read include file: #{path}: #{e}"
    unresolved_include!(target, reader)
    return
  end

  if selector && selector.respond_to?(:first_included_lineno)
    incl_offset = selector.first_included_lineno
  end

  unless lines.empty?
    reader.push_include(lines, path, target, incl_offset || 1, attributes)
  end
end