asciidoc_parser/blocks/
simple.rs

1use crate::{
2    HasSpan, Parser, Span,
3    attributes::Attrlist,
4    blocks::{
5        CompoundDelimitedBlock, ContentModel, IsBlock, RawDelimitedBlock, metadata::BlockMetadata,
6    },
7    content::{Content, SubstitutionGroup},
8    span::MatchedItem,
9    strings::CowStr,
10};
11
12/// The style of a simple block.
13#[derive(Clone, Copy, Eq, PartialEq)]
14pub enum SimpleBlockStyle {
15    /// A paragraph block with normal substitutions.
16    Paragraph,
17
18    /// A literal block with no substitutions.
19    Literal,
20
21    /// Blocks and paragraphs assigned the listing style display their rendered
22    /// content exactly as you see it in the source. Listing content is
23    /// converted to preformatted text (i.e., `<pre>`). The content is presented
24    /// in a fixed-width font and endlines are preserved. Only [special
25    /// characters] and callouts are replaced when the document is converted.
26    ///
27    /// [special characters]: https://docs.asciidoctor.org/asciidoc/latest/subs/special-characters/
28    Listing,
29
30    /// A source block is a specialization of a listing block. Developers are
31    /// accustomed to seeing source code colorized to emphasize the code’s
32    /// structure (i.e., keywords, types, delimiters, etc.).
33    Source,
34}
35
36impl std::fmt::Debug for SimpleBlockStyle {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match self {
39            SimpleBlockStyle::Paragraph => write!(f, "SimpleBlockStyle::Paragraph"),
40            SimpleBlockStyle::Literal => write!(f, "SimpleBlockStyle::Literal"),
41            SimpleBlockStyle::Listing => write!(f, "SimpleBlockStyle::Listing"),
42            SimpleBlockStyle::Source => write!(f, "SimpleBlockStyle::Source"),
43        }
44    }
45}
46
47/// A block that's treated as contiguous lines of paragraph text (and subject to
48/// normal substitutions) (e.g., a paragraph block).
49#[derive(Clone, Debug, Eq, PartialEq)]
50pub struct SimpleBlock<'src> {
51    content: Content<'src>,
52    source: Span<'src>,
53    style: SimpleBlockStyle,
54    title_source: Option<Span<'src>>,
55    title: Option<String>,
56    anchor: Option<Span<'src>>,
57    anchor_reftext: Option<Span<'src>>,
58    attrlist: Option<Attrlist<'src>>,
59}
60
61impl<'src> SimpleBlock<'src> {
62    pub(crate) fn parse(
63        metadata: &BlockMetadata<'src>,
64        parser: &mut Parser,
65    ) -> Option<MatchedItem<'src, Self>> {
66        let MatchedItem {
67            item: (content, style),
68            after,
69        } = parse_lines(metadata.block_start, &metadata.attrlist, parser)?;
70
71        Some(MatchedItem {
72            item: Self {
73                content,
74                source: metadata
75                    .source
76                    .trim_remainder(after)
77                    .trim_trailing_whitespace(),
78                style,
79                title_source: metadata.title_source,
80                title: metadata.title.clone(),
81                anchor: metadata.anchor,
82                anchor_reftext: metadata.anchor_reftext,
83                attrlist: metadata.attrlist.clone(),
84            },
85            after: after.discard_empty_lines(),
86        })
87    }
88
89    pub(crate) fn parse_fast(
90        source: Span<'src>,
91        parser: &Parser,
92    ) -> Option<MatchedItem<'src, Self>> {
93        let MatchedItem {
94            item: (content, style),
95            after,
96        } = parse_lines(source, &None, parser)?;
97
98        let source = content.original();
99
100        Some(MatchedItem {
101            item: Self {
102                content,
103                source,
104                style,
105                title_source: None,
106                title: None,
107                anchor: None,
108                anchor_reftext: None,
109                attrlist: None,
110            },
111            after: after.discard_empty_lines(),
112        })
113    }
114
115    /// Return the interpreted content of this block.
116    pub fn content(&self) -> &Content<'src> {
117        &self.content
118    }
119
120    /// Return the style of this block.
121    pub fn style(&self) -> SimpleBlockStyle {
122        self.style
123    }
124}
125
126/// Parse the content-bearing lines for this block.
127fn parse_lines<'src>(
128    source: Span<'src>,
129    attrlist: &Option<Attrlist<'src>>,
130    parser: &Parser,
131) -> Option<MatchedItem<'src, (Content<'src>, SimpleBlockStyle)>> {
132    let source_after_whitespace = source.discard_whitespace();
133    let strip_indent = source_after_whitespace.col() - 1;
134
135    let mut style = if source_after_whitespace.col() == source.col() {
136        SimpleBlockStyle::Paragraph
137    } else {
138        SimpleBlockStyle::Literal
139    };
140
141    // Block style can override the interpretation of literal from reading
142    // indentation.
143    if let Some(attrlist) = attrlist {
144        match attrlist.block_style() {
145            Some("normal") => {
146                style = SimpleBlockStyle::Paragraph;
147            }
148
149            Some("literal") => {
150                style = SimpleBlockStyle::Literal;
151            }
152
153            Some("listing") => {
154                style = SimpleBlockStyle::Listing;
155            }
156
157            Some("source") => {
158                style = SimpleBlockStyle::Source;
159            }
160
161            _ => {}
162        }
163    }
164
165    let mut next = source;
166    let mut filtered_lines: Vec<&'src str> = vec![];
167
168    while let Some(line_mi) = next.take_non_empty_line() {
169        let mut line = line_mi.item;
170
171        // There are several stop conditions for simple paragraph blocks. These
172        // "shouldn't" be encountered on the first line (we shouldn't be calling
173        // `SimpleBlock::parse` in these conditions), but in case it is, we simply
174        // ignore them on the first line.
175        if !filtered_lines.is_empty() {
176            if line.data() == "+" {
177                break;
178            }
179
180            if line.starts_with('[') && line.ends_with(']') {
181                break;
182            }
183
184            if (line.starts_with('/')
185                || line.starts_with('-')
186                || line.starts_with('.')
187                || line.starts_with('+')
188                || line.starts_with('=')
189                || line.starts_with('*')
190                || line.starts_with('_'))
191                && (RawDelimitedBlock::is_valid_delimiter(&line)
192                    || CompoundDelimitedBlock::is_valid_delimiter(&line))
193            {
194                break;
195            }
196        }
197
198        next = line_mi.after;
199
200        if line.starts_with("//") && !line.starts_with("///") {
201            continue;
202        }
203
204        // Strip at most the number of leading whitespace characters found on the first
205        // line.
206        if strip_indent > 0
207            && let Some(n) = line.position(|c| c != ' ' && c != '\t')
208        {
209            line = line.into_parse_result(n.min(strip_indent)).after;
210        };
211
212        filtered_lines.push(line.trim_trailing_whitespace().data());
213    }
214
215    let source = source.trim_remainder(next).trim_trailing_whitespace();
216    if source.is_empty() {
217        return None;
218    }
219
220    let filtered_lines = filtered_lines.join("\n");
221    let mut content: Content<'src> = Content::from_filtered(source, filtered_lines);
222
223    SubstitutionGroup::Normal
224        .override_via_attrlist(attrlist.as_ref())
225        .apply(&mut content, parser, attrlist.as_ref());
226
227    Some(MatchedItem {
228        item: (content, style),
229        after: next.discard_empty_lines(),
230    })
231}
232
233impl<'src> IsBlock<'src> for SimpleBlock<'src> {
234    fn content_model(&self) -> ContentModel {
235        ContentModel::Simple
236    }
237
238    fn raw_context(&self) -> CowStr<'src> {
239        "paragraph".into()
240    }
241
242    fn title_source(&'src self) -> Option<Span<'src>> {
243        self.title_source
244    }
245
246    fn title(&self) -> Option<&str> {
247        self.title.as_deref()
248    }
249
250    fn anchor(&'src self) -> Option<Span<'src>> {
251        self.anchor
252    }
253
254    fn anchor_reftext(&'src self) -> Option<Span<'src>> {
255        self.anchor_reftext
256    }
257
258    fn attrlist(&'src self) -> Option<&'src Attrlist<'src>> {
259        self.attrlist.as_ref()
260    }
261}
262
263impl<'src> HasSpan<'src> for SimpleBlock<'src> {
264    fn span(&self) -> Span<'src> {
265        self.source
266    }
267}
268
269#[cfg(test)]
270mod tests {
271    #![allow(clippy::unwrap_used)]
272
273    use std::ops::Deref;
274
275    use pretty_assertions_sorted::assert_eq;
276
277    use crate::{
278        Parser,
279        blocks::{ContentModel, IsBlock, SimpleBlockStyle, metadata::BlockMetadata},
280        content::SubstitutionGroup,
281        tests::prelude::*,
282    };
283
284    #[test]
285    fn impl_clone() {
286        // Silly test to mark the #[derive(...)] line as covered.
287        let mut parser = Parser::default();
288
289        let b1 =
290            crate::blocks::SimpleBlock::parse(&BlockMetadata::new("abc"), &mut parser).unwrap();
291
292        let b2 = b1.item.clone();
293        assert_eq!(b1.item, b2);
294    }
295
296    #[test]
297    fn empty_source() {
298        let mut parser = Parser::default();
299        assert!(crate::blocks::SimpleBlock::parse(&BlockMetadata::new(""), &mut parser).is_none());
300    }
301
302    #[test]
303    fn only_spaces() {
304        let mut parser = Parser::default();
305        assert!(
306            crate::blocks::SimpleBlock::parse(&BlockMetadata::new("    "), &mut parser).is_none()
307        );
308    }
309
310    #[test]
311    fn single_line() {
312        let mut parser = Parser::default();
313        let mi =
314            crate::blocks::SimpleBlock::parse(&BlockMetadata::new("abc"), &mut parser).unwrap();
315
316        assert_eq!(
317            mi.item,
318            SimpleBlock {
319                content: Content {
320                    original: Span {
321                        data: "abc",
322                        line: 1,
323                        col: 1,
324                        offset: 0,
325                    },
326                    rendered: "abc",
327                },
328                source: Span {
329                    data: "abc",
330                    line: 1,
331                    col: 1,
332                    offset: 0,
333                },
334                style: SimpleBlockStyle::Paragraph,
335                title_source: None,
336                title: None,
337                anchor: None,
338                anchor_reftext: None,
339                attrlist: None,
340            },
341        );
342
343        assert_eq!(mi.item.content_model(), ContentModel::Simple);
344        assert_eq!(mi.item.raw_context().deref(), "paragraph");
345        assert_eq!(mi.item.resolved_context().deref(), "paragraph");
346        assert!(mi.item.declared_style().is_none());
347        assert!(mi.item.id().is_none());
348        assert!(mi.item.roles().is_empty());
349        assert!(mi.item.options().is_empty());
350        assert!(mi.item.title_source().is_none());
351        assert!(mi.item.title().is_none());
352        assert!(mi.item.anchor().is_none());
353        assert!(mi.item.anchor_reftext().is_none());
354        assert!(mi.item.attrlist().is_none());
355        assert_eq!(mi.item.substitution_group(), SubstitutionGroup::Normal);
356
357        assert_eq!(
358            mi.after,
359            Span {
360                data: "",
361                line: 1,
362                col: 4,
363                offset: 3
364            }
365        );
366    }
367
368    #[test]
369    fn multiple_lines() {
370        let mut parser = Parser::default();
371        let mi = crate::blocks::SimpleBlock::parse(&BlockMetadata::new("abc\ndef"), &mut parser)
372            .unwrap();
373
374        assert_eq!(
375            mi.item,
376            SimpleBlock {
377                content: Content {
378                    original: Span {
379                        data: "abc\ndef",
380                        line: 1,
381                        col: 1,
382                        offset: 0,
383                    },
384                    rendered: "abc\ndef",
385                },
386                source: Span {
387                    data: "abc\ndef",
388                    line: 1,
389                    col: 1,
390                    offset: 0,
391                },
392                style: SimpleBlockStyle::Paragraph,
393                title_source: None,
394                title: None,
395                anchor: None,
396                anchor_reftext: None,
397                attrlist: None,
398            }
399        );
400
401        assert_eq!(
402            mi.after,
403            Span {
404                data: "",
405                line: 2,
406                col: 4,
407                offset: 7
408            }
409        );
410    }
411
412    #[test]
413    fn consumes_blank_lines_after() {
414        let mut parser = Parser::default();
415        let mi = crate::blocks::SimpleBlock::parse(&BlockMetadata::new("abc\n\ndef"), &mut parser)
416            .unwrap();
417
418        assert_eq!(
419            mi.item,
420            SimpleBlock {
421                content: Content {
422                    original: Span {
423                        data: "abc",
424                        line: 1,
425                        col: 1,
426                        offset: 0,
427                    },
428                    rendered: "abc",
429                },
430                source: Span {
431                    data: "abc",
432                    line: 1,
433                    col: 1,
434                    offset: 0,
435                },
436                style: SimpleBlockStyle::Paragraph,
437                title_source: None,
438                title: None,
439                anchor: None,
440                anchor_reftext: None,
441                attrlist: None,
442            }
443        );
444
445        assert_eq!(
446            mi.after,
447            Span {
448                data: "def",
449                line: 3,
450                col: 1,
451                offset: 5
452            }
453        );
454    }
455
456    #[test]
457    fn overrides_sub_group_via_subs_attribute() {
458        let mut parser = Parser::default();
459        let mi = crate::blocks::SimpleBlock::parse(
460            &BlockMetadata::new("[subs=quotes]\na<b>c *bold*\n\ndef"),
461            &mut parser,
462        )
463        .unwrap();
464
465        assert_eq!(
466            mi.item,
467            SimpleBlock {
468                content: Content {
469                    original: Span {
470                        data: "a<b>c *bold*",
471                        line: 2,
472                        col: 1,
473                        offset: 14,
474                    },
475                    rendered: "a<b>c <strong>bold</strong>",
476                },
477                source: Span {
478                    data: "[subs=quotes]\na<b>c *bold*",
479                    line: 1,
480                    col: 1,
481                    offset: 0,
482                },
483                style: SimpleBlockStyle::Paragraph,
484                title_source: None,
485                title: None,
486                anchor: None,
487                anchor_reftext: None,
488                attrlist: Some(Attrlist {
489                    attributes: &[ElementAttribute {
490                        name: Some("subs"),
491                        value: "quotes",
492                        shorthand_items: &[],
493                    },],
494                    anchor: None,
495                    source: Span {
496                        data: "subs=quotes",
497                        line: 1,
498                        col: 2,
499                        offset: 1,
500                    },
501                },),
502            }
503        );
504
505        assert_eq!(
506            mi.after,
507            Span {
508                data: "def",
509                line: 4,
510                col: 1,
511                offset: 28
512            }
513        );
514    }
515}