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
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
use crate::tests::prelude::*;

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

non_normative!(
    r#"
= Attribute Entries

== What is an attribute entry?

Before you can use a document attribute in your document, you have to declare it.
An [.term]*attribute entry* is the primary mechanism for defining a document attribute in an AsciiDoc document.
You can think of an attribute entry as a global variable assignment for AsciiDoc.
The document attribute it creates becomes available from that point forward in the document.
Attribute entries are also frequently used to toggle features.

"#
);

#[test]
fn set_boolean_attribute() {
    verifies!(
        r#"
An attribute entry consists of two parts: an attribute *name* and an attribute *value*.
The attribute name comes first, followed by the optional value.
Each attribute entry must be entered on its own line.
An attribute entry starts with an opening colon (`:`), directly followed by the attribute's name, and then a closing colon (`:`).
This [.term]*sets* -- that is, turns on -- the document attribute so you can use it in your document.

[source]
----
:name-of-an-attribute: <.>
----
<.> The attribute's name is directly preceded with a opening colon (`:`) and directly followed by a closing colon (`:`).

"#
    );

    let mi = crate::document::Attribute::parse(
        crate::Span::new(":name-of-an-attribute:"),
        &Parser::default(),
    )
    .unwrap();

    assert_eq!(
        mi.item,
        Attribute {
            name: Span {
                data: "name-of-an-attribute",
                line: 1,
                col: 2,
                offset: 1,
            },
            value_source: None,
            value: InterpretedValue::Set,
            source: Span {
                data: ":name-of-an-attribute:",
                line: 1,
                col: 1,
                offset: 0,
            },
        }
    );

    assert_eq!(mi.item.value(), &InterpretedValue::Set);
}

#[test]
fn explicit_value() {
    verifies!(
        r#"
In many cases, you explicitly assign a value to a document attribute by entering information after its name in the attribute entry.
The value must be offset from the closing colon (`:`) by at least one space.

[source]
----
:name-of-an-attribute: value of the attribute <.>
----
<.> An explicitly assigned value is offset from the closing colon (`:`) by at least one space.
At the end of the value, press kbd:[Enter].

"#
    );

    let mut parser = Parser::default();

    let doc = parser.parse("= Testing\n:name-of-an-attribute: value of the attribute\n\nThe value of the attribute named `name-of-an-attribute` is: {name-of-an-attribute}");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: Some(Span {
                    data: "Testing",
                    line: 1,
                    col: 3,
                    offset: 2,
                },),
                title: Some("Testing"),
                attributes: &[Attribute {
                    name: Span {
                        data: "name-of-an-attribute",
                        line: 2,
                        col: 2,
                        offset: 11,
                    },
                    value_source: Some(Span {
                        data: "value of the attribute",
                        line: 2,
                        col: 24,
                        offset: 33,
                    }),
                    value: InterpretedValue::Value("value of the attribute"),
                    source: Span {
                        data: ":name-of-an-attribute: value of the attribute",
                        line: 2,
                        col: 1,
                        offset: 10,
                    },
                },],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: "= Testing\n:name-of-an-attribute: value of the attribute",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "The value of the attribute named `name-of-an-attribute` is: {name-of-an-attribute}",
                        line: 4,
                        col: 1,
                        offset: 57,
                    },
                    rendered: "The value of the attribute named <code>name-of-an-attribute</code> is: value of the attribute",
                },
                source: Span {
                    data: "The value of the attribute named `name-of-an-attribute` is: {name-of-an-attribute}",
                    line: 4,
                    col: 1,
                    offset: 57,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },),],
            source: Span {
                data: "= Testing\n:name-of-an-attribute: value of the attribute\n\nThe value of the attribute named `name-of-an-attribute` is: {name-of-an-attribute}",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog::default(),
        }
    );

    assert_eq!(
        parser.attribute_value("name-of-an-attribute"),
        InterpretedValue::Value("value of the attribute")
    );
}

#[test]
fn header_substitutions_applied() {
    verifies!(
        r#"
Take note that xref:attribute-entry-substitutions.adoc[header substitutions] automatically get applied to the value by default.
That means you don't need to escape special characters such in an HTML tag.
"#
    );

    let mut parser = Parser::default();

    let doc = parser.parse("= Testing\n:lt-attribute: <\n\nThe value of the attribute named `lt-attribute` is: {lt-attribute}");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: Some(Span {
                    data: "Testing",
                    line: 1,
                    col: 3,
                    offset: 2,
                },),
                title: Some("Testing"),
                attributes: &[Attribute {
                    name: Span {
                        data: "lt-attribute",
                        line: 2,
                        col: 2,
                        offset: 11,
                    },
                    value_source: Some(Span {
                        data: "<",
                        line: 2,
                        col: 16,
                        offset: 25,
                    },),
                    value: InterpretedValue::Value("&lt;"),
                    source: Span {
                        data: ":lt-attribute: <",
                        line: 2,
                        col: 1,
                        offset: 10,
                    },
                },],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: "= Testing\n:lt-attribute: <",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "The value of the attribute named `lt-attribute` is: {lt-attribute}",
                        line: 4,
                        col: 1,
                        offset: 28,
                    },
                    rendered: "The value of the attribute named <code>lt-attribute</code> is: &lt;",
                },
                source: Span {
                    data: "The value of the attribute named `lt-attribute` is: {lt-attribute}",
                    line: 4,
                    col: 1,
                    offset: 28,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },),],
            source: Span {
                data: "= Testing\n:lt-attribute: <\n\nThe value of the attribute named `lt-attribute` is: {lt-attribute}",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog::default(),
        }
    );
}

#[test]
fn reference_existing_attributes() {
    verifies!(
        r#"
It also means you can reference the value of attributes which have already been defined when defining the value of an attribute.
Attribute references in the value of an attribute entry are resolved immediately.

[source]
----
:url-org: https://example.org/projects
:url-project: {url-org}/project-name <.>
----
<.> You can reuse the value of an attribute which has already been set using using an attribute reference in the value.

"#
    );

    let mut parser = Parser::default();

    let doc = parser
        .parse(":url-org: https://example.org/projects\n:url-project: {url-org}/project-name");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: None,
                title: None,
                attributes: &[
                    Attribute {
                        name: Span {
                            data: "url-org",
                            line: 1,
                            col: 2,
                            offset: 1,
                        },
                        value_source: Some(Span {
                            data: "https://example.org/projects",
                            line: 1,
                            col: 11,
                            offset: 10,
                        },),
                        value: InterpretedValue::Value("https://example.org/projects",),
                        source: Span {
                            data: ":url-org: https://example.org/projects",
                            line: 1,
                            col: 1,
                            offset: 0,
                        },
                    },
                    Attribute {
                        name: Span {
                            data: "url-project",
                            line: 2,
                            col: 2,
                            offset: 40,
                        },
                        value_source: Some(Span {
                            data: "{url-org}/project-name",
                            line: 2,
                            col: 15,
                            offset: 53,
                        },),
                        value: InterpretedValue::Value("https://example.org/projects/project-name",),
                        source: Span {
                            data: ":url-project: {url-org}/project-name",
                            line: 2,
                            col: 1,
                            offset: 39,
                        },
                    },
                ],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: ":url-org: https://example.org/projects\n:url-project: {url-org}/project-name",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[],
            source: Span {
                data: ":url-org: https://example.org/projects\n:url-project: {url-org}/project-name",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog::default(),
        }
    );

    assert_eq!(
        parser.attribute_value("url-project"),
        InterpretedValue::Value("https://example.org/projects/project-name")
    );
}

#[test]
fn implicit_value() {
    verifies!(
        r#"
Some built-in attributes don't require a value to be explicitly assigned in an attribute entry because they're a boolean attribute or have an implied value.

[source]
----
:name-of-an-attribute: <.>
----
<.> If you don't want to explicitly assign a value to the attribute, press kbd:[Enter] after the closing colon (`:`).

When set, the value of a built-in boolean attribute is always empty (i.e., an _empty string_).
If you set a built-in attribute and leave its value empty, the AsciiDoc processor may infer a value at processing time.

"#
    );

    let mut parser = Parser::default();

    let doc = parser.parse(":name-of-an-attribute:");

    assert_eq!(
        doc,
        Document {
            header: Header {
                title_source: None,
                title: None,
                attributes: &[Attribute {
                    name: Span {
                        data: "name-of-an-attribute",
                        line: 1,
                        col: 2,
                        offset: 1,
                    },
                    value_source: None,
                    value: InterpretedValue::Set,
                    source: Span {
                        data: ":name-of-an-attribute:",
                        line: 1,
                        col: 1,
                        offset: 0,
                    },
                },],
                author_line: None,
                revision_line: None,
                comments: &[],
                source: Span {
                    data: ":name-of-an-attribute:",
                    line: 1,
                    col: 1,
                    offset: 0,
                },
            },
            blocks: &[],
            source: Span {
                data: ":name-of-an-attribute:",
                line: 1,
                col: 1,
                offset: 0,
            },
            warnings: &[],
            source_map: SourceMap(&[]),
            catalog: Catalog::default(),
        }
    );

    assert_eq!(
        parser.attribute_value("name-of-an-attribute"),
        InterpretedValue::Set
    );
}

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

    non_normative!(
        r#"
== Where can an attribute entry be declared?

An attribute entry is most often declared in the document header.
"#
    );

    #[test]
    fn declared_between_blocks() {
        verifies!(
            r#"
For attributes that allow it (which includes general purpose attributes), the attribute entry can alternately be declared between blocks in the document body (i.e., the portion of the document below the header).

"#
        );
        let mut parser = Parser::default().with_intrinsic_attribute(
            "agreed",
            "yes",
            ModificationContext::Anywhere,
        );

        let doc =
            parser.parse("We are agreed? {agreed}\n\n:agreed: no\n\nAre we still agreed? {agreed}");

        let mut blocks = doc.nested_blocks();

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

        assert_eq!(
            block1,
            &Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "We are agreed? {agreed}",
                        line: 1,
                        col: 1,
                        offset: 0,
                    },
                    rendered: "We are agreed? yes",
                },
                source: Span {
                    data: "We are agreed? {agreed}",
                    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,
            })
        );

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

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

        assert_eq!(
            block3,
            &Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "Are we still agreed? {agreed}",
                        line: 5,
                        col: 1,
                        offset: 38,
                    },
                    rendered: "Are we still agreed? no",
                },
                source: Span {
                    data: "Are we still agreed? {agreed}",
                    line: 5,
                    col: 1,
                    offset: 38,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            })
        );

        let mut warnings = doc.warnings();
        assert!(warnings.next().is_none());
    }

    non_normative!(
        r#"
WARNING: An attribute entry should not be declared inside the boundaries of a delimited block.
When an attribute entry is declared inside a delimited block, the behavior is undefined.
What's certain is that preprocessor directives (i.e., include directive, conditional directives) cannot see attributes defined inside a delimited block.

When an attribute is defined in the document header using an attribute entry, that's referred to as a header attribute.
A header attribute is available to the entire document until it is unset.
A header attribute is also accessible from the document metadata for use by built-in behavior, extensions, and other applications that need to consult its value (e.g., `source-highlighter`).

When an attribute is defined in the document body using an attribute entry, that's simply referred to as a document attribute.
For any attribute defined in the body, the attribute is available from the point it is set until it is unset.
Attributes defined in the body are not available via the document metadata.

Unless the attribute is locked, it can be unset or assigned a new value in the document header or body.
However, note that unsetting or redefining a header attribute that controls behavior in the document body usually has no affect.
See the xref:document-attributes-ref.adoc[] for where in a document each attribute can be set.

"#
    );
}

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

    // Non-normative because we have a different API and no CLI.
    non_normative!(
        r#"
== Defining document attributes without an attribute entry

Document attributes can also be declared (set with an optional value or unset) outside the document via the CLI and API.
The attribute entry syntax is not used in these cases.
Rather, they are declared using the provided option.
For the API, attributes are declared using the `:attributes` option (which supports various entry formats).
For the CLI, the attribute is declared using the `-a` option.

"#
    );

    #[test]
    fn no_substitutions_applied() {
        verifies!(
            r#"
When an attribute is assigned a value outside of the document, the value is stored as is, meaning substitutions are not applied to it.
That also means that the xref:subs:index.adoc[special characters and quote substitutions] are not applied to the value of that attribute when it is referenced in the document.
However, subsequent substitutions, such as the macro substitution, do get applied.
This behavior is due to that fact that the attributes substitution is applied after the special characters and quote substitutions.
In order to force these substitutions to be applied to the value of the attribute, you must alter the substitution order at the point of reference.
Here's an example using the inline pass macro.

[,asciidoc]
----
pass:a,q[{attribute-with-formatted-text}]
----

"#
        );

        let mut parser = Parser::default().with_intrinsic_attribute(
            "attribute-with-formatted-text",
            "attribute with *formatted* _text_",
            ModificationContext::Anywhere,
        );

        let doc =
            parser.parse("formatting applied: pass:a,q[{attribute-with-formatted-text}]\n\nformatting suppressed: {attribute-with-formatted-text}");

        let mut blocks = doc.nested_blocks();

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

        assert_eq!(
            block1,
            &Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "formatting applied: pass:a,q[{attribute-with-formatted-text}]",
                        line: 1,
                        col: 1,
                        offset: 0,
                    },
                    rendered: "formatting applied: attribute with <strong>formatted</strong> <em>text</em>",
                },
                source: Span {
                    data: "formatting applied: pass:a,q[{attribute-with-formatted-text}]",
                    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,
            })
        );

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

        assert_eq!(
            block2,
            &Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "formatting suppressed: {attribute-with-formatted-text}",
                        line: 3,
                        col: 1,
                        offset: 63,
                    },
                    rendered: "formatting suppressed: attribute with *formatted* _text_",
                },
                source: Span {
                    data: "formatting suppressed: {attribute-with-formatted-text}",
                    line: 3,
                    col: 1,
                    offset: 63,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            })
        );
    }

    // Non-normative because we have a different API and no CLI.
    non_normative!(
        r#"
When an attribute is declared from the command line or API, it is implicitly a document header attribute.
By default, the attribute becomes locked (i.e., hard set or unset) and thus cannot be changed by the document.
This behavior can be changed by adding an `@` to the end of the attribute name or value (i.e., the soft set modifier).
See xref:assignment-precedence.adoc[] for more information.

The one exception to this rule is the `sectnums` attribute, which can always be changed.

"#
    );
}

// Non-normative because this block is commented out.
non_normative!(
    r#"
////
An exclamation point (`!`) before (or after) the attribute name unsets the attribute.

[source]
----
:!name: <1>
----
<1> The leading `!` indicates this attribute should be unset.
In this case, the value is ignored.

An attribute entry must start at the beginning of the line.
If the attribute entry follows a paragraph, it must be offset by an empty line.
////
"#
);