1

I'm trying to create a simple macro to render a text item in red in asciidoctor. The following does not work:

:redtext: [red]#some important text in red that occurs a lot#

{redtext}

or for an even simpler example:

:redcross: [red]#✘#

I am not clear on the rules for what can and cannot be substituted by a macro. The asciidoctor manual has a blank space for macros at present (http://asciidoctor.org/docs/user-manual/#macros). The asciidoc manual (http://www.methods.co.nz/asciidoc/chunked/ch21.html) isn't that clear either but may not apply to asciidoctor anyway.

A related unanswered question is Resuable markup fragments with Asciidoctor. A related question to that suggests using includes which is overkill for this.

What are the limitations of macros?

Community
  • 1
  • 1
Bruce Adams
  • 4,953
  • 4
  • 48
  • 111

2 Answers2

1

What you have defined is not a macro, it's an attribute. (When you use it, it's called an attribute reference).

You can perform substitution eagerly in the definition of an attribute using the inline pass macro. In the target position, it accepts a comma-separated list of substitution names (or substitution letters).

In your case, you can write:

:redtext: pass:q[[red]#some important text in red that occurs a lot#]

The relevant part is:

pass:q[...]

See substitutions in an attribute entry for details.

Dan Allen
  • 2,418
  • 1
  • 16
  • 13
0

I think includes work well enough. We have a single glossary.asciidoc file to contain all the re-usable snippets, for example:

tag::redtext[]
[red]#some important text in red that occurs a lot#
end::redtext[]

In index.asciidoc you can add a little helper:

:g: glossary.asciidoc

Then wherever you need this snippet:

include::{g}[tag=redtext]
Daniel Darabos
  • 26,991
  • 10
  • 102
  • 114
  • That introduces several new problems: * The include can only be used at the start of the line * Your document is no longer a single source file * The include is almost as verbose as the text I want to include. – Bruce Adams Nov 08 '16 at 01:00