Re: Create section in block macro

Posted by mojavelinux on
URL: https://discuss.asciidoctor.org/Create-section-in-block-macro-tp8127p8128.html

When the block macro processor runs, the section you're currently in has not yet been added to the document (it's still a fragment). Therefore, while you can add additional children to the parent (e.g., current section), you cannot break out of the section and add blocks after it. (They will always end up out of order).

To accomplish what you want to do, you'd need to store those new sections and use a tree processor to insert them into the document. A tree processor runs once all blocks have been added to the document.

Yes, this is a limitation of the current design of the parser. We could consider adding the section eagerly, which would allow you to add blocks after it from a block macro processor.

-Dan

On Fri, Jul 31, 2020 at 2:38 AM dalai [via Asciidoctor :: Discussion] <[hidden email]> wrote:
I want to add a section using a macro. The problem is when I don't want it directly under the parent. My code is based on https://discuss.asciidoctor.org/How-to-generate-sections-in-extension-tp2142p2236.html

Assuming a document of this form:

  == Chapter 1

  === Section 1.1

  sample::[]


With the following macro it works as expected and the added section gets the number 1.1.1:

  block_macro do
    named :sample

    process do |parent, target, attrs|
      sect = create_section parent, "Added Section", { }
      parent << sect
      nil
    end
  end

If I set the level to 2 though the number is still 1.1.1 instead of 1.2. If I set the level to 1, the section number is 1 instead of 2. In all cases the styling is correct, just the numbering is wrong.

I also tried to add it to the parent.parent or the parent.parent.parent. In that case, it seems numbering and styling works correctly, but the added section is added before Section 1.1 or Chapter 1, respectively.

I understand that this use-case is not exactly foreseen for block macros. Is this a bug or is this a limitation of the extension? Is there an alternative or workaround? Should this be documented for the create_section method?

Thanks
Konstantinos


If you reply to this email, your message will be added to the discussion below:
https://discuss.asciidoctor.org/Create-section-in-block-macro-tp8127.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML


--
Dan Allen (he, him, his) | @mojavelinux | https://twitter.com/mojavelinux