summaryrefslogtreecommitdiffstats
path: root/tools/asciidoctor-includetracker.rb
blob: bcf9ce0302246acfb1e598fe32f13ae0803d9e16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# Copyright (C) 2023 Thomas Weißschuh <thomas@t-8ch.de>
# Extensions for asciidoctor to write dependency files for include directives.

require 'asciidoctor/extensions'

module IncludeTracker

  class Preprocessor < Asciidoctor::Extensions::Preprocessor
    def process document, reader
      document.attributes["include_dependencies"] = []
      reader
    end
  end

  class IncludeProcessor < Asciidoctor::Extensions::IncludeProcessor
    def process doc, reader, target, attributes
      docdir = doc.attributes["docdir"]
      path = target
      file = File.expand_path path, docdir
      data = File.read file
      reader.push_include data, file, path, 1, attributes
      doc.attributes["include_dependencies"] << file
      reader
    end
  end

  class Postprocessor < Asciidoctor::Extensions::Postprocessor
    def process document, output
      outfile = document.attributes["outfile"]
      fail if !outfile
      File.open outfile + '.deps', 'w' do |f|
        f.write outfile + ": " +
          document.attributes["include_dependencies"].join(' ')
      end
      output
    end
  end

  Asciidoctor::Extensions.register do
    preprocessor Preprocessor
    include_processor IncludeProcessor
    postprocessor Postprocessor
  end

end