panache-parser 0.10.0

Lossless CST parser and syntax wrappers for Pandoc markdown, Quarto, and RMarkdown
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
//! Continuation/blank-line handling policy.
//!
//! This module centralizes the parser's "should this line continue an existing container?"
//! logic (especially across blank lines). Keeping this logic in one place reduces the
//! risk of scattered ad-hoc heuristics diverging as blocks move into the dispatcher.

use crate::options::{PandocCompat, ParserOptions};

use crate::parser::block_dispatcher::{BlockContext, BlockParserRegistry};
use crate::parser::blocks::blockquotes::{count_blockquote_markers, strip_n_blockquote_markers};
use crate::parser::blocks::{definition_lists, html_blocks, lists, raw_blocks};
use crate::parser::utils::container_stack::{ContainerStack, leading_indent};
use crate::parser::utils::helpers::is_blank_line;

pub(crate) struct ContinuationPolicy<'a, 'cfg> {
    config: &'cfg ParserOptions,
    block_registry: &'a BlockParserRegistry,
}

impl<'a, 'cfg> ContinuationPolicy<'a, 'cfg> {
    pub(crate) fn new(
        config: &'cfg ParserOptions,
        block_registry: &'a BlockParserRegistry,
    ) -> Self {
        Self {
            config,
            block_registry,
        }
    }

    fn definition_min_block_indent(&self, content_col: usize) -> usize {
        if self.config.effective_pandoc_compat() == PandocCompat::V3_7 {
            content_col.max(4)
        } else {
            content_col
        }
    }

    pub(crate) fn compute_levels_to_keep(
        &self,
        current_bq_depth: usize,
        containers: &ContainerStack,
        lines: &[&str],
        next_line_pos: usize,
        next_line: &str,
    ) -> usize {
        let (next_bq_depth, next_inner) = count_blockquote_markers(next_line);
        let (raw_indent_cols, _) = leading_indent(next_inner);
        let next_marker = lists::try_parse_list_marker(next_inner, self.config);
        let next_is_definition_marker =
            definition_lists::try_parse_definition_marker(next_inner).is_some();
        let next_is_definition_term = !is_blank_line(next_inner)
            && definition_lists::next_line_is_definition_marker(lines, next_line_pos).is_some();

        // Re-detect the definition marker after stripping a content-container
        // indent (e.g. the 4-space footnote body indent). Without this, a `:`
        // line nested inside a footnote body fails the 0-3-space marker test
        // and the parent DefinitionList/DefinitionItem incorrectly closes
        // across blank lines, splitting one logical item into many.
        let stripped_is_definition_marker = |content_indent_so_far: usize| -> bool {
            if content_indent_so_far == 0 || raw_indent_cols < content_indent_so_far {
                return false;
            }
            let strip_bytes = crate::parser::utils::container_stack::byte_index_at_column(
                next_inner,
                content_indent_so_far,
            );
            if strip_bytes > next_inner.len() {
                return false;
            }
            definition_lists::try_parse_definition_marker(&next_inner[strip_bytes..]).is_some()
        };

        // `current_bq_depth` is used for proper indent calculation when the next line
        // increases blockquote nesting.

        let mut keep_level = 0;
        let mut content_indent_so_far = 0usize;

        // First, account for blockquotes
        for (i, c) in containers.stack.iter().enumerate() {
            match c {
                crate::parser::utils::container_stack::Container::BlockQuote { .. } => {
                    let bq_count = containers.stack[..=i]
                        .iter()
                        .filter(|x| {
                            matches!(
                                x,
                                crate::parser::utils::container_stack::Container::BlockQuote { .. }
                            )
                        })
                        .count();
                    if bq_count <= next_bq_depth {
                        keep_level = i + 1;
                    }
                }
                crate::parser::utils::container_stack::Container::FootnoteDefinition {
                    content_col,
                    ..
                } => {
                    content_indent_so_far += *content_col;
                    let min_indent = (*content_col).max(4);
                    if raw_indent_cols >= min_indent {
                        keep_level = i + 1;
                    }
                }
                crate::parser::utils::container_stack::Container::Definition {
                    content_col,
                    ..
                } => {
                    // A blank line does not necessarily end a definition, but the continuation
                    // indent must be measured relative to any outer content containers (e.g.
                    // footnotes). Otherwise a line indented only for the footnote would wrongly
                    // continue the definition.
                    let min_indent = self.definition_min_block_indent(*content_col);
                    let effective_indent = raw_indent_cols.saturating_sub(content_indent_so_far);
                    if effective_indent >= min_indent {
                        keep_level = i + 1;
                    }
                    content_indent_so_far += *content_col;
                }
                crate::parser::utils::container_stack::Container::DefinitionItem { .. }
                    if next_is_definition_marker
                        || stripped_is_definition_marker(content_indent_so_far) =>
                {
                    keep_level = i + 1;
                }
                crate::parser::utils::container_stack::Container::DefinitionList { .. }
                    if next_is_definition_marker
                        || next_is_definition_term
                        || stripped_is_definition_marker(content_indent_so_far) =>
                {
                    keep_level = i + 1;
                }
                crate::parser::utils::container_stack::Container::List {
                    marker,
                    base_indent_cols,
                    ..
                } => {
                    let definition_ancestor_kept = containers.stack[..i]
                        .iter()
                        .enumerate()
                        .rev()
                        .find_map(|(idx, container)| {
                            matches!(
                                container,
                                crate::parser::utils::container_stack::Container::Definition { .. }
                            )
                            .then_some(keep_level > idx)
                        })
                        .unwrap_or(true);
                    if !definition_ancestor_kept {
                        continue;
                    }

                    let effective_indent = raw_indent_cols.saturating_sub(content_indent_so_far);
                    let continues_list = if let Some(ref marker_match) = next_marker {
                        // Ordered markers can be right-aligned across items
                        // (e.g. `i.`, `ii.`, `iii.`), so they need a symmetric
                        // drift tolerance. Bullets are directional: a marker
                        // outdented from the list's base indent belongs to an
                        // outer list, not this one. Without that lower bound,
                        // a blank line followed by an outer-level marker keeps
                        // the inner list open and parks the BLANK_LINE inside
                        // it, breaking idempotency for nested-list outputs.
                        let indent_in_range = match marker {
                            lists::ListMarker::Ordered(_) => {
                                effective_indent.abs_diff(*base_indent_cols) <= 3
                            }
                            lists::ListMarker::Bullet(_) => {
                                // A bullet marker at indent ≥ 4 cannot continue
                                // a shallow-base bullet list across a blank line:
                                // pandoc treats the would-be marker as the start
                                // of an indented code block once the list is
                                // ineligible to absorb it as a sublist of the
                                // open item. The LIST_ITEM branch below still
                                // rescues the LIST when the previous item's
                                // content column accommodates the new indent
                                // (keep_level is monotonic), so this guard only
                                // closes the list when no item can absorb it.
                                let jumps_out_of_shallow_list =
                                    effective_indent >= 4 && *base_indent_cols < 4;
                                if jumps_out_of_shallow_list {
                                    false
                                } else if effective_indent >= *base_indent_cols {
                                    effective_indent <= base_indent_cols + 3
                                } else {
                                    // Bullets are directional, but only when an
                                    // outer bullet list with matching marker can
                                    // absorb the outdented marker. With no such
                                    // outer list, pandoc keeps the current list
                                    // open (the marker continues this list with
                                    // a small leftward drift). Closing here would
                                    // split one logical list into two and surface
                                    // as an idempotency failure once the
                                    // formatter normalizes indents.
                                    let has_outer_match =
                                        containers.stack[..i].iter().any(|outer| {
                                            matches!(
                                                outer,
                                                crate::parser::utils::container_stack::Container::List {
                                                    marker: outer_marker,
                                                    base_indent_cols: outer_base,
                                                    ..
                                                } if matches!(
                                                    outer_marker,
                                                    lists::ListMarker::Bullet(_)
                                                ) && lists::markers_match(
                                                    outer_marker,
                                                    &marker_match.marker,
                                                    self.config.dialect,
                                                ) && *outer_base <= effective_indent
                                            )
                                        });
                                    !has_outer_match
                                        && base_indent_cols.saturating_sub(effective_indent) <= 3
                                }
                            }
                        };
                        lists::markers_match(marker, &marker_match.marker, self.config.dialect)
                            && indent_in_range
                    } else {
                        let item_content_col = containers
                            .stack
                            .get(i + 1)
                            .and_then(|c| match c {
                                crate::parser::utils::container_stack::Container::ListItem {
                                    content_col,
                                    ..
                                } => Some(*content_col),
                                _ => None,
                            })
                            .unwrap_or(1);
                        effective_indent >= item_content_col
                    };
                    if continues_list {
                        keep_level = i + 1;
                    }
                }
                crate::parser::utils::container_stack::Container::ListItem {
                    content_col,
                    marker_only,
                    ..
                } => {
                    let definition_ancestor_kept = containers.stack[..i]
                        .iter()
                        .enumerate()
                        .rev()
                        .find_map(|(idx, container)| {
                            matches!(
                                container,
                                crate::parser::utils::container_stack::Container::Definition { .. }
                            )
                            .then_some(keep_level > idx)
                        })
                        .unwrap_or(true);
                    if !definition_ancestor_kept {
                        continue;
                    }

                    // CommonMark §5.2: a list item that has only seen its
                    // marker line is closed by the first blank line. Any
                    // subsequent indented content is no longer part of the
                    // item. Pandoc keeps the item open across the blank.
                    if *marker_only && self.config.dialect == crate::options::Dialect::CommonMark {
                        // If the next line doesn't start another list marker,
                        // the parent List has nothing to continue with — close
                        // it too. (The List's own branch above optimistically
                        // kept itself based on indent ≥ content_col, which
                        // assumes a continuing item; that assumption fails
                        // once the empty item is closed by the blank.)
                        if next_marker.is_none() && i > 0 && keep_level == i {
                            keep_level = i - 1;
                        }
                        continue;
                    }

                    let effective_indent = if next_bq_depth > current_bq_depth {
                        let after_current_bq =
                            strip_n_blockquote_markers(next_line, current_bq_depth);
                        let (spaces_before_next_marker, _) = leading_indent(after_current_bq);
                        spaces_before_next_marker.saturating_sub(content_indent_so_far)
                    } else {
                        raw_indent_cols.saturating_sub(content_indent_so_far)
                    };

                    let is_new_item_at_outer_level = if next_marker.is_some() {
                        effective_indent < *content_col
                    } else {
                        false
                    };

                    if !is_new_item_at_outer_level && effective_indent >= *content_col {
                        keep_level = i + 1;
                    }
                }
                _ => {}
            }
        }

        keep_level
    }

    /// Checks whether a line inside a definition should be treated as a plain continuation
    /// (and buffered into the definition PLAIN), rather than parsed as a new block.
    pub(crate) fn definition_plain_can_continue(
        &self,
        stripped_content: &str,
        raw_content: &str,
        content_indent: usize,
        block_ctx: &BlockContext,
        lines: &[&str],
        pos: usize,
    ) -> bool {
        let prev_line_blank = if pos > 0 {
            let prev_line = lines[pos - 1];
            let (prev_bq_depth, prev_inner) = count_blockquote_markers(prev_line);
            is_blank_line(prev_line) || (prev_bq_depth > 0 && is_blank_line(prev_inner))
        } else {
            false
        };

        // A blank line that isn't indented to the definition content column ends the definition.
        let (indent_cols, _) = leading_indent(raw_content);
        if is_blank_line(raw_content) && indent_cols < content_indent {
            return false;
        }
        let min_block_indent = self.definition_min_block_indent(content_indent);
        if prev_line_blank && indent_cols < min_block_indent {
            return false;
        }

        // If it's a block element marker, don't continue as plain.
        if definition_lists::try_parse_definition_marker(stripped_content).is_some()
            && leading_indent(raw_content).0 <= 3
            && !stripped_content.starts_with(':')
        {
            let is_next_definition = self
                .block_registry
                .detect_prepared(block_ctx, lines, pos)
                .map(|match_result| {
                    match_result.effect
                        == crate::parser::block_dispatcher::BlockEffect::OpenDefinitionList
                })
                .unwrap_or(false);
            if is_next_definition {
                return false;
            }
        }
        if lists::try_parse_list_marker(stripped_content, self.config).is_some() {
            if prev_line_blank {
                return false;
            }
            if block_ctx.in_list {
                return false;
            }
            // A list marker indented to the definition's content column opens a
            // nested list inside the definition (matches pandoc-native), even
            // without a separating blank line.
            let (raw_indent_cols, _) = leading_indent(raw_content);
            if content_indent > 0 && raw_indent_cols >= content_indent {
                return false;
            }
        }
        if count_blockquote_markers(stripped_content).0 > 0 {
            return false;
        }
        if self.config.extensions.raw_html
            && html_blocks::try_parse_html_block_start(
                stripped_content,
                self.config.dialect == crate::options::Dialect::CommonMark,
            )
            .is_some()
        {
            return false;
        }
        if self.config.extensions.raw_tex
            && raw_blocks::extract_environment_name(stripped_content).is_some()
        {
            return false;
        }

        if let Some(match_result) = self.block_registry.detect_prepared(block_ctx, lines, pos) {
            if match_result.effect == crate::parser::block_dispatcher::BlockEffect::OpenList
                && !prev_line_blank
            {
                return true;
            }
            if match_result.effect
                == crate::parser::block_dispatcher::BlockEffect::OpenDefinitionList
                && match_result
                    .payload
                    .as_ref()
                    .and_then(|payload| {
                        payload
                            .downcast_ref::<crate::parser::block_dispatcher::DefinitionPrepared>()
                    })
                    .is_some_and(|prepared| {
                        matches!(
                            prepared,
                            crate::parser::block_dispatcher::DefinitionPrepared::Term { .. }
                        )
                    })
            {
                return true;
            }
            return false;
        }

        true
    }
}