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 |
Administrator
|
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 -- Dan Allen (he, him, his) | @mojavelinux | https://twitter.com/mojavelinux |
Thanks, that's what I thought based on the observed behavior.
I am not sure my use case deserves a change to the parser behavior. I will need to create the parent section outside my macro but that is not a big problem. |
It’s also possible to add the section immediately and use a tree processor to reset the section levels correctly. I think it’s important to preserve the order of the blocks, and this strategy makes that easier in my opinion. I have implemented this in asciidoctor-ainclude, where the added sections are in isolated sub-documents. My experience was that getting the algorithm to reset section levels to work was tricky.
|
Free forum by Nabble | Edit this page |