Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Subsection offset - parent needed #55

Open
jxxcarlson opened this issue Aug 14, 2015 · 0 comments
Open

Subsection offset - parent needed #55

jxxcarlson opened this issue Aug 14, 2015 · 0 comments

Comments

@jxxcarlson
Copy link

Contents

  • PROBLEM
  • EXAMPLE
  • HTML Output
  • Code

@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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant