unparser-shared 0.12.0

Shared plumbing for the un* document extraction family — C-ABI conventions today; markdown refine and VLM/AI capabilities land as opt-in features
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
413
414
415
416
417
418
419
420
421
422
423
424
//! What `markdown::image`, `markdown::link`, `markdown::link_destination` and `markdown::code_span`
//! write, read back by a CommonMark parser: exactly one image, link or code span, whose text,
//! destination and title are the ones given.

use proptest::prelude::*;
use pulldown_cmark::{Event, Options, Parser, Tag, TagEnd};
use unparser_shared::markdown::{code_span, image, link, link_destination};

/// The images in `markdown`, each as (alt text, destination).
fn images(markdown: &str) -> Vec<(String, String)> {
    let mut found = Vec::new();
    let mut current: Option<(String, String)> = None;
    for event in Parser::new_ext(markdown, Options::ENABLE_TABLES) {
        match event {
            Event::Start(Tag::Image { dest_url, .. }) => {
                current = Some((String::new(), dest_url.to_string()))
            }
            Event::End(TagEnd::Image) => found.extend(current.take()),
            Event::Text(text) | Event::Code(text) => {
                if let Some((alt, _)) = current.as_mut() {
                    alt.push_str(&text);
                }
            }
            _ => {}
        }
    }
    found
}

/// The destination a parser should read back: the URL, its line endings percent-encoded.
fn expected_destination(url: &str) -> String {
    url.replace('\n', "%0A").replace('\r', "%0D")
}

fn flattened(alt: &str) -> String {
    alt.split_whitespace().collect::<Vec<_>>().join(" ")
}

#[test]
fn a_multi_paragraph_description_stays_one_image() {
    let md = image(
        "A person standing in front of a building\n\nAI-generated content may be incorrect.",
        "image1.jpeg",
        false,
    );
    assert_eq!(
        images(&md),
        [(
            "A person standing in front of a building AI-generated content may be incorrect."
                .to_string(),
            "image1.jpeg".to_string()
        )]
    );
}

#[test]
fn destinations_that_the_bare_form_cannot_carry() {
    for url in [
        "notes).txt",
        "a(b",
        "my folder/file.png",
        "a<b>c d",
        "",
        "C:\\dir\\file.png",
        "trailing\\",
        "a\\(b",
        "line\nbreak",
        "?a=1&amp;b=2",
        "tab\there",
    ] {
        let md = image("x", url, false);
        assert_eq!(
            images(&md),
            [("x".to_string(), expected_destination(url))],
            "{url:?} written as {md:?}"
        );
    }
}

#[test]
fn bare_destinations_stay_bare() {
    assert_eq!(
        link_destination("https://example.com/a(b)c", false),
        "https://example.com/a(b)c"
    );
    assert_eq!(
        link_destination("C:\\dir\\file.png", false),
        "C:\\dir\\file.png"
    );
    assert_eq!(link_destination("?a=1&b=2", false), "?a=1&b=2");
}

#[test]
fn an_image_in_a_table_cell_keeps_the_cell() {
    let md = format!(
        "| a | b |\n|---|---|\n| {} | z |\n",
        image("p | q", "i.png", true)
    );
    let cells = Parser::new_ext(&md, Options::ENABLE_TABLES)
        .filter(|e| matches!(e, Event::Start(Tag::TableCell)))
        .count();
    assert_eq!(cells, 4, "{md}");
    assert_eq!(images(&md), [("p | q".to_string(), "i.png".to_string())]);
}

/// Alt text the escaping is meant to carry through unchanged (after whitespace flattening):
/// brackets, backslashes, backticks, angle brackets, character references, pipes, letters
/// from any script. `*`, `_` and `~` are excluded — a matched pair is emphasis by design.
fn alt_text() -> impl Strategy<Value = String> {
    prop::collection::vec(
        prop::sample::select(vec![
            "a",
            "Z",
            "7",
            " ",
            "\n",
            "\n\n",
            "\t",
            "[",
            "]",
            "\\",
            "`",
            "``",
            "<",
            ">",
            "<b>",
            "&",
            "&amp;",
            "&#35;",
            "&#x41;",
            "|",
            "(",
            ")",
            "!",
            "#",
            "-",
            "가",
            "한",
            "é",
            ":",
            "/",
            ".",
            "http://x.y",
        ]),
        0..12,
    )
    .prop_map(|parts| parts.concat())
}

fn url_text() -> impl Strategy<Value = String> {
    prop::collection::vec(
        prop::sample::select(vec![
            "a", "Z", "7", " ", "(", ")", "<", ">", "\\", "\\(", "&", "&amp;", "&#35;", "%20", "/",
            ".", ":", "?", "=", "#", "가", "\n", "\t", "\"", "'", "|", "a|b",
        ]),
        0..12,
    )
    .prop_map(|parts| parts.concat())
}

proptest! {
    #[test]
    fn any_alt_and_destination_read_back_as_one_image(alt in alt_text(), url in url_text()) {
        let md = image(&alt, &url, false);
        prop_assert_eq!(
            images(&md),
            vec![(flattened(&alt), expected_destination(&url))],
            "written as {:?}", md
        );
    }

    #[test]
    fn inside_a_table_cell_too(alt in alt_text(), url in url_text()) {
        let md = format!("| h |\n|---|\n| {} |\n", image(&alt, &url, true));
        prop_assert_eq!(
            images(&md),
            vec![(flattened(&alt), expected_destination(&url))],
            "written as {:?}", md
        );
    }
}

/// Footnotes on, as the writers' output uses them (`[^1]`), so a label that could read as a
/// footnote reference is caught.
fn options() -> Options {
    Options::ENABLE_TABLES | Options::ENABLE_FOOTNOTES
}

/// The links in `markdown`, each as (text, destination, title). Code spans count as their
/// text; a soft break as a space.
fn links(markdown: &str) -> Vec<(String, String, String)> {
    let mut found = Vec::new();
    let mut current: Option<(String, String, String)> = None;
    for event in Parser::new_ext(markdown, options()) {
        match event {
            Event::Start(Tag::Link {
                dest_url, title, ..
            }) => current = Some((String::new(), dest_url.to_string(), title.to_string())),
            Event::End(TagEnd::Link) => found.extend(current.take()),
            Event::Text(text) | Event::Code(text) => {
                if let Some((t, _, _)) = current.as_mut() {
                    t.push_str(&text);
                }
            }
            Event::SoftBreak => {
                if let Some((t, _, _)) = current.as_mut() {
                    t.push(' ');
                }
            }
            _ => {}
        }
    }
    found
}

fn one_link(text: &str, dest: &str, title: &str) -> Vec<(String, String, String)> {
    vec![(text.to_string(), dest.to_string(), title.to_string())]
}

#[test]
fn a_label_with_brackets_stays_one_link() {
    for label in ["see [3]", "a ] b", "[", "x [y](z) w", "]]"] {
        let md = link(label, "https://example.com", None, false);
        assert_eq!(
            links(&md),
            one_link(label, "https://example.com", ""),
            "{md}"
        );
    }
}

#[test]
fn a_rendered_label_keeps_its_markup_and_escapes() {
    // Styling stays styling; an escape the writer made stays an escape.
    let md = link(r"**bold** and \*star\*", "u", None, false);
    assert_eq!(links(&md), one_link("bold and *star*", "u", ""), "{md}");
    let md = link(r"already \[escaped\]", "u", None, false);
    assert_eq!(md, r"[already \[escaped\]](u)");
    assert_eq!(links(&md), one_link("already [escaped]", "u", ""));
}

#[test]
fn a_code_span_in_the_label_is_left_as_it_is() {
    let md = link("call `f[0]` or ``a`]b``", "u", None, false);
    assert_eq!(md, "[call `f[0]` or ``a`]b``](u)");
    assert_eq!(links(&md), one_link("call f[0] or a`]b", "u", ""));
    // An unclosed backtick is literal, and the bracket after it is escaped.
    let md = link("a ` b ]", "u", None, false);
    assert_eq!(links(&md), one_link("a ` b ]", "u", ""), "{md}");
}

#[test]
fn a_blank_line_in_the_label_does_not_end_the_paragraph() {
    let md = link("first\n\nsecond", "u", None, false);
    assert_eq!(links(&md), one_link("first  second", "u", ""), "{md}");
}

#[test]
fn a_trailing_backslash_does_not_escape_the_closing_bracket() {
    let md = link(r"C:\dir\", "u", None, false);
    assert_eq!(links(&md), one_link(r"C:\dir\", "u", ""), "{md}");
}

/// The inline link wins over a footnote reference of the same label, so `^` needs no escape.
#[test]
fn a_caret_label_is_not_a_footnote_reference() {
    let md = format!("{}\n\n[^1]: the note\n", link("^1", "u", None, false));
    assert_eq!(links(&md), one_link("^1", "u", ""), "{md}");
}

#[test]
fn titles_read_back_as_given() {
    for title in [
        r#"say "hi""#,
        r"back\slash",
        r"ends with \",
        r#"\""#,
        "&amp; stays",
        "two\nlines",
        "a | b",
    ] {
        let md = link("x", "u", Some(title), false);
        let expected = title.replace('\n', " ");
        assert_eq!(
            links(&md),
            one_link("x", "u", &expected),
            "{title:?} as {md}"
        );
    }
    assert_eq!(link("x", "u", Some(""), false), "[x](u)");
}

#[test]
fn a_link_in_a_table_cell_keeps_the_cell() {
    let md = format!(
        "| a | b |\n|---|---|\n| {} | z |\n",
        link("p | q `r|s`", "a|b", Some("t|u"), true)
    );
    let cells = Parser::new_ext(&md, options())
        .filter(|e| matches!(e, Event::Start(Tag::TableCell)))
        .count();
    assert_eq!(cells, 4, "{md}");
    assert_eq!(links(&md), one_link("p | q r|s", "a|b", "t|u"), "{md}");
}

/// Plain label text — no characters that style (`*`, `_`, `~`) or are taken as markup by the
/// writer's own escaping (`\`, `` ` ``, `<`, `&`) — which must read back as itself.
fn plain_label() -> impl Strategy<Value = String> {
    prop::collection::vec(
        prop::sample::select(vec![
            "a", "Z", "7", " ", "\n", "\n\n", "[", "]", "[x](y)", "|", "(", ")", "!", "#", "-",
            "^", "가", "é", ":", "/", ".", "\"",
        ]),
        0..12,
    )
    .prop_map(|parts| parts.concat())
}

fn title_text() -> impl Strategy<Value = String> {
    prop::collection::vec(
        prop::sample::select(vec![
            "a", " ", "\"", "\\", "\\\"", "&", "&amp;", "&#35;", "\n", "|", "(", ")", "'", "가",
        ]),
        0..10,
    )
    .prop_map(|parts| parts.concat())
}

/// What a parser reads back from a label: line endings as spaces, and — since a paragraph
/// trims them — no whitespace at either end.
fn read_back_label(label: &str) -> String {
    label.replace(['\n', '\r'], " ").trim().to_string()
}

fn normalized(links: Vec<(String, String, String)>) -> Vec<(String, String, String)> {
    links
        .into_iter()
        .map(|(t, d, ti)| (t.trim().to_string(), d, ti))
        .collect()
}

proptest! {
    #[test]
    fn any_label_destination_and_title_read_back_as_one_link(
        label in plain_label(),
        url in url_text(),
        title in title_text(),
    ) {
        let md = link(&label, &url, Some(&title), false);
        prop_assert_eq!(
            normalized(links(&md)),
            one_link(&read_back_label(&label), &expected_destination(&url), &title.replace('\n', " ")),
            "written as {:?}", md
        );
    }

    #[test]
    fn a_link_inside_a_table_cell_too(label in plain_label(), url in url_text(), title in title_text()) {
        let md = format!("| h |\n|---|\n| {} |\n", link(&label, &url, Some(&title), true));
        prop_assert_eq!(
            normalized(links(&md)),
            one_link(&read_back_label(&label), &expected_destination(&url), &title.replace('\n', " ")),
            "written as {:?}", md
        );
    }
}

/// The code spans in `markdown`, as the parser reads their text back.
fn code_spans(markdown: &str) -> Vec<String> {
    Parser::new_ext(markdown, Options::ENABLE_TABLES)
        .filter_map(|event| match event {
            Event::Code(text) => Some(text.to_string()),
            _ => None,
        })
        .collect()
}

/// What a reader gives back for a code span of `text`: each line ending read as one space.
fn read_back_code(text: &str) -> String {
    text.replace("\r\n", " ").replace(['\r', '\n'], " ")
}

/// Code text the fencing is meant to carry through unchanged: backtick runs of every length at
/// either end, backslashes (printed, never an escape), spaces at the ends, line endings, pipes,
/// Markdown punctuation and letters from any script.
fn code_text() -> impl Strategy<Value = String> {
    prop::collection::vec(
        prop::sample::select(vec![
            "a", "Z", "7", " ", "  ", "\n", "\r\n", "`", "``", "```", "\\", "\\`", "|", "\\|", "[",
            "]", "*", "_", "<b>", "&amp;", "#", "-", "가", "😀",
        ]),
        1..12,
    )
    .prop_map(|parts| parts.concat())
}

#[test]
fn code_spans_that_need_a_longer_fence_or_padding() {
    for text in [
        r"C:\dir\", "a`b", "``", "`x`", " a ", "a ", "   ", "x\n\ny", "|",
    ] {
        let md = code_span(text, false);
        assert_eq!(code_spans(&md), [read_back_code(text)], "written as {md:?}");
    }
}

#[test]
fn an_empty_code_span_writes_nothing() {
    assert_eq!(code_span("", false), "");
}

proptest! {
    #[test]
    fn any_code_text_reads_back_as_one_code_span(text in code_text()) {
        let md = format!("see {} here", code_span(&text, false));
        prop_assert_eq!(code_spans(&md), vec![read_back_code(&text)], "written as {:?}", md);
    }

    #[test]
    fn a_code_span_inside_a_table_cell_too(text in code_text()) {
        let md = format!("| h |\n|---|\n| {} |\n", code_span(&text, true));
        prop_assert_eq!(code_spans(&md), vec![read_back_code(&text)], "written as {:?}", md);
    }
}