Login  Register

Conditional inclusion

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

Conditional inclusion

rockyallen
I would like to customize my documents for audience so that the same source files can generate slightly different outputs depending on some command line parameter; something like C #def or DocBook profiling.
Is there an existing Asciidoctor method for this?

Manual section 41 "conditional preprocessor directives", looks most hopeful, but I can't see any details as to how it is done.




Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Conditional inclusion

mojavelinux
Administrator

On Thu, Dec 18, 2014 at 4:17 PM, rockyallen [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Manual section 41 "conditional preprocessor directives", looks most hopeful, but I can't see any details as to how it is done.

That's the place you need to be. Conditional preprocessor directives are controlled using document-level attributes. For example, you might do something like:

```
ifdef::audience-a[]
Hi audience A!
endif::[]
ifdef::audience-b[]
Hi audience B!
endif::[]
```

Then you'd enable the audience using a commandline attribute:

 $ asciidoctor -a audience-a document.adoc

There are several other ways to structure the logic. For instance, you can use ifeval if you want to check the value of the attribute:

```
ifeval::["{audience}" == "A"]
Hi audience A!
endif::[]
```

And pass:

 $ asciidoctor -a audience=A document.adoc

--
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Conditional inclusion

rockyallen
Worked perfectly, thanks.
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Conditional inclusion

agavazuk
In reply to this post by mojavelinux
Hi dear users,

I'm using AsciidoctorJ, could you suggest how to pass parameter(s)  (document-level attributes) to convert* methods for conditional evaluation blocks?
Now I'm trying to populate a map and send it through however the ifdef block is not being rendered thus I assume I'm doing that in incorrect way.
   

Map<String,Object> attributes = new HashMap<>();
attributes.put("paramA","paramA");
asciidoctor.convertFile("adoc file",attributes);

and adoc snippet

ifdef::paramA[]
Problem
endif::[]

ifndef::paramA[]
No Problem
endif::[]
Reply | Threaded
Open this post in threaded view
| More
Print post
Permalink

Re: Conditional inclusion

mojavelinux
Administrator
You almost have it right. You need to pass the map of attributes to the API via the options map as demonstrated in the "Conversion options" section of the AsciidoctorJ manual:

http://asciidoctor.org/docs/asciidoctorj/#conversion-options

Good luck!

-Dan

On Fri, Jan 9, 2015 at 9:31 AM, agavazuk [via Asciidoctor :: Discussion] <[hidden email]> wrote:
Hi dear users,

I'm using AsciidoctorJ, could you suggest how to pass parameter(s)  (document-level attributes) to convert* methods for conditional evaluation blocks?
Now I'm trying to populate a map and send it through however the ifdef block is not being rendered thus I assume I'm doing that in incorrect way.
   

Map<String,Object> attributes = new HashMap<>();
attributes.put("paramA","paramA");
asciidoctor.convertFile("adoc file",attributes);

and adoc snippet

ifdef::paramA[]
Problem
endif::[]

ifndef::paramA[]
No Problem
endif::[]



If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Conditional-inclusion-tp2568p2658.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--