|
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 |
|
Afaik, the structure is a tree, you'll have to traverse recursivity.
|
|
Administrator
|
That's also why we have the findBy method: Here's the backing method in the Ruby API: https://www.rubydoc.info/gems/asciidoctor/2.0.9/Asciidoctor/AbstractBlock#find_by-instance_method Cheers, -Dan 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. -- Dan Allen | @mojavelinux | https://twitter.com/mojavelinux |
|
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 |
|
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’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 |
| Free forum by Nabble | Edit this page |
