A lab for testing and demonstrating Asciidoctor extensions. Please do not use this code in production. If you want to use one of these extensions in your application, create a new project, import the code, and distribute it as a RubyGem. You can then request to make it a top-level project under the Asciidoctor organization.
In the example below a modification to sectnumoffset-treeprocessor.rb is used to offset subsection
numbers in the first section that is processed. I would like to insert a parent section at the head
of the document and eliminate the text == Outer Test\n so that the first line of the
HTML output is
<h3 id="_inner_test_a">2.3. Inner Test A</h3>
Help is needed from the Ascii Doctor.
EXAMPLE:
require 'asciidoctor-latex'
source = "== Outer Test\n=== Inner Test A\n$a^2 = 1$\n\n=== Inner Test B\nfoo\n\n== Outer again\nho ho ho!\n\n=== Yikes!"
puts Asciidoctor.convert(source, backend: 'html5', attributes: 'sectnums sectnumoffset=1 subsectnumoffset=2')
Extensions.register do
# A treeprocessor that increments each level-1 section number by the value of
# the `sectnumoffset` attribute.
#
# In addition, if `subsectnumoffset` is defined and greater than zero,
# the numbers of subsections in the first section encountered will
# be incremented by the offset.
#
# 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
subsectnumoffset = (document.attr 'subsectnumoffset', 0).to_i
warn "subsectnumoffset: #{subsectnumoffset}".red
section_count = 0
if subsectnumoffset > 0
warn "Insert parent section at 'head' of document with offset #{sectnumoffset}".cyan
end
((document.find_by context: :section) || []).each do |sect|
next unless sect.level <= 2
if sect.level == 1
section_count += 1
sect.number += sectnumoffset
elsif sect.level == 2 && section_count == 1
sect.number += subsectnumoffset
end
end
end
nil
end
end
end
Contents
@mojavelinux, could you take a look at the below?
PROBLEM: Add offsets for subsections
In the example below a modification to
sectnumoffset-treeprocessor.rb
is used to offset subsection numbers in the first section that is processed. I would like to insert a parent section at the head of the document and eliminate the text== Outer Test\n
so that the first line of the HTML output isHelp is needed from the Ascii Doctor.
EXAMPLE:
HTML output:
CODE: