znote 1.0.0

A lightweight desktop editor for Typst with live preview, built with Tauri
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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
use std::collections::HashMap;
use std::fs::{self, File};
use std::io::Read;
use std::path::Path;

use roxmltree::{Document, Node};
use zip::ZipArchive;

pub const OFFICE: [&str; 6] = ["docx", "pptx", "xlsx", "odt", "odp", "ods"];

const MAX_PART_BYTES: u64 = 32 * 1024 * 1024;

type Archive = ZipArchive<File>;

fn part(archive: &mut Archive, name: &str) -> Result<String, String> {
    let mut text = String::new();
    let entry = archive.by_name(name).map_err(|error| error.to_string())?;
    entry
        .take(MAX_PART_BYTES)
        .read_to_string(&mut text)
        .map_err(|error| error.to_string())?;
    Ok(text)
}

fn numbered(archive: &Archive, prefix: &str) -> Vec<String> {
    let number = |name: &str| {
        name.strip_prefix(prefix)?
            .strip_suffix(".xml")?
            .parse::<u32>()
            .ok()
    };
    let mut names: Vec<(u32, String)> = archive
        .file_names()
        .filter_map(|name| name.ok())
        .filter_map(|name| Some((number(&name)?, name.to_string())))
        .collect();
    names.sort();
    names.into_iter().map(|(_, name)| name).collect()
}

fn named(node: &Node, name: &str) -> bool {
    node.tag_name().name() == name
}

fn one_of(node: &Node, names: &[&str]) -> bool {
    names.contains(&node.tag_name().name())
}

fn attr<'a>(node: &Node<'a, '_>, name: &str) -> Option<&'a str> {
    node.attributes()
        .find(|found| found.name() == name)
        .map(|found| found.value())
}

fn child<'a, 'input>(node: Node<'a, 'input>, name: &str) -> Option<Node<'a, 'input>> {
    node.children().find(|inside| named(inside, name))
}

fn said(node: Node) -> String {
    node.descendants()
        .filter(Node::is_text)
        .filter_map(|text| text.text())
        .collect()
}

fn parsed(xml: &str) -> Result<Document<'_>, String> {
    Document::parse(xml).map_err(|error| error.to_string())
}

const BLOCKS: [&str; 4] = ["p", "h", "tbl", "table"];
const TABLES: [&str; 2] = ["tbl", "table"];
const ROWS: [&str; 2] = ["tr", "table-row"];
const CELLS: [&str; 3] = ["tc", "table-cell", "covered-table-cell"];
const MAX_ROWS: usize = 2000;
const MAX_COLUMNS: usize = 50;

type Headings = HashMap<String, usize>;

fn heading(style: &str) -> Option<usize> {
    let style = style.to_lowercase().replace(' ', "");
    if style == "title" {
        return Some(1);
    }
    style
        .strip_prefix("heading")?
        .parse()
        .ok()
        .filter(|level| (1..=6).contains(level))
}

fn headings(xml: &str) -> Headings {
    let Ok(document) = parsed(xml) else {
        return Headings::new();
    };
    document
        .descendants()
        .filter(|node| named(node, "style"))
        .filter_map(|style| {
            let level = heading(attr(&child(style, "name")?, "val")?)?;
            Some((attr(&style, "styleId")?.to_string(), level))
        })
        .collect()
}

fn spoken(node: Node) -> String {
    node.descendants()
        .filter(|inside| one_of(inside, &["p", "h"]))
        .map(|inside| said(inside).trim().to_string())
        .filter(|text| !text.is_empty())
        .collect::<Vec<_>>()
        .join(" ")
}

fn paragraph(node: Node, headings: &Headings) -> Option<(String, bool)> {
    let text = said(node).trim().to_string();
    if text.is_empty() {
        return None;
    }
    let props = child(node, "pPr");
    let level = if named(&node, "h") {
        Some(
            attr(&node, "outline-level")
                .and_then(|level| level.parse().ok())
                .unwrap_or(1usize)
                .clamp(1, 6),
        )
    } else {
        props
            .and_then(|props| child(props, "pStyle"))
            .and_then(|style| attr(&style, "val"))
            .and_then(|style| headings.get(style).copied().or_else(|| heading(style)))
    };
    if let Some(level) = level {
        return Some((format!("{} {text}", "#".repeat(level)), false));
    }
    let lists = node
        .ancestors()
        .filter(|above| named(above, "list"))
        .count();
    let depth = props
        .and_then(|props| child(props, "numPr"))
        .map(|numbered| {
            child(numbered, "ilvl")
                .and_then(|level| attr(&level, "val"))
                .and_then(|level| level.parse().ok())
                .unwrap_or(0usize)
        })
        .or((lists > 0).then(|| lists - 1));
    Some(match depth {
        Some(depth) => (format!("{}- {text}", "  ".repeat(depth.min(5))), true),
        None => (text, false),
    })
}

fn filled(cells: &[String]) -> bool {
    cells.iter().any(|cell| !cell.is_empty())
}

fn pipes(rows: &[Vec<String>]) -> String {
    let width = rows
        .iter()
        .filter_map(|cells| cells.iter().rposition(|cell| !cell.is_empty()))
        .max()
        .map_or(0, |last| last + 1);
    let line = |cells: &[String]| {
        let cells: Vec<String> = (0..width)
            .map(|index| {
                cells
                    .get(index)
                    .map(|cell| {
                        cell.split_whitespace()
                            .collect::<Vec<_>>()
                            .join(" ")
                            .replace('|', "\\|")
                    })
                    .unwrap_or_default()
            })
            .collect();
        format!("| {} |", cells.join(" | "))
    };
    let mut rows = rows.iter().filter(|cells| filled(cells));
    let Some(head) = rows.next() else {
        return String::new();
    };
    let mut lines = vec![line(head), format!("|{}", " --- |".repeat(width))];
    lines.extend(rows.map(|cells| line(cells)));
    lines.join("\n")
}

fn in_table(row: &Node, table: Node) -> bool {
    row.ancestors().skip(1).find(|above| one_of(above, &TABLES)) == Some(table)
}

fn grid(table: Node) -> String {
    let rows: Vec<Vec<String>> = table
        .descendants()
        .filter(|row| one_of(row, &ROWS) && in_table(row, table))
        .map(|row| {
            row.children()
                .filter(|cell| one_of(cell, &CELLS))
                .map(spoken)
                .collect()
        })
        .collect();
    pipes(&rows)
}

fn blocks(root: Node, headings: &Headings, skipped: Option<Node>) -> String {
    let found: Vec<(String, bool)> = root
        .descendants()
        .filter(|node| {
            one_of(node, &BLOCKS)
                && !node
                    .ancestors()
                    .skip(1)
                    .any(|above| one_of(&above, &BLOCKS))
                && !skipped.is_some_and(|skipped| node.ancestors().any(|above| above == skipped))
        })
        .filter_map(|node| {
            if one_of(&node, &TABLES) {
                Some((grid(node), false)).filter(|(table, _)| !table.is_empty())
            } else {
                paragraph(node, headings)
            }
        })
        .collect();
    let mut text = String::new();
    for (index, (block, item)) in found.iter().enumerate() {
        if index > 0 {
            text.push_str(if *item && found[index - 1].1 {
                "\n"
            } else {
                "\n\n"
            });
        }
        text.push_str(block);
    }
    text
}

fn section(heading: &str, body: &str) -> String {
    if body.is_empty() {
        format!("## {heading}")
    } else {
        format!("## {heading}\n\n{body}")
    }
}

fn slide(number: usize, root: Node, title: Option<Node>) -> String {
    let named = title
        .map(spoken)
        .filter(|title| !title.is_empty())
        .map_or(format!("Slide {number}"), |title| {
            format!("Slide {number}: {title}")
        });
    section(&named, &blocks(root, &Headings::new(), title))
}

fn sheet_table(mut rows: Vec<Vec<String>>) -> String {
    let more = rows.len() > MAX_ROWS;
    rows.truncate(MAX_ROWS);
    let table = pipes(&rows);
    if more {
        format!("{table}\n\n(More rows not shown.)")
    } else {
        table
    }
}

fn column(reference: &str) -> Option<usize> {
    reference
        .chars()
        .take_while(char::is_ascii_alphabetic)
        .take(3)
        .fold(0usize, |number, letter| {
            number * 26 + (letter.to_ascii_uppercase() as usize - 'A' as usize + 1)
        })
        .checked_sub(1)
}

fn sheet(xml: &str, shared: &[String]) -> Result<String, String> {
    let value = |cell: Node| {
        let value = child(cell, "v").map(said);
        match (attr(&cell, "t"), value) {
            (Some("s"), Some(index)) => index
                .parse()
                .ok()
                .and_then(|index: usize| shared.get(index).cloned())
                .unwrap_or_default(),
            (_, Some(value)) => value,
            _ => cell
                .descendants()
                .filter(|inside| named(inside, "t"))
                .map(said)
                .collect(),
        }
    };
    let document = parsed(xml)?;
    let mut rows = Vec::new();
    for row in document.descendants().filter(|node| named(node, "row")) {
        let mut cells: Vec<String> = Vec::new();
        for cell in row.children().filter(|inside| named(inside, "c")) {
            let at = attr(&cell, "r")
                .and_then(column)
                .unwrap_or(cells.len())
                .max(cells.len());
            if at >= MAX_COLUMNS {
                break;
            }
            cells.resize(at, String::new());
            cells.push(value(cell));
        }
        if filled(&cells) {
            rows.push(cells);
            if rows.len() > MAX_ROWS {
                break;
            }
        }
    }
    Ok(sheet_table(rows))
}

fn repeated(node: &Node, name: &str) -> usize {
    attr(node, name)
        .and_then(|count| count.parse().ok())
        .unwrap_or(1)
}

fn open_sheet(table: Node) -> String {
    let mut rows = Vec::new();
    for row in table
        .descendants()
        .filter(|row| named(row, "table-row") && in_table(row, table))
    {
        let mut cells: Vec<String> = Vec::new();
        for cell in row.children().filter(|cell| one_of(cell, &CELLS)) {
            let text = spoken(cell);
            let count = repeated(&cell, "number-columns-repeated").min(MAX_COLUMNS - cells.len());
            cells.extend(std::iter::repeat_n(text, count));
            if cells.len() >= MAX_COLUMNS {
                break;
            }
        }
        if !filled(&cells) {
            continue;
        }
        let count = repeated(&row, "number-rows-repeated").min(MAX_ROWS + 1 - rows.len());
        rows.extend(std::iter::repeat_n(cells, count));
        if rows.len() > MAX_ROWS {
            break;
        }
    }
    sheet_table(rows)
}

fn worksheets(archive: &mut Archive) -> Vec<(String, String)> {
    let mut listed = || -> Result<Vec<(String, String)>, String> {
        let links = part(archive, "xl/_rels/workbook.xml.rels")?;
        let links = parsed(&links)?;
        let targets: HashMap<&str, &str> = links
            .descendants()
            .filter(|node| named(node, "Relationship"))
            .filter_map(|link| Some((attr(&link, "Id")?, attr(&link, "Target")?)))
            .collect();
        let workbook = part(archive, "xl/workbook.xml")?;
        let found = parsed(&workbook)?
            .descendants()
            .filter(|node| named(node, "sheet"))
            .filter_map(|sheet| {
                let target = targets.get(attr(&sheet, "id")?)?;
                let path = target
                    .strip_prefix('/')
                    .map_or(format!("xl/{target}"), str::to_string);
                Some((attr(&sheet, "name")?.to_string(), path))
            })
            .filter(|(_, path)| archive.index_for_name(path).is_some())
            .collect();
        Ok(found)
    };
    match listed() {
        Ok(found) if !found.is_empty() => found,
        _ => numbered(archive, "xl/worksheets/sheet")
            .into_iter()
            .enumerate()
            .map(|(index, name)| (format!("Sheet {}", index + 1), name))
            .collect(),
    }
}

fn office(file: &Path, kind: &str) -> Result<String, String> {
    let mut archive = File::open(file)
        .map_err(|error| error.to_string())
        .and_then(|file| ZipArchive::new(file).map_err(|error| error.to_string()))?;
    let mut sections = Vec::new();
    match kind {
        "docx" => {
            let styles = part(&mut archive, "word/styles.xml")
                .map(|xml| headings(&xml))
                .unwrap_or_default();
            let xml = part(&mut archive, "word/document.xml")?;
            sections.push(blocks(parsed(&xml)?.root(), &styles, None));
        }
        "pptx" => {
            for (index, name) in numbered(&archive, "ppt/slides/slide").iter().enumerate() {
                let xml = part(&mut archive, name)?;
                let document = parsed(&xml)?;
                let title = document
                    .descendants()
                    .find(|node| {
                        named(node, "ph")
                            && matches!(attr(node, "type"), Some("title" | "ctrTitle"))
                    })
                    .and_then(|place| place.ancestors().find(|above| named(above, "sp")));
                sections.push(slide(index + 1, document.root(), title));
            }
        }
        "xlsx" => {
            let shared: Vec<String> = match part(&mut archive, "xl/sharedStrings.xml") {
                Ok(xml) => parsed(&xml)?
                    .descendants()
                    .filter(|node| named(node, "si"))
                    .map(said)
                    .collect(),
                Err(_) => Vec::new(),
            };
            for (name, path) in worksheets(&mut archive) {
                sections.push(section(
                    &name,
                    &sheet(&part(&mut archive, &path)?, &shared)?,
                ));
            }
        }
        _ => {
            let xml = part(&mut archive, "content.xml")?;
            let document = parsed(&xml)?;
            match kind {
                "odp" => {
                    for (index, page) in document
                        .descendants()
                        .filter(|node| named(node, "page"))
                        .enumerate()
                    {
                        let title = page.descendants().find(|node| {
                            named(node, "frame") && attr(node, "class") == Some("title")
                        });
                        sections.push(slide(index + 1, page, title));
                    }
                }
                "ods" => {
                    for (index, table) in document
                        .descendants()
                        .filter(|node| {
                            named(node, "table")
                                && !node.ancestors().skip(1).any(|above| named(&above, "table"))
                        })
                        .enumerate()
                    {
                        let name = attr(&table, "name")
                            .map_or(format!("Sheet {}", index + 1), str::to_string);
                        sections.push(section(&name, &open_sheet(table)));
                    }
                }
                _ => sections.push(blocks(document.root(), &Headings::new(), None)),
            }
        }
    }
    Ok(sections.join("\n\n"))
}

fn pdf(file: &Path) -> Result<String, String> {
    let document = lopdf::Document::load(file).map_err(|error| error.to_string())?;
    let pages: Vec<String> = document
        .get_pages()
        .into_keys()
        .filter_map(|page| {
            Some(document.extract_text(&[page]).ok()?.trim().to_string())
                .filter(|text| !text.is_empty())
                .map(|text| section(&format!("Page {page}"), &text))
        })
        .collect();
    if pages.is_empty() {
        return Err("it has no text in it. It may be a scan.".into());
    }
    Ok(pages.join("\n\n"))
}

pub fn text(file: &Path) -> Result<String, String> {
    crate::guarded("This file could not be read: it may be damaged.", || {
        read(file)
    })
}

fn read(file: &Path) -> Result<String, String> {
    let kind = file
        .extension()
        .and_then(|kind| kind.to_str())
        .unwrap_or_default()
        .to_lowercase();
    let read = match kind.as_str() {
        "pdf" => pdf(file),
        kind if OFFICE.contains(&kind) => office(file, kind),
        _ => {
            return fs::read_to_string(file)
                .map_err(|_| "This file cannot be read as text.".to_string());
        }
    };
    read.map_err(|error| format!("This {kind} file cannot be read: {error}"))
}

#[cfg(test)]
mod tests {
    use std::io::Write;
    use std::sync::Arc;

    use zip::ZipWriter;
    use zip::write::SimpleFileOptions;

    use super::*;
    use crate::compile::{PageWorld, font_store};

    fn scratch(test: &str) -> std::path::PathBuf {
        let folder =
            std::env::temp_dir().join(format!("znote-extract-{test}-{}", std::process::id()));
        fs::create_dir_all(&folder).unwrap();
        folder
    }

    fn packed(file: &Path, parts: &[(&str, &str)]) {
        let mut archive = ZipWriter::new(File::create(file).unwrap());
        for (name, xml) in parts {
            archive
                .start_file(*name, SimpleFileOptions::default())
                .unwrap();
            archive.write_all(xml.as_bytes()).unwrap();
        }
        archive.finish().unwrap();
    }

    #[test]
    fn a_pdf_gives_its_text_and_a_broken_one_gives_a_reason() {
        let folder = scratch("pdf");
        let fonts = Arc::new(font_store(&folder.join("no-themes")));
        let mut world = PageWorld::new(
            &folder,
            "main.typ",
            &folder.join("no-themes"),
            fonts,
            |_| {},
        )
        .unwrap();
        let file = folder.join("report.PDF");
        fs::write(
            &file,
            crate::export::pdf(
                &world
                    .document("Hello znote\n#pagebreak()\nSecond".into())
                    .unwrap(),
            )
            .unwrap(),
        )
        .unwrap();
        let read = text(&file).unwrap();
        assert!(
            read.contains("## Page 1\n\nHello znote") && read.contains("## Page 2\n\nSecond"),
            "{read}"
        );

        fs::write(&file, b"%PDF-1.7 nothing here").unwrap();
        assert!(
            text(&file)
                .unwrap_err()
                .starts_with("This pdf file cannot be read")
        );
    }

    #[test]
    fn office_files_give_their_text() {
        let folder = scratch("office");
        let file = |name: &str, parts: &[(&str, &str)]| {
            packed(&folder.join(name), parts);
            text(&folder.join(name))
        };
        let word = r#"<w:document xmlns:w="w"><w:body><w:p><w:r><w:t>Tom &amp; </w:t></w:r><w:r><w:t>Ann</w:t></w:r></w:p><w:p/><w:p><w:r><w:t>Next</w:t></w:r></w:p></w:body></w:document>"#;
        assert_eq!(
            file("a.docx", &[("word/document.xml", word)]).unwrap(),
            "Tom & Ann\n\nNext"
        );

        let slide = |said: &str| {
            format!(
                r#"<p:sld xmlns:p="p" xmlns:a="a"><a:p><a:r><a:t>{said}</a:t></a:r></a:p></p:sld>"#
            )
        };
        let slides = [
            ("ppt/slides/slide10.xml", slide("Last")),
            ("ppt/slides/slide2.xml", slide("First")),
        ];
        let slides: Vec<(&str, &str)> = slides
            .iter()
            .map(|(name, xml)| (*name, xml.as_str()))
            .collect();
        assert_eq!(
            file("a.pptx", &slides).unwrap(),
            "## Slide 1\n\nFirst\n\n## Slide 2\n\nLast"
        );

        let shared = "<sst><si><t>Name</t></si><si><r><t>Mi</t></r><r><t>lk</t></r></si></sst>";
        let cells = r#"<worksheet><sheetData><row><c t="s"><v>0</v></c><c t="inlineStr"><is><t>Price</t></is></c></row><row><c t="s"><v>1</v></c><c><f>1+1</f><v>2</v></c></row></sheetData></worksheet>"#;
        assert_eq!(
            file(
                "a.xlsx",
                &[
                    ("xl/sharedStrings.xml", shared),
                    ("xl/worksheets/sheet1.xml", cells)
                ]
            )
            .unwrap(),
            "## Sheet 1\n\n| Name | Price |\n| --- | --- |\n| Milk | 2 |"
        );

        let open = r#"<office:document-content xmlns:office="o" xmlns:text="t"><text:p>Hello <text:span>there</text:span></text:p></office:document-content>"#;
        assert_eq!(
            file("a.odt", &[("content.xml", open)]).unwrap(),
            "Hello there"
        );

        assert!(file("b.docx", &[("other.xml", "<a/>")]).is_err());
        fs::write(folder.join("c.docx"), "not a zip").unwrap();
        assert!(text(&folder.join("c.docx")).is_err());
        fs::write(folder.join("notes.md"), "plain").unwrap();
        assert_eq!(text(&folder.join("notes.md")).unwrap(), "plain");
        fs::write(folder.join("picture.png"), [0xff, 0xd8, 0x00]).unwrap();
        assert!(text(&folder.join("picture.png")).is_err());
    }

    #[test]
    fn office_files_keep_their_structure() {
        let folder = scratch("structure");
        let file = |name: &str, parts: &[(&str, &str)]| {
            packed(&folder.join(name), parts);
            text(&folder.join(name)).unwrap()
        };
        let styles = r#"<w:styles xmlns:w="w"><w:style w:styleId="Kop2"><w:name w:val="heading 2"/></w:style></w:styles>"#;
        let word = r#"<w:document xmlns:w="w"><w:body>
            <w:p><w:pPr><w:pStyle w:val="Title"/></w:pPr><w:r><w:t>Plan</w:t></w:r></w:p>
            <w:p><w:pPr><w:pStyle w:val="Kop2"/></w:pPr><w:r><w:t>Goals</w:t></w:r></w:p>
            <w:p><w:pPr><w:numPr><w:ilvl w:val="0"/></w:numPr></w:pPr><w:r><w:t>One</w:t></w:r></w:p>
            <w:p><w:pPr><w:numPr><w:ilvl w:val="1"/></w:numPr></w:pPr><w:r><w:t>Two</w:t></w:r></w:p>
            <w:tbl><w:tr><w:tc><w:p><w:r><w:t>Name</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:t>Role</w:t></w:r></w:p></w:tc></w:tr>
            <w:tr><w:tc><w:p><w:r><w:t>Tom | Ann</w:t></w:r></w:p><w:p><w:r><w:t>Smith</w:t></w:r></w:p></w:tc><w:tc/></w:tr></w:tbl>
            <w:p><w:r><w:t>End</w:t></w:r></w:p>
        </w:body></w:document>"#;
        assert_eq!(
            file(
                "plan.docx",
                &[("word/document.xml", word), ("word/styles.xml", styles)]
            ),
            "# Plan\n\n## Goals\n\n- One\n  - Two\n\n| Name | Role |\n| --- | --- |\n| Tom \\| Ann Smith |  |\n\nEnd"
        );

        let open = r#"<office:document-content xmlns:office="o" xmlns:text="t"><office:body><office:text>
            <text:h text:outline-level="3">Scope</text:h>
            <text:list><text:list-item><text:p>Alpha</text:p></text:list-item></text:list>
            </office:text></office:body></office:document-content>"#;
        assert_eq!(
            file("a.odt", &[("content.xml", open)]),
            "### Scope\n\n- Alpha"
        );

        let slide = r#"<p:sld xmlns:p="p" xmlns:a="a"><p:sp><p:nvSpPr><p:nvPr><p:ph type="title"/></p:nvPr></p:nvSpPr><a:p><a:r><a:t>Intro</a:t></a:r></a:p></p:sp><p:sp><a:p><a:r><a:t>Hello</a:t></a:r></a:p></p:sp></p:sld>"#;
        assert_eq!(
            file("a.pptx", &[("ppt/slides/slide1.xml", slide)]),
            "## Slide 1: Intro\n\nHello"
        );

        let book = r#"<workbook xmlns:r="r"><sheets><sheet name="Budget" sheetId="1" r:id="rId7"/></sheets></workbook>"#;
        let links = r#"<Relationships><Relationship Id="rId7" Target="worksheets/sheet3.xml"/></Relationships>"#;
        let cells = r#"<worksheet><sheetData><row><c r="A1" t="inlineStr"><is><t>Item</t></is></c><c r="C1" t="inlineStr"><is><t>Cost</t></is></c></row><row/><row><c r="C3"><v>4</v></c></row></sheetData></worksheet>"#;
        assert_eq!(
            file(
                "a.xlsx",
                &[
                    ("xl/workbook.xml", book),
                    ("xl/_rels/workbook.xml.rels", links),
                    ("xl/worksheets/sheet3.xml", cells)
                ]
            ),
            "## Budget\n\n| Item |  | Cost |\n| --- | --- | --- |\n|  |  | 4 |"
        );

        let calc = r#"<office:document-content xmlns:office="o" xmlns:table="t" xmlns:text="x"><office:body><office:spreadsheet>
            <table:table table:name="Stock"><table:table-row><table:table-cell><text:p>Box</text:p></table:table-cell><table:table-cell table:number-columns-repeated="16384"/></table:table-row>
            <table:table-row table:number-rows-repeated="3"><table:table-cell><text:p>x</text:p></table:table-cell></table:table-row>
            <table:table-row table:number-rows-repeated="1048576"><table:table-cell table:number-columns-repeated="16384"/></table:table-row></table:table>
            </office:spreadsheet></office:body></office:document-content>"#;
        assert_eq!(
            file("a.ods", &[("content.xml", calc)]),
            "## Stock\n\n| Box |\n| --- |\n| x |\n| x |\n| x |"
        );

        let many: String = (0..MAX_ROWS + 5)
            .map(|row| format!("<row><c><v>{row}</v></c></row>"))
            .collect();
        let many = format!("<worksheet><sheetData>{many}</sheetData></worksheet>");
        let read = file("big.xlsx", &[("xl/worksheets/sheet1.xml", &many)]);
        assert!(
            read.ends_with(&format!("| {} |\n\n(More rows not shown.)", MAX_ROWS - 1)),
            "{read}"
        );

        let show = r#"<office:document-content xmlns:office="o" xmlns:draw="d" xmlns:presentation="p" xmlns:text="t"><office:body><office:presentation>
            <draw:page><draw:frame presentation:class="title"><draw:text-box><text:p>Welcome</text:p></draw:text-box></draw:frame><draw:frame><draw:text-box><text:p>Body</text:p></draw:text-box></draw:frame></draw:page>
            <draw:page/></office:presentation></office:body></office:document-content>"#;
        assert_eq!(
            file("a.odp", &[("content.xml", show)]),
            "## Slide 1: Welcome\n\nBody\n\n## Slide 2"
        );
    }
}