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
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
use std::slice::Iter;

use crate::{
    HasSpan, Parser, Span,
    attributes::Attrlist,
    blocks::{
        AdmonitionVariant, Block, CompoundDelimitedBlock, ContentModel, IsBlock, RawDelimitedBlock,
        SimpleBlock, TableBlock, metadata::BlockMetadata,
    },
    content::Content,
    document::InterpretedValue,
    internal::debug::DebugSliceReference,
    span::MatchedItem,
    strings::CowStr,
    warnings::MatchAndWarnings,
};

/// An admonition draws attention to a statement by taking it out of the
/// content's flow and labeling it with a priority (its type).
///
/// An admonition can be written in two ways:
///
/// * As a **paragraph** whose first line begins with one of the five admonition
///   labels followed by a colon (e.g., `NOTE: This is a note.`). This produces
///   an admonition with the [`Simple`] content model.
/// * As a **block** by setting one of the labels as the block style in an
///   attribute list (e.g., `[NOTE]`). This is referred to as *masquerading*.
///   When the style is set on a delimited block that can contain other blocks
///   (such as an example block), the admonition uses the [`Compound`] content
///   model; otherwise it uses the [`Simple`] content model.
///
/// [`Simple`]: ContentModel::Simple
/// [`Compound`]: ContentModel::Compound
#[derive(Clone, Eq, PartialEq)]
pub struct AdmonitionBlock<'src> {
    variant: AdmonitionVariant,
    label: String,
    icons_font: bool,
    content_model: ContentModel,
    content: Option<Content<'src>>,
    blocks: Vec<Block<'src>>,
    source: Span<'src>,
    title_source: Option<Span<'src>>,
    title: Option<String>,
    anchor: Option<Span<'src>>,
    anchor_reftext: Option<Span<'src>>,
    attrlist: Option<Attrlist<'src>>,
}

impl<'src> AdmonitionBlock<'src> {
    /// Parse an admonition block, if the given metadata and content describe
    /// one.
    ///
    /// Returns `None` (without consuming the block) when the content is not an
    /// admonition, so that the caller can fall through to other block parsers.
    pub(crate) fn parse(
        metadata: &BlockMetadata<'src>,
        parser: &mut Parser,
    ) -> Option<MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>>> {
        // Masquerade form: the block style names an admonition type.
        if let Some(style) = metadata.attrlist.as_ref().and_then(|a| a.block_style())
            && let Some(variant) = AdmonitionVariant::from_style(style)
        {
            return Some(Self::parse_masquerade(metadata, parser, variant));
        }

        // Paragraph form: the first line begins with `LABEL: `. This form does
        // not apply when a block style was declared (that would be a
        // masquerade), so it is only reached when there is no admonition style.
        if metadata
            .attrlist
            .as_ref()
            .and_then(|a| a.block_style())
            .is_none()
            && let Some((variant, content_start)) =
                admonition_paragraph_prefix(metadata.block_start)
        {
            return Some(Self::parse_paragraph(
                metadata,
                parser,
                variant,
                content_start,
            ));
        }

        None
    }

    /// Parse the masquerade form, where the admonition style is set on a block.
    fn parse_masquerade(
        metadata: &BlockMetadata<'src>,
        parser: &mut Parser,
        variant: AdmonitionVariant,
    ) -> MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>> {
        let first_line = metadata.block_start.take_normalized_line().item;

        // An admonition style masquerades only over an example block, an open
        // block, or a paragraph. Over an example or open block it yields
        // compound content.
        //
        // For a valid example/open delimiter, `CompoundDelimitedBlock::parse`
        // always returns `item: Some(..)` — even for an unterminated block, in
        // which case it also reports an `UnterminatedDelimitedBlock` warning.
        // Those `warnings` are bound here and forwarded through `finish`, so no
        // diagnostic is lost on the masquerade path.
        if is_example_or_open_delimiter(&first_line)
            && let Some(MatchAndWarnings {
                item: Some(inner),
                warnings,
            }) = CompoundDelimitedBlock::parse(metadata, parser)
        {
            let after = inner.after;
            let blocks = inner.item.into_nested_blocks();
            return Self::finish(
                metadata,
                parser,
                variant,
                ContentModel::Compound,
                None,
                blocks,
                after,
                warnings,
            );
        }

        // Any other structural container (sidebar, quote, listing, literal,
        // passthrough, table, comment) keeps its own context and ignores the
        // admonition style, so this is not an admonition.
        if RawDelimitedBlock::is_valid_delimiter(&first_line)
            || CompoundDelimitedBlock::is_valid_delimiter(&first_line)
            || TableBlock::is_table_delimiter(&first_line)
        {
            return MatchAndWarnings {
                item: None,
                warnings: vec![],
            };
        }

        // Otherwise the style is applied to a paragraph: simple content.
        if let Some(inner) = SimpleBlock::parse(metadata, parser) {
            return Self::finish(
                metadata,
                parser,
                variant,
                ContentModel::Simple,
                Some(inner.item.content().clone()),
                vec![],
                inner.after,
                vec![],
            );
        }

        MatchAndWarnings {
            item: None,
            warnings: vec![],
        }
    }

    /// Parse the paragraph form, where the first line begins with `LABEL: `.
    fn parse_paragraph(
        metadata: &BlockMetadata<'src>,
        parser: &mut Parser,
        variant: AdmonitionVariant,
        content_start: Span<'src>,
    ) -> MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>> {
        // Build a metadata that begins after the stripped label so that the
        // paragraph content is parsed without it. The admonition retains the
        // outer block's title, anchor, and attribute list.
        let inner_metadata = BlockMetadata {
            title_source: None,
            title: None,
            anchor: None,
            anchor_reftext: None,
            attrlist: None,
            source: content_start,
            block_start: content_start,
        };

        if let Some(inner) = SimpleBlock::parse(&inner_metadata, parser) {
            return Self::finish(
                metadata,
                parser,
                variant,
                ContentModel::Simple,
                Some(inner.item.content().clone()),
                vec![],
                inner.after,
                vec![],
            );
        }

        // Unreachable in practice: `parse_paragraph` is only called after
        // `admonition_paragraph_prefix` matched, which requires a non-whitespace
        // character after the label on the first line (trailing whitespace is
        // trimmed before the match). `content_start` therefore points at
        // non-empty content, so `SimpleBlock::parse` always returns `Some`. This
        // fall-through is kept as a defensive default that mirrors
        // `parse_masquerade`.
        MatchAndWarnings {
            item: None,
            warnings: vec![],
        }
    }

    /// Assemble the final admonition block from its parsed parts, resolving the
    /// caption and icon state from the current document attributes.
    #[allow(clippy::too_many_arguments)]
    fn finish(
        metadata: &BlockMetadata<'src>,
        parser: &Parser,
        variant: AdmonitionVariant,
        content_model: ContentModel,
        content: Option<Content<'src>>,
        blocks: Vec<Block<'src>>,
        after: Span<'src>,
        warnings: Vec<crate::warnings::Warning<'src>>,
    ) -> MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>> {
        let source = metadata
            .source
            .trim_remainder(after)
            .trim_trailing_whitespace();

        MatchAndWarnings {
            item: Some(MatchedItem {
                item: Self {
                    variant,
                    label: resolve_caption(variant, parser),
                    icons_font: icons_are_font(parser),
                    content_model,
                    content,
                    blocks,
                    source,
                    title_source: metadata.title_source,
                    title: metadata.title.clone(),
                    anchor: metadata.anchor,
                    anchor_reftext: metadata.anchor_reftext,
                    attrlist: metadata.attrlist.clone(),
                },
                after,
            }),
            warnings,
        }
    }

    /// Returns the admonition type (e.g., [`AdmonitionVariant::Note`]).
    pub fn variant(&self) -> AdmonitionVariant {
        self.variant
    }

    /// Returns the lowercase name for this admonition (e.g., `note`).
    pub fn name(&self) -> &'static str {
        self.variant.name()
    }

    /// Returns the caption (label) text shown for this admonition (e.g.,
    /// `Note`).
    ///
    /// This is the value of the `<type>-caption` document attribute if set, or
    /// the default caption for the admonition type otherwise.
    pub fn label(&self) -> &str {
        &self.label
    }

    /// Returns `true` if font-based icons are enabled (i.e., the `icons`
    /// document attribute is set to `font`).
    pub fn icons_font(&self) -> bool {
        self.icons_font
    }

    /// Returns the simple content of this admonition, if it has the
    /// [`Simple`](ContentModel::Simple) content model.
    pub fn content(&self) -> Option<&Content<'src>> {
        self.content.as_ref()
    }
}

/// Returns `true` if `line` is the opening delimiter of an example block
/// (`====`) or an open block (`--`).
///
/// These are the only delimited blocks over which an admonition style may
/// masquerade.
fn is_example_or_open_delimiter(line: &Span<'_>) -> bool {
    let data = line.data();

    if data == "--" {
        return true;
    }

    data.len() >= 4 && data.chars().all(|c| c == '=')
}

/// Resolve the caption (label) text for an admonition variant from the current
/// document attributes, falling back to the variant's default caption.
fn resolve_caption(variant: AdmonitionVariant, parser: &Parser) -> String {
    let key = format!("{}-caption", variant.name());
    match parser.attribute_value(key) {
        InterpretedValue::Value(value) => value,
        _ => variant.default_caption().to_string(),
    }
}

/// Returns `true` if the `icons` document attribute is set to `font`.
fn icons_are_font(parser: &Parser) -> bool {
    matches!(parser.attribute_value("icons"), InterpretedValue::Value(value) if value == "font")
}

/// If the first line of `block_start` begins with an admonition label followed
/// by a colon and at least one space (e.g., `NOTE: `), return the admonition
/// variant and a span positioned at the start of the paragraph content (after
/// the stripped label).
pub(crate) fn admonition_paragraph_prefix(
    block_start: Span<'_>,
) -> Option<(AdmonitionVariant, Span<'_>)> {
    let line = block_start.take_normalized_line().item;
    let data = line.data();

    for variant in [
        AdmonitionVariant::Note,
        AdmonitionVariant::Tip,
        AdmonitionVariant::Important,
        AdmonitionVariant::Caution,
        AdmonitionVariant::Warning,
    ] {
        if let Some(rest) = data.strip_prefix(variant.style())
            && let Some(rest) = rest.strip_prefix(':')
            && (rest.starts_with(' ') || rest.starts_with('\t'))
        {
            let whitespace_len = rest.len() - rest.trim_start_matches([' ', '\t']).len();
            let prefix_len = variant.style().len() + 1 + whitespace_len;
            return Some((variant, block_start.discard(prefix_len)));
        }
    }

    None
}

/// Returns `true` if the first line of `line` begins with an admonition
/// paragraph label (e.g., `NOTE: `).
pub(crate) fn starts_with_admonition_label(line: Span<'_>) -> bool {
    admonition_paragraph_prefix(line).is_some()
}

impl<'src> IsBlock<'src> for AdmonitionBlock<'src> {
    fn content_model(&self) -> ContentModel {
        self.content_model
    }

    fn raw_context(&self) -> CowStr<'src> {
        "admonition".into()
    }

    fn declared_style(&'src self) -> Option<&'src str> {
        Some(self.variant.style())
    }

    fn rendered_content(&'src self) -> Option<&'src str> {
        self.content.as_ref().map(|content| content.rendered())
    }

    fn nested_blocks(&'src self) -> Iter<'src, Block<'src>> {
        self.blocks.iter()
    }

    fn nested_blocks_mut(&mut self) -> &mut [Block<'src>] {
        &mut self.blocks
    }

    fn content_mut(&mut self) -> Option<&mut Content<'src>> {
        self.content.as_mut()
    }

    fn title_source(&'src self) -> Option<Span<'src>> {
        self.title_source
    }

    fn title(&self) -> Option<&str> {
        self.title.as_deref()
    }

    fn anchor(&'src self) -> Option<Span<'src>> {
        self.anchor
    }

    fn anchor_reftext(&'src self) -> Option<Span<'src>> {
        self.anchor_reftext
    }

    fn attrlist(&'src self) -> Option<&'src Attrlist<'src>> {
        self.attrlist.as_ref()
    }
}

impl<'src> HasSpan<'src> for AdmonitionBlock<'src> {
    fn span(&self) -> Span<'src> {
        self.source
    }
}

impl std::fmt::Debug for AdmonitionBlock<'_> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("AdmonitionBlock")
            .field("variant", &self.variant)
            .field("label", &self.label)
            .field("icons_font", &self.icons_font)
            .field("content_model", &self.content_model)
            .field("content", &self.content)
            .field("blocks", &DebugSliceReference(&self.blocks))
            .field("source", &self.source)
            .field("title_source", &self.title_source)
            .field("title", &self.title)
            .field("anchor", &self.anchor)
            .field("anchor_reftext", &self.anchor_reftext)
            .field("attrlist", &self.attrlist)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    #![allow(clippy::unwrap_used)]
    #![allow(clippy::panic)]

    use std::ops::Deref;

    use crate::{
        blocks::{AdmonitionVariant, Block, ContentModel, IsBlock},
        tests::prelude::*,
    };

    fn parse_one(input: &'static str) -> Block<'static> {
        let mut parser = Parser::default();
        Block::parse(crate::Span::new(input), &mut parser)
            .unwrap_if_no_warnings()
            .unwrap()
            .item
    }

    fn as_admonition<'a>(block: &'a Block<'a>) -> &'a crate::blocks::AdmonitionBlock<'a> {
        match block {
            Block::Admonition(admonition) => admonition,
            // Only reached if a test parses an input that is not an admonition;
            // it exists to fail that test loudly, so it is uncovered while the
            // tests pass.
            other => panic!("expected an admonition block, got {other:?}"),
        }
    }

    mod admonition_variant {
        use crate::blocks::AdmonitionVariant;

        #[test]
        fn style() {
            assert_eq!(AdmonitionVariant::Note.style(), "NOTE");
            assert_eq!(AdmonitionVariant::Tip.style(), "TIP");
            assert_eq!(AdmonitionVariant::Important.style(), "IMPORTANT");
            assert_eq!(AdmonitionVariant::Caution.style(), "CAUTION");
            assert_eq!(AdmonitionVariant::Warning.style(), "WARNING");
        }

        #[test]
        fn name() {
            assert_eq!(AdmonitionVariant::Note.name(), "note");
            assert_eq!(AdmonitionVariant::Tip.name(), "tip");
            assert_eq!(AdmonitionVariant::Important.name(), "important");
            assert_eq!(AdmonitionVariant::Caution.name(), "caution");
            assert_eq!(AdmonitionVariant::Warning.name(), "warning");
        }

        #[test]
        fn default_caption() {
            assert_eq!(AdmonitionVariant::Note.default_caption(), "Note");
            assert_eq!(AdmonitionVariant::Tip.default_caption(), "Tip");
            assert_eq!(AdmonitionVariant::Important.default_caption(), "Important");
            assert_eq!(AdmonitionVariant::Caution.default_caption(), "Caution");
            assert_eq!(AdmonitionVariant::Warning.default_caption(), "Warning");
        }

        #[test]
        fn from_style() {
            assert_eq!(
                AdmonitionVariant::from_style("NOTE"),
                Some(AdmonitionVariant::Note)
            );
            assert_eq!(
                AdmonitionVariant::from_style("TIP"),
                Some(AdmonitionVariant::Tip)
            );
            assert_eq!(
                AdmonitionVariant::from_style("IMPORTANT"),
                Some(AdmonitionVariant::Important)
            );
            assert_eq!(
                AdmonitionVariant::from_style("CAUTION"),
                Some(AdmonitionVariant::Caution)
            );
            assert_eq!(
                AdmonitionVariant::from_style("WARNING"),
                Some(AdmonitionVariant::Warning)
            );

            // The match is case-sensitive and rejects non-labels.
            assert_eq!(AdmonitionVariant::from_style("note"), None);
            assert_eq!(AdmonitionVariant::from_style("Note"), None);
            assert_eq!(AdmonitionVariant::from_style("example"), None);
        }

        #[test]
        fn impl_debug() {
            assert_eq!(
                format!("{:?}", AdmonitionVariant::Note),
                "AdmonitionVariant::Note"
            );
            assert_eq!(
                format!("{:?}", AdmonitionVariant::Tip),
                "AdmonitionVariant::Tip"
            );
            assert_eq!(
                format!("{:?}", AdmonitionVariant::Important),
                "AdmonitionVariant::Important"
            );
            assert_eq!(
                format!("{:?}", AdmonitionVariant::Caution),
                "AdmonitionVariant::Caution"
            );
            assert_eq!(
                format!("{:?}", AdmonitionVariant::Warning),
                "AdmonitionVariant::Warning"
            );
        }

        #[test]
        fn impl_clone() {
            // Silly test to mark the #[derive(...)] line as covered.
            let v1 = AdmonitionVariant::Note;
            let v2 = v1;
            assert_eq!(v1, v2);
        }
    }

    #[test]
    fn paragraph_form() {
        let block = parse_one("NOTE: This is a note.");
        let admonition = as_admonition(&block);

        assert_eq!(admonition.variant(), AdmonitionVariant::Note);
        assert_eq!(admonition.name(), "note");
        assert_eq!(admonition.label(), "Note");
        assert!(!admonition.icons_font());
        assert_eq!(admonition.content_model(), ContentModel::Simple);
        assert_eq!(admonition.content().unwrap().rendered(), "This is a note.");
        assert_eq!(admonition.rendered_content(), Some("This is a note."));
        assert_eq!(admonition.raw_context().deref(), "admonition");
        assert_eq!(admonition.resolved_context().deref(), "admonition");
        assert_eq!(admonition.declared_style(), Some("NOTE"));
        assert!(admonition.nested_blocks().next().is_none());
        assert!(admonition.title().is_none());
        assert!(admonition.attrlist().is_none());
    }

    #[test]
    fn paragraph_form_all_variants() {
        for (input, variant, name) in [
            ("NOTE: x", AdmonitionVariant::Note, "note"),
            ("TIP: x", AdmonitionVariant::Tip, "tip"),
            ("IMPORTANT: x", AdmonitionVariant::Important, "important"),
            ("CAUTION: x", AdmonitionVariant::Caution, "caution"),
            ("WARNING: x", AdmonitionVariant::Warning, "warning"),
        ] {
            let block = parse_one(input);
            let admonition = as_admonition(&block);
            assert_eq!(admonition.variant(), variant);
            assert_eq!(admonition.name(), name);
        }
    }

    #[test]
    fn paragraph_form_multiline() {
        let block = parse_one("NOTE: first line\nsecond line");
        let admonition = as_admonition(&block);
        assert_eq!(
            admonition.content().unwrap().rendered(),
            "first line\nsecond line"
        );
    }

    #[test]
    fn paragraph_form_tab_separator() {
        let block = parse_one("NOTE:\tindented with a tab");
        let admonition = as_admonition(&block);
        assert_eq!(admonition.variant(), AdmonitionVariant::Note);
        assert_eq!(
            admonition.content().unwrap().rendered(),
            "indented with a tab"
        );
    }

    #[test]
    fn not_an_admonition_when_lowercase_label() {
        let block = parse_one("note: not an admonition");
        assert_eq!(block.raw_context().deref(), "paragraph");
    }

    #[test]
    fn not_an_admonition_without_separating_space() {
        let block = parse_one("NOTE:no space");
        assert_eq!(block.raw_context().deref(), "paragraph");
    }

    #[test]
    fn not_an_admonition_for_partial_label() {
        let block = parse_one("NOTES: a longer word");
        assert_eq!(block.raw_context().deref(), "paragraph");
    }

    #[test]
    fn indented_label_is_a_literal_block_not_an_admonition() {
        // Indented content is a literal block; the leading whitespace means the
        // line is not an admonition paragraph.
        let block = parse_one("  NOTE: indented text");
        assert_eq!(block.raw_context().deref(), "paragraph");
    }

    #[test]
    fn masquerade_on_example_block_is_compound() {
        let block = parse_one("[NOTE]\n====\nCompound content.\n====");
        let admonition = as_admonition(&block);
        assert_eq!(admonition.variant(), AdmonitionVariant::Note);
        assert_eq!(admonition.content_model(), ContentModel::Compound);
        assert!(admonition.content().is_none());
        assert!(admonition.rendered_content().is_none());
        assert_eq!(admonition.nested_blocks().count(), 1);
    }

    #[test]
    fn masquerade_propagates_unterminated_warning() {
        // An unterminated example block under an admonition style still parses
        // as a (compound) admonition, and its `UnterminatedDelimitedBlock`
        // warning is not swallowed by the masquerade path.
        let mut parser = Parser::default();
        let maw = Block::parse(crate::Span::new("[NOTE]\n====\nunclosed"), &mut parser);

        let block = maw.item.unwrap().item;
        assert_eq!(block.raw_context().deref(), "admonition");
        assert_eq!(maw.warnings.len(), 1);
        assert_eq!(
            maw.warnings.first().unwrap().warning,
            crate::warnings::WarningType::UnterminatedDelimitedBlock
        );
    }

    #[test]
    fn masquerade_on_open_block_is_compound() {
        let block = parse_one("[WARNING]\n--\npara one\n\npara two\n--");
        let admonition = as_admonition(&block);
        assert_eq!(admonition.variant(), AdmonitionVariant::Warning);
        assert_eq!(admonition.content_model(), ContentModel::Compound);
        assert_eq!(admonition.nested_blocks().count(), 2);
    }

    #[test]
    fn masquerade_on_paragraph_is_simple() {
        let block = parse_one("[TIP]\nA single paragraph.");
        let admonition = as_admonition(&block);
        assert_eq!(admonition.variant(), AdmonitionVariant::Tip);
        assert_eq!(admonition.content_model(), ContentModel::Simple);
        assert_eq!(
            admonition.content().unwrap().rendered(),
            "A single paragraph."
        );
    }

    #[test]
    fn masquerade_with_title() {
        let block = parse_one("[NOTE]\n.A title\n====\nContent.\n====");
        let admonition = as_admonition(&block);
        assert_eq!(admonition.title(), Some("A title"));
    }

    #[test]
    fn masquerade_does_not_apply_to_other_containers() {
        // Sidebar, quote, listing, literal, and passthrough blocks keep their
        // own context; the admonition style is ignored.
        assert_eq!(
            parse_one("[NOTE]\n****\nx\n****").raw_context().deref(),
            "sidebar"
        );
        assert_eq!(
            parse_one("[NOTE]\n____\nx\n____").raw_context().deref(),
            "quote"
        );
        assert_eq!(
            parse_one("[NOTE]\n----\nx\n----").raw_context().deref(),
            "listing"
        );
        assert_eq!(
            parse_one("[NOTE]\n....\nx\n....").raw_context().deref(),
            "literal"
        );
        assert_eq!(
            parse_one("[NOTE]\n++++\nx\n++++").raw_context().deref(),
            "pass"
        );
    }

    #[test]
    fn impl_debug() {
        let block = parse_one("NOTE: hi");
        let admonition = as_admonition(&block);
        let debug = format!("{admonition:?}");
        assert!(debug.starts_with("AdmonitionBlock {"));
        assert!(debug.contains("variant: AdmonitionVariant::Note"));
    }

    #[test]
    fn impl_clone() {
        // Silly test to mark the #[derive(...)] line as covered.
        let block = parse_one("NOTE: clone me");
        let admonition = as_admonition(&block).clone();
        assert_eq!(admonition.variant(), AdmonitionVariant::Note);
    }

    #[test]
    fn masquerade_without_content_is_not_an_admonition() {
        // A masquerade style with no following block content cannot form an
        // admonition; the masquerade parser reports no block and the line is
        // treated as an ordinary block with a missing-block warning.
        let mut parser = Parser::default();
        let maw = Block::parse(crate::Span::new("[NOTE]\n"), &mut parser);

        // The lone attribute list has no block to attach to: a missing-block
        // warning is emitted and the line is treated as an ordinary paragraph,
        // not an admonition.
        let block = maw.item.unwrap().item;
        assert_eq!(block.raw_context().deref(), "paragraph");
        assert_eq!(maw.warnings.len(), 1);
        assert_eq!(
            maw.warnings.first().unwrap().warning,
            crate::warnings::WarningType::MissingBlockAfterTitleOrAttributeList
        );
    }

    #[test]
    fn block_enum_delegates_to_admonition() {
        // Exercise the `Block`-level `IsBlock`/`Debug` arms for an admonition
        // (rather than calling through the unwrapped `AdmonitionBlock`).
        let simple = parse_one("NOTE: text");
        assert_eq!(simple.content_model(), ContentModel::Simple);
        assert_eq!(simple.rendered_content(), Some("text"));
        assert_eq!(simple.raw_context().deref(), "admonition");
        assert_eq!(simple.declared_style(), Some("NOTE"));
        assert!(simple.title_source().is_none());
        assert!(simple.anchor_reftext().is_none());
        assert_eq!(simple.substitution_group(), SubstitutionGroup::Normal);
        assert!(simple.nested_blocks().next().is_none());
        assert!(format!("{simple:?}").starts_with("Block::Admonition"));

        let compound = parse_one("[NOTE]\n====\nx\n====");
        assert_eq!(compound.content_model(), ContentModel::Compound);
        assert_eq!(compound.nested_blocks().count(), 1);
    }

    #[test]
    fn block_declared_style_for_non_admonition_kinds() {
        // The `Block::declared_style` delegation returns `None` for block kinds
        // that have no positional style: a media block, a list item, and a
        // document-attribute block.
        let media = parse_one("image::a.png[]");
        assert_eq!(media.raw_context().deref(), "image");
        assert!(media.declared_style().is_none());

        let list = parse_one("* item");
        let item = list.nested_blocks().next().unwrap();
        assert_eq!(item.raw_context().deref(), "list_item");
        assert!(item.declared_style().is_none());

        let attribute = parse_one(":foo: bar");
        assert_eq!(attribute.raw_context().deref(), "attribute");
        assert!(attribute.declared_style().is_none());
    }

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

        #[test]
        fn default_caption_when_icons_unset() {
            let doc = Parser::default().parse("CAUTION: Slippery when wet.");
            assert_xpath(
                &doc,
                "//td[@class=\"icon\"]/div[@class=\"title\"][text()=\"Caution\"]",
                1,
            );
        }

        #[test]
        fn caption_override_via_attribute() {
            let doc = Parser::default().parse(":note-caption: Remarque\n\nNOTE: Bonjour.");
            assert_xpath(
                &doc,
                "//td[@class=\"icon\"]/div[@class=\"title\"][text()=\"Remarque\"]",
                1,
            );
        }

        #[test]
        fn font_icons_when_enabled() {
            let doc = Parser::default().parse(":icons: font\n\nNOTE: This is a note.");
            assert_css(&doc, "td.icon i.fa.icon-note", 1);
            assert_css(&doc, "td.icon .title", 0);
        }
    }
}