croft-software 0.2.32

VSCode-style TUI written in Rust
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
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
//! docx / odt read-only rich-text preview (#181): both formats are
//! zip + XML. The walker emits MARKDOWN from the document XML -
//! headings, emphasis, lists, tables, and embedded images (extracted
//! into the session scratch directory) - and the proven markdown
//! builder turns that into the preview's (lines, images) state, so
//! styling, wrapping, and the inline-image overlay all reuse. Nothing
//! edits or saves; fidelity is document STRUCTURE, not layout.

use quick_xml::events::Event;
use std::io::Read as _;
use std::path::{Path, PathBuf};

/// Source cap: these parse fully in memory. This bounds the DECOMPRESSED
/// bytes a member may yield, which is why it is generous.
pub const MAX_DOC_BYTES: u64 = 50 * 1024 * 1024;

/// Pre-parse cap on the FILE, checked before `ZipArchive::new` (#506).
///
/// A different limit from [`MAX_DOC_BYTES`], and conflating them is the bug
/// this fixes: that one bounds how much a member may DECOMPRESS to once the
/// archive is open, while the constructor's cost is the central-directory
/// parse it performs to open it at all. That parse allocates per member
/// before the entry count is knowable, so it cannot be bounded from the
/// inside.
///
/// A CRUDE PROXY, and the honest description of it. File size is not a
/// measure of parse cost: media is bytes without members, a crafted archive
/// is members without bytes. Measured against this 8 MB cap:
///
/// | shape | size | members | parse | verdict |
/// |---|---|---|---|---|
/// | crafted, empty members | 8.39 MB | 97,799 | 384 ms | ADMITTED |
/// | document of 600 KB photos | 12.3 MB | 21 | 0.19 ms | REFUSED |
///
/// The most expensive thing it admits costs about 2000x the cheapest thing
/// it blocks. It orders the two cases backwards: uncorrelated with cost
/// benignly, anti-correlated adversarially.
///
/// The benign threshold is a RANGE, not a number. Holding this cap fixed
/// and varying only the media size inside moves it 27x -- 165 members of
/// 50 KB images, 26 of 330 KB, 6 of 2 MB photographs. So do not read a
/// member count out of these rows and tune against it: any such figure
/// describes the fixture it came from, not the format.
///
/// The residual is explicit: a crafted archive of ~98k empty members fits
/// under this cap and still costs ~400 ms on the frame loop. Bounding the
/// real cost needs a member-count limit, which needs a partial
/// central-directory reader.
///
/// It stays at `ZIP_LIST_CAP` rather than being raised toward
/// [`MAX_DOC_BYTES`], although the rows above show size buys little. That
/// sibling's own doc accepts ~800 ms as its bounded worst case at this
/// value, so the ~400 ms this admits is INSIDE the budget #504 chose
/// deliberately for the same constructor. Raising this one would take the
/// same parse several times past that bound on one of its two routes,
/// break the alias that keeps them honest, and trade a recoverable refusal
/// for a longer freeze -- the direction the asymmetry below argues against.
///
/// It ships anyway because file size is the only thing checkable BEFORE the
/// call whose cost we are bounding: `ZipArchive::len()` exists, but only on
/// a constructed archive, so asking how many members there are costs
/// exactly what we are trying to avoid. Bounding the count properly needs a
/// partial central-directory reader, which is a design change rather than a
/// constant.
///
/// This is a TRADE, not an improvement. A large real document that opened
/// under the old 50 MB gate will now be refused. The justification is the
/// asymmetry of the two failures: a refused document is recoverable -- it
/// opens in the hex viewer and says why -- and a frame loop frozen inside
/// the parse is not.
///
/// Aliased to `archive::ZIP_LIST_CAP` rather than repeating its value: it
/// is the same parse reached by a second route, the editor's docx/odt
/// branch, which `croft view` (#362) makes reachable from any pane. One
/// definition means the two cannot drift.
pub const MAX_DOC_LISTING_BYTES: u64 = crate::archive::ZIP_LIST_CAP;

/// Whether the preview declined this file only because of its SIZE (#506),
/// as opposed to not recognising it. The cascade needs the distinction:
/// `archive::kind_from_ext` claims `.zip`/`.jar`/`.whl` and NOT `.docx`
/// or `.odt`, so an over-cap document does not reach the archive route
/// and cannot pick up that route's cap refusal. Without this it falls
/// past every viewer to hex with nothing said, which is the silent dump
/// #504 added `route_note` to prevent.
pub fn is_past_listing_cap(path: &Path) -> bool {
    extension_is_doc(
        path.extension()
            .and_then(|e| e.to_str())
            .unwrap_or_default(),
    ) && std::fs::metadata(path).is_ok_and(|m| m.len() > MAX_DOC_LISTING_BYTES)
}

pub fn extension_is_doc(ext: &str) -> bool {
    matches!(ext.to_ascii_lowercase().as_str(), "docx" | "odt")
}

/// Escape markdown metacharacters in document text (#200 review): the
/// walker SYNTHESISES markdown, so literal #, -, *, _, |, etc. in the
/// document must not become structure.
pub(crate) fn md_escape(text: &str) -> String {
    let mut out = String::with_capacity(text.len());
    for c in text.chars() {
        if matches!(
            c,
            '\\' | '`' | '*' | '_' | '#' | '|' | '[' | ']' | '<' | '>' | '~'
        ) {
            out.push('\\');
        }
        out.push(c);
    }
    out
}

fn read_member(z: &mut zip::ZipArchive<std::fs::File>, name: &str) -> Option<String> {
    // Cap the DECOMPRESSED bytes (#200 review): the on-disk cap says
    // nothing about what a crafted member inflates to.
    let mut s = String::new();
    let member = z.by_name(name).ok()?;
    let mut limited = member.take(MAX_DOC_BYTES + 1);
    limited.read_to_string(&mut s).ok()?;
    (s.len() as u64 <= MAX_DOC_BYTES).then_some(s)
}

/// Extract an embedded image into `scratch` (hash-named) and return
/// its path.
fn extract_image(
    z: &mut zip::ZipArchive<std::fs::File>,
    member: &str,
    scratch: &Path,
) -> Option<PathBuf> {
    let mut bytes = Vec::new();
    let m = z.by_name(member).ok()?;
    let mut limited = m.take(MAX_DOC_BYTES + 1);
    limited.read_to_end(&mut bytes).ok()?;
    if bytes.len() as u64 > MAX_DOC_BYTES {
        return None;
    }
    image::load_from_memory(&bytes).ok()?;
    use std::hash::{Hash as _, Hasher as _};
    let mut h = std::collections::hash_map::DefaultHasher::new();
    bytes.hash(&mut h);
    let ext = Path::new(member)
        .extension()
        .and_then(|e| e.to_str())
        .unwrap_or("png");
    let path = scratch.join(format!("doc-{:016x}.{ext}", h.finish()));
    if !path.is_file() {
        std::fs::create_dir_all(scratch).ok()?;
        std::fs::write(&path, &bytes).ok()?;
    }
    Some(path)
}

/// Convert the document into markdown text plus the scratch dir images
/// resolve against. `None` when the file is not a recognisable
/// docx/odt.
pub fn to_markdown(path: &Path, scratch: &Path) -> Option<String> {
    let meta = std::fs::metadata(path).ok()?;
    // Before the open, not after: the constructor below is where the
    // per-member central-directory cost is paid (#506).
    if meta.len() > MAX_DOC_LISTING_BYTES {
        return None;
    }
    let f = std::fs::File::open(path).ok()?;
    let mut z = zip::ZipArchive::new(f).ok()?;
    if let Some(xml) = read_member(&mut z, "word/document.xml") {
        let rels = read_member(&mut z, "word/_rels/document.xml.rels").unwrap_or_default();
        return Some(docx_to_md(&xml, &rels, &mut z, scratch));
    }
    if let Some(xml) = read_member(&mut z, "content.xml") {
        return Some(odt_to_md(&xml, &mut z, scratch));
    }
    None
}

/// Map r:embed relationship ids to media member names.
fn rel_targets(rels: &str) -> std::collections::HashMap<String, String> {
    let mut out = std::collections::HashMap::new();
    let mut r = quick_xml::Reader::from_str(rels);
    let dec = r.decoder();
    loop {
        match r.read_event() {
            Ok(Event::Empty(e)) | Ok(Event::Start(e))
                if e.local_name().as_ref() == b"Relationship" =>
            {
                let mut id = None;
                let mut target = None;
                for a in e.attributes().flatten() {
                    match a.key.local_name().as_ref() {
                        b"Id" => {
                            id = a
                                .decode_and_unescape_value(dec)
                                .ok()
                                .map(|v| v.into_owned())
                        }
                        b"Target" => {
                            target = a
                                .decode_and_unescape_value(dec)
                                .ok()
                                .map(|v| v.into_owned())
                        }
                        _ => {}
                    }
                }
                if let (Some(id), Some(t)) = (id, target) {
                    out.insert(id, format!("word/{}", t.trim_start_matches("./")));
                }
            }
            Ok(Event::Eof) | Err(_) => break,
            _ => {}
        }
    }
    out
}

/// Most copies a `number-*-repeated` count may make. LibreOffice pads a row
/// to the sheet's full width (and a table to its full height) this way, so
/// the count can be a million; the trailing blanks are trimmed anyway.
const MAX_REPEAT: usize = 256;

/// The count in attribute `name` (a span or repeat), at least 1.
fn attr_count(
    e: &quick_xml::events::BytesStart,
    dec: quick_xml::encoding::Decoder,
    name: &[u8],
) -> usize {
    e.attributes()
        .flatten()
        .find(|a| a.key.local_name().as_ref() == name)
        .and_then(|a| a.decode_and_unescape_value(dec).ok()?.parse().ok())
        .unwrap_or(1)
        .clamp(1, MAX_REPEAT)
}

/// Repeat the last item until it appears `n` times in all.
fn repeat_last<T: Clone>(items: &mut Vec<T>, n: usize) {
    if let Some(last) = items.last().cloned() {
        items.extend(std::iter::repeat_n(last, n.saturating_sub(1)));
    }
}

/// Write `rows` as a pipe table, header first. Trailing columns and rows
/// with nothing in them are dropped, so padding never widens the table.
fn push_table(md: &mut String, mut rows: Vec<Vec<String>>) {
    let cols = rows
        .iter()
        .filter_map(|r| r.iter().rposition(|c| !c.is_empty()))
        .max()
        .map_or(0, |last| last + 1);
    while rows.last().is_some_and(|r| r.iter().all(String::is_empty)) {
        rows.pop();
    }
    if cols == 0 || rows.is_empty() {
        return;
    }
    for (i, row) in rows.iter().enumerate() {
        md.push('|');
        for c in 0..cols {
            md.push_str(row.get(c).map(String::as_str).unwrap_or(""));
            md.push('|');
        }
        md.push('\n');
        if i == 0 {
            md.push('|');
            for _ in 0..cols {
                md.push_str("---|");
            }
            md.push('\n');
        }
    }
    md.push('\n');
}

fn docx_to_md(
    xml: &str,
    rels: &str,
    z: &mut zip::ZipArchive<std::fs::File>,
    scratch: &Path,
) -> String {
    let targets = rel_targets(rels);
    let mut md = String::new();
    let mut r = quick_xml::Reader::from_str(xml);
    let dec = r.decoder();
    let mut para = String::new();
    let mut heading = 0usize;
    let mut listed = false;
    let mut bold = false;
    let mut italic = false;
    let mut in_rpr = false;
    let mut table: Option<Vec<Vec<String>>> = None;
    // How many grid columns the open cell covers (`<w:gridSpan>`, #1225).
    let mut span = 1usize;
    while let Ok(ev) = r.read_event() {
        match &ev {
            Event::Start(e) | Event::Empty(e) => match e.local_name().as_ref() {
                b"p" => {
                    para.clear();
                    heading = 0;
                    listed = false;
                }
                b"gridSpan" => span = attr_count(e, dec, b"val"),
                b"rPr" => in_rpr = true,
                b"b" if in_rpr => bold = true,
                b"i" if in_rpr => italic = true,
                b"pStyle" => {
                    for a in e.attributes().flatten() {
                        if a.key.local_name().as_ref() == b"val"
                            && let Ok(v) = a.decode_and_unescape_value(dec)
                            && let Some(n) = v.strip_prefix("Heading")
                        {
                            heading = n.parse().unwrap_or(0);
                        }
                    }
                }
                b"numPr" => listed = true,
                b"tbl" => table = Some(Vec::new()),
                b"tr" => {
                    if let Some(t) = table.as_mut() {
                        t.push(Vec::new());
                    }
                }
                b"tc" => {
                    span = 1;
                    if let Some(row) = table.as_mut().and_then(|t| t.last_mut()) {
                        row.push(String::new());
                    }
                }
                b"blip" => {
                    for a in e.attributes().flatten() {
                        if a.key.local_name().as_ref() == b"embed"
                            && let Ok(id) = a.decode_and_unescape_value(dec)
                            && let Some(member) = targets.get(id.as_ref())
                            && let Some(p) = extract_image(z, member, scratch)
                        {
                            para.push_str(&format!("![image]({})", p.display()));
                        }
                    }
                }
                _ => {}
            },
            Event::End(e) => match e.local_name().as_ref() {
                b"rPr" => in_rpr = false,
                b"r" => {
                    bold = false;
                    italic = false;
                }
                b"p" => {
                    let text = para.trim();
                    if let Some(row) = table
                        .as_mut()
                        .and_then(|t| t.last_mut())
                        .and_then(|r| r.last_mut())
                    {
                        if !text.is_empty() {
                            if !row.is_empty() {
                                row.push(' ');
                            }
                            row.push_str(text);
                        }
                    } else if !text.is_empty() {
                        if heading > 0 {
                            md.push_str(&"#".repeat(heading.min(6)));
                            md.push(' ');
                        } else if listed {
                            md.push_str("- ");
                        }
                        md.push_str(text);
                        md.push_str("\n\n");
                    }
                    para.clear();
                }
                // A merged cell holds every grid column it covers, so the
                // cells after it stay under their own headers.
                b"tc" => {
                    if let Some(row) = table.as_mut().and_then(|t| t.last_mut()) {
                        row.extend(std::iter::repeat_n(String::new(), span - 1));
                    }
                    span = 1;
                }
                b"tbl" => {
                    if let Some(rows) = table.take() {
                        push_table(&mut md, rows);
                    }
                }
                _ => {}
            },
            Event::Text(t) => {
                if let Ok(text) = t.xml_content() {
                    let text = md_escape(&text);
                    let styled = if bold && italic {
                        format!("***{text}***")
                    } else if bold {
                        format!("**{text}**")
                    } else if italic {
                        format!("*{text}*")
                    } else {
                        text
                    };
                    para.push_str(&styled);
                }
            }
            Event::Eof => break,
            _ => {}
        }
    }
    md
}

fn odt_to_md(xml: &str, z: &mut zip::ZipArchive<std::fs::File>, scratch: &Path) -> String {
    let mut md = String::new();
    let mut r = quick_xml::Reader::from_str(xml);
    let dec = r.decoder();
    let mut para = String::new();
    let mut heading = 0usize;
    let mut list_depth = 0usize;
    let mut table: Option<Vec<Vec<String>>> = None;
    // The open row's and cell's `number-*-repeated` counts.
    let mut row_repeat = 1usize;
    let mut cell_repeat = 1usize;
    while let Ok(ev) = r.read_event() {
        let empty = matches!(&ev, Event::Empty(_));
        match &ev {
            Event::Start(e) | Event::Empty(e) => match e.local_name().as_ref() {
                b"table" if !empty => table = Some(Vec::new()),
                b"table-row" => {
                    if let Some(t) = table.as_mut() {
                        t.push(Vec::new());
                        row_repeat = attr_count(e, dec, b"number-rows-repeated");
                        if empty {
                            repeat_last(t, row_repeat);
                        }
                    }
                }
                // A spanned cell is followed by a covered cell for each
                // column it covers, so a covered cell is an empty one and
                // the span itself needs no padding.
                b"table-cell" | b"covered-table-cell" => {
                    if let Some(row) = table.as_mut().and_then(|t| t.last_mut()) {
                        row.push(String::new());
                        cell_repeat = attr_count(e, dec, b"number-columns-repeated");
                        if empty {
                            repeat_last(row, cell_repeat);
                        }
                    }
                }
                b"h" => {
                    heading = 1;
                    for a in e.attributes().flatten() {
                        if a.key.local_name().as_ref() == b"outline-level"
                            && let Ok(v) = a.decode_and_unescape_value(dec)
                        {
                            heading = v.parse().unwrap_or(1);
                        }
                    }
                    para.clear();
                }
                b"p" => para.clear(),
                b"list" if matches!(&ev, Event::Start(_)) => list_depth += 1,
                b"image" => {
                    for a in e.attributes().flatten() {
                        if a.key.local_name().as_ref() == b"href"
                            && let Ok(v) = a.decode_and_unescape_value(dec)
                            && let Some(p) = extract_image(z, v.as_ref(), scratch)
                        {
                            para.push_str(&format!("![image]({})", p.display()));
                        }
                    }
                }
                _ => {}
            },
            Event::End(e) => match e.local_name().as_ref() {
                b"table" => {
                    if let Some(rows) = table.take() {
                        push_table(&mut md, rows);
                    }
                }
                b"table-row" => {
                    if let Some(t) = table.as_mut() {
                        repeat_last(t, row_repeat);
                    }
                    row_repeat = 1;
                }
                b"table-cell" | b"covered-table-cell" => {
                    if let Some(row) = table.as_mut().and_then(|t| t.last_mut()) {
                        repeat_last(row, cell_repeat);
                    }
                    cell_repeat = 1;
                }
                // Text inside a table cell joins that cell (#1225).
                b"h" | b"p"
                    if table
                        .as_ref()
                        .and_then(|t| t.last())
                        .is_some_and(|r| !r.is_empty()) =>
                {
                    let text = para.trim();
                    if let Some(cell) = table
                        .as_mut()
                        .and_then(|t| t.last_mut())
                        .and_then(|r| r.last_mut())
                        && !text.is_empty()
                    {
                        if !cell.is_empty() {
                            cell.push(' ');
                        }
                        cell.push_str(text);
                    }
                    heading = 0;
                    para.clear();
                }
                b"h" => {
                    if !para.trim().is_empty() {
                        md.push_str(&"#".repeat(heading.clamp(1, 6)));
                        md.push(' ');
                        md.push_str(para.trim());
                        md.push_str("\n\n");
                    }
                    heading = 0;
                    para.clear();
                }
                b"p" => {
                    if !para.trim().is_empty() {
                        if list_depth > 0 {
                            md.push_str("- ");
                        }
                        md.push_str(para.trim());
                        md.push_str("\n\n");
                    }
                    para.clear();
                }
                b"list" => list_depth = list_depth.saturating_sub(1),
                _ => {}
            },
            Event::Text(t) => {
                if let Ok(text) = t.xml_content() {
                    para.push_str(&md_escape(&text));
                }
            }
            Event::Eof => break,
            _ => {}
        }
    }
    md
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::io::Write as _;

    fn docx_fixture(p: &Path) {
        let f = std::fs::File::create(p).unwrap();
        let mut z = zip::ZipWriter::new(f);
        let o = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        z.start_file("word/document.xml", o).unwrap();
        z.write_all(
            br#"<?xml version="1.0"?>
<w:document xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
<w:body>
<w:p><w:pPr><w:pStyle w:val="Heading1"/></w:pPr><w:r><w:t>Title Here</w:t></w:r></w:p>
<w:p><w:r><w:t>plain </w:t></w:r><w:r><w:rPr><w:b/></w:rPr><w:t>bold</w:t></w:r></w:p>
<w:p><w:pPr><w:numPr/></w:pPr><w:r><w:t>item one</w:t></w:r></w:p>
<w:tbl><w:tr><w:tc><w:p><w:r><w:t>h1</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:t>h2</w:t></w:r></w:p></w:tc></w:tr>
<w:tr><w:tc><w:p><w:r><w:t>a</w:t></w:r></w:p></w:tc><w:tc><w:p><w:r><w:t>b</w:t></w:r></w:p></w:tc></w:tr></w:tbl>
</w:body></w:document>"#,
        )
        .unwrap();
        z.finish().unwrap();
    }

    #[test]
    fn docx_walks_headings_emphasis_lists_and_tables_into_markdown() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("d.docx");
        docx_fixture(&p);
        let md = to_markdown(&p, tmp.path()).expect("recognised");
        assert!(md.contains("# Title Here"), "{md}");
        assert!(md.contains("plain **bold**"), "{md}");
        assert!(md.contains("- item one"), "{md}");
        assert!(md.contains("|h1|h2|"), "{md}");
        assert!(md.contains("|a|b|"), "{md}");
    }

    /// A document whose FILE size clears the listing cap is refused before
    /// `ZipArchive::new` runs, so the crafted many-member layout cannot put
    /// its central directory through the constructor (#506). The fixture is
    /// a valid docx -- it holds a real `word/document.xml` -- so what the
    /// cap refuses is the size, not the shape.
    #[test]
    fn a_document_past_the_listing_cap_is_refused_before_the_parse() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("big.docx");
        docx_fixture(&p);
        // Pad past the cap without building a real many-member archive:
        // the gate reads the file's length, which is what a crafted
        // central directory inflates.
        let f = std::fs::OpenOptions::new().append(true).open(&p).unwrap();
        f.set_len(MAX_DOC_LISTING_BYTES + 1).unwrap();
        // Both bounds, so the refusal can only be the NEW cap. Past the
        // listing cap says the gate under test should fire; still under
        // `MAX_DOC_BYTES` says the old gate cannot be what fired instead.
        // Without the second, raising the listing cap above 50 MB would
        // leave this test green while the old bound did the work -- passing
        // for a reason it does not claim.
        let len = std::fs::metadata(&p).unwrap().len();
        assert!(len > MAX_DOC_LISTING_BYTES, "fixture clears the new cap");
        assert!(
            len <= MAX_DOC_BYTES,
            "and stays under the old one, so only the new cap can refuse it"
        );
        assert!(
            to_markdown(&p, tmp.path()).is_none(),
            "a file past the listing cap must not reach the parse"
        );
        // And the same fixture under the cap still opens, so the refusal
        // above is the size and not the padding.
        let q = tmp.path().join("small.docx");
        docx_fixture(&q);
        assert!(
            std::fs::metadata(&q).unwrap().len() <= MAX_DOC_LISTING_BYTES,
            "fixture premise: the unpadded document is under the cap"
        );
        assert!(to_markdown(&q, tmp.path()).is_some(), "under the cap opens");
    }

    /// An over-cap document does NOT reach the archive route, and that is
    /// why the size refusal has to be named here (#506).
    /// `archive::kind_from_ext` claims `.zip`/`.jar`/`.whl` and not
    /// `.docx`/`.odt`, so the cascade's archive branch is skipped and the
    /// file would otherwise fall to hex with no reason shown. Pinning the
    /// premise here means a later widening of `kind_from_ext` fails this
    /// test rather than silently making the message redundant.
    #[test]
    fn an_over_cap_document_needs_its_own_refusal_because_the_archive_route_skips_it() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("big.docx");
        docx_fixture(&p);
        let f = std::fs::OpenOptions::new().append(true).open(&p).unwrap();
        f.set_len(MAX_DOC_LISTING_BYTES + 1).unwrap();

        assert!(
            to_markdown(&p, tmp.path()).is_none(),
            "the preview declines"
        );
        assert!(
            crate::archive::kind_from_ext(&p).is_none(),
            "premise: the archive route does not claim a .docx, so it cannot \
             supply the refusal reason on this path"
        );
        // Control: the same call DOES claim a plain zip, so the assertion
        // above is about .docx and not a broken call.
        assert!(
            crate::archive::kind_from_ext(&tmp.path().join("x.zip")).is_some(),
            "control: kind_from_ext claims a .zip"
        );
        assert!(
            is_past_listing_cap(&p),
            "so the size refusal must be named by the doc path itself"
        );
    }

    /// The listing cap must stay the tighter of the two (#506). A
    /// `const` block rather than a runtime test: both are compile-time
    /// constants, so an assertion on them has a constant value, which
    /// clippy rejects and which would never have run as a check anyway --
    /// it fails the BUILD if the relationship is ever inverted, which is
    /// strictly earlier than a test could.
    ///
    /// The equality to `archive::ZIP_LIST_CAP` that stood here is gone
    /// deliberately: `MAX_DOC_LISTING_BYTES` is now defined AS that
    /// constant, so asserting they match compares a thing to itself and
    /// can never fail. The alias is what enforces it.
    const _: () = assert!(
        MAX_DOC_LISTING_BYTES < MAX_DOC_BYTES,
        "the pre-parse cap must be tighter than the decompressed-member cap"
    );

    #[test]
    fn odt_walks_headings_and_paragraphs() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("d.odt");
        let f = std::fs::File::create(&p).unwrap();
        let mut z = zip::ZipWriter::new(f);
        let o = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        z.start_file("content.xml", o).unwrap();
        z.write_all(
            br#"<?xml version="1.0"?>
<office:document-content xmlns:office="o" xmlns:text="t">
<office:body><office:text>
<text:h text:outline-level="2">Sub Head</text:h>
<text:p>body words</text:p>
<text:list><text:list-item><text:p>li</text:p></text:list-item></text:list>
</office:text></office:body></office:document-content>"#,
        )
        .unwrap();
        z.finish().unwrap();
        let md = to_markdown(&p, tmp.path()).expect("recognised");
        assert!(md.contains("## Sub Head"), "{md}");
        assert!(md.contains("body words"), "{md}");
        assert!(md.contains("- li"), "{md}");
    }

    fn odt_fixture(p: &Path, body: &str) {
        let f = std::fs::File::create(p).unwrap();
        let mut z = zip::ZipWriter::new(f);
        let o = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        z.start_file("content.xml", o).unwrap();
        let xml = format!(
            r#"<?xml version="1.0"?>
<office:document-content xmlns:office="o" xmlns:text="t" xmlns:table="b">
<office:body><office:text>{body}</office:text></office:body></office:document-content>"#
        );
        z.write_all(xml.as_bytes()).unwrap();
        z.finish().unwrap();
    }

    fn odt_row(cells: &[&str]) -> String {
        let cells: String = cells
            .iter()
            .map(|c| format!("<table:table-cell><text:p>{c}</text:p></table:table-cell>"))
            .collect();
        format!("<table:table-row>{cells}</table:table-row>")
    }

    /// #1225: an .odt table is a table, not one paragraph per cell, and the
    /// text around it stays paragraphs.
    #[test]
    fn odt_tables_render_as_tables() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("budget.odt");
        let rows = [
            odt_row(&["Region", "Q1", "Q2"]),
            odt_row(&["North", "10", "12"]),
            odt_row(&["South", "8", "9"]),
        ]
        .concat();
        odt_fixture(
            &p,
            &format!(
                "<text:p>before</text:p><table:table>{rows}</table:table><text:p>after</text:p>"
            ),
        );
        let md = to_markdown(&p, tmp.path()).expect("recognised");
        assert_eq!(
            md,
            "before\n\n|Region|Q1|Q2|\n|---|---|---|\n|North|10|12|\n|South|8|9|\n\nafter\n\n"
        );
    }

    /// #1225: a spanned .odt cell is followed by covered cells, which hold
    /// their columns, so the next cell stays under its own header.
    #[test]
    fn odt_spanned_cells_keep_the_row_under_its_columns() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("t.odt");
        let total = "<table:table-row>\
<table:table-cell table:number-columns-spanned=\"2\"><text:p>Total Q2</text:p></table:table-cell>\
<table:covered-table-cell/>\
<table:table-cell><text:p>21</text:p></table:table-cell></table:table-row>";
        let rows = [odt_row(&["Region", "Q1", "Q2"]), total.to_string()].concat();
        odt_fixture(&p, &format!("<table:table>{rows}</table:table>"));
        let md = to_markdown(&p, tmp.path()).expect("recognised");
        assert!(md.contains("|Total Q2||21|"), "{md}");
    }

    /// #1225: LibreOffice writes runs of equal cells once with a repeat
    /// count. A repeated cell fills its columns; the trailing run of empty
    /// cells a whole-width row ends in does not widen the table.
    #[test]
    fn odt_repeated_cells_fill_their_columns_without_widening_the_table() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("t.odt");
        let row = "<table:table-row>\
<table:table-cell table:number-columns-repeated=\"2\"><text:p>x</text:p></table:table-cell>\
<table:table-cell><text:p>y</text:p></table:table-cell>\
<table:table-cell table:number-columns-repeated=\"1021\"/></table:table-row>\
<table:table-row table:number-rows-repeated=\"1048572\">\
<table:table-cell table:number-columns-repeated=\"1024\"/></table:table-row>";
        odt_fixture(&p, &format!("<table:table>{row}</table:table>"));
        let md = to_markdown(&p, tmp.path()).expect("recognised");
        assert_eq!(md, "|x|x|y|\n|---|---|---|\n\n");
    }

    /// #1225: a .docx cell merged across two columns (`gridSpan`) holds both,
    /// so the cell after it stays under its own header.
    #[test]
    fn a_merged_docx_cell_keeps_the_rest_of_its_row_under_its_columns() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("budget.docx");
        let f = std::fs::File::create(&p).unwrap();
        let mut z = zip::ZipWriter::new(f);
        let o = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        z.start_file("word/document.xml", o).unwrap();
        let cell = |v: &str| format!("<w:tc><w:p><w:r><w:t>{v}</w:t></w:r></w:p></w:tc>");
        let row = |cells: &[&str]| {
            format!(
                "<w:tr>{}</w:tr>",
                cells.iter().map(|c| cell(c)).collect::<String>()
            )
        };
        let total = "<w:tr><w:tc><w:tcPr><w:gridSpan w:val=\"2\"/></w:tcPr>\
<w:p><w:r><w:t>Total Q2</w:t></w:r></w:p></w:tc>\
<w:tc><w:p><w:r><w:t>21</w:t></w:r></w:p></w:tc></w:tr>";
        let xml = format!(
            r#"<w:document xmlns:w="x"><w:body><w:tbl>{}{}{}</w:tbl></w:body></w:document>"#,
            row(&["Region", "Q1", "Q2"]),
            row(&["North", "10", "12"]),
            total
        );
        z.write_all(xml.as_bytes()).unwrap();
        z.finish().unwrap();
        let md = to_markdown(&p, tmp.path()).unwrap();
        assert!(md.contains("|Region|Q1|Q2|"), "{md}");
        assert!(md.contains("|Total Q2||21|"), "21 stays under Q2: {md}");
        // Negative: an unmerged row is untouched.
        assert!(md.contains("|North|10|12|"), "{md}");
    }

    #[test]
    fn literal_metacharacters_stay_text_not_structure() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("d.docx");
        let f = std::fs::File::create(&p).unwrap();
        let mut z = zip::ZipWriter::new(f);
        let o = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        z.start_file("word/document.xml", o).unwrap();
        z.write_all(
            br#"<w:document xmlns:w="x"><w:body>
<w:p><w:r><w:t># not a heading</w:t></w:r></w:p>
<w:tbl><w:tr><w:tc><w:p><w:r><w:t>a|b</w:t></w:r></w:p></w:tc></w:tr></w:tbl>
</w:body></w:document>"#,
        )
        .unwrap();
        z.finish().unwrap();
        let md = to_markdown(&p, tmp.path()).unwrap();
        assert!(md.contains("\\# not a heading"), "{md}");
        assert!(md.contains("a\\|b"), "pipes stay inside the cell: {md}");
    }

    #[test]
    fn a_plain_zip_is_not_a_document() {
        let tmp = tempfile::tempdir().unwrap();
        let p = tmp.path().join("x.zip");
        let f = std::fs::File::create(&p).unwrap();
        let mut z = zip::ZipWriter::new(f);
        let o = zip::write::SimpleFileOptions::default()
            .compression_method(zip::CompressionMethod::Stored);
        z.start_file("readme.txt", o).unwrap();
        z.write_all(b"x").unwrap();
        z.finish().unwrap();
        assert!(to_markdown(&p, tmp.path()).is_none());
    }
}