Quantcast

Variable substitutions in include directives with asciidoctor-gradle

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Variable substitutions in include directives with asciidoctor-gradle

ctargett
I think I've come to the point where this just isn't going to work, but thought I would describe my problem, and maybe someone can help me with a simple way out.

We are using variables in our Gradle builds for version numbers and the like (in the form of ${productName} and ${versionNumber}), but I have found that when those are in an external .adoc file processed with an include directive, the substitutions don't happen.

At build time, we convert a file with a few include directives such as:

include::file1.adoc[]
include::file-2.adoc[]

file1.adoc, for example, looks like this:

Blah blah, ${productName}, blah blah

include::{file1-github}/readme.adoc[tags=tag1]
include::{file1-github}/readme.adoc[tags=tag3]

Note that the include defines getting the document from an external source. The attributes are defined in the build and the content is retrieved fine. The '${productName}' variable in the intro text is substituted fine. But if there are any of this style in the external "readme.adoc", they are not substituted.

Has anyone seen this before? I have tried playing with the "subs=attributes" parameter, but these aren't Asciidoctor attributes, instead they are defined in gradle.properties.

The only solution I've been able to come up with is to change these so they are defined as Asciidoctor attributes and not Gradle substitutions, but since it was very easy to simply maintain versions, names, etc., in one place, I thought I'd check before changing everything to maintain it in 2 places.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Variable substitutions in include directives with asciidoctor-gradle

ctargett
It turns out I was able to solve my own problem.

We modified the attributes in the asciidoctor section of build.gradle as so:

  asciidoctor {
    outputDir = new File('docs')
    sourceDir = new File('docs/main')
    options = [
        attributes: [
            'build-gradle'         : file('build.gradle'),
            'namePackage'       : "${namePackage}",
            'productName'       : "${productName}",

And it nicely picks up the values for those variables from gradle.properties. This seems to solve our problems, but an important note for others is the variables need to be in double-quotes - single quotes won't do the substitutions.
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate

Re: Variable substitutions in include directives with asciidoctor-gradle

mojavelinux
Administrator
@ctargett,

You are exactly right. In the Gradle script, you need to transfer any Gradle variables you want to reference in your AsciiDoc content to AsciiDoc attributes. There are only a handful of built-in attributes that the Gradle plugin provides:

project.projectDir is mapped to projectdir
project.rootDir is mapped to rootdir
project.name is mapped to project-name
project.group is mapped to project-group
project.version is mapped to project-version

Aside from those mappings, you are responsible for assigning any additional, custom attributes.

Another way to look at it is that AsciiDoc can't "see" the Gradle project. It only can see data that has been explicitly passed from the Gradle project to the AsciiDoc context through attributes.

Cheers,

-Dan

On Tue, Jun 7, 2016 at 2:28 PM, ctargett [via Asciidoctor :: Discussion] <[hidden email]> wrote:
It turns out I was able to solve my own problem.

We modified the attributes in the asciidoctor section of build.gradle as so:

  asciidoctor {
    outputDir = new File('docs')
    sourceDir = new File('docs/main')
    options = [
        attributes: [
            'build-gradle'         : file('build.gradle'),
            'namePackage'       : "${namePackage}",
            'productName'       : "${productName}",

And it nicely picks up the values for those variables from gradle.properties. This seems to solve our problems, but an important note for others is the variables need to be in double-quotes - single quotes won't do the substitutions.


If you reply to this email, your message will be added to the discussion below:
http://discuss.asciidoctor.org/Variable-substitutions-in-include-directives-with-asciidoctor-gradle-tp4744p4745.html
To start a new topic under Asciidoctor :: Discussion, email [hidden email]
To unsubscribe from Asciidoctor :: Discussion, click here.
NAML



--
Dan Allen | @mojavelinux | https://twitter.com/mojavelinux
Loading...