Login  Register

Re: How to exploit ContentPart attributes (RubyHash) from Java

Posted by Michaël Melchiore on Apr 04, 2019; 4:10pm
URL: https://discuss.asciidoctor.org/How-to-exploit-ContentPart-attributes-RubyHash-from-Java-tp6827p6831.html

Dan,

On a simple example, I think I don't understand the new API:

:subsystem: Test
= Sample Document

:subsystem: NewValue
== Sample Section
:subsystem: NewValue2

        Document document = asciidoctor.loadFile(file, Collections.emptyMap());

        // Initialize attributes with header content
        Map<String, Object> attributes = new HashMap<>(document.getAttributes());

        System.out.println(document.getAttributes());
        System.out.println(document.getOptions());
        System.out.println(document.getSubstitutions());


        for (StructuralNode node : document.getBlocks()) {
            
            // Then overwrite with section attributes
            Map<String, Object> nodeAttributes = node.getAttributes();
            attributes.putAll(nodeAttributes);

            System.out.println(node.getAttributes());
            System.out.println(node.getBlocks());

            for (StructuralNode block : node.getBlocks()) {
                System.out.println("SUBNODE");
                System.out.println(block.getAttributes());
                System.out.println(block.getBlocks());
            }
       }

document.getAttributes returns expected values. But,  node.getAttributes() only returns empty maps. I have checked other available methods to see if additional ast navigation would help but, so far,  I fail to see how I can get new attributes values from the exposed AST.

Cheers,

Michaël


Michaël Melchiore wrote
Dan,

I have switched to AsciidoctorJ 1.6.0 to use the new API.

Thank you for your input.

Michaël

Le mer. 3 avr. 2019 à 20:51, mojavelinux [via Asciidoctor :: Discussion] <
[hidden email]> a écrit :

> Michaël,
>
> You should no longer being using the ContentPart API. That API has been
> deprecated for some time and has been removed from AsciidoctorJ 1.6.0 and
> 2.0.0. But I have good news for you! That API has been replaced with a much
> better API that aligns closely with Asciidoctor Ruby and is far more
> capable. Here's a link to the API docs for that API:
>
>
> https://static.javadoc.io/org.asciidoctor/asciidoctorj/2.0.0-RC.1/org/asciidoctor/ast/package-summary.html
>
> https://static.javadoc.io/org.asciidoctor/asciidoctorj/2.0.0-RC.1/org/asciidoctor/ast/Document.html
>
> The newer API converts all types as you would expect them to be converted.
>
> Cheers,
>
> -Dan
>
> On Wed, Apr 3, 2019 at 7:03 AM Michaël Melchiore [via Asciidoctor ::
> Discussion] <[hidden email]
> <http:///user/SendEmail.jtp?type=node&node=6828&i=0>> wrote:
>
>> Hello,
>>
>> I am writing a simple tool in Java to parse the content of simple
>> AsciiDoc files using AsciidoctorJ 1.5.6.
>>
>> The documents define some header attributes which may be overriden later
>> in the document.
>>
>> In the following code, I initialize an attribute map from the document
>> header. Then, I want to update it with attributes from the current
>> ContentPart.
>>
>>
>>         StructuredDocument document =
>> asciidoctor.readDocumentStructure(file, Collections.emptyMap());
>>
>>         DocumentHeader header = document.getHeader();
>>
>>         // Initialize attributes with header content
>>         Map<String, Object> attributes = new
>> HashMap<>(header.getAttributes());
>>
>>         for (ContentPart part : document.getParts()) {
>>
>>             // Then overwrite with section attributes
>>             attributes.putAll(part.getAttributes());
>>
>>             String id = (String) attributes.get("id");
>>             String title = (String) attributes.get("title");
>>            ....
>>         }
>>
>> Sadly, this does not work. The document header attributes are exposed as
>> a Java HashMap but content part attributes are exposed through a RubyHash
>> object with a surprising structure (at least to me).
>>
>> Can someone help me convert the RubyHash into a HashMap ?
>>
>> Kind regards,
>>
>> Michaël
>>
>>
>> ------------------------------
>> If you reply to this email, your message will be added to the discussion
>> below:
>>
>> http://discuss.asciidoctor.org/How-to-exploit-ContentPart-attributes-RubyHash-from-Java-tp6827.html
>> To start a new topic under Asciidoctor :: Discussion, email [hidden
>> email] <http:///user/SendEmail.jtp?type=node&node=6828&i=1>
>> To unsubscribe from Asciidoctor :: Discussion, click here.
>> NAML
>> <http://discuss.asciidoctor.org/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>>
>
>
> --
> Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
>
>
> ------------------------------
> If you reply to this email, your message will be added to the discussion
> below:
>
> http://discuss.asciidoctor.org/How-to-exploit-ContentPart-attributes-RubyHash-from-Java-tp6827p6828.html
> To unsubscribe from How to exploit ContentPart attributes (RubyHash) from
> Java, click here
> <http://discuss.asciidoctor.org/template/NamlServlet.jtp?macro=unsubscribe_by_code&node=6827&code=cm9oZWwwMUBnbWFpbC5jb218NjgyN3wxMTk0MTEyMzQ0>
> .
> NAML
> <http://discuss.asciidoctor.org/template/NamlServlet.jtp?macro=macro_viewer&id=instant_html%21nabble%3Aemail.naml&base=nabble.naml.namespaces.BasicNamespace-nabble.view.web.template.NabbleNamespace-nabble.view.web.template.NodeNamespace&breadcrumbs=notify_subscribers%21nabble%3Aemail.naml-instant_emails%21nabble%3Aemail.naml-send_instant_email%21nabble%3Aemail.naml>
>