cortexkit-rich-ui-syntax 0.2.0

Bounded rich UI language parser and syntax-tree-derived tool output reference scanner
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
use crate::expression::Result;
use crate::parser::Engine;
use crate::DiagnosticCode as C;
use crate::*;
use pulldown_cmark::{CodeBlockKind, Event, Options, Parser, Tag};
use std::collections::VecDeque;

/// Extension spans are opaque to Markdown. Keeping byte lengths unchanged lets
/// the Markdown engine and the language nodes share original source offsets.
pub(crate) fn assemble(
    engine: &mut Engine<'_>,
    start: usize,
    end: usize,
    specials: Vec<Node>,
    inline: bool,
) -> Result<Vec<Node>> {
    let original = &engine.source[start..end];
    let mut masked = context_source(original).into_bytes();
    for node in &specials {
        let a = node.span.start - start;
        let b = node.span.end - start;
        masked[a..b].fill(b'x');
        // A consumed fence includes its final newline, which is still a block
        // boundary for the Markdown that follows the opaque code block.
        if matches!(node.kind, Kind::Markdown(Markdown::CodeBlock { .. })) {
            for i in (a..b)
                .rev()
                .take_while(|i| matches!(original.as_bytes()[*i], b'\n' | b'\r'))
            {
                masked[i] = original.as_bytes()[i];
            }
        }
    }
    let masked = String::from_utf8(masked).map_err(|_| C::Syntax)?;
    let options =
        Options::ENABLE_TABLES | Options::ENABLE_STRIKETHROUGH | Options::ENABLE_TASKLISTS;
    let events = Parser::new_ext(&masked, options).into_offset_iter();
    let mut pending: VecDeque<Node> = specials.into();
    let mut stack: Vec<Node> = Vec::new();
    let mut roots = Vec::new();
    let mut suppressed = Vec::new();
    let mut block_depth = 0;
    let mut emphasis_depth = 0;
    for (event, range) in events {
        engine.work(1)?;
        let span = Span::new(start + range.start, start + range.end);
        match event {
            Event::Start(tag) => {
                let kind = convert_tag(&tag);
                let is_block = matches!(tag, Tag::BlockQuote(_) | Tag::List(_));
                let is_emphasis = matches!(tag, Tag::Emphasis | Tag::Strong | Tag::Strikethrough);
                if is_block {
                    block_depth += 1;
                }
                if is_emphasis {
                    emphasis_depth += 1;
                }
                let skip = ((is_block || matches!(tag, Tag::Item)) && block_depth > MARKDOWN_DEPTH)
                    || (is_emphasis && emphasis_depth > EMPHASIS_DEPTH);
                let delimiter = match tag {
                    Tag::Strong | Tag::Strikethrough => 2,
                    Tag::Emphasis => 1,
                    _ => 0,
                };
                suppressed.push((skip, is_block, is_emphasis, delimiter));
                if skip {
                    let len = if is_emphasis {
                        delimiter
                    } else if matches!(tag, Tag::BlockQuote(_)) {
                        1
                    } else if matches!(tag, Tag::List(_)) {
                        engine.source[span.start..span.end]
                            .find(char::is_whitespace)
                            .unwrap_or(1)
                    } else {
                        0
                    };
                    if len > 0 {
                        let marker = Span::new(span.start, (span.start + len).min(span.end));
                        append(
                            engine.node(
                                marker,
                                Kind::Literal(engine.source[marker.start..marker.end].to_owned()),
                                vec![],
                            )?,
                            &mut stack,
                            &mut roots,
                        );
                    }
                    continue;
                }
                if matches!(
                    kind,
                    Markdown::Paragraph
                        | Markdown::Heading(_)
                        | Markdown::Quote
                        | Markdown::List { .. }
                        | Markdown::Item
                        | Markdown::Table
                        | Markdown::CodeBlock { .. }
                ) {
                    engine.blocks += 1;
                    if engine.blocks > BLOCKS {
                        engine.fatal = true;
                        return Err(C::LimitBlocks);
                    }
                }
                stack.push(engine.node(span, Kind::Markdown(kind), vec![])?);
            }
            Event::End(_) => {
                let (skip, is_block, is_emphasis, delimiter) = suppressed.pop().ok_or(C::Syntax)?;
                if is_block {
                    block_depth -= 1;
                }
                if is_emphasis {
                    emphasis_depth -= 1;
                }
                if skip {
                    if is_emphasis {
                        let marker =
                            Span::new(span.end.saturating_sub(delimiter).max(span.start), span.end);
                        append(
                            engine.node(
                                marker,
                                Kind::Literal(engine.source[marker.start..marker.end].to_owned()),
                                vec![],
                            )?,
                            &mut stack,
                            &mut roots,
                        );
                    }
                    continue;
                }
                let mut node = stack.pop().ok_or(C::Syntax)?;
                node.span.end = span.end;
                if inline && matches!(node.kind, Kind::Markdown(Markdown::Paragraph)) {
                    for child in node.children {
                        append(child, &mut stack, &mut roots);
                    }
                } else {
                    append(node, &mut stack, &mut roots);
                }
            }
            Event::Text(text) | Event::Html(text) | Event::InlineHtml(text) => {
                emit_text(engine, span, &text, &mut pending, &mut stack, &mut roots)?;
            }
            Event::Code(text) => {
                append(
                    engine.node(
                        span,
                        Kind::Markdown(Markdown::Code(text.into_string())),
                        vec![],
                    )?,
                    &mut stack,
                    &mut roots,
                );
            }
            Event::Rule => append(
                engine.node(span, Kind::Markdown(Markdown::ThematicBreak), vec![])?,
                &mut stack,
                &mut roots,
            ),
            Event::SoftBreak | Event::HardBreak => {
                if pending
                    .front()
                    .is_some_and(|node| node.span.start <= span.start && node.span.end > span.start)
                {
                    emit_text(engine, span, "\n", &mut pending, &mut stack, &mut roots)?;
                } else {
                    let kind = if matches!(event, Event::SoftBreak) {
                        Markdown::SoftBreak
                    } else {
                        Markdown::HardBreak
                    };
                    append(
                        engine.node(span, Kind::Markdown(kind), vec![])?,
                        &mut stack,
                        &mut roots,
                    );
                }
            }
            Event::TaskListMarker(checked) => append(
                engine.node(span, Kind::Markdown(Markdown::Task(checked)), vec![])?,
                &mut stack,
                &mut roots,
            ),
            _ => {}
        }
    }
    // A Markdown structural event may consume a masked span (for example a
    // table delimiter). Such a span must never silently lose a language node.
    if !pending.is_empty() {
        return Err(C::Syntax);
    }
    split_blocks(engine, roots)
}

fn append(node: Node, stack: &mut [Node], roots: &mut Vec<Node>) {
    if let Some(parent) = stack.last_mut() {
        parent.children.push(node);
    } else {
        roots.push(node);
    }
}

fn emit_text(
    engine: &mut Engine<'_>,
    span: Span,
    decoded: &str,
    pending: &mut VecDeque<Node>,
    stack: &mut [Node],
    roots: &mut Vec<Node>,
) -> Result<()> {
    let mut pos = span.start;
    let intersects = pending.front().is_some_and(|n| n.span.start < span.end);
    if !intersects {
        let text = decoded
            .replace('\u{1}', "<")
            .replace('\u{2}', " ")
            .replace('\u{3}', "=")
            .replace('\u{4}', "[")
            .replace('\0', "\u{fffd}");
        if !text.is_empty() {
            append(engine.node(span, Kind::Text(text), vec![])?, stack, roots);
        }
        return Ok(());
    }
    while let Some(node) = pending.front() {
        if node.span.start >= span.end {
            break;
        }
        if pos < node.span.start {
            append(
                engine.node(
                    Span::new(pos, node.span.start),
                    Kind::Text(engine.source[pos..node.span.start].replace('\0', "\u{fffd}")),
                    vec![],
                )?,
                stack,
                roots,
            );
        }
        let node = pending.pop_front().ok_or(C::Syntax)?;
        pos = node.span.end;
        append(node, stack, roots);
    }
    if pos < span.end {
        append(
            engine.node(
                Span::new(pos, span.end),
                Kind::Text(engine.source[pos..span.end].replace('\0', "\u{fffd}")),
                vec![],
            )?,
            stack,
            roots,
        );
    }
    Ok(())
}

fn convert_tag(tag: &Tag<'_>) -> Markdown {
    match tag {
        Tag::Paragraph => Markdown::Paragraph,
        Tag::Heading { level, .. } => Markdown::Heading(*level as u8),
        Tag::BlockQuote(_) => Markdown::Quote,
        Tag::CodeBlock(CodeBlockKind::Fenced(info)) => Markdown::CodeBlock {
            language: info.split_whitespace().next().unwrap_or("").into(),
            text: String::new(),
        },
        Tag::CodeBlock(_) => Markdown::Paragraph,
        Tag::List(start) => Markdown::List { start: *start },
        Tag::Item => Markdown::Item,
        Tag::Table(_) => Markdown::Table,
        Tag::TableHead => Markdown::TableHead,
        Tag::TableRow => Markdown::TableRow,
        Tag::TableCell => Markdown::TableCell,
        Tag::Emphasis => Markdown::Emphasis,
        Tag::Strong => Markdown::Strong,
        Tag::Strikethrough => Markdown::Strikethrough,
        Tag::Link {
            dest_url, title, ..
        }
        | Tag::Image {
            dest_url, title, ..
        } => Markdown::Link {
            destination: dest_url.to_string(),
            title: title.to_string(),
            image: matches!(tag, Tag::Image { .. }),
        },
        _ => Markdown::Paragraph,
    }
}

fn split_blocks(engine: &mut Engine<'_>, nodes: Vec<Node>) -> Result<Vec<Node>> {
    let mut result = Vec::new();
    for mut node in nodes {
        if matches!(node.kind, Kind::Markdown(Markdown::Paragraph))
            && node.children.iter().any(is_block_extension)
        {
            let mut run = Vec::new();
            for child in node.children {
                if is_block_extension(&child) {
                    flush_paragraph(engine, &mut run, &mut result)?;
                    result.push(child);
                } else {
                    run.push(child);
                }
            }
            flush_paragraph(engine, &mut run, &mut result)?;
        } else {
            node.children = split_blocks(engine, node.children)?;
            result.push(node);
        }
    }
    Ok(result)
}
fn is_block_extension(node: &Node) -> bool {
    matches!(
        node.kind,
        Kind::Tag { block: true, .. }
            | Kind::Declaration { .. }
            | Kind::Interpolation { block: true }
            | Kind::Markdown(Markdown::CodeBlock { .. } | Markdown::ThematicBreak)
    )
}
fn flush_paragraph(
    engine: &mut Engine<'_>,
    run: &mut Vec<Node>,
    result: &mut Vec<Node>,
) -> Result<()> {
    if let (Some(first), Some(last)) = (run.first(), run.last()) {
        result.push(engine.node(
            Span::new(first.span.start, last.span.end),
            Kind::Markdown(Markdown::Paragraph),
            std::mem::take(run),
        )?);
    }
    Ok(())
}

/// Disable syntax outside the dialect before asking CommonMark for contexts.
/// Each replacement occupies one original byte, including lone-CR newlines.
pub(crate) fn context_source(original: &str) -> String {
    let mut masked = original.as_bytes().to_vec();
    for i in 0..masked.len() {
        if masked[i] == b'<' {
            masked[i] = 1;
        }
        if masked[i] == b'\r' && masked.get(i + 1) != Some(&b'\n') {
            masked[i] = b'\n';
        }
    }
    let mut line = 0;
    while line < masked.len() {
        let stop = masked[line..]
            .iter()
            .position(|b| matches!(b, b'\n' | b'\r'))
            .map_or(masked.len(), |i| line + i);
        let spaces = masked[line..stop]
            .iter()
            .take_while(|b| **b == b' ')
            .count();
        let content = &masked[line + spaces..stop];
        let setext = !content.is_empty() && content.iter().all(|b| *b == b'=');
        let definition = content.starts_with(b"[") && content.windows(2).any(|w| w == b"]:");
        if setext {
            masked[line + spaces] = 3;
        }
        if definition {
            masked[line + spaces] = 4;
        }
        line = stop + 1;
    }
    // Neutralize only actual indented-code regions, not the indentation that
    // CommonMark needs to recognize nested lists and their continuation lines.
    let temporary = String::from_utf8(masked).expect("ASCII substitutions preserve UTF-8");
    let indented: Vec<_> = Parser::new(&temporary)
        .into_offset_iter()
        .filter_map(|(event, range)| {
            matches!(event, Event::Start(Tag::CodeBlock(CodeBlockKind::Indented))).then_some(range)
        })
        .collect();
    let mut masked = temporary.into_bytes();
    for range in indented {
        let mut line = original[..range.start]
            .rfind(['\r', '\n'])
            .map_or(0, |i| i + 1);
        while line < range.end {
            let stop = masked[line..]
                .iter()
                .position(|b| matches!(b, b'\n' | b'\r'))
                .map_or(masked.len(), |i| line + i);
            let spaces = masked[line..stop]
                .iter()
                .take_while(|b| **b == b' ')
                .count();
            if spaces >= 4 {
                masked[line + spaces - 4] = 2;
            }
            line = stop + 1;
        }
    }
    String::from_utf8(masked).expect("ASCII substitutions preserve UTF-8")
}