Skip to main content

asciidoc_parser/blocks/
context.rs

1pub(crate) fn is_built_in_context(context: &str) -> bool {
2    matches!(
3        context,
4        "admonition"
5            | "audio"
6            | "colist"
7            | "dlist"
8            | "document"
9            | "example"
10            | "floating_title"
11            | "image"
12            | "list_item"
13            | "listing"
14            | "literal"
15            | "olist"
16            | "open"
17            | "page_break"
18            | "paragraph"
19            | "pass"
20            | "preamble"
21            | "quote"
22            | "section"
23            | "sidebar"
24            | "table"
25            | "table_cell"
26            | "thematic_break"
27            | "toc"
28            | "ulist"
29            | "verse"
30            | "video"
31    )
32}
33
34/// The five admonition types provided by the AsciiDoc language.
35///
36/// An admonition draws attention to a statement by taking it out of the
37/// content's flow and labeling it with a priority. The variant is determined by
38/// the assigned type (i.e., the uppercase label), which is specified either as
39/// a special paragraph prefix (e.g., `NOTE:`) or as the block style in an
40/// attribute list (e.g., `[NOTE]`).
41#[derive(Clone, Copy, Eq, PartialEq)]
42pub enum AdmonitionVariant {
43    /// The `NOTE` admonition type.
44    Note,
45
46    /// The `TIP` admonition type.
47    Tip,
48
49    /// The `IMPORTANT` admonition type.
50    Important,
51
52    /// The `CAUTION` admonition type.
53    Caution,
54
55    /// The `WARNING` admonition type.
56    Warning,
57}
58
59impl AdmonitionVariant {
60    /// Resolve an uppercase style label (e.g., `NOTE`) to its admonition
61    /// variant, if it names one.
62    ///
63    /// The match is case-sensitive: the label must be uppercase, per the
64    /// AsciiDoc language specification.
65    pub(crate) fn from_style(style: &str) -> Option<Self> {
66        Some(match style {
67            "NOTE" => Self::Note,
68            "TIP" => Self::Tip,
69            "IMPORTANT" => Self::Important,
70            "CAUTION" => Self::Caution,
71            "WARNING" => Self::Warning,
72            _ => return None,
73        })
74    }
75
76    /// Returns the uppercase style label for this variant (e.g., `NOTE`).
77    pub fn style(self) -> &'static str {
78        match self {
79            Self::Note => "NOTE",
80            Self::Tip => "TIP",
81            Self::Important => "IMPORTANT",
82            Self::Caution => "CAUTION",
83            Self::Warning => "WARNING",
84        }
85    }
86
87    /// Returns the lowercase name for this variant (e.g., `note`).
88    ///
89    /// This is the name used for the block's CSS class and for the
90    /// `<type>-caption` document attribute that controls the label text.
91    pub fn name(self) -> &'static str {
92        match self {
93            Self::Note => "note",
94            Self::Tip => "tip",
95            Self::Important => "important",
96            Self::Caution => "caution",
97            Self::Warning => "warning",
98        }
99    }
100
101    /// Returns the default caption (label) text for this variant (e.g.,
102    /// `Note`).
103    ///
104    /// This text is shown to the reader (in place of an icon) unless it is
105    /// overridden by setting the `<type>-caption` document attribute.
106    pub fn default_caption(self) -> &'static str {
107        match self {
108            Self::Note => "Note",
109            Self::Tip => "Tip",
110            Self::Important => "Important",
111            Self::Caution => "Caution",
112            Self::Warning => "Warning",
113        }
114    }
115}
116
117impl std::fmt::Debug for AdmonitionVariant {
118    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
119        match self {
120            Self::Note => write!(f, "AdmonitionVariant::Note"),
121            Self::Tip => write!(f, "AdmonitionVariant::Tip"),
122            Self::Important => write!(f, "AdmonitionVariant::Important"),
123            Self::Caution => write!(f, "AdmonitionVariant::Caution"),
124            Self::Warning => write!(f, "AdmonitionVariant::Warning"),
125        }
126    }
127}