Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
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 |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
|
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 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]> wrote: Hello, ... [show rest of quote] -- Dan Allen | @mojavelinux | https://twitter.com/mojavelinux |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
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 :
... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
This post was updated on Apr 04, 2019; 4:24pm.
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
... [show rest of quote]
|
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
To give a bit of context, I intended to use attributes to tag special values for easy programmatic access when parsing the document.
For example, I was thinking on writing software requirement documents with AsciiDoc and use AsciidoctorJ to implement some tooling. Each top level section of the document would be dedicated to a software requirement. I thought section attributes would be useful to capture some metadata about each requirement (allocated subsystem, target version...). I could use the attribute in two interesting ways: * reference in the requirement text to avoid duplicating information * from the Java API to efficiently access the metadata value without complex and fragile requirement text parsing logic So my use case is that one section has a set of fixed attributes each with a single value. My previous example could have let you to believe I want to detect attribute value changes in arbitray places of the document. This is not the case, my need is much simpler and I control the layout of the document. For instance, I could enforce one top level Asciidoc document referencing sub documents each containing a single requirement. This would work since I can already define and access document attributes. Still, I find the current API surprising. Clarifications are welcomed :) Michaël |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
|
Michaël, I think what you're looking for are block attributes instead of document attributes. These are specified differently. [subsystem=NewValue] == Sample Section Now, when you look for the attributes on a node, you will find "subsystem" among them. Document attributes that are changed mid-document are not available to the AST (without doing some magic incantations). That's because document attributes are not part of the AST beyond the document header. This is explained here: https://github.com/asciidoctor/asciidoctor/issues/1485#issuecomment-142184143 Cheers, -Dan On Thu, Apr 4, 2019 at 10:49 AM Michaël Melchiore [via Asciidoctor :: Discussion] <[hidden email]> wrote: To give a bit of context, I intended to use attributes to tag special values for easy programmatic access when parsing the document. ... [show rest of quote] -- Dan Allen | @mojavelinux | https://twitter.com/mojavelinux |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Dear Dan, Thanks for your inputs. How can I reference the block attributes ? For example, [subsystem=NewValue] == {subsystem} requirements This section describes the requirements allocated to {subsystem} Kind regards, Michaël Le jeu. 4 avr. 2019 à 23:25, mojavelinux [via Asciidoctor :: Discussion] <[hidden email]> a écrit :
... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Currently, I use this hack :subsystem: NewValue [subsystem='{subsystem}'] == {subsystem} requirements This section describes the requirements allocated to {subsystem} If you know of a better way, I am interested. Kind regards, Michaël Le ven. 5 avr. 2019 à 10:47, Michaël Melchiore [via Asciidoctor :: Discussion] <[hidden email]> a écrit :
... [show rest of quote] |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
|
In reply to this post by Michaël Melchiore
Michaël, You can't. These are two different things. Block attributes are for labeling. Document attributes are for content. (Though you can use a document attribute in the value of a block attribute). Cheers, -Dan On Fri, Apr 5, 2019 at 2:47 AM Michaël Melchiore [via Asciidoctor :: Discussion] <[hidden email]> wrote:
... [show rest of quote] -- Dan Allen | @mojavelinux | https://twitter.com/mojavelinux |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Administrator
|
In reply to this post by Michaël Melchiore
That's not a hack. That's the way it's designed. But you don't need to use single quotes for the block attribute because attributes are replaced by default. (You can just use double quotes or no quotes). Cheers, -Dan On Fri, Apr 5, 2019 at 2:52 AM Michaël Melchiore [via Asciidoctor :: Discussion] <[hidden email]> wrote:
... [show rest of quote] -- Dan Allen | @mojavelinux | https://twitter.com/mojavelinux |
Loading... |
Reply to author |
Edit post |
Move post |
Delete this post |
Delete this post and replies |
Change post date |
Print post |
Permalink |
Raw mail |
Ok, I understand now. Thank you for your time. Michaël Le ven. 5 avr. 2019 à 10:55, mojavelinux [via Asciidoctor :: Discussion] <[hidden email]> a écrit :
... [show rest of quote] |
Free forum by Nabble | Edit this page |