asciidoctor / asciidoctor-diagram

:left_right_arrow: Asciidoctor diagram extension, with support for AsciiToSVG, BlockDiag (BlockDiag, SeqDiag, ActDiag, NwDiag), Ditaa, Erd, GraphViz, Mermaid, Msc, PlantUML, Shaape, SvgBob, Syntrax, UMLet, Vega, Vega-Lite and WaveDrom.
http://asciidoctor.org
MIT License
442 stars 107 forks source link

Conditional using Asciidoc attributes within inline plantuml files #282

Closed yaron-top closed 4 years ago

yaron-top commented 4 years ago

Hi I’m looking for a way to incorporate an Asciidoc attribute in a plantuml section so that the attribute influences the parsing evaluation of a puml section.

When I use the Asciidoc “Ifndef::[]” statement in the Asciidoc file everyting goes fine. However when I place the Ifndef::[] statement in the puml file, the conditional is not interpreted and the plant uml gets it as is.

How can I add conditionals to plant uml based on an asccidoc attribute?

Code example to demonstrate the problem: Test.adoc content: [plantuml, target=”test1”, format=”png”] …. Ifndef::flag[] // NO PROBLEM HERE @startuml Component “This works fine” @enduml endif::[]

…. [plantuml, target=”test2”, format=”png”] …. @startuml // HERE THERE IS A PROBLEM Ifndef::flag[] Component “This yields syntax error” Endif::[] @enduml

Expected behavior: 2 diagrams of components. One with the string “This works fine”, and one with the string “This file yields syntax error” Actual behavior: One diagram appears fine and then a diagram announcing a Syntax error: [From string (line 4)] @startuml skinparam dpi 200. // HERE THERE IS A PROBLEM Ifndef::flag[] Syntax Error?

Environment: AsciidoctorJ 2.2.0 (Asciidoctor 2.0.10)

pepijnve commented 4 years ago

I’ve never tried this myself, but I think you need to set the subs attribute on blocs to get this behaviour. It will have to include macros to get ifdef processing. I’m not 100% sure this will work right away. This might require some code changes to the extension as well.

yaron-top commented 4 years ago

Hi pepijnve,

Thanks a lot for the tip.

I wrote the following asciidoc test using subs attributes:

= Test
:flag: 9

component  “flag={flag}”

[subs=”attributes+”]
….
component  “In block flag={flag}”
….

[plantuml, subs=”attributes+”]
….
component  “In plantuml flag={flag}”
….

Expected behavior

= Test
component  “flag=9”
component  “In block flag=9”
In plantuml flag=9

Actual behavior

= Test
component  “flag=9”
component  “In block flag=9”
In plantuml flag={flag}

The 3rd line appear inside a UML component notation picture.

It seems that there is a problem with the plantuml block. Is there any workaround to solve this problem? If so, a small code example would be highly appreciated.

pepijnve commented 4 years ago

I had a quick look at the code already. Seems like it should be working already. I’ll have to do some testing to be able to tell you more. I might have some time for that this evening or tomorrow.

mojavelinux commented 4 years ago

The preprocessor directives run before any extension touches the lines, so they should get processed no matter where they are.

The problem might be that you're writing the conditional directive as "Ifdef" instead of "ifdef". The first "i" should be lowercase. Same for the "e" in "endif".

yaron-top commented 4 years ago

The preprocessor directives run before any extension touches the lines, so they should get processed no matter where they are.

The problem might be that you're writing the conditional directive as "Ifdef" instead of "ifdef". The first "i" should be lowercase. Same for the "e" in "endif".

Thank you @mojavelinux, I had a typo, the proprocessor is not working using lower cases as well.

As @pepijnve suggested, I tried the subs="attributes+" and it seems like the right direction, however, the plantuml block is not affected.

Can you please refer to my new comment of testing with subs?

Regards.

mojavelinux commented 4 years ago

subs have no affect on preprocessor directives. The preprocessor directives don't see the structure of the document at all. They are processed before the block is parsed (ahead of parsing).

mojavelinux commented 4 years ago

If you are using attribute references in a verbatim block that are not part of a preprocessor directive (such as ifdef or include), then you do need subs. The subs affect lines as they exist after the preprocessor directives have run.

mojavelinux commented 4 years ago

However, when the subs get applied relative to when the diagram is generated is something that is up to the extension.

pepijnve commented 4 years ago

@yaron-top I tested this locally and it works correctly as far as I can tell. I did have to massage the asciidoc code you provided a bit. The original contains and instead of ". There was already a test case covering this exact behaviour (see https://github.com/asciidoctor/asciidoctor-diagram/blob/master/spec/plantuml_spec.rb#L1000).

yaron-top commented 4 years ago

I am sorry for the trouble. You are right. Everything works fine. It seems that the problem was in the editor I used (AsciidocFx). Thanks a lot for your help.

StefH commented 3 weeks ago

In case someone wants a full working asciidoc example:

:version: stef
[source, subs="attributes+"]
----
<version>{version}</version>
----

[plantuml, subs="attributes+"]
----
@startuml
Component "version? {version}"
@enduml
----

Renders as: image