asciidoc_parser/blocks/is_block.rs
1use std::{fmt::Debug, slice::Iter};
2
3use crate::{
4 Span,
5 attributes::Attrlist,
6 blocks::{Block, is_built_in_context},
7 content::SubstitutionGroup,
8 strings::CowStr,
9};
10
11/// **Block elements** form the main structure of an AsciiDoc document, starting
12/// with the document itself.
13///
14/// A block element (aka **block**) is a discrete, line-oriented chunk of
15/// content in an AsciiDoc document. Once parsed, that chunk of content becomes
16/// a block element in the parsed document model. Certain blocks may contain
17/// other blocks, so we say that blocks can be nested. The converter visits each
18/// block in turn, in document order, converting it to a corresponding chunk of
19/// output.
20///
21/// This trait implements many of the same core methods as the [`Block`] enum
22/// but provides a mechanism for third-party code to extend the behavior of
23/// blocks.
24pub trait IsBlock<'src>: Debug + Eq + PartialEq {
25 /// Returns the [`ContentModel`] for this block.
26 fn content_model(&self) -> ContentModel;
27
28 /// Returns the rendered content for this block, if any.
29 ///
30 /// Some blocks (especially compound blocks) do not directly contain
31 /// content. In such cases, this function will return `None`.
32 ///
33 /// This content will contain the text _after_ substitutions have been
34 /// applied.
35 fn rendered_content(&'src self) -> Option<&'src str> {
36 None
37 }
38
39 /// Returns the resolved context for this block.
40 ///
41 /// A block’s context is also sometimes referred to as a name, such as an
42 /// example block, a sidebar block, an admonition block, or a section.
43 ///
44 /// Every block has a context. The context is often implied by the syntax,
45 /// but can be declared explicitly in certain cases. The context is what
46 /// distinguishes one kind of block from another. You can think of the
47 /// context as the block’s type.
48 ///
49 /// For that reason, the context is not defined as an enumeration, but
50 /// rather as a string type that is optimized for the case where predefined
51 /// constants are viable.
52 ///
53 /// A block's context can be replaced by a block style that matches a
54 /// built-in context. Unlike [`raw_context()`], that transformation _is_
55 /// performed by this function.
56 ///
57 /// [`raw_context()`]: Self::raw_context
58 fn resolved_context(&'src self) -> CowStr<'src> {
59 if let Some(declared_style) = self.declared_style()
60 && is_built_in_context(declared_style)
61 {
62 return declared_style.into();
63 }
64
65 self.raw_context()
66 }
67
68 /// Returns the raw (uninterpreted) context for this block.
69 ///
70 /// A block’s context is also sometimes referred to as a name, such as an
71 /// example block, a sidebar block, an admonition block, or a section.
72 ///
73 /// Every block has a context. The context is often implied by the syntax,
74 /// but can be declared explicitly in certain cases. The context is what
75 /// distinguishes one kind of block from another. You can think of the
76 /// context as the block’s type.
77 ///
78 /// For that reason, the context is not defined as an enumeration, but
79 /// rather as a string type that is optimized for the case where predefined
80 /// constants are viable.
81 ///
82 /// A block's context can be replaced by a block style that matches a
83 /// built-in context. That transformation is only performed by
84 /// [`resolved_context()`], not this function.
85 ///
86 /// [`resolved_context()`]: Self::resolved_context
87 fn raw_context(&self) -> CowStr<'src>;
88
89 /// Returns the declared (uninterpreted) style for this block.
90 ///
91 /// Above some blocks, you may notice a name at the start of the block
92 /// attribute list (e.g., `[source]` or `[verse]`). The first positional
93 /// (unnamed) attribute in the block attribute list is used to declare the
94 /// block style.
95 ///
96 /// The declared block style is the value the author supplies.
97 ///
98 /// That value is then interpreted and resolved. That interpretation is not
99 /// performed by this function.
100 fn declared_style(&'src self) -> Option<&'src str> {
101 self.attrlist()
102 .and_then(|attrlist| attrlist.nth_attribute(1))
103 .and_then(|attr| attr.block_style())
104 }
105
106 /// Returns an iterator over the nested blocks contained within
107 /// this block.
108 ///
109 /// Many block types do not have nested blocks so the default implementation
110 /// returns an empty iterator.
111 fn nested_blocks(&'src self) -> Iter<'src, Block<'src>> {
112 const NO_BLOCKS: &[Block<'static>] = &[];
113 NO_BLOCKS.iter()
114 }
115
116 /// Returns the ID for this block, if present.
117 ///
118 /// You can assign an ID to a block using the shorthand syntax, the longhand
119 /// syntax, or a legacy block anchor.
120 ///
121 /// In the shorthand syntax, you prefix the name with a hash (`#`) in the
122 /// first position attribute:
123 ///
124 /// ```asciidoc
125 /// [#goals]
126 /// * Goal 1
127 /// * Goal 2
128 /// ```
129 ///
130 /// In the longhand syntax, you use a standard named attribute:
131 ///
132 /// ```asciidoc
133 /// [id=goals]
134 /// * Goal 1
135 /// * Goal 2
136 /// ```
137 ///
138 /// In the legacy block anchor syntax, you surround the name with double
139 /// square brackets:
140 ///
141 /// ```asciidoc
142 /// [[goals]]
143 /// * Goal 1
144 /// * Goal 2
145 /// ```
146 fn id(&'src self) -> Option<&'src str> {
147 self.anchor()
148 .map(|a| a.data())
149 .or_else(|| self.attrlist().and_then(|attrlist| attrlist.id()))
150 }
151
152 /// Returns any role attributes that were found.
153 ///
154 /// You can assign one or more roles to blocks and most inline elements
155 /// using the `role` attribute. The `role` attribute is a [named attribute].
156 /// Even though the attribute name is singular, it may contain multiple
157 /// (space-separated) roles. Roles may also be defined using a shorthand
158 /// (dot-prefixed) syntax.
159 ///
160 /// A role:
161 /// 1. adds additional semantics to an element
162 /// 2. can be used to apply additional styling to a group of elements (e.g.,
163 /// via a CSS class selector)
164 /// 3. may activate additional behavior if recognized by the converter
165 ///
166 /// **TIP:** The `role` attribute in AsciiDoc always get mapped to the
167 /// `class` attribute in the HTML output. In other words, role names are
168 /// synonymous with HTML class names, thus allowing output elements to be
169 /// identified and styled in CSS using class selectors (e.g.,
170 /// `sidebarblock.role1`).
171 ///
172 /// [named attribute]: https://docs.asciidoctor.org/asciidoc/latest/attributes/positional-and-named-attributes/#named
173 fn roles(&'src self) -> Vec<&'src str> {
174 match self.attrlist() {
175 Some(attrlist) => attrlist.roles(),
176 None => vec![],
177 }
178 }
179
180 /// Returns any option attributes that were found.
181 ///
182 /// The `options` attribute (often abbreviated as `opts`) is a versatile
183 /// [named attribute] that can be assigned one or more values. It can be
184 /// defined globally as document attribute as well as a block attribute on
185 /// an individual block.
186 ///
187 /// There is no strict schema for options. Any options which are not
188 /// recognized are ignored.
189 ///
190 /// You can assign one or more options to a block using the shorthand or
191 /// formal syntax for the options attribute.
192 ///
193 /// # Shorthand options syntax for blocks
194 ///
195 /// To assign an option to a block, prefix the value with a percent sign
196 /// (`%`) in an attribute list. The percent sign implicitly sets the
197 /// `options` attribute.
198 ///
199 /// ## Example 1: Sidebar block with an option assigned using the shorthand dot
200 ///
201 /// ```asciidoc
202 /// [%option]
203 /// ****
204 /// This is a sidebar with an option assigned to it, named option.
205 /// ****
206 /// ```
207 ///
208 /// You can assign multiple options to a block by prest
209 /// fixing each value with
210 /// a percent sign (`%`).
211 ///
212 /// ## Example 2: Sidebar with two options assigned using the shorthand dot
213 /// ```asciidoc
214 /// [%option1%option2]
215 /// ****
216 /// This is a sidebar with two options assigned to it, named option1 and option2.
217 /// ****
218 /// ```
219 ///
220 /// # Formal options syntax for blocks
221 ///
222 /// Explicitly set `options` or `opts`, followed by the equals sign (`=`),
223 /// and then the value in an attribute list.
224 ///
225 /// ## Example 3. Sidebar block with an option assigned using the formal syntax
226 /// ```asciidoc
227 /// [opts=option]
228 /// ****
229 /// This is a sidebar with an option assigned to it, named option.
230 /// ****
231 /// ```
232 ///
233 /// Separate multiple option values with commas (`,`).
234 ///
235 /// ## Example 4. Sidebar with three options assigned using the formal syntax
236 /// ```asciidoc
237 /// [opts="option1,option2"]
238 /// ****
239 /// This is a sidebar with two options assigned to it, option1 and option2.
240 /// ****
241 /// ```
242 ///
243 /// [named attribute]: https://docs.asciidoctor.org/asciidoc/latest/attributes/positional-and-named-attributes/#named
244 fn options(&'src self) -> Vec<&'src str> {
245 match self.attrlist() {
246 Some(attrlist) => attrlist.options(),
247 None => vec![],
248 }
249 }
250
251 /// Returns `true` if this block has the named option.
252 ///
253 /// See [`options()`] for a description of option syntax.
254 ///
255 /// [`options()`]: Self::options
256 fn has_option<N: AsRef<str>>(&'src self, name: N) -> bool {
257 self.attrlist()
258 .is_some_and(|attrlist| attrlist.has_option(name))
259 }
260
261 /// Returns the source text for the title for this block, if present.
262 fn title_source(&'src self) -> Option<Span<'src>>;
263
264 /// Returns the rendered title for this block, if present.
265 fn title(&self) -> Option<&str>;
266
267 /// Returns the anchor for this block, if present.
268 fn anchor(&'src self) -> Option<Span<'src>>;
269
270 /// Returns the reference text for this block's anchor, if present.
271 fn anchor_reftext(&'src self) -> Option<Span<'src>>;
272
273 /// Returns the attribute list for this block, if present.
274 fn attrlist(&'src self) -> Option<&'src Attrlist<'src>>;
275
276 /// Returns the default substitution group that is applied unless you
277 /// customize the substitutions for a particular element.
278 fn substitution_group(&'src self) -> SubstitutionGroup {
279 SubstitutionGroup::Normal
280 }
281}
282
283/// The content model of a block determines what kind of content the block can
284/// have (if any) and how that content is processed.
285#[derive(Clone, Copy, Eq, PartialEq)]
286pub enum ContentModel {
287 /// A block that may only contain other blocks (e.g., a section)
288 Compound,
289
290 /// A block that's treated as contiguous lines of paragraph text (and
291 /// subject to normal substitutions) (e.g., a paragraph block)
292 Simple,
293
294 /// A block that holds verbatim text (displayed "as is") (and subject to
295 /// verbatim substitutions) (e.g., a listing block)
296 Verbatim,
297
298 /// A block that holds unprocessed content passed directly through to the
299 /// output with no substitutions applied (e.g., a passthrough block)
300 Raw,
301
302 /// A block that has no content (e.g., an image block)
303 Empty,
304
305 /// A special content model reserved for tables that enforces a fixed
306 /// structure
307 Table,
308}
309
310impl std::fmt::Debug for ContentModel {
311 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
312 match self {
313 ContentModel::Compound => write!(f, "ContentModel::Compound"),
314 ContentModel::Simple => write!(f, "ContentModel::Simple"),
315 ContentModel::Verbatim => write!(f, "ContentModel::Verbatim"),
316 ContentModel::Raw => write!(f, "ContentModel::Raw"),
317 ContentModel::Empty => write!(f, "ContentModel::Empty"),
318 ContentModel::Table => write!(f, "ContentModel::Table"),
319 }
320 }
321}
322
323#[cfg(test)]
324mod tests {
325 #![allow(clippy::unwrap_used)]
326
327 mod content_model {
328 mod impl_debug {
329 use pretty_assertions_sorted::assert_eq;
330
331 use crate::blocks::ContentModel;
332
333 #[test]
334 fn compound() {
335 let content_model = ContentModel::Compound;
336 let debug_output = format!("{:?}", content_model);
337 assert_eq!(debug_output, "ContentModel::Compound");
338 }
339
340 #[test]
341 fn simple() {
342 let content_model = ContentModel::Simple;
343 let debug_output = format!("{:?}", content_model);
344 assert_eq!(debug_output, "ContentModel::Simple");
345 }
346
347 #[test]
348 fn verbatim() {
349 let content_model = ContentModel::Verbatim;
350 let debug_output = format!("{:?}", content_model);
351 assert_eq!(debug_output, "ContentModel::Verbatim");
352 }
353
354 #[test]
355 fn raw() {
356 let content_model = ContentModel::Raw;
357 let debug_output = format!("{:?}", content_model);
358 assert_eq!(debug_output, "ContentModel::Raw");
359 }
360
361 #[test]
362 fn empty() {
363 let content_model = ContentModel::Empty;
364 let debug_output = format!("{:?}", content_model);
365 assert_eq!(debug_output, "ContentModel::Empty");
366 }
367
368 #[test]
369 fn table() {
370 let content_model = ContentModel::Table;
371 let debug_output = format!("{:?}", content_model);
372 assert_eq!(debug_output, "ContentModel::Table");
373 }
374 }
375 }
376}