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