37. Text Substitutions
Text substitution elements replace characters, markup, attribute references, and macros with converter specific styles and values. When Asciidoctor processes a document it uses a set of six text substitution elements. The processor runs the text substitution elements in the following order.
-
Special characters
-
Quotes
-
Attribute references
-
Replacements
-
Inline macros
-
Post replacements
In turn, these substitutions are organized into composite value groups. The table below shows which substitution elements are included in each group.
| Group | Special characters | Quotes | Attributes | Replacements | Macros | Post replacements |
|---|---|---|---|---|---|---|
Header |
||||||
None |
||||||
Normal |
||||||
Pass |
||||||
Verbatim |
By default, the normal substitution group is applied to most block and inline elements.
However, there are a few exceptions.
The header substitution group is applied to metadata lines (author and revision information) in the document header.
This group is also applied to the values of attribute entries, regardless of whether those entries are defined in the header or elsewhere in the document.
In the header, only special characters and attribute references are replaced.
In attribute entries, you can also use the inline pass macro.
Fenced, literal, listing, and source blocks are processed with the verbatim substitution group.
Only special characters are replaced in these blocks.
The pass substitution group can only be applied to passthrough elements.
Attribute references and macros are replaced in passthroughs.
The none substitution group is applied to comment blocks.
37.1. Special Characters
When applicable, the first text substitution to occur is the replacement of any special characters.
This process is handled by the specialchars element.
The specialchars element searches for three characters (<, >, &) and replaces them with their named character references.
-
The less than symbol,
<, is replaced with the named character reference<. -
The greater than symbol,
>, is replaced with the named character reference>. -
An ampersand,
&, is replaced with the named character reference&.
By default, the special characters substitution occurs on all inline and block elements except for comments and certain passthroughs. The substitution of special characters can be controlled on blocks using the subs attribute and on inline elements using the passthrough macro.
|
Special character substitution precedes attribute substitution, so you will need to manually escape any attributes containing special characters that you set in the CLI or API.
For example, on the command line, type |
37.2. Quotes
The quotes substitution replaces the formatting markup on inline elements.
For example, when a document is converted to HTML, any asterisks enclosing text are replaced with <strong> HTML tags.
Happy werewolves are *really* slobbery.
Happy werewolves are <strong>really</strong> slobbery.
The following table shows the HTML markup that is generated by the quotes substitution process.
| Name | AsciiDoc | HTML |
|---|---|---|
emphasis |
_word_ |
<em>word</em> |
strong |
*word* |
<strong>word</strong> |
monospace |
`word` |
<code>word</code> |
superscript |
^word^ |
<sup>word</sup> |
subscript |
~word~ |
<sub>word</sub> |
double curved quotes |
"`word`" |
“word” |
single curved quotes |
'`word`' |
‘word’ |
The quotes substitution occurs on formatted text within title, paragraph, example, quote, sidebar, and verse blocks.
| Element | quotes substitution |
|---|---|
Attribute Entry Value |
|
Comment |
|
Example |
|
Fenced |
|
Header |
|
Literal |
|
Listing |
|
Macro |
|
Open |
Varies |
Paragraph |
|
Passthrough |
|
Quote |
|
Sidebar |
|
Source |
|
Special sections |
|
Table |
Varies |
Title |
|
Verse |
37.3. Attributes
Attribute references are replaced with their values when they’re processed by the attributes substitution.
| Element | attributes substitution |
|---|---|
Attribute Entry Value |
|
Comment |
|
Example |
|
Fenced |
|
Header |
|
Literal |
|
Listing |
|
Macro |
|
Open |
Varies |
Paragraph |
|
Passthrough |
|
Quote |
|
Sidebar |
|
Source |
|
Special sections |
|
Table |
Varies |
Title |
|
Verse |
37.4. Replacements
The replacements substitution processes textual characters such as marks, arrows and dashes and replaces them with the decimal format of their Unicode code point, i.e. their numeric character reference.
| Name | Syntax | Unicode Replacement | Rendered | Notes |
|---|---|---|---|---|
Copyright |
(C) |
© |
© |
|
Registered |
(R) |
® |
® |
|
Trademark |
(TM) |
™ |
™ |
|
Em dash |
-- |
— |
— |
Only replaced if between two word characters, between a word character and a line boundary, or flanked by spaces. When flanked by space characters (e.g., |
Ellipsis |
... |
… |
… |
|
Single right arrow |
-> |
→ |
→ |
|
Double right arrow |
=> |
⇒ |
⇒ |
|
Single left arrow |
<- |
← |
← |
|
Double left arrow |
<= |
⇐ |
⇐ |
|
Typographic apostrophe |
Sam's |
Sam’s |
Sam’s |
The typewriter apostrophe is replaced with the typographic (aka curly) apostrophe. |
The replacements element depends on the substitutions completed by the specialcharacters element.
This is important to keep in mind when applying custom substitutions to a block.
See the section about applying custom substitutions for more information.
|
The replacements substitution also recognizes HTML and XML character references as well as decimal and hexadecimal Unicode code points and substitutes them for their corresponding decimal form Unicode code point.
For example, to produce the § symbol you could write §, §, or §.
When the document is processed, replacements will replace the section symbol reference, regardless of whether it is a named character reference or a numeric character reference, with §.
In turn, § will display as §.
| Asciidoctor also provides numerous built-in attributes for representing characters and symbols. These attributes and their corresponding output are listed in Predefined attributes for character replacements [1][2][3]. |
The replacements substitution occurs within title, paragraph, example, quote, sidebar, and verse blocks.
| Element | replacements substitution |
|---|---|
Attribute Entry Value |
|
Comment |
|
Example |
|
Fenced |
|
Header |
|
Literal |
|
Listing |
|
Macro |
|
Open |
Varies |
Paragraph |
|
Passthrough |
|
Quote |
|
Sidebar |
|
Source |
|
Special sections |
|
Table |
Varies |
Title |
|
Verse |
37.5. Macros
Macros are processed by the macros element.
The macros substitution replaces a macro’s content with the appropriate built-in and user-defined configuration.
| Element | macros substitution |
|---|---|
Attribute Entry Value |
inline pass macro only |
Comment |
|
Example |
|
Fenced |
|
Header |
|
Literal |
|
Listing |
|
Macro |
|
Open |
Varies |
Paragraph |
|
Passthrough |
|
Quote |
|
Sidebar |
|
Source |
|
Special sections |
|
Table |
Varies |
Title |
|
Verse |
37.6. Post Replacements
The line break character, +, is replaced when the post_replacements process runs.
| Element | post_replacements substitution |
|---|---|
Attribute Entry Value |
|
Comment |
|
Example |
|
Fenced |
|
Header |
|
Literal |
|
Listing |
|
Macro |
|
Open |
Varies |
Paragraph |
|
Passthrough |
|
Quote |
|
Sidebar |
|
Source |
|
Special sections |
|
Table |
Varies |
Title |
|
Verse |
37.7. Applying Substitutions
Specific substitution elements can be applied to any block or paragraph by setting the subs attribute.
The subs attribute can be assigned a comma separated list of the following substitution elements and groups.
none
|
Disables substitutions |
normal
|
Performs all substitutions except for callouts |
verbatim
|
Replaces special characters and processes callouts |
specialchars, specialcharacters
|
Replaces |
quotes
|
Applies text formatting |
attributes
|
Replaces attribute references |
replacements
|
Substitutes textual and character reference replacements |
macros
|
Processes macros |
post_replacements
|
Replaces the line break character ( |
Let’s look at an example where you only want to process special characters, formatting markup, and callouts in a literal block.
By default, literal blocks are only subject to special characters substitution.
But you can change this behavior by setting the subs attribute in the block’s attribute list.
[source,java,subs="verbatim,quotes"] (1)
----
System.out.println("Hello *bold* text"). (2)
----
| 1 | The subs attribute is set in the attribute list and assigned the verbatim and quotes values. |
| 2 | The formatting markup in this line will be replaced when the quotes substitution runs. |
System.out.println("Hello bold text"). (1) (2)
| 1 | The verbatim value enables the callouts to be processed. |
| 2 | The quotes value enables the text formatting to be processed. |
|
If you are applying the same set of substitutions to numerous blocks, you should consider making them an attribute entry to ensure consistency.
Another way to ensure consistency and keep your documents clean and simple is to use the TreeProcessor extension. |
37.8. Incremental Substitutions
When you set the subs attribute on a block, you automatically remove all of its default substitutions.
For example, if you set subs on a literal block, and assign it a value of attributes, only attributes are substituted.
The verbatim substitution will not be applied.
To remedy this situation, Asciidoctor provides a syntax to append or remove substitutions instead of replacing them outright.
You can add or remove a substitution from the default substitution list using the plus (+) and minus (-) modifiers.
These are known as incremental substitutions.
<substitution>+
|
Prepends the substitution to the default list. |
+<substitution>
|
Appends the substitution to the default list. |
-<substitution>
|
Removes the substitution from the default list. |
For example, you can add the attributes substitution to a listing block’s default substitution list.
[source,xml,subs="attributes+"]
----
<version>{version}</version>
----
Similarly, you can remove the callouts substitution.
[source,xml,subs="-callouts"]
.An illegal XML tag
----
<1>
content inside "1" tag
</1>
----
You can also specify whether the substitution is placed at the beginning or end of the substitution list.
If a + comes before the name of the substitution, then it’s added to the end of the existing list, and if a + comes after the name, it’s added to the beginning of the list.
[source,xml,subs="attributes+,+replacements,-callouts"]
----
<version>{version}</version>
<copyright>(C) ACME</copyright>
<1>
content inside "1" tag
</1>
----
In the above example, the quotes, then the special characters, and then the attributes substitutions will be applied to the listing block.
37.8.1. Applying Substitutions to Inline Elements
Custom substitutions can also be applied to some inline elements, such as the pass macro.
For example, the quotes text substitution value is assigned in the inline pass macro below.
The text pass:q[<u>underline *me*</u>] is underlined and the word "`me`" is bold.
The text underline me is underlined and the word “me” is bold.
37.9. Preventing Substitutions
Asciidoctor provides several approaches for preventing substitutions.
To prevent punctuation from being interpreted as formatting markup, precede it with a backslash (\).
If the formatting punctuation begins with two characters (e.g., __), you need to precede it with two backslashes (\\).
This is also how you can prevent character and attribute references from substitution.
When your document is processed, the backslash is removed so it doesn’t display in your output.
\*Stars* will appear as *Stars*, not as bold text.
\§ will appear as an entity, not the § symbol.
\\__func__ will appear as __func__, not as emphasized text.
\{two-semicolons} will appear {two-semicolons}, not resolved as ;;.
You can also prevent substitutions with macro and block passthroughs.