Module: Asciidoctor::Reducer::IncludeDirectiveTracker

Defined in:
lib/asciidoctor/reducer/include_directive_tracker.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.extended(instance) ⇒ Object



5
6
7
8
9
# File 'lib/asciidoctor/reducer/include_directive_tracker.rb', line 5

def self.extended instance
  instance.singleton_class.send :attr_reader, :include_replacements
  instance.instance_variable_set :@include_replacements, ([{}].extend CurrentPosition)
  instance.instance_variable_set :@x_reducer, {}
end

Instance Method Details

#preprocess_include_directive(target, attrlist) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/asciidoctor/reducer/include_directive_tracker.rb', line 11

def preprocess_include_directive target, attrlist
  @x_reducer[:include_directive_line] = %(include::#{target}[#{attrlist}])
  @x_reducer[:include_pushed] = false
  directive_lineno = @lineno # we're currently on the include line, which is 1-based
  result = super
  unless @x_reducer[:include_pushed]
    if ((ln = peek_line true)&.end_with? ']') && !(unresolved = ln.start_with? 'Unresolved directive in ') &&
        directive_lineno == @lineno && (unresolved = ln.start_with? 'link:') && !(ln.include? '[role=')
      ln = ln.sub '[', %([role=include#{ln[-2] == '[' ? '' : ','})
    end
    push_include_replacement directive_lineno, (unresolved ? [ln] : []), 0, unresolved
  end
  @x_reducer.clear
  result
end

#push_include(data, file, path, lineno, attrs) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/asciidoctor/reducer/include_directive_tracker.rb', line 27

def push_include data, file, path, lineno, attrs
  @x_reducer[:include_pushed] = true
  directive_lineno = @lineno - 1 # we're below the include line, which is 1-based
  prev_inc_depth = @include_stack.size
  offset = lineno > 1 ? lineno - 1 : 0
  result = super
  if @include_stack.size > prev_inc_depth
    inc_lines = lines
    offset -= 2 if (attrs.key? 'leveloffset') && (inc_lines[0].start_with? ':leveloffset: ') && inc_lines[1]&.empty?
  end
  push_include_replacement directive_lineno, inc_lines || [], offset
  result
end