asciidoctor / asciidoctor-extensions-lab

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.
Other
104 stars 101 forks source link

Subsection offset - parent needed #55

Open jxxcarlson opened 9 years ago

jxxcarlson commented 9 years ago

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 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')

HTML output:

[~/dev/asciidoctor-latex/work] ruby-2.2.2 $ ruby y.rb   
subsectnumoffset: 2
Insert parent section at 'head' of document with offset 1
<div class="sect1">
<h2 id="_outer_test">2. Outer Test</h2>
<div class="sectionbody">
<div class="sect2">
<h3 id="_inner_test_a">2.3. Inner Test A</h3>
<div class="paragraph">
<p>\(a^2 = 1\)</p>
</div>
</div>
<div class="sect2">
<h3 id="_inner_test_b">2.4. Inner Test B</h3>
<div class="paragraph">
<p>foo</p>
</div>
</div>
</div>
</div>
<div class="sect1">
<h2 id="_outer_again">3. Outer again</h2>
<div class="sectionbody">
<div class="paragraph">
<p>ho ho ho!</p>
</div>
<div class="sect2">
<h3 id="_yikes">3.1. Yikes!</h3>

</div>
</div>
</div>

CODE:

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