asciidoc-parser 0.19.0

Parser for AsciiDoc format
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
use crate::tests::prelude::*;

track_file!("ref/asciidoc-lang/docs/modules/attributes/pages/attribute-entry-substitutions.adoc");

non_normative!(
    r#"
= Attribute Entry Substitutions

The AsciiDoc processor automatically applies substitutions from the header substitution group to the value of an attribute entry prior to the assignment, regardless of where the attribute entry is declared in the document.
"#
);

#[test]
fn applies_header_subs() {
    verifies!(
        r#"
The header substitution group, which replaces xref:subs:special-characters.adoc[special characters] followed by xref:subs:attributes.adoc[attribute references], is applied to the values of attribute entries, regardless of whether the entries are defined in the header or in the document body.
This is the same group that gets applied to metadata lines (author and revision information) in the document header.

That means that any inline formatting in an attribute value isn't interpreted because:

. inline formatting is not applied when the AsciiDoc processor sets an attribute, and
. inline formatting is not applied when an attribute is referenced since the relevant substitutions come before attributes are resolved.

"#
    );

    let mut parser = Parser::default();
    parser.parse(":special_chars: <tag_this>\n:y: yes\n:answer: {y}\n:format: *bold*");

    assert_eq!(
        parser
            .attribute_value("special_chars")
            .as_maybe_str()
            .unwrap(),
        "&lt;tag_this&gt;"
    );

    assert_eq!(
        parser.attribute_value("answer").as_maybe_str().unwrap(),
        "yes"
    );

    assert_eq!(
        parser.attribute_value("format").as_maybe_str().unwrap(),
        "*bold*"
    );
}

mod change_subs_when_assigning {
    use crate::{blocks::ContentModel, content::SubstitutionStep, tests::prelude::*};

    non_normative!(
        r#"
[#pass-macro]
== Change substitutions when assigning a value

If you want the value of an attribute entry to be used *as is* (not subject to substitutions), or you want to alter the substitutions that are applied, you can enclose the value in the xref:pass:pass-macro.adoc[inline pass macro] (i.e., `\pass:[]`).
The inline pass macro accepts a list of zero or more substitutions in the target slot, which can be used to control which substitutions are applied to the value.
If no substitutions are specified, no substitutions will be applied.

In order for the inline macro to work in this context, it must completely surround the attribute value.
If it's used anywhere else in the value, it will be ignored.

"#
    );

    #[test]
    fn prevent_subs_on_assignment() {
        verifies!(
            r#"
Here's how to prevent substitutions from being applied to the value of an attribute entry:

[source]
----
:cols: pass:[.>2,.>4]
----

This might be useful if you're referencing the attribute in a place that depends on the unaltered text, such as the value of the `cols` attribute on a table.

"#
        );

        let mut parser = Parser::default();
        parser.parse(":cols: pass:[.>2,.>4]");

        assert_eq!(
            parser.attribute_value("cols").as_maybe_str().unwrap(),
            ".>2,.>4"
        );
    }

    #[test]
    fn apply_quotes_subs() {
        verifies!(
            r#"
Here's how we can apply the xref:subs:quotes.adoc[quotes substitution] to the value of an attribute entry:

[source]
----
:app-name: pass:quotes[MyApp^2^]
----

Internally, the value is stored as `MyApp<sup>2</sup>`.
You can inspect the value stored in an attribute using this trick:

[source]
----
[subs=attributes+]
------
{app-name}
------
----

"#
        );

        let mut parser = Parser::default();
        let doc = parser.parse(
            ":app-name: pass:quotes[MyApp^2^]\n\n[subs=attributes+]\n------\n{app-name}\n------",
        );

        assert_eq!(
            parser.attribute_value("app-name").as_maybe_str().unwrap(),
            "MyApp<sup>2</sup>"
        );

        let mut blocks = doc.nested_blocks();

        let block1 = blocks.next().unwrap();

        assert_eq!(
            block1,
            &Block::RawDelimited(RawDelimitedBlock {
                content: Content {
                    original: Span {
                        data: "{app-name}",
                        line: 5,
                        col: 1,
                        offset: 60,
                    },
                    rendered: "MyApp&lt;sup&gt;2&lt;/sup&gt;",
                },
                content_model: ContentModel::Verbatim,
                context: "listing",
                source: Span {
                    data: "[subs=attributes+]\n------\n{app-name}\n------",
                    line: 3,
                    col: 1,
                    offset: 34,
                },
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: Some(Attrlist {
                    attributes: &[ElementAttribute {
                        name: Some("subs"),
                        value: "attributes+",
                        shorthand_items: &[],
                    },],
                    anchor: None,
                    source: Span {
                        data: "subs=attributes+",
                        line: 3,
                        col: 2,
                        offset: 35,
                    },
                },),
                substitution_group: SubstitutionGroup::Custom(vec![
                    SubstitutionStep::AttributeReferences,
                    SubstitutionStep::SpecialCharacters,
                    SubstitutionStep::Callouts,
                ],),
            },)
        );
    }

    #[test]
    fn apply_quotes_single_char_alias() {
        verifies!(
            r#"
You can also specify the substitution using the single-character alias, `q`.

[source]
----
:app-name: pass:q[MyApp^2^]
----

The inline pass macro kind of works like an attribute value preprocessor.
If the processor detects that an inline pass macro completely surrounds the attribute value, it:

. reads the list of substitutions from the target slot of the macro
. unwraps the value from the macro
. applies the substitutions to the value

If the macro is absent, the value is processed with the header substitution group.

"#
        );

        let mut parser = Parser::default();
        let doc = parser
            .parse(":app-name: pass:q[MyApp^2^]\n\n[subs=attributes+]\n------\n{app-name}\n------");

        assert_eq!(
            parser.attribute_value("app-name").as_maybe_str().unwrap(),
            "MyApp<sup>2</sup>"
        );

        let mut blocks = doc.nested_blocks();

        let block1 = blocks.next().unwrap();

        assert_eq!(
            block1,
            &Block::RawDelimited(RawDelimitedBlock {
                content: Content {
                    original: Span {
                        data: "{app-name}",
                        line: 5,
                        col: 1,
                        offset: 55,
                    },
                    rendered: "MyApp&lt;sup&gt;2&lt;/sup&gt;",
                },
                content_model: ContentModel::Verbatim,
                context: "listing",
                source: Span {
                    data: "[subs=attributes+]\n------\n{app-name}\n------",
                    line: 3,
                    col: 1,
                    offset: 29,
                },
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: Some(Attrlist {
                    attributes: &[ElementAttribute {
                        name: Some("subs"),
                        value: "attributes+",
                        shorthand_items: &[],
                    },],
                    anchor: None,
                    source: Span {
                        data: "subs=attributes+",
                        line: 3,
                        col: 2,
                        offset: 30,
                    },
                },),
                substitution_group: SubstitutionGroup::Custom(vec![
                    SubstitutionStep::AttributeReferences,
                    SubstitutionStep::SpecialCharacters,
                    SubstitutionStep::Callouts,
                ],),
            },)
        );
    }
}

mod attributes_defined_outside_document {
    use crate::tests::prelude::*;

    // Non-normative because we have a different API and no CLI.
    non_normative!(
        r#"
== Substitutions for attributes defined outside the document

Unlike attribute entries, substitutions are *not* applied to the value of an attribute passed in to the AsciiDoc processor.
An attribute can be passed into the AsciiDoc processor using the `-a` CLI option or the `:attributes` API option.
When attributes are defined external to the document, the value must be prepared so it's ready to be referenced as is.
If the value contains XML special characters, that means those characters must be pre-escaped.
The exception would be if you intend for XML/HTML tags in the value to be preserved.
If the value needs to reference other attributes, those values must be pre-replaced.

"#
    );

    #[test]
    fn escape_ampersand_example() {
        verifies!(
            r#"
Let's consider the case when the value of an attribute defined external to the document contains an ampersand.
In order to reference this attribute safely in the AsciiDoc document, the ampersand must be escaped:

 $ asciidoctor -a equipment="a bat &amp; ball" document.adoc

You can reference the attribute as follows:

[,asciidoc]
----
To play, you'll need {equipment}.
----

"#
        );

        let mut parser = Parser::default().with_intrinsic_attribute(
            "equipment",
            "a bat &amp; ball",
            crate::parser::ModificationContext::Anywhere,
        );

        let doc = parser.parse("To play, you'll need {equipment}.");

        let mut blocks = doc.nested_blocks();

        let block1 = blocks.next().unwrap();

        assert_eq!(
            block1,
            &Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "To play, you'll need {equipment}.",
                        line: 1,
                        col: 1,
                        offset: 0,
                    },
                    rendered: "To play, you&#8217;ll need a bat &amp; ball.",
                },
                source: Span {
                    data: "To play, you'll need {equipment}.",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },)
        );
    }

    #[test]
    fn defined_inline_example() {
        verifies!(
            r#"
If the attribute were to be defined in the document, this escaping would not be necessary.

[,asciidoc]
----
:equipment: a bat & ball
----

That's because, in contrast, substitutions are applied to the value of an attribute entry.

"#
        );

        let mut parser = Parser::default();

        let doc = parser.parse(
            ":equipment: a bat & ball
\n\nTo play, you'll need {equipment}.",
        );

        let mut blocks = doc.nested_blocks();

        let block1 = blocks.next().unwrap();

        assert_eq!(
            block1,
            &Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "To play, you'll need {equipment}.",
                        line: 4,
                        col: 1,
                        offset: 27,
                    },
                    rendered: "To play, you&#8217;ll need a bat &amp; ball.",
                },
                source: Span {
                    data: "To play, you'll need {equipment}.",
                    line: 4,
                    col: 1,
                    offset: 27,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },)
        );
    }
}

#[test]
fn change_subs_when_referencing() {
    verifies!(
        r#"
== Change substitutions when referencing an attribute

You can also change the substitutions that are applied to an attribute at the time it is resolved.
This is done by manipulating the substitutions applied to the text where it is referenced.
For example, here's how we could get the processor to apply quote substitutions to the value of an attribute:

[source]
----
:app-name: MyApp^2^

[subs="specialchars,attributes,quotes,replacements,macros,post_replacements"]
The application is called {app-name}.
----

Notice that we've swapped the order of the `attributes` and `quotes` substitutions.
This strategy is akin to post-processing the attribute value.
"#
    );

    let mut parser = Parser::default();

    let doc = parser.parse(":app-name: MyApp^2^\n\n[subs=\"specialchars,attributes,quotes,replacements,macros,post_replacements\"]\nThe application is called {app-name}.");

    assert_eq!(
        parser.attribute_value("app-name").as_maybe_str().unwrap(),
        "MyApp^2^"
    );

    let mut blocks = doc.nested_blocks();

    let block1 = blocks.next().unwrap();

    assert_eq!(
        block1,
        &Block::Simple(SimpleBlock {
            content: Content {
                original: Span {
                    data: "The application is called {app-name}.",
                    line: 4,
                    col: 1,
                    offset: 99,
                },
                rendered: "The application is called MyApp<sup>2</sup>.",
            },
            source: Span {
                data: "[subs=\"specialchars,attributes,quotes,replacements,macros,post_replacements\"]\nThe application is called {app-name}.",
                line: 3,
                col: 1,
                offset: 21,
            },
            style: SimpleBlockStyle::Paragraph,
            title_source: None,
            title: None,
            caption: None,
            number: None,
            anchor: None,
            anchor_reftext: None,
            attrlist: Some(Attrlist {
                attributes: &[ElementAttribute {
                    name: Some("subs"),
                    value: "specialchars,attributes,quotes,replacements,macros,post_replacements",
                    shorthand_items: &[],
                },],
                anchor: None,
                source: Span {
                    data: "subs=\"specialchars,attributes,quotes,replacements,macros,post_replacements\"",
                    line: 3,
                    col: 2,
                    offset: 22,
                },
            },),
        },)
    );
}