-
Notifications
You must be signed in to change notification settings - Fork 101
/
sectnumoffset-treeprocessor.rb
30 lines (28 loc) · 1.17 KB
/
sectnumoffset-treeprocessor.rb
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
require 'asciidoctor/extensions' unless RUBY_ENGINE == 'opal'
include Asciidoctor
Extensions.register do
# A treeprocessor that increments each level-1 section number by the value of
# the `sectnumoffset` attribute. The numbers of all subsections will be
# incremented automatically since those values are calculated dynamically.
#
# Run using:
#
# asciidoctor -r ./lib/sectnumoffset-treeprocessor.rb -a sectnums -a sectnumoffset=1 lib/sectnumoffset-treeprocessor/sample.adoc
#
treeprocessor do
process do |document|
if (document.attr? 'sectnums') && (sectnumoffset = (document.attr 'sectnumoffset', 0).to_i) != 0
if sectnumoffset > 0
document.find_by(context: :section) {|sect| sect.level == 1 && sect.numbered }.each do |sect|
sectnumoffset.times { sect.numeral = sect.numeral.next } rescue (sect.number += sectnumoffset)
end
else
document.find_by(context: :section) {|sect| sect.level == 1 && sect.numbered }.each do |sect|
sectnumoffset.abs.times { sect.numeral = sect.numeral.to_i.pred.to_s } rescue (sect.number += sectnumoffset)
end
end
end
nil
end
end
end