Login  Register

Is AST Document limited to see blocks - level 1,2 only

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
5 messages Options Options
Embed post
Permalink
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Is AST Document limited to see blocks - level 1,2 only

ch007m
Hi,

When we develop a Java extension for Asciidoctor using the TreeProcessor, then the following code (see hereafter) only sees the sections level 2 and not level 3, 4 (=== level, ...).
Do we have to specify a parameter to enable that and get them with the method "block.getBlocks()" ?

Code
@Override
    public Document process(Document document) {
        processBlock((StructuralNode) document);
        return document;
    }

    private void processBlock(StructuralNode block) {
        List<StructuralNode> blocks = block.getBlocks(); // WE ONLY GOT LEVEL 2

Regards

Charles
Charles Moulliard
Apache Committer / Technologist Evangelist / Blogger / MiddleWare Specialist
Twitter : @cmoulliard
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is AST Document limited to see blocks - level 1,2 only

abelsromero
Afaik, the structure is a tree, you'll have to traverse recursivity.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is AST Document limited to see blocks - level 1,2 only

mojavelinux
Administrator

On Thu, Apr 9, 2020 at 4:01 AM abelsromero [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Afaik, the structure is a tree, you'll have to traverse recursivity.


If you reply to this email, your message will be added to the discussion below:
https://discuss.asciidoctor.org/Is-AST-Document-limited-to-see-blocks-level-1-2-only-tp7858p7860.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML


--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is AST Document limited to see blocks - level 1,2 only

ch007m
After digging into the doc, I was able to fix it and to access the levels 3,4,.... Sorry ;-)

    private void processBlock(StructuralNode node) {
        List<StructuralNode> sections = node.getBlocks();
        for (int i = 0; i < sections.size(); i++) {
            final StructuralNode currentBlock = sections.get(i);
            System.out.println("Node : " + currentBlock.getContext() + ", title \"" + currentBlock.getTitle() + "\"");

            if ("section".equals(currentBlock.getContext())) {
                List<StructuralNode> subLevels = currentBlock.getBlocks();
                for (int j = 0; j < subLevels.size(); j++) {
                    final StructuralNode subLevel = subLevels.get(j);
                    if (subLevel.getTitle() == null) {
                        System.out.println("  Node : " + subLevel.getContext());
                    } else {
                        System.out.println("  Node : " + subLevel.getContext() + ", title \"" + subLevel.getTitle() + "\"");
                    }
                }
            }
Charles Moulliard
Apache Committer / Technologist Evangelist / Blogger / MiddleWare Specialist
Twitter : @cmoulliard
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Is AST Document limited to see blocks - level 1,2 only

ch007m
In reply to this post by mojavelinux
I observe a strange behavior. If I execute the following code on my document

public class DynamicTable extends Treeprocessor {

    @Override
    public Document process(Document document) {
        processBlock((StructuralNode) document);
        return document;
    }

    private void processBlock(StructuralNode node) {
        System.out.println("==== FindBy \":section\" selector");
        selector = new HashMap<Object, Object>();
        selector.put("context", ":section");
        findBy = node.findBy(selector);
        for (int j = 0; j < findBy.size(); j++) {
            final StructuralNode subNode = findBy.get(j);
            System.out.println("Node content: " + subNode.getContent());
        }

then I get the rendered HTLM content

==== FindBy ":section" selector
Node content: <div class="paragraph">
<p>Let&#8217;s start digging into the <a href="https://docs.ansible.com/">Ansible</a> documentation.</p>
</div>
<div class="paragraph">
...


Can we use `FindBy` method on the AST document objects only and not the HTML rendered ?
Charles Moulliard
Apache Committer / Technologist Evangelist / Blogger / MiddleWare Specialist
Twitter : @cmoulliard