Class: Asciidoctor::IndirExt::IndirIncludeProcessor

Inherits:
Asciidoctor::IncludeExt::IncludeProcessor
  • Object
show all
Defined in:
lib/asciidoctor/indir_ext/extension.rb

Overview

Asciidoctor extension that adds a variable “indir”, pointing at the directory of included asciidoc files.

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ IndirIncludeProcessor

Returns a new instance of IndirIncludeProcessor.



8
9
10
11
12
# File 'lib/asciidoctor/indir_ext/extension.rb', line 8

def initialize(*args, &block)
  # temporary storage helper that won't be frozen by Asciidoctor
  @tmp = { }
  super()
end

Instance Method Details

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



14
15
16
17
18
# File 'lib/asciidoctor/indir_ext/extension.rb', line 14

def process(document, reader, target, attributes)
  @tmp[:document] = document
  @tmp[:reader] = reader
  super
end

#read_lines(filename, selector) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/asciidoctor/indir_ext/extension.rb', line 20

def read_lines(filename, selector)
  # read content using functionality from super
  content = super(filename, selector)

  # Ignore non-asciidoc files
  if not ['.asciidoc', '.adoc', '.ad', '.asc', '.txt'].include? File.extname(filename) then return content end

  # split content into a list of lines if it has been provided as string
  if content.is_a? String then content = content.lines end

  # Set variables at the beginning of the included content
  included_docfile = filename
  included_docdir = ::File.dirname filename
  content.unshift ''
  content.unshift %(:indir: #{included_docdir})

  # Reset the variables at the end of the included content
  parent_docfile = @tmp[:reader].include_stack&.dig(-1, 1) || (@tmp[:document].attr 'docfile')
  parent_docdir = ::File.dirname parent_docfile
  content << ''
  content << %(:indir: #{parent_docdir})

  return content
end