Skip to main content

asciidoc_parser/blocks/
quote.rs

1use std::sync::Arc;
2
3use self_cell::self_cell;
4
5use crate::{
6    HasSpan, Parser, Span,
7    attributes::Attrlist,
8    blocks::{
9        Block, ChildBlocks, CompoundDelimitedBlock, ContentModel, IsBlock, ListItemMarker,
10        RawDelimitedBlock, SimpleBlock, TableBlock, metadata::BlockMetadata,
11        parse_utils::parse_blocks_until,
12    },
13    content::{Content, SubstitutionGroup},
14    internal::debug::DebugSliceReference,
15    parser::{InlineSubstitutionRenderer, ReferenceResolver, ReferenceWarnings},
16    span::MatchedItem,
17    strings::CowStr,
18    warnings::{MatchAndWarnings, Warning, WarningType},
19};
20
21self_cell! {
22    /// A Markdown-style blockquote's nested blocks, which borrow the owned
23    /// (`>`-stripped) source the block carries.
24    pub struct OwnedQuoteBlocks {
25        owner: String,
26
27        #[covariant]
28        dependent: OwnedQuoteBlocksInner,
29    }
30
31    impl {Debug, Eq, Hash, PartialEq}
32}
33
34/// The parsed blocks of an [`OwnedQuoteBlocks`], borrowing its owned source.
35#[derive(Debug, Eq, PartialEq)]
36struct OwnedQuoteBlocksInner<'src> {
37    blocks: Vec<Block<'src>>,
38}
39
40/// Distinguishes the two block types that share the blockquote syntax.
41///
42/// Prose excerpts and quotes use the [`Quote`](Self::Quote) type, which does
43/// not preserve line breaks. Verses (e.g., poems or song lyrics) use the
44/// [`Verse`](Self::Verse) type, which preserves line breaks in the output.
45#[derive(Clone, Copy, Eq, Hash, PartialEq)]
46pub enum QuoteType {
47    /// A prose excerpt or quote. Line breaks are not preserved.
48    Quote,
49
50    /// A verse (e.g., a poem or song lyric). Line breaks are preserved.
51    Verse,
52}
53
54impl QuoteType {
55    /// Returns the lowercase name for this type (e.g., `quote`).
56    ///
57    /// This is the block's context and the basis for its CSS class
58    /// (`quoteblock` or `verseblock`).
59    pub fn name(self) -> &'static str {
60        match self {
61            Self::Quote => "quote",
62            Self::Verse => "verse",
63        }
64    }
65}
66
67impl std::fmt::Debug for QuoteType {
68    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
69        match self {
70            Self::Quote => write!(f, "QuoteType::Quote"),
71            Self::Verse => write!(f, "QuoteType::Verse"),
72        }
73    }
74}
75
76/// A blockquote: a quote, prose excerpt, or verse, optionally attributed to a
77/// person and a source citation.
78///
79/// A blockquote can be written in several ways, all of which produce this
80/// block:
81///
82/// * A **delimited block** bounded by lines of four underscores (`____`),
83///   optionally with a `[quote,…]` or `[verse,…]` attribute list. A `quote`
84///   block has the [`Compound`](ContentModel::Compound) content model (it
85///   contains other blocks); a `verse` block has the
86///   [`Simple`](ContentModel::Simple) content model and preserves its line
87///   breaks.
88/// * A **styled paragraph** introduced by a `[quote,…]` or `[verse,…]`
89///   attribute list. This produces the [`Simple`](ContentModel::Simple) content
90///   model.
91/// * A **quoted paragraph**: a paragraph wrapped in double quotes and followed
92///   by an attribution line introduced by two hyphens (`-- …`). This produces a
93///   `quote` block with the [`Simple`](ContentModel::Simple) content model.
94#[derive(Clone, Eq, Hash, PartialEq)]
95pub struct QuoteBlock<'src> {
96    type_: QuoteType,
97    content_model: ContentModel,
98    content: Option<Content<'src>>,
99    blocks: Vec<Block<'src>>,
100    markdown_blocks: Option<Arc<OwnedQuoteBlocks>>,
101    attribution: Option<String>,
102    citetitle: Option<String>,
103    source: Span<'src>,
104    title_source: Option<Span<'src>>,
105    title: Option<Content<'src>>,
106    anchor: Option<Span<'src>>,
107    anchor_reftext: Option<Span<'src>>,
108    attrlist: Option<Attrlist<'src>>,
109}
110
111impl<'src> QuoteBlock<'src> {
112    /// Returns a document-order iterator over this block's direct child blocks.
113    ///
114    /// This reaches the children of a Markdown-style blockquote (which borrow
115    /// the block's own owned source), matching [`blocks()`](Self::blocks). For
116    /// the full subtree, or to search from a [`Block`] or [`Document`], use
117    /// [`FindBlocks`](crate::blocks::FindBlocks).
118    ///
119    /// [`Document`]: crate::Document
120    pub fn child_blocks(&'src self) -> ChildBlocks<'src> {
121        ChildBlocks::from_slice(self.blocks())
122    }
123
124    /// Returns the block's title as a mutable [`Content`], if the block has
125    /// one.
126    ///
127    /// This narrow seam exists for the document-order title resolution pass
128    /// (see `document::title_refs`), which installs the re-rendered title
129    /// after resolving any cross-references embedded in it. All other access
130    /// goes through the read-only [`IsBlock::title`] accessor.
131    pub(crate) fn title_content_mut(&mut self) -> Option<&mut Content<'src>> {
132        self.title.as_mut()
133    }
134
135    /// Parse a blockquote, if the given metadata and content describe one.
136    ///
137    /// Returns `None` (without consuming the block) when the content is not a
138    /// blockquote, so that the caller can fall through to other block parsers.
139    pub(crate) fn parse(
140        metadata: &BlockMetadata<'src>,
141        parser: &mut Parser,
142    ) -> Option<MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>>> {
143        let style = metadata.attrlist.as_ref().and_then(|a| a.block_style());
144        let styled_type = match style {
145            Some("quote") => Some(QuoteType::Quote),
146            Some("verse") => Some(QuoteType::Verse),
147            _ => None,
148        };
149
150        let first_line = metadata.block_start.take_normalized_line().item;
151        let is_quote_delimiter = is_quote_verse_delimiter(&first_line);
152
153        // A `[quote]` or `[verse]` style masquerades over a quote-delimited
154        // block, an open block, or a paragraph.
155        if let Some(type_) = styled_type {
156            if is_quote_delimiter {
157                return Some(Self::parse_delimited(metadata, parser, type_));
158            }
159
160            // A `[quote]`/`[verse]` style also masquerades over an open block
161            // (`--`): the open delimiter adopts the quote/verse context. This is
162            // unique to the open block – every other structural container (below)
163            // keeps its own context and ignores the style.
164            if first_line.data() == "--" {
165                return Some(Self::parse_delimited(metadata, parser, type_));
166            }
167
168            // The style is set on some other structural container (example,
169            // sidebar, listing, literal, passthrough, table). That container
170            // keeps its own context and the style is ignored, so this is not a
171            // blockquote.
172            if RawDelimitedBlock::is_valid_delimiter(&first_line)
173                || CompoundDelimitedBlock::is_valid_delimiter(&first_line)
174                || TableBlock::is_table_delimiter(&first_line)
175            {
176                return None;
177            }
178
179            return Self::parse_styled_paragraph(metadata, parser, type_);
180        }
181
182        // A bare quote-delimited block (no `[quote]`/`[verse]` style) is a quote
183        // block. Any other style on it (e.g. an admonition label) is ignored.
184        if is_quote_delimiter {
185            return Some(Self::parse_delimited(metadata, parser, QuoteType::Quote));
186        }
187
188        // A Markdown-style blockquote, introduced by a `>` marker.
189        if first_line.data().starts_with('>') {
190            return Self::parse_markdown(metadata, parser);
191        }
192
193        // A quoted paragraph: text wrapped in double quotes followed by a `-- `
194        // attribution line.
195        Self::parse_quoted_paragraph(metadata, parser)
196    }
197
198    /// Parse a quote- or verse-delimited block (bounded by `____` lines).
199    fn parse_delimited(
200        metadata: &BlockMetadata<'src>,
201        parser: &mut Parser,
202        type_: QuoteType,
203    ) -> MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>> {
204        let delimiter = metadata.block_start.take_normalized_line();
205
206        let mut next = delimiter.after;
207        let (closing_delimiter, after) = loop {
208            if next.is_empty() {
209                break (next, next);
210            }
211
212            let line = next.take_normalized_line();
213            if line.item.data() == delimiter.item.data() {
214                break (line.item, line.after);
215            }
216            next = line.after;
217        };
218
219        let inside_delimiters = delimiter.after.trim_remainder(closing_delimiter);
220
221        let (attribution, citetitle) = extract_attribution(metadata.attrlist.as_ref());
222
223        let (content_model, content, blocks, mut warnings) = match type_ {
224            // A quote block contains other blocks.
225            QuoteType::Quote => {
226                let maw_blocks = parse_blocks_until(inside_delimiters, |_, _| false, parser);
227                (
228                    ContentModel::Compound,
229                    None,
230                    maw_blocks.item.item,
231                    maw_blocks.warnings,
232                )
233            }
234
235            // A verse block preserves its content verbatim (line breaks
236            // included), with normal substitutions applied.
237            QuoteType::Verse => {
238                let content = render_verbatim(inside_delimiters, parser);
239                (ContentModel::Simple, Some(content), vec![], vec![])
240            }
241        };
242
243        let source = metadata
244            .source
245            .trim_remainder(closing_delimiter.discard_all())
246            .trim_trailing_whitespace();
247
248        if closing_delimiter.is_empty() {
249            warnings.insert(
250                0,
251                Warning {
252                    source: delimiter.item,
253                    warning: WarningType::UnterminatedDelimitedBlock,
254                    origin: None,
255                },
256            );
257        }
258
259        MatchAndWarnings {
260            item: Some(MatchedItem {
261                item: Self {
262                    type_,
263                    content_model,
264                    content,
265                    blocks,
266                    markdown_blocks: None,
267                    attribution,
268                    citetitle,
269                    source,
270                    title_source: metadata.title_source,
271                    title: metadata.title.clone(),
272                    anchor: metadata.anchor,
273                    anchor_reftext: metadata.anchor_reftext,
274                    attrlist: metadata.attrlist.clone(),
275                },
276                after,
277            }),
278            warnings,
279        }
280    }
281
282    /// Parse a `[quote,…]` or `[verse,…]` styled paragraph.
283    fn parse_styled_paragraph(
284        metadata: &BlockMetadata<'src>,
285        parser: &mut Parser,
286        type_: QuoteType,
287    ) -> Option<MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>>> {
288        // The paragraph text is read as a simple block. The `quote`/`verse`
289        // block style is not one of the verbatim styles, so the content is
290        // parsed as a normal paragraph (normal substitutions, line breaks
291        // preserved in the rendered text).
292        let inner = SimpleBlock::parse(metadata, parser)?;
293
294        let (attribution, citetitle) = extract_attribution(metadata.attrlist.as_ref());
295
296        let source = metadata
297            .source
298            .trim_remainder(inner.after)
299            .trim_trailing_whitespace();
300
301        Some(MatchAndWarnings {
302            item: Some(MatchedItem {
303                item: Self {
304                    type_,
305                    content_model: ContentModel::Simple,
306                    content: Some(inner.item.content().clone()),
307                    blocks: vec![],
308                    markdown_blocks: None,
309                    attribution,
310                    citetitle,
311                    source,
312                    title_source: metadata.title_source,
313                    title: metadata.title.clone(),
314                    anchor: metadata.anchor,
315                    anchor_reftext: metadata.anchor_reftext,
316                    attrlist: metadata.attrlist.clone(),
317                },
318                after: inner.after,
319            }),
320            warnings: vec![],
321        })
322    }
323
324    /// Parse a quoted paragraph: text wrapped in double quotes followed by a
325    /// `-- ` attribution line.
326    ///
327    /// Returns `None` if the content does not match this shape.
328    fn parse_quoted_paragraph(
329        metadata: &BlockMetadata<'src>,
330        parser: &mut Parser,
331    ) -> Option<MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>>> {
332        // A quoted paragraph must begin with a double quote. Test that on the
333        // block's first byte *before* scanning the whole paragraph. `read_paragraph`
334        // (below) walks every line up to the next blank line, so testing the first
335        // byte here avoids that scan for the common non-quoted paragraph: without
336        // it, a long run of non-blank lines that never forms a quoted paragraph is
337        // rescanned in full for every block, making the parse O(n²) on pathological
338        // input (e.g. thousands of consecutive delimiter lines with no blank line
339        // between them). `read_paragraph` starts at `block_start`, so a paragraph
340        // beginning with `"` shares this first byte — testing it here loses nothing.
341        if !metadata.block_start.data().starts_with('"') {
342            return None;
343        }
344
345        // The paragraph extends to the first blank line.
346        let para = read_paragraph(metadata.block_start);
347        let data = para.data();
348
349        // Locate the attribution line: the first line that begins with `--`
350        // followed by whitespace and at least one more character. Splits the
351        // paragraph into the quoted text and the attribution text.
352        let (quoted, attribution_text) = split_at_attribution_line(data)?;
353
354        // The quoted text, with surrounding whitespace removed, must be wrapped
355        // in double quotes.
356        let inner = quoted
357            .trim_end()
358            .strip_prefix('"')
359            .and_then(|s| s.strip_suffix('"'))
360            .filter(|inner| !inner.is_empty())?;
361
362        // The quoted text, with the surrounding quotes removed, becomes the
363        // blockquote content. Build a span that points just past the opening
364        // quote so source positions stay meaningful.
365        let inner_span = para.slice(1..1 + inner.len());
366        let mut content = Content::from(inner_span);
367        SubstitutionGroup::Normal.apply(&mut content, parser, None);
368
369        // The attribution line provides the attribution and (optional) citation.
370        let (attribution, citetitle) = split_attribution_line(attribution_text.trim(), parser);
371
372        let source = metadata
373            .source
374            .trim_remainder(read_paragraph_after(metadata.block_start))
375            .trim_trailing_whitespace();
376
377        Some(MatchAndWarnings {
378            item: Some(MatchedItem {
379                item: Self {
380                    type_: QuoteType::Quote,
381                    content_model: ContentModel::Simple,
382                    content: Some(content),
383                    blocks: vec![],
384                    markdown_blocks: None,
385                    attribution,
386                    citetitle,
387                    source,
388                    title_source: metadata.title_source,
389                    title: metadata.title.clone(),
390                    anchor: metadata.anchor,
391                    anchor_reftext: metadata.anchor_reftext,
392                    attrlist: metadata.attrlist.clone(),
393                },
394                after: read_paragraph_after(metadata.block_start).discard_empty_lines(),
395            }),
396            warnings: vec![],
397        })
398    }
399
400    /// Parse a Markdown-style blockquote: a run of lines introduced by a `>`
401    /// marker.
402    ///
403    /// Returns `None` when the content is not a Markdown-style blockquote,
404    /// including the case where it is a description list (which takes
405    /// precedence, since the `>` marker is a valid description-list term
406    /// prefix).
407    fn parse_markdown(
408        metadata: &BlockMetadata<'src>,
409        parser: &mut Parser,
410    ) -> Option<MatchAndWarnings<'src, Option<MatchedItem<'src, Self>>>> {
411        let first_line = metadata.block_start.take_normalized_line().item;
412
413        // The first line must begin the Markdown blockquote: a `>` followed by a
414        // space, or a bare `>`. A `>` immediately followed by other text (e.g.
415        // `>foo`) is not a blockquote.
416        if !is_markdown_marker_line(first_line.data()) {
417            return None;
418        }
419
420        // A description list takes precedence over a Markdown-style blockquote:
421        // the `>` marker is a valid prefix for a description-list term, so a
422        // line like `> term:: def` is a description list, not a blockquote.
423        if let Some(MatchedItem {
424            item: ListItemMarker::DefinedTerm { .. },
425            ..
426        }) = ListItemMarker::parse(metadata.block_start, parser)
427        {
428            return None;
429        }
430
431        // The blockquote spans the paragraph: every line up to the first blank
432        // line. Within it, each line's `>` marker is stripped (a bare `>`
433        // becomes a blank line); a line with no marker is a lazy continuation
434        // and is kept as-is.
435        let chunk = read_paragraph(metadata.block_start);
436        let after = read_paragraph_after(metadata.block_start);
437
438        let mut lines: Vec<String> = chunk
439            .data()
440            .split('\n')
441            .map(|line| {
442                if line == ">" {
443                    String::new()
444                } else if let Some(rest) = line.strip_prefix("> ") {
445                    rest.to_string()
446                } else {
447                    line.to_string()
448                }
449            })
450            .collect();
451
452        // A trailing attribution line (introduced by `--`) is pulled out of the
453        // content and used as the attribution and citation.
454        let (attribution, citetitle) = take_trailing_attribution(&mut lines, parser);
455
456        let body = lines.join("\n");
457
458        // The stripped body owns its source; its blocks borrow from it. Warnings
459        // from the owned parse reference spans in that owned source, so they
460        // cannot be returned to the caller as `Warning<'src>`. Their
461        // [`WarningType`]s (which carry no borrowed data) are collected instead
462        // and re-anchored at the blockquote's own source span below: the
463        // `>`-stripped body is not contiguous in the document source, so a
464        // precise location is not available anyway, but the diagnostic itself
465        // must not be lost.
466        let mut nested_warning_types: Vec<WarningType> = vec![];
467        let owned = OwnedQuoteBlocks::new(body, |source| {
468            // The blocks parse from `source`, an owned copy that does not map to
469            // the document. Mark that so a footnote defined inside records no
470            // (misleading) document location; see `Parser::owned_subsource_depth`.
471            parser.owned_subsource_depth += 1;
472            let mut maw = parse_blocks_until(Span::new(source), |_, _| false, parser);
473            parser.owned_subsource_depth -= 1;
474            nested_warning_types.extend(maw.warnings.drain(..).map(|w| w.warning));
475            OwnedQuoteBlocksInner {
476                blocks: maw.item.item,
477            }
478        });
479
480        let source = metadata
481            .source
482            .trim_remainder(after)
483            .trim_trailing_whitespace();
484
485        let warnings = nested_warning_types
486            .into_iter()
487            .map(|warning| Warning {
488                source,
489                warning,
490                origin: None,
491            })
492            .collect();
493
494        Some(MatchAndWarnings {
495            item: Some(MatchedItem {
496                item: Self {
497                    type_: QuoteType::Quote,
498                    content_model: ContentModel::Compound,
499                    content: None,
500                    blocks: vec![],
501                    markdown_blocks: Some(Arc::new(owned)),
502                    attribution,
503                    citetitle,
504                    source,
505                    title_source: metadata.title_source,
506                    title: metadata.title.clone(),
507                    anchor: metadata.anchor,
508                    anchor_reftext: metadata.anchor_reftext,
509                    attrlist: metadata.attrlist.clone(),
510                },
511                after: after.discard_empty_lines(),
512            }),
513            warnings,
514        })
515    }
516
517    /// Returns the blockquote type (quote or verse).
518    pub fn type_(&self) -> QuoteType {
519        self.type_
520    }
521
522    /// Returns the rendered attribution (the person the content is attributed
523    /// to), if any.
524    pub fn attribution(&self) -> Option<&str> {
525        self.attribution.as_deref()
526    }
527
528    /// Returns the rendered citation title (the work the content is drawn
529    /// from), if any.
530    pub fn citetitle(&self) -> Option<&str> {
531        self.citetitle.as_deref()
532    }
533
534    /// Returns the simple content of this blockquote, if it has the
535    /// [`Simple`](ContentModel::Simple) content model.
536    pub fn content(&self) -> Option<&Content<'src>> {
537        self.content.as_ref()
538    }
539
540    /// Returns the nested blocks of a compound blockquote as a slice.
541    ///
542    /// This includes the blocks of a Markdown-style blockquote, which borrow
543    /// the block's own owned source rather than the document source. See
544    /// [`child_blocks()`](Self::child_blocks) for the iterator form.
545    pub fn blocks(&self) -> &[Block<'_>] {
546        match &self.markdown_blocks {
547            Some(owned) => &owned.borrow_dependent().blocks,
548            None => &self.blocks,
549        }
550    }
551
552    /// Resolves any deferred cross-references in this blockquote's nested
553    /// blocks.
554    ///
555    /// The blocks of a `____`-delimited quote are reached through
556    /// [`child_blocks_mut()`](IsBlock::child_blocks_mut) by the generic block
557    /// walker; this method handles the Markdown-style case, whose blocks borrow
558    /// the block's own owned source.
559    pub(crate) fn resolve_references(
560        &mut self,
561        resolver: &dyn ReferenceResolver,
562        renderer: &dyn InlineSubstitutionRenderer,
563        warnings: &mut ReferenceWarnings<'src>,
564    ) {
565        let source = self.source;
566
567        // The owned store is shared behind an `Arc`, but references are resolved
568        // immediately after parsing while the block is still its sole owner, so
569        // `get_mut` succeeds.
570        if let Some(owned) = self.markdown_blocks.as_mut()
571            && let Some(owned) = Arc::get_mut(owned)
572        {
573            owned.with_dependent_mut(|_, dependent| {
574                // These blocks borrow the quote's own owned source, so their
575                // warnings are collected separately and then re-anchored to the
576                // quote block's span in the document.
577                let mut owned_warnings = ReferenceWarnings::default();
578
579                for block in &mut dependent.blocks {
580                    block.resolve_references(resolver, renderer, &mut owned_warnings);
581                }
582
583                owned_warnings.rehome_into(warnings, source);
584            });
585        }
586    }
587}
588
589/// Returns `true` if `line` introduces a Markdown-style blockquote: a `>`
590/// followed by a space, or a bare `>`.
591fn is_markdown_marker_line(line: &str) -> bool {
592    line == ">" || line.starts_with("> ")
593}
594
595/// Removes a trailing attribution line (introduced by `--`) from `lines`,
596/// returning the rendered attribution and citation.
597///
598/// Trailing blank lines are discarded first. If the last remaining line begins
599/// with `--` followed by whitespace and text, it is consumed and split into the
600/// attribution and (optional) citation.
601fn take_trailing_attribution(
602    lines: &mut Vec<String>,
603    parser: &Parser,
604) -> (Option<String>, Option<String>) {
605    while lines.last().is_some_and(|line| line.is_empty()) {
606        lines.pop();
607    }
608
609    if let Some(last) = lines.last()
610        && let Some(rest) = last.strip_prefix("--")
611        && (rest.starts_with(' ') || rest.starts_with('\t'))
612    {
613        let result = split_attribution_line(rest.trim(), parser);
614        lines.pop();
615        while lines.last().is_some_and(|line| line.is_empty()) {
616            lines.pop();
617        }
618        return result;
619    }
620
621    (None, None)
622}
623
624/// Returns `true` if `line` is a quote/verse delimiter (a line of four or more
625/// underscores).
626pub(crate) fn is_quote_verse_delimiter(line: &Span<'_>) -> bool {
627    let data = line.data();
628    data.len() >= 4 && data.starts_with("____") && data.chars().all(|c| c == '_')
629}
630
631/// Builds a verse block's verbatim content: the text between the delimiters
632/// with normal substitutions applied and line breaks preserved.
633fn render_verbatim<'src>(inside: Span<'src>, parser: &Parser) -> Content<'src> {
634    let trimmed = inside.discard_empty_lines().trim_trailing_whitespace();
635    let mut content = Content::from(trimmed);
636    SubstitutionGroup::Normal.apply(&mut content, parser, None);
637    content
638}
639
640/// Renders a fragment of attribution text (the attribution or citation) by
641/// applying normal inline substitutions, returning the owned result.
642fn render_inline(parser: &Parser, text: &str) -> String {
643    let span = Span::new(text);
644    let mut content = Content::from(span);
645    SubstitutionGroup::Normal.apply(&mut content, parser, None);
646    content.rendered_owned()
647}
648
649/// Extracts the attribution and citation from a block's attribute list.
650///
651/// The attribution is the `attribution` named attribute or the second
652/// positional attribute; the citation is the `citetitle` named attribute or the
653/// third positional attribute. An empty value is treated as absent.
654///
655/// The values are used as-is: an attribute list value already has its
656/// substitutions applied (e.g. a single-quoted value is rendered when the
657/// attribute list is parsed), so they must not be re-rendered here.
658fn extract_attribution(attrlist: Option<&Attrlist<'_>>) -> (Option<String>, Option<String>) {
659    let Some(attrlist) = attrlist else {
660        return (None, None);
661    };
662
663    let attribution = attrlist
664        .named_or_positional_attribute("attribution", 2)
665        .map(|a| a.value())
666        .filter(|v| !v.is_empty())
667        .map(str::to_string);
668
669    let citetitle = attrlist
670        .named_or_positional_attribute("citetitle", 3)
671        .map(|a| a.value())
672        .filter(|v| !v.is_empty())
673        .map(str::to_string);
674
675    (attribution, citetitle)
676}
677
678/// Splits an attribution line's text (everything after the leading `-- `) into
679/// the attribution and the optional citation, separated by the first comma.
680fn split_attribution_line(text: &str, parser: &Parser) -> (Option<String>, Option<String>) {
681    match text.split_once(',') {
682        Some((attribution, citetitle)) => {
683            let attribution = attribution.trim();
684            let citetitle = citetitle.trim();
685            (
686                non_empty(attribution).map(|v| render_inline(parser, v)),
687                non_empty(citetitle).map(|v| render_inline(parser, v)),
688            )
689        }
690        None => (
691            non_empty(text.trim()).map(|v| render_inline(parser, v)),
692            None,
693        ),
694    }
695}
696
697fn non_empty(s: &str) -> Option<&str> {
698    if s.is_empty() { None } else { Some(s) }
699}
700
701/// Finds the attribution line in a quoted paragraph and splits it from the
702/// quoted text.
703///
704/// The attribution line begins with `--` followed by at least one space or tab
705/// and then more text. The **last** such line is used (matching Asciidoctor's
706/// greedy match and the Markdown-style path), so a `-- …` that appears earlier
707/// in the quoted body does not prematurely terminate the quote. Returns the
708/// text before that line (the quoted text) and the attribution text (everything
709/// after the `--` marker and its trailing whitespace), or `None` if no
710/// attribution line is present.
711fn split_at_attribution_line(data: &str) -> Option<(&str, &str)> {
712    let mut line_start = 0;
713    let mut attribution: Option<(usize, &str)> = None;
714
715    for line in data.split_inclusive('\n') {
716        let trimmed = line.strip_suffix('\n').unwrap_or(line);
717
718        if let Some(rest) = trimmed.strip_prefix("--")
719            && (rest.starts_with(' ') || rest.starts_with('\t'))
720        {
721            let attribution_text = rest.trim_start_matches([' ', '\t']);
722
723            // `line_start > 0` ensures there is at least one line of quoted text
724            // before the attribution line.
725            if !attribution_text.is_empty() && line_start > 0 {
726                attribution = Some((line_start, attribution_text));
727            }
728        }
729
730        line_start += line.len();
731    }
732
733    // `line_start` is always a line boundary, so `split_at` never lands inside a
734    // character.
735    attribution.map(|(start, text)| (data.split_at(start).0, text))
736}
737
738/// Returns the span of the paragraph that begins at `source`: all lines up to
739/// (but not including) the first blank line.
740fn read_paragraph(source: Span<'_>) -> Span<'_> {
741    source.trim_remainder(read_paragraph_after(source))
742}
743
744/// Returns the span that follows the paragraph beginning at `source` (starting
745/// at the first blank line or end of input).
746fn read_paragraph_after(source: Span<'_>) -> Span<'_> {
747    let mut next = source;
748    while let Some(line) = next.take_non_empty_line() {
749        next = line.after;
750    }
751    next
752}
753
754impl<'src> IsBlock<'src> for QuoteBlock<'src> {
755    fn content_model(&self) -> ContentModel {
756        self.content_model
757    }
758
759    fn raw_context(&self) -> CowStr<'src> {
760        self.type_.name().into()
761    }
762
763    fn declared_style(&'src self) -> Option<&'src str> {
764        self.attrlist
765            .as_ref()
766            .and_then(|attrlist| attrlist.block_style())
767    }
768
769    fn rendered_content(&'src self) -> Option<&'src str> {
770        self.content.as_ref().map(|content| content.rendered())
771    }
772
773    /// Returns a mutable slice of the nested blocks of a `____`-delimited
774    /// quote.
775    ///
776    /// **Note:** a Markdown-style blockquote's nested blocks borrow the block's
777    /// own owned source rather than the document source, so they are not
778    /// reachable through this `'src`-bound hook and this slice is empty for
779    /// them. (Rendering and reference resolution go through
780    /// [`blocks()`](Self::blocks) and an explicit crate-internal
781    /// `resolve_references`, so this gap is internal to the crate.)
782    fn child_blocks_mut(&mut self) -> &mut [Block<'src>] {
783        &mut self.blocks
784    }
785
786    fn content_mut(&mut self) -> Option<&mut Content<'src>> {
787        self.content.as_mut()
788    }
789
790    fn title_source(&'src self) -> Option<Span<'src>> {
791        self.title_source
792    }
793
794    fn title(&self) -> Option<&str> {
795        self.title.as_ref().map(Content::rendered_str)
796    }
797
798    fn anchor(&'src self) -> Option<Span<'src>> {
799        self.anchor
800    }
801
802    fn anchor_reftext(&'src self) -> Option<Span<'src>> {
803        self.anchor_reftext
804    }
805
806    fn attrlist(&'src self) -> Option<&'src Attrlist<'src>> {
807        self.attrlist.as_ref()
808    }
809}
810
811impl<'src> HasSpan<'src> for QuoteBlock<'src> {
812    fn span(&self) -> Span<'src> {
813        self.source
814    }
815}
816
817impl std::fmt::Debug for QuoteBlock<'_> {
818    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
819        f.debug_struct("QuoteBlock")
820            .field("type_", &self.type_)
821            .field("content_model", &self.content_model)
822            .field("content", &self.content)
823            .field("blocks", &DebugSliceReference(&self.blocks))
824            .field("attribution", &self.attribution)
825            .field("citetitle", &self.citetitle)
826            .field("source", &self.source)
827            .field("title_source", &self.title_source)
828            .field("title", &self.title)
829            .field("anchor", &self.anchor)
830            .field("anchor_reftext", &self.anchor_reftext)
831            .field("attrlist", &self.attrlist)
832            .finish()
833    }
834}
835
836#[cfg(test)]
837mod tests {
838    #![allow(clippy::unwrap_used)]
839    #![allow(clippy::panic)]
840
841    use std::ops::Deref;
842
843    use crate::{
844        blocks::{Block, ContentModel, IsBlock, QuoteType},
845        tests::prelude::*,
846    };
847
848    fn parse_one(input: &'static str) -> Block<'static> {
849        let mut parser = Parser::default();
850        Block::parse(crate::Span::new(input), &mut parser)
851            .unwrap_if_no_warnings()
852            .unwrap()
853            .item
854    }
855
856    fn as_quote<'a>(block: &'a Block<'a>) -> &'a crate::blocks::QuoteBlock<'a> {
857        match block {
858            Block::Quote(quote) => quote,
859
860            // Only reached if a test parses an input that is not a quote block;
861            // it exists to fail that test loudly, so it is uncovered while the
862            // tests pass.
863            other => panic!("expected a quote block, got {other:?}"),
864        }
865    }
866
867    mod quote_type {
868        use crate::blocks::QuoteType;
869
870        #[test]
871        fn name() {
872            assert_eq!(QuoteType::Quote.name(), "quote");
873            assert_eq!(QuoteType::Verse.name(), "verse");
874        }
875
876        #[test]
877        fn impl_debug() {
878            assert_eq!(format!("{:?}", QuoteType::Quote), "QuoteType::Quote");
879            assert_eq!(format!("{:?}", QuoteType::Verse), "QuoteType::Verse");
880        }
881
882        #[test]
883        fn impl_clone() {
884            // Silly test to mark the #[derive(...)] line as covered.
885            let v1 = QuoteType::Quote;
886            let v2 = v1;
887            assert_eq!(v1, v2);
888        }
889    }
890
891    #[test]
892    fn delimited_quote_is_compound() {
893        let block = parse_one("____\nA quote.\n\nWith two paragraphs.\n____");
894        let quote = as_quote(&block);
895
896        assert_eq!(quote.type_(), QuoteType::Quote);
897        assert_eq!(quote.content_model(), ContentModel::Compound);
898        assert_eq!(quote.raw_context().deref(), "quote");
899        assert!(quote.content().is_none());
900        assert!(quote.attribution().is_none());
901        assert!(quote.citetitle().is_none());
902        assert_eq!(quote.blocks().len(), 2);
903        assert_eq!(quote.child_blocks().count(), 2);
904    }
905
906    #[test]
907    fn delimited_quote_with_attribution_and_citation() {
908        let block =
909            parse_one("[quote,Abraham Lincoln,Gettysburg Address]\n____\nFour score.\n____");
910        let quote = as_quote(&block);
911
912        assert_eq!(quote.attribution(), Some("Abraham Lincoln"));
913        assert_eq!(quote.citetitle(), Some("Gettysburg Address"));
914        assert_eq!(quote.declared_style(), Some("quote"));
915    }
916
917    #[test]
918    fn styled_paragraph_quote_is_simple() {
919        let block = parse_one("[quote,Albert Einstein]\nA person who never made a mistake.");
920        let quote = as_quote(&block);
921
922        assert_eq!(quote.type_(), QuoteType::Quote);
923        assert_eq!(quote.content_model(), ContentModel::Simple);
924        assert_eq!(
925            quote.content().unwrap().rendered(),
926            "A person who never made a mistake."
927        );
928        assert_eq!(
929            quote.rendered_content(),
930            Some("A person who never made a mistake.")
931        );
932        assert_eq!(quote.attribution(), Some("Albert Einstein"));
933        assert!(quote.citetitle().is_none());
934    }
935
936    #[test]
937    fn verse_paragraph_is_simple() {
938        let block = parse_one("[verse,Carl Sandburg,Fog]\nThe fog comes\non little cat feet.");
939        let quote = as_quote(&block);
940
941        assert_eq!(quote.type_(), QuoteType::Verse);
942        assert_eq!(quote.content_model(), ContentModel::Simple);
943        assert_eq!(quote.raw_context().deref(), "verse");
944        assert_eq!(
945            quote.content().unwrap().rendered(),
946            "The fog comes\non little cat feet."
947        );
948        assert_eq!(quote.attribution(), Some("Carl Sandburg"));
949        assert_eq!(quote.citetitle(), Some("Fog"));
950    }
951
952    #[test]
953    fn verse_delimited_preserves_line_breaks() {
954        let block = parse_one("[verse]\n____\nA verse\ndelimited block\n____");
955        let quote = as_quote(&block);
956
957        assert_eq!(quote.type_(), QuoteType::Verse);
958        assert_eq!(quote.content_model(), ContentModel::Simple);
959        assert_eq!(
960            quote.content().unwrap().rendered(),
961            "A verse\ndelimited block"
962        );
963        assert!(quote.child_blocks().next().is_none());
964    }
965
966    #[test]
967    fn quote_or_verse_style_over_other_container_is_not_a_quote() {
968        // A `quote`/`verse` style over an example, sidebar, listing, literal, or
969        // passthrough block keeps that container's own context.
970        assert_eq!(
971            parse_one("[quote]\n====\nx\n====").raw_context().deref(),
972            "example"
973        );
974        assert_eq!(
975            parse_one("[verse]\n****\nx\n****").raw_context().deref(),
976            "sidebar"
977        );
978        assert_eq!(
979            parse_one("[quote]\n----\nx\n----").raw_context().deref(),
980            "listing"
981        );
982    }
983
984    #[test]
985    fn quoted_paragraph_with_attribution_and_citation() {
986        let block = parse_one("\"A little rebellion is good.\"\n-- Thomas Jefferson, Volume 11");
987        let quote = as_quote(&block);
988
989        assert_eq!(quote.type_(), QuoteType::Quote);
990        assert_eq!(quote.content_model(), ContentModel::Simple);
991        assert_eq!(
992            quote.content().unwrap().rendered(),
993            "A little rebellion is good."
994        );
995        assert_eq!(quote.attribution(), Some("Thomas Jefferson"));
996        assert_eq!(quote.citetitle(), Some("Volume 11"));
997    }
998
999    #[test]
1000    fn quoted_paragraph_without_citation() {
1001        let block = parse_one("\"A quote.\"\n-- Anonymous");
1002        let quote = as_quote(&block);
1003
1004        assert_eq!(quote.attribution(), Some("Anonymous"));
1005        assert!(quote.citetitle().is_none());
1006    }
1007
1008    #[test]
1009    fn quoted_paragraph_requires_attribution_line() {
1010        // Without a `-- ` attribution line, a quoted sentence is just a
1011        // paragraph.
1012        let block = parse_one("\"Just a quoted sentence.\"");
1013        assert_eq!(block.raw_context().deref(), "paragraph");
1014    }
1015
1016    #[test]
1017    fn quoted_paragraph_requires_opening_quote() {
1018        // The attribution line alone, with no opening quote, is not a quoted
1019        // paragraph.
1020        let block = parse_one("Not quoted.\n-- Someone");
1021        assert_eq!(block.raw_context().deref(), "paragraph");
1022    }
1023
1024    #[test]
1025    fn empty_quoted_paragraph_is_not_a_quote() {
1026        // A pair of quotes wrapping no text is not a quoted paragraph.
1027        let block = parse_one("\"\"\n-- Someone");
1028        assert_eq!(block.raw_context().deref(), "paragraph");
1029    }
1030
1031    #[test]
1032    fn unclosed_quoted_paragraph_is_not_a_quote() {
1033        // An opening quote with no closing quote is not a quoted paragraph.
1034        let block = parse_one("\"no closing quote\n-- Someone");
1035        assert_eq!(block.raw_context().deref(), "paragraph");
1036    }
1037
1038    #[test]
1039    fn dash_line_without_attribution_text_is_not_a_quote() {
1040        // A `-- ` line with no attribution text after it is not an attribution
1041        // line, so this is an ordinary paragraph.
1042        let block = parse_one("\"A quote.\"\n--  ");
1043        assert_eq!(block.raw_context().deref(), "paragraph");
1044    }
1045
1046    #[test]
1047    fn quoted_paragraph_tab_separated_attribution() {
1048        // The `--` attribution marker may be separated from the text by a tab.
1049        let block = parse_one("\"A quote.\"\n--\tSomeone");
1050        let quote = as_quote(&block);
1051        assert_eq!(quote.attribution(), Some("Someone"));
1052    }
1053
1054    #[test]
1055    fn quoted_paragraph_uses_last_attribution_line() {
1056        // A `-- …` that appears earlier in the quoted body does not terminate
1057        // the quote; the last attribution line wins (matching Asciidoctor).
1058        let block =
1059            parse_one("\"line one\n-- not really an attribution\nline two\"\n-- Real Attribution");
1060        let quote = as_quote(&block);
1061        assert_eq!(quote.attribution(), Some("Real Attribution"));
1062        let rendered = quote.content().unwrap().rendered();
1063        assert!(
1064            rendered.contains("line one") && rendered.contains("line two"),
1065            "content was: {rendered}"
1066        );
1067    }
1068
1069    #[test]
1070    fn attribution_with_empty_name_keeps_citation() {
1071        // A `--` line of the form `-- , citation` has no attribution but still
1072        // yields a citation.
1073        let block = parse_one("\"A quote.\"\n-- , Just a citation");
1074        let quote = as_quote(&block);
1075        assert!(quote.attribution().is_none());
1076        assert_eq!(quote.citetitle(), Some("Just a citation"));
1077    }
1078
1079    #[test]
1080    fn styled_paragraph_with_no_content_is_not_a_quote() {
1081        // A `[quote]` style with no following content cannot form a styled
1082        // paragraph; the lone attribute list is treated as an ordinary block.
1083        let mut parser = Parser::default();
1084        let maw = Block::parse(crate::Span::new("[quote]\n"), &mut parser);
1085        let block = maw.item.unwrap().item;
1086        assert_eq!(block.raw_context().deref(), "paragraph");
1087    }
1088
1089    #[test]
1090    fn markdown_blockquote_tab_attribution_after_blank() {
1091        // A trailing blank (`>`) line before the `--` attribution is discarded,
1092        // and the attribution marker may be tab-separated.
1093        let block = parse_one("> A quote.\n>\n> --\tSomeone");
1094        let quote = as_quote(&block);
1095        assert_eq!(quote.attribution(), Some("Someone"));
1096        assert_eq!(quote.blocks().len(), 1);
1097    }
1098
1099    #[test]
1100    fn markdown_blockquote_propagates_nested_warning() {
1101        // A warning produced while parsing the (owned, `>`-stripped) body – here
1102        // an unterminated nested delimited block – is re-anchored at the
1103        // blockquote's own span and surfaced to the caller, rather than being
1104        // dropped (or panicking a debug build).
1105        let mut parser = Parser::default();
1106        let maw = Block::parse(crate::Span::new("> ____\n> unclosed"), &mut parser);
1107
1108        let block = maw.item.unwrap().item;
1109        assert_eq!(block.raw_context().deref(), "quote");
1110        assert_eq!(
1111            maw.warnings.first().unwrap().warning,
1112            WarningType::UnterminatedDelimitedBlock
1113        );
1114
1115        // The warning is anchored at the blockquote's source span.
1116        assert_eq!(maw.warnings.first().unwrap().source, block.span());
1117    }
1118
1119    #[test]
1120    fn markdown_blockquote_double_dash_without_space_is_content() {
1121        // A trailing `--` not followed by whitespace is content, not an
1122        // attribution.
1123        let block = parse_one("> A quote.\n> --nospace");
1124        let quote = as_quote(&block);
1125        assert!(quote.attribution().is_none());
1126    }
1127
1128    #[test]
1129    fn markdown_blockquote_basic() {
1130        let block = parse_one("> A markdown quote.");
1131        let quote = as_quote(&block);
1132
1133        assert_eq!(quote.type_(), QuoteType::Quote);
1134        assert_eq!(quote.content_model(), ContentModel::Compound);
1135        assert_eq!(quote.blocks().len(), 1);
1136
1137        // A Markdown blockquote's nested blocks borrow the block's owned source,
1138        // but `child_blocks()` still exposes them (matching `blocks()`).
1139        assert_eq!(quote.child_blocks().count(), 1);
1140    }
1141
1142    #[test]
1143    fn markdown_blockquote_with_attribution() {
1144        let block = parse_one("> A quote.\n> -- Someone");
1145        let quote = as_quote(&block);
1146
1147        assert_eq!(quote.attribution(), Some("Someone"));
1148        assert_eq!(quote.blocks().len(), 1);
1149    }
1150
1151    #[test]
1152    fn markdown_blockquote_lazy_continuation() {
1153        // A line without a `>` marker continues the current paragraph.
1154        let block = parse_one("> line one\nline two");
1155        let quote = as_quote(&block);
1156
1157        assert_eq!(quote.blocks().len(), 1);
1158        let inner = quote.blocks().first().unwrap();
1159        assert_eq!(inner.rendered_content(), Some("line one\nline two"));
1160    }
1161
1162    #[test]
1163    fn markdown_marker_requires_space() {
1164        // A `>` immediately followed by other text is not a blockquote.
1165        let block = parse_one(">foo bar");
1166        assert_eq!(block.raw_context().deref(), "paragraph");
1167    }
1168
1169    #[test]
1170    fn markdown_yields_to_description_list() {
1171        // A `>`-prefixed description-list term is a description list, not a
1172        // Markdown-style blockquote.
1173        let block = parse_one("> term:: definition");
1174        assert_eq!(block.raw_context().deref(), "list");
1175    }
1176
1177    #[test]
1178    fn unterminated_delimited_quote_warns() {
1179        let mut parser = Parser::default();
1180        let maw = Block::parse(crate::Span::new("____\nunclosed"), &mut parser);
1181
1182        let block = maw.item.unwrap().item;
1183        assert_eq!(block.raw_context().deref(), "quote");
1184        assert_eq!(maw.warnings.len(), 1);
1185        assert_eq!(
1186            maw.warnings.first().unwrap().warning,
1187            WarningType::UnterminatedDelimitedBlock
1188        );
1189    }
1190
1191    #[test]
1192    fn citation_receives_inline_substitutions() {
1193        // A link in the citation is rendered to an anchor.
1194        let block = parse_one(
1195            "[quote,Lewis Carroll,'See https://example.com/lc[the bio]']\n____\nAny road.\n____",
1196        );
1197        let quote = as_quote(&block);
1198        let citetitle = quote.citetitle().unwrap();
1199        assert!(
1200            citetitle.contains("<a href=\"https://example.com/lc\">the bio</a>"),
1201            "citation was: {citetitle}"
1202        );
1203    }
1204
1205    #[test]
1206    fn block_enum_delegates_to_quote() {
1207        // Exercise the `Block`-level `IsBlock`/`Debug` arms for a quote block.
1208        let compound = parse_one("____\nx\n____");
1209        assert_eq!(compound.content_model(), ContentModel::Compound);
1210        assert_eq!(compound.raw_context().deref(), "quote");
1211        assert!(compound.title_source().is_none());
1212        assert!(compound.anchor().is_none());
1213        assert!(compound.anchor_reftext().is_none());
1214        assert!(compound.attrlist().is_none());
1215        assert_eq!(compound.substitution_group(), SubstitutionGroup::Normal);
1216        assert_eq!(compound.child_blocks().count(), 1);
1217        assert!(compound.title().is_none());
1218        assert!(compound.declared_style().is_none());
1219        assert!(format!("{compound:?}").starts_with("Block::Quote"));
1220
1221        let simple = parse_one("[verse]\nverse text");
1222        assert_eq!(simple.rendered_content(), Some("verse text"));
1223        assert_eq!(simple.content_model(), ContentModel::Simple);
1224    }
1225
1226    #[test]
1227    fn impl_debug() {
1228        let block = parse_one("____\nx\n____");
1229        let quote = as_quote(&block);
1230        let debug = format!("{quote:?}");
1231        assert!(debug.starts_with("QuoteBlock {"));
1232        assert!(debug.contains("type_: QuoteType::Quote"));
1233    }
1234
1235    #[test]
1236    fn impl_clone() {
1237        // Silly test to mark the #[derive(...)] line as covered.
1238        let block = parse_one("____\nclone me\n____");
1239        let quote = as_quote(&block).clone();
1240        assert_eq!(quote.type_(), QuoteType::Quote);
1241    }
1242
1243    #[test]
1244    fn title_renders_inside_quote_block() {
1245        let doc = Parser::default()
1246            .parse(".A title\n[quote,Captain Kirk]\nEverybody remember where we parked.");
1247        let block = doc.child_blocks().next().unwrap();
1248        let quote = as_quote(block);
1249        assert_eq!(quote.title(), Some("A title"));
1250    }
1251
1252    /// The quoted-paragraph parser must reject a non-quote paragraph on its
1253    /// first byte, before scanning the paragraph to the next blank line. A
1254    /// document of many consecutive delimiter lines with no blank line between
1255    /// them otherwise makes every block rescan the entire remaining input,
1256    /// giving quadratic (O(n²)) parse time and a practical denial of service on
1257    /// modestly-sized input. This guards that the parse stays roughly linear.
1258    ///
1259    /// The bound is deliberately loose (seconds, versus a handful of
1260    /// milliseconds when linear) so the test is not flaky on a slow or loaded
1261    /// machine, while still failing decisively if the quadratic behavior
1262    /// returns — the quadratic parse of this input takes tens of seconds.
1263    #[test]
1264    fn many_consecutive_delimiters_parse_in_roughly_linear_time() {
1265        use std::time::{Duration, Instant};
1266
1267        // Each of these patterns previously exercised the quadratic path: none
1268        // contains a blank line, so the quoted-paragraph scan ran to end of
1269        // input on every block.
1270        let example_run = "====\n".repeat(20_000);
1271
1272        let mut example_run_with_text = "====\n".repeat(10_000);
1273        example_run_with_text.push_str("text\n");
1274        example_run_with_text.push_str(&"====\n".repeat(10_000));
1275
1276        let open_run = "--\n".repeat(20_000);
1277
1278        let budget = Duration::from_secs(10);
1279
1280        for source in [&example_run, &example_run_with_text, &open_run] {
1281            let start = Instant::now();
1282            let _ = Parser::default().parse(source);
1283            let elapsed = start.elapsed();
1284
1285            assert!(
1286                elapsed < budget,
1287                "parsing {} delimiter lines took {elapsed:?}, exceeding the {budget:?} budget \
1288                 (a sign the quadratic quoted-paragraph rescan has returned)",
1289                source.lines().count(),
1290            );
1291        }
1292    }
1293}