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
pub(crate) fn is_built_in_context(context: &str) -> bool {
    BuiltInContext::from_str(context).is_some()
}

/// The set of built-in block contexts defined by the AsciiDoc language.
///
/// A block's context (see [`IsBlock::resolved_context`]) is a stringly-typed
/// value so that third-party extensions can introduce their own contexts. The
/// contexts understood natively by this parser, however, are drawn from a fixed
/// vocabulary. This enum names that vocabulary so a downstream consumer (for
/// example, a converter that dispatches on
/// [`resolved_context`](crate::blocks::IsBlock::resolved_context)) can match
/// against a shared definition rather than hard-coding and maintaining its own
/// copy of the context strings.
///
/// Use [`from_str`](Self::from_str) to classify a context string (such as the
/// value returned by
/// [`resolved_context`](crate::blocks::IsBlock::resolved_context)) and
/// [`as_str`](Self::as_str) to recover the canonical string for a variant.
///
/// [`IsBlock::resolved_context`]: crate::blocks::IsBlock::resolved_context
/// [`resolved_context`]: crate::blocks::IsBlock::resolved_context
#[derive(Clone, Copy, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum BuiltInContext {
    /// The `admonition` context.
    Admonition,

    /// The `audio` context.
    Audio,

    /// The `colist` (callout list) context.
    Colist,

    /// The `dlist` (description list) context.
    Dlist,

    /// The `document` context.
    Document,

    /// The `example` context.
    Example,

    /// The `floating_title` (discrete heading) context.
    FloatingTitle,

    /// The `image` context.
    Image,

    /// The `list_item` context.
    ListItem,

    /// The `listing` context.
    Listing,

    /// The `literal` context.
    Literal,

    /// The `olist` (ordered list) context.
    Olist,

    /// The `open` context.
    Open,

    /// The `page_break` context.
    PageBreak,

    /// The `paragraph` context.
    Paragraph,

    /// The `pass` (passthrough) context.
    Pass,

    /// The `preamble` context.
    Preamble,

    /// The `quote` context.
    Quote,

    /// The `section` context.
    Section,

    /// The `sidebar` context.
    Sidebar,

    /// The `stem` context.
    Stem,

    /// The `table` context.
    Table,

    /// The `table_cell` context.
    TableCell,

    /// The `thematic_break` context.
    ThematicBreak,

    /// The `toc` context.
    Toc,

    /// The `ulist` (unordered list) context.
    Ulist,

    /// The `verse` context.
    Verse,

    /// The `video` context.
    Video,
}

impl BuiltInContext {
    /// Every built-in context, in a stable order.
    ///
    /// This lets a consumer enumerate the full vocabulary (for example, to
    /// build a dispatch table keyed by [`as_str`](Self::as_str)).
    pub const ALL: &'static [BuiltInContext] = &[
        Self::Admonition,
        Self::Audio,
        Self::Colist,
        Self::Dlist,
        Self::Document,
        Self::Example,
        Self::FloatingTitle,
        Self::Image,
        Self::ListItem,
        Self::Listing,
        Self::Literal,
        Self::Olist,
        Self::Open,
        Self::PageBreak,
        Self::Paragraph,
        Self::Pass,
        Self::Preamble,
        Self::Quote,
        Self::Section,
        Self::Sidebar,
        Self::Stem,
        Self::Table,
        Self::TableCell,
        Self::ThematicBreak,
        Self::Toc,
        Self::Ulist,
        Self::Verse,
        Self::Video,
    ];

    /// Returns the canonical context string for this variant (e.g.,
    /// `"paragraph"`).
    ///
    /// This is the same string that [`resolved_context`] yields for a block of
    /// this context.
    ///
    /// [`resolved_context`]: crate::blocks::IsBlock::resolved_context
    pub fn as_str(self) -> &'static str {
        match self {
            Self::Admonition => "admonition",
            Self::Audio => "audio",
            Self::Colist => "colist",
            Self::Dlist => "dlist",
            Self::Document => "document",
            Self::Example => "example",
            Self::FloatingTitle => "floating_title",
            Self::Image => "image",
            Self::ListItem => "list_item",
            Self::Listing => "listing",
            Self::Literal => "literal",
            Self::Olist => "olist",
            Self::Open => "open",
            Self::PageBreak => "page_break",
            Self::Paragraph => "paragraph",
            Self::Pass => "pass",
            Self::Preamble => "preamble",
            Self::Quote => "quote",
            Self::Section => "section",
            Self::Sidebar => "sidebar",
            Self::Stem => "stem",
            Self::Table => "table",
            Self::TableCell => "table_cell",
            Self::ThematicBreak => "thematic_break",
            Self::Toc => "toc",
            Self::Ulist => "ulist",
            Self::Verse => "verse",
            Self::Video => "video",
        }
    }

    /// Resolves a context string to its [`BuiltInContext`] variant, or `None`
    /// if the string is not a built-in context.
    ///
    /// The match is case-sensitive: built-in contexts are always lowercase.
    // This is deliberately an inherent method returning `Option` (rather than an
    // implementation of `std::str::FromStr`, which would return `Result`): a
    // non-built-in context is an ordinary, expected outcome here, not an error.
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(context: &str) -> Option<Self> {
        Some(match context {
            "admonition" => Self::Admonition,
            "audio" => Self::Audio,
            "colist" => Self::Colist,
            "dlist" => Self::Dlist,
            "document" => Self::Document,
            "example" => Self::Example,
            "floating_title" => Self::FloatingTitle,
            "image" => Self::Image,
            "list_item" => Self::ListItem,
            "listing" => Self::Listing,
            "literal" => Self::Literal,
            "olist" => Self::Olist,
            "open" => Self::Open,
            "page_break" => Self::PageBreak,
            "paragraph" => Self::Paragraph,
            "pass" => Self::Pass,
            "preamble" => Self::Preamble,
            "quote" => Self::Quote,
            "section" => Self::Section,
            "sidebar" => Self::Sidebar,
            "stem" => Self::Stem,
            "table" => Self::Table,
            "table_cell" => Self::TableCell,
            "thematic_break" => Self::ThematicBreak,
            "toc" => Self::Toc,
            "ulist" => Self::Ulist,
            "verse" => Self::Verse,
            "video" => Self::Video,
            _ => return None,
        })
    }
}

impl std::fmt::Debug for BuiltInContext {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "BuiltInContext::{}", {
            match self {
                Self::Admonition => "Admonition",
                Self::Audio => "Audio",
                Self::Colist => "Colist",
                Self::Dlist => "Dlist",
                Self::Document => "Document",
                Self::Example => "Example",
                Self::FloatingTitle => "FloatingTitle",
                Self::Image => "Image",
                Self::ListItem => "ListItem",
                Self::Listing => "Listing",
                Self::Literal => "Literal",
                Self::Olist => "Olist",
                Self::Open => "Open",
                Self::PageBreak => "PageBreak",
                Self::Paragraph => "Paragraph",
                Self::Pass => "Pass",
                Self::Preamble => "Preamble",
                Self::Quote => "Quote",
                Self::Section => "Section",
                Self::Sidebar => "Sidebar",
                Self::Stem => "Stem",
                Self::Table => "Table",
                Self::TableCell => "TableCell",
                Self::ThematicBreak => "ThematicBreak",
                Self::Toc => "Toc",
                Self::Ulist => "Ulist",
                Self::Verse => "Verse",
                Self::Video => "Video",
            }
        })
    }
}

/// The five admonition types provided by the AsciiDoc language.
///
/// An admonition draws attention to a statement by taking it out of the
/// content's flow and labeling it with a priority. The variant is determined by
/// the assigned type (i.e., the uppercase label), which is specified either as
/// a special paragraph prefix (e.g., `NOTE:`) or as the block style in an
/// attribute list (e.g., `[NOTE]`).
#[derive(Clone, Copy, Eq, PartialEq)]
pub enum AdmonitionVariant {
    /// The `NOTE` admonition type.
    Note,

    /// The `TIP` admonition type.
    Tip,

    /// The `IMPORTANT` admonition type.
    Important,

    /// The `CAUTION` admonition type.
    Caution,

    /// The `WARNING` admonition type.
    Warning,
}

impl AdmonitionVariant {
    /// Resolve an uppercase style label (e.g., `NOTE`) to its admonition
    /// variant, if it names one.
    ///
    /// The match is case-sensitive: the label must be uppercase, per the
    /// AsciiDoc language specification.
    pub(crate) fn from_style(style: &str) -> Option<Self> {
        Some(match style {
            "NOTE" => Self::Note,
            "TIP" => Self::Tip,
            "IMPORTANT" => Self::Important,
            "CAUTION" => Self::Caution,
            "WARNING" => Self::Warning,
            _ => return None,
        })
    }

    /// Returns the uppercase style label for this variant (e.g., `NOTE`).
    pub fn style(self) -> &'static str {
        match self {
            Self::Note => "NOTE",
            Self::Tip => "TIP",
            Self::Important => "IMPORTANT",
            Self::Caution => "CAUTION",
            Self::Warning => "WARNING",
        }
    }

    /// Returns the lowercase name for this variant (e.g., `note`).
    ///
    /// This is the name used for the block's CSS class and for the
    /// `<type>-caption` document attribute that controls the label text.
    pub fn name(self) -> &'static str {
        match self {
            Self::Note => "note",
            Self::Tip => "tip",
            Self::Important => "important",
            Self::Caution => "caution",
            Self::Warning => "warning",
        }
    }

    /// Returns the default caption (label) text for this variant (e.g.,
    /// `Note`).
    ///
    /// This text is shown to the reader (in place of an icon) unless it is
    /// overridden by setting the `<type>-caption` document attribute.
    pub fn default_caption(self) -> &'static str {
        match self {
            Self::Note => "Note",
            Self::Tip => "Tip",
            Self::Important => "Important",
            Self::Caution => "Caution",
            Self::Warning => "Warning",
        }
    }
}

impl std::fmt::Debug for AdmonitionVariant {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Note => write!(f, "AdmonitionVariant::Note"),
            Self::Tip => write!(f, "AdmonitionVariant::Tip"),
            Self::Important => write!(f, "AdmonitionVariant::Important"),
            Self::Caution => write!(f, "AdmonitionVariant::Caution"),
            Self::Warning => write!(f, "AdmonitionVariant::Warning"),
        }
    }
}

#[cfg(test)]
mod tests {
    mod built_in_context {
        use crate::blocks::{BuiltInContext, context::is_built_in_context};

        #[test]
        fn round_trips_every_variant() {
            // Every variant's `as_str` must resolve back to the same variant,
            // and each canonical string is recognized as a built-in context.
            for &context in BuiltInContext::ALL {
                assert_eq!(BuiltInContext::from_str(context.as_str()), Some(context));
                assert!(is_built_in_context(context.as_str()));
            }
        }

        #[test]
        fn all_is_complete_and_deduplicated() {
            // `ALL` should list each of the 28 built-in contexts exactly once.
            assert_eq!(BuiltInContext::ALL.len(), 28);

            let mut seen: Vec<&str> = BuiltInContext::ALL.iter().map(|c| c.as_str()).collect();
            seen.sort_unstable();
            seen.dedup();
            assert_eq!(seen.len(), BuiltInContext::ALL.len());
        }

        #[test]
        fn known_contexts() {
            assert_eq!(
                BuiltInContext::from_str("paragraph"),
                Some(BuiltInContext::Paragraph)
            );
            assert_eq!(BuiltInContext::Listing.as_str(), "listing");
            assert_eq!(BuiltInContext::TableCell.as_str(), "table_cell");
        }

        #[test]
        fn unknown_context_is_none() {
            assert_eq!(BuiltInContext::from_str("verbatim"), None);
            // The match is case-sensitive.
            assert_eq!(BuiltInContext::from_str("Paragraph"), None);
            assert!(!is_built_in_context("not-a-context"));
        }

        #[test]
        fn impl_debug() {
            // Spot-check one exact rendering...
            assert_eq!(
                format!("{:?}", BuiltInContext::FloatingTitle),
                "BuiltInContext::FloatingTitle"
            );

            // ...then exercise every variant's `Debug` arm, checking each
            // renders as a distinct `BuiltInContext::`-prefixed name.
            let mut seen = std::collections::HashSet::new();
            for &context in BuiltInContext::ALL {
                let debug = format!("{context:?}");
                assert!(debug.starts_with("BuiltInContext::"));
                assert!(!debug.contains(' '));
                assert!(seen.insert(debug), "duplicate Debug rendering");
            }
            assert_eq!(seen.len(), BuiltInContext::ALL.len());
        }
    }
}