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
703
704
705
706
707
708
709
710
711
use crate::{blocks::ContentModel, tests::prelude::*};

track_file!("ref/asciidoc-lang/docs/modules/blocks/pages/add-title.adoc");

non_normative!(
    r#"
= Add a Title to a Block

You can assign a title to a block, whether it's styled using its style name or delimiters.

"#
);

#[test]
fn block_title_syntax() {
    verifies!(
        r#"
== Block title syntax

A block title is defined on its own line directly above the block's attribute list, opening delimiter, or block content--which ever comes first.
As shown in <<ex-basic>>, the line must begin with a dot (`.`) and immediately be followed by the text of the title.
The block title must only occupy a single line and thus cannot be wrapped.

.Block title syntax
[#ex-basic]
----
.This is the title of a sidebar block
****
This is the content of the sidebar block.
****
----

"#
    );

    let mut parser = Parser::default();

    let block = crate::blocks::Block::parse(crate::Span::new(
        ".This is the title of a sidebar block\n****\nThis is the content of the sidebar block.\n****\n",
    ), &mut parser)
    .unwrap_if_no_warnings()
    .unwrap()
    .item;

    assert_eq!(
        block,
        Block::CompoundDelimited(CompoundDelimitedBlock {
            blocks: &[Block::Simple(SimpleBlock {
                content: Content {
                    original: Span {
                        data: "This is the content of the sidebar block.",
                        line: 3,
                        col: 1,
                        offset: 43,
                    },
                    rendered: "This is the content of the sidebar block.",
                },
                source: Span {
                    data: "This is the content of the sidebar block.",
                    line: 3,
                    col: 1,
                    offset: 43,
                },
                style: SimpleBlockStyle::Paragraph,
                title_source: None,
                title: None,
                caption: None,
                number: None,
                anchor: None,
                anchor_reftext: None,
                attrlist: None,
            },),],
            context: "sidebar",
            source: Span {
                data: ".This is the title of a sidebar block\n****\nThis is the content of the sidebar block.\n****",
                line: 1,
                col: 1,
                offset: 0,
            },
            title_source: Some(Span {
                data: "This is the title of a sidebar block",
                line: 1,
                col: 2,
                offset: 1,
            },),
            title: Some("This is the title of a sidebar block"),
            caption: None,
            number: None,
            anchor: None,
            anchor_reftext: None,
            attrlist: None,
        },)
    );
}

non_normative!(
    r#"
CAUTION: The block title line should not be confused with an ordered list item that uses the `.` marker.
A block title line has no space after the `.`, whereas the space after a list marker is required.

The next sections will show how to add titles to delimited blocks and blocks with attribute lists.

"#
);

#[test]
fn add_title_to_delimited_block() {
    verifies!(
        r#"
== Add a title to a delimited block

Any delimited block can have a title.
If the block doesn't have an attribute list, enter the title on a new line directly above the opening delimiter.
The delimited literal block in <<ex-title>> is titled _Terminal Output_.

.Add a title to a delimited block
[#ex-title]
----
.Terminal Output <.>
.... <.>
From github.com:asciidoctor/asciidoctor
 * branch        main   -> FETCH_HEAD
Already up to date.
....
----
<.> The block title is entered on a new line.
The title must begin with a dot (`.`).
Don't put a space between the dot and the first character of the title.
<.> If you aren't applying attributes to a block, enter the opening delimiter on a new line directly after the title.

"#
    );

    let mut parser = Parser::default();

    let block = crate::blocks::Block::parse(crate::Span::new(
        ".Terminal Output\n....\nFrom github.com:asciidoctor/asciidoctor\n* branch        main   -> FETCH_HEAD\nAlready up to date.\n....\n",
    ), &mut parser)
    .unwrap_if_no_warnings()
    .unwrap()
    .item;

    assert_eq!(
        block,
        Block::RawDelimited(RawDelimitedBlock {
            content: Content {
                original: Span {
                    data: "From github.com:asciidoctor/asciidoctor\n* branch        main   -> FETCH_HEAD\nAlready up to date.",
                    line: 3,
                    col: 1,
                    offset: 22,
                },
                rendered: "From github.com:asciidoctor/asciidoctor\n* branch        main   -&gt; FETCH_HEAD\nAlready up to date.",
            },
            content_model: ContentModel::Verbatim,
            context: "literal",
            source: Span {
                data: ".Terminal Output\n....\nFrom github.com:asciidoctor/asciidoctor\n* branch        main   -> FETCH_HEAD\nAlready up to date.\n....",
                line: 1,
                col: 1,
                offset: 0,
            },
            title_source: Some(Span {
                data: "Terminal Output",
                line: 1,
                col: 2,
                offset: 1,
            },),
            title: Some("Terminal Output"),
            caption: None,
            number: None,
            anchor: None,
            anchor_reftext: None,
            attrlist: None,
            substitution_group: SubstitutionGroup::Verbatim,
        },)
    );
}

non_normative!(
    r#"
The result of <<ex-title>> is displayed below.

.Terminal Output
....
From github.com:asciidoctor/asciidoctor
 * branch        main   -> FETCH_HEAD
Already up to date.
....

In the next section, you'll see how a title is placed on a block that has an attribute list.

"#
);

#[test]
fn add_title_to_block_with_attributes() {
    verifies!(
        r#"
== Add a title to a block with attributes

When you're applying attributes to a block, the title is placed on the line above the attribute list (or lists).
<<ex-title-list>> shows a delimited source code block that's titled _Specify GitLab CI stages_.

.Add a title to a delimited source code block
[source#ex-title-list]
....
.Specify GitLab CI stages <.>
[source,yaml] <.>
----
image: node:16-buster
stages: [ init, verify, deploy ]
----
....
<.> The block title is entered on a new line.
<.> The block's attribute list is entered on a new line directly after the title.

"#
    );

    let mut parser = Parser::default();

    let block = crate::blocks::Block::parse(crate::Span::new(
        ".Specify GitLab CI stages\n[source,yaml]\n----\nimage: node:16-buster\nstages: [ init, verify, deploy ]\n----",
    ), &mut parser)
    .unwrap_if_no_warnings()
    .unwrap()
    .item;

    assert_eq!(
        block,
        Block::RawDelimited(RawDelimitedBlock {
            content: Content {
                original: Span {
                    data: "image: node:16-buster\nstages: [ init, verify, deploy ]",
                    line: 4,
                    col: 1,
                    offset: 45,
                },
                rendered: "image: node:16-buster\nstages: [ init, verify, deploy ]",
            },
            content_model: ContentModel::Verbatim,
            context: "listing",
            source: Span {
                data: ".Specify GitLab CI stages\n[source,yaml]\n----\nimage: node:16-buster\nstages: [ init, verify, deploy ]\n----",
                line: 1,
                col: 1,
                offset: 0,
            },
            title_source: Some(Span {
                data: "Specify GitLab CI stages",
                line: 1,
                col: 2,
                offset: 1,
            },),
            title: Some("Specify GitLab CI stages"),
            caption: None,
            number: None,
            anchor: None,
            anchor_reftext: None,
            attrlist: Some(Attrlist {
                attributes: &[
                    ElementAttribute {
                        name: None,
                        shorthand_items: &["source"],
                        value: "source"
                    },
                    ElementAttribute {
                        name: None,
                        shorthand_items: &[],
                        value: "yaml"
                    },
                ],
                anchor: None,
                source: Span {
                    data: "source,yaml",
                    line: 2,
                    col: 2,
                    offset: 27,
                },
            },),
            substitution_group: SubstitutionGroup::Verbatim,
        },)
    );
}

non_normative!(
    r#"
The result of <<ex-title-list>> is displayed below.

[caption=]
.Specify GitLab CI stages
[source,yaml]
----
image: node:16-buster
stages: [ init, verify, deploy ]
----

"#
);

#[test]
fn add_title_to_non_delimited_block() {
    verifies!(
        r#"
As shown in <<ex-title-style>>, a block's title is placed above the attribute list when a block isn't delimited.

.Add a title to a non-delimited block
[#ex-title-style]
----
.Mint
[sidebar]
Mint has visions of global conquest.
If you don't plant it in a container, it will take over your garden.
----

"#
    );

    let mut parser = Parser::default();

    let block = crate::blocks::Block::parse(crate::Span::new(
        ".Mint\n[sidebar]\nMint has visions of global conquest.\nIf you don't plant it in a container, it will take over your garden.\n",
    ), &mut parser)
    .unwrap_if_no_warnings()
    .unwrap()
    .item;

    assert_eq!(
        block,
        Block::Simple(SimpleBlock {
            content: Content {
                original: Span {
                    data: "Mint has visions of global conquest.\nIf you don't plant it in a container, it will take over your garden.",
                    line: 3,
                    col: 1,
                    offset: 16,
                },
                rendered: "Mint has visions of global conquest.\nIf you don&#8217;t plant it in a container, it will take over your garden.",
            },
            source: Span {
                data: ".Mint\n[sidebar]\nMint has visions of global conquest.\nIf you don't plant it in a container, it will take over your garden.",
                line: 1,
                col: 1,
                offset: 0,
            },
            style: SimpleBlockStyle::Paragraph,
            title_source: Some(Span {
                data: "Mint",
                line: 1,
                col: 2,
                offset: 1,
            },),
            title: Some("Mint"),
            caption: None,
            number: None,
            anchor: None,
            anchor_reftext: None,
            attrlist: Some(Attrlist {
                attributes: &[ElementAttribute {
                    name: None,
                    shorthand_items: &["sidebar"],
                    value: "sidebar"
                },],
                anchor: None,
                source: Span {
                    data: "sidebar",
                    line: 2,
                    col: 2,
                    offset: 7,
                },
            },),
        },)
    );

    // The result of <<ex-title-style>> is displayed below.

    // .Mint
    // [sidebar]
    // Mint has visions of global conquest.
    // If you don't plant it in a container, it will take over your garden.

    // You may notice that unlike the titles in the previous rendered listing
    // and source block examples, the sidebar's title is centered and
    // displayed inside the sidebar's background. How the title of a block
    // is displayed depends on the converter and stylesheet you're applying
    // to your AsciiDoc documents.
}

non_normative!(
    r#"
The result of <<ex-title-style>> is displayed below.

.Mint
[sidebar]
Mint has visions of global conquest.
If you don't plant it in a container, it will take over your garden.

You may notice that unlike the titles in the previous rendered listing and source block examples, the sidebar's title is centered and displayed inside the sidebar's background.
How the title of a block is displayed depends on the converter and stylesheet you're applying to your AsciiDoc documents.

"#
);

#[test]
fn captioned_titles() {
    verifies!(
        r#"
== Captioned titles

Several block contexts support captioned titles.
A [.term]*captioned title* is a title that's prefixed with a caption label and a number followed by a dot (e.g., `Table 1. Properties`).

The captioned title is only used if the corresponding caption attribute is set.
Otherwise, the original title is displayed.

The following table lists the blocks that support captioned titles and the attributes that the converter uses to generate and control them.

"#
    );

    // A titled, captionable block is given a caption: a label and an
    // automatically assigned number, followed by a dot and a space (e.g.
    // `Example 1. `).
    let doc = Parser::default().parse(".Block content title\n====\nBlock content.\n====");
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.caption(), Some("Example 1. "));
    assert_eq!(block.number(), Some(1));

    // The caption is only applied when the corresponding caption attribute is
    // set. The `listing-caption` attribute is unset by default, so a titled
    // listing keeps just its original title with no caption...
    let doc = Parser::default().parse(".Terminal\n----\ncode\n----");
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.title(), Some("Terminal"));
    assert_eq!(block.caption(), None);

    // ...whereas setting `listing-caption` enables the captioned title.
    let doc = Parser::default().parse(":listing-caption: Listing\n\n.Terminal\n----\ncode\n----");
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.caption(), Some("Listing 1. "));
}

#[test]
fn blocks_that_support_captioned_titles() {
    verifies!(
        r#"
.Blocks that support captioned titles
[cols=1;m;m]
|===
|Block context | Caption attribute | Counter attribute

|appendix
|appendix-caption
|appendix-number

|example
|example-caption
|example-number

|image
|figure-caption
|figure-number

|listing, source
|listing-caption
|listing-number

|table
|table-caption
|table-number
|===

All caption attributes are set by default except for the attribute for listing and source blocks (`listing-caption`).
The number is sequential, computed automatically, and stored in a corresponding counter attribute.

"#
    );

    // appendix -> appendix-caption: a level-1 `[appendix]` section is captioned
    // with the `appendix-caption` label ("Appendix" by default).
    let doc = Parser::default().parse("= Doc\n\n[appendix]\n== Acknowledgements\n\nThanks.");
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.caption(), Some("Appendix A: "));

    // example -> example-caption, counted via example-number (set by default).
    let doc = Parser::default().parse(".Onomatopoeia\n====\nboom\n====");
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.caption(), Some("Example 1. "));
    assert_eq!(block.number(), Some(1));

    // image -> figure-caption, counted via figure-number (set by default).
    let doc = Parser::default().parse(".Sunset\nimage::sunset.jpg[]");
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.caption(), Some("Figure 1. "));
    assert_eq!(block.number(), Some(1));

    // listing, source -> listing-caption. Unlike the other contexts,
    // `listing-caption` is *not* set by default, so a titled listing or source
    // block has no caption until the attribute is set...
    let doc = Parser::default().parse(".Output\n----\ncode\n----");
    assert_eq!(doc.nested_blocks().next().unwrap().caption(), None);
    let doc = Parser::default().parse(".Output\n[source,ruby]\n----\ncode\n----");
    assert_eq!(doc.nested_blocks().next().unwrap().caption(), None);
    // ...whereupon both the listing and source contexts are captioned via
    // `listing-caption` (a source block resolves to the `listing` context).
    let doc = Parser::default().parse(":listing-caption: Listing\n\n.Output\n----\ncode\n----");
    assert_eq!(
        doc.nested_blocks().next().unwrap().caption(),
        Some("Listing 1. ")
    );
    let doc = Parser::default()
        .parse(":listing-caption: Listing\n\n.Output\n[source,ruby]\n----\ncode\n----");
    assert_eq!(
        doc.nested_blocks().next().unwrap().caption(),
        Some("Listing 1. ")
    );

    // table -> table-caption, counted via table-number (set by default).
    let doc = Parser::default().parse(".Properties\n|===\n|Name |Value\n|===");
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.caption(), Some("Table 1. "));
    assert_eq!(block.number(), Some(1));

    // The number is sequential and computed automatically: two titled examples
    // are numbered 1 and 2 in document order.
    let doc = Parser::default().parse(".One\n====\na\n====\n\n.Two\n====\nb\n====");
    let numbers: Vec<_> = doc.nested_blocks().map(|b| b.number()).collect();
    assert_eq!(numbers, vec![Some(1), Some(2)]);

    // The number is stored in the context's counter attribute (here
    // `example-number`), so a later reference to that attribute resolves to the
    // assigned number.
    assert_eq!(
        rendered_paragraphs(
            &Parser::default().parse(".Onomatopoeia\n====\nboom\n====\n\n{example-number}")
        ),
        vec!["boom".to_string(), "1".to_string()]
    );
}

#[test]
fn captioned_title_example_block() {
    verifies!(
        r#"
Let's assume you've added a title to an example block as follows:

[,asciidoc]
----
.Block that supports captioned title
====
Block content
====
----

The block title will be displayed with a caption label and number, as shown here:

"#
    );

    let doc =
        Parser::default().parse(".Block that supports captioned title\n====\nBlock content\n====");
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.title(), Some("Block that supports captioned title"));
    assert_eq!(block.caption(), Some("Example 1. "));
    assert_eq!(block.number(), Some(1));
}

#[test]
fn example_number_counter_save_and_restore() {
    // This block is documentation scaffolding that saves and restores the
    // `example-number` counter (via `ifdef::` conditional directives) so the
    // rendered example below always displays as `Example 1.`, regardless of the
    // counter's value in the surrounding document.
    verifies!(
        r#"
:example-caption: Example
ifdef::example-number[:prev-example-number: {example-number}]
:example-number: 0

.Block that supports captioned title
====
Block content
====

:!example-caption:
ifdef::prev-example-number[:example-number: {prev-example-number}]
:!prev-example-number:

"#
    );

    // On a pristine parser `example-number` is unset, so both conditionals are
    // false: the save and restore are no-ops, the counter is reset to 0, and the
    // example is numbered 1 and displayed with its caption.
    let doc = Parser::default().parse(
        ":example-caption: Example\nifdef::example-number[:prev-example-number: {example-number}]\n:example-number: 0\n\n.Block that supports captioned title\n====\nBlock content\n====\n\n:!example-caption:\nifdef::prev-example-number[:example-number: {prev-example-number}]\n:!prev-example-number:",
    );
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.title(), Some("Block that supports captioned title"));
    assert_eq!(block.caption(), Some("Example 1. "));
    assert_eq!(block.number(), Some(1));

    // When `example-number` is already set, the idiom saves it, resets the
    // counter so the scaffolded example is numbered 1, then restores it so a
    // later example continues the original sequence (7 -> restored -> 8).
    let doc = Parser::default().parse(
        ":example-number: 7\n\n:example-caption: Example\nifdef::example-number[:prev-example-number: {example-number}]\n:example-number: 0\n\n.Saved\n====\nx\n====\n\nifdef::prev-example-number[:example-number: {prev-example-number}]\n\n.Restored\n====\ny\n====",
    );
    let mut examples = doc.nested_blocks().filter(|b| b.title().is_some());
    // The first conditional fired (`example-number` was set), saving 7 into
    // `prev-example-number`; the counter was then reset to 0, so this example is 1.
    let saved = examples.next().unwrap();
    assert_eq!(saved.number(), Some(1));
    // The second conditional fired (`prev-example-number` was set), restoring
    // `example-number` to 7, so the following example continues at 8.
    let restored = examples.next().unwrap();
    assert_eq!(restored.number(), Some(8));
}

#[test]
fn unset_example_caption_drops_caption() {
    verifies!(
        r#"
If you unset the `example-caption` attribute, the caption will not be prepended to the title.

.Block that supports captioned title
====
Block content
====

"#
    );

    let doc = Parser::default().parse(
        ":!example-caption:\n\n.Block that supports captioned title\n====\nBlock content\n====",
    );
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.title(), Some("Block that supports captioned title"));
    assert_eq!(block.caption(), None);
    assert_eq!(block.number(), None);
}

#[test]
fn counter_attribute_influences_start_number() {
    verifies!(
        r#"
The counter attribute (e.g., `example-number`) can be used to influence the start number for the first block with that context or the next number selected in the sequence for subsequent occurrences.
However, this practice should be used judiciously.

"#
    );

    // Seeding `example-number` influences the next number in the sequence: with
    // the counter set to 5, the following example is numbered 6.
    let doc = Parser::default().parse(":example-number: 5\n\n.Later\n====\nx\n====");
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.caption(), Some("Example 6. "));
    assert_eq!(block.number(), Some(6));
}

#[test]
fn custom_caption_override() {
    verifies!(
        r#"
The caption can be overridden using the `caption` attribute on the block.
The value of the caption attribute replaces the entire caption, including the space that precedes the title.

Here's how to define a custom caption on a block:

[,asciidoc]
----
.Block Title
[caption="Example {counter:my-example-number:A}: "]
====
Block content
====
----

Here's how the block will be displayed with the custom caption:

.Block Title
[caption="Example {counter:my-example-number:A}: "]
====
Block content
====

"#
    );

    let doc = Parser::default().parse(
        ".Block Title\n[caption=\"Example {counter:my-example-number:A}: \"]\n====\nBlock content\n====",
    );
    let block = doc.nested_blocks().next().unwrap();
    assert_eq!(block.title(), Some("Block Title"));

    // The custom caption replaces the entire caption verbatim (the counter
    // reference is resolved), including the trailing space, and the block is not
    // auto-numbered.
    assert_eq!(block.caption(), Some("Example A: "));
    assert_eq!(block.number(), None);
}

non_normative!(
    r#"
Notice we've used a counter attribute in the value of the caption attribute to create a custom number sequence.

If you refer to a block with a custom caption using an xref, you may not get the result that you expect.
Therefore, it's always best to define custom xref:attributes:id.adoc#customize-automatic-xreftext[xreftext] when you define a custom caption.
"#
);