anydoc 0.2.0

Convert documents (doc, docx, odt, rtf, epub, pdf, presentations, spreadsheets, csv) to GitHub-Flavored Markdown
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
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
//! OOXML PresentationML (.pptx / .pptm / .ppsx): slides in `sldIdLst` order,
//! with the full text cascade - slide -> layout -> master placeholder /
//! `txStyles` -> presentation defaults. Speaker notes are included (fixed
//! policy), rendered as a quote after each slide's content.

mod cascade;

use crate::error::ConvertError;
use crate::model::{
    Block, Cell, Document, GridBuilder, ImageSource, Inline, LinkTarget, Style, TableKind,
    inlines_are_empty,
};
use crate::package::relationships::{
    RelTarget, Relationships, TargetMode, read_rels, rel_target_bytes, rel_type, rels_part_for,
};
use crate::package::xml::{Element, ns, parse_xml};
use crate::package::{Package, archive::probe_ole, path};
use crate::shared::assets::AssetSink;
use crate::shared::delta::rebase_emphasis;
use crate::shared::fields::classify_rel_target;
use crate::shared::header::resolve_header_rows;
use crate::shared::list::{ListEntry, ListKey, MarkerKind, flush_list};
use crate::shared::text::clean_text;
use cascade::{Bullet, LevelStyle, Placeholder, TextProps, TitleClass};
use std::cell::{Cell as StdCell, RefCell};
use std::collections::HashMap;

const LAYOUT_REL: &str =
    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideLayout";
const MASTER_REL: &str =
    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slideMaster";
const NOTES_REL: &str =
    "http://schemas.openxmlformats.org/officeDocument/2006/relationships/notesSlide";
const SLIDE_REL: &str = "http://schemas.openxmlformats.org/officeDocument/2006/relationships/slide";

/// Namespaces whose markup this frontend understands; `mc:Choice` branches
/// requiring anything else fall back to `mc:Fallback`.
const SUPPORTED_NS: &[&str] = &[ns::P, ns::A, ns::R, ns::MC];

struct LayoutInfo {
    placeholders: Vec<Placeholder>,
    master_path: Option<String>,
}

struct MasterInfo {
    title: LevelStyle,
    body: LevelStyle,
    other: LevelStyle,
    placeholders: Vec<Placeholder>,
}

pub fn parse(bytes: &[u8]) -> Result<Document, ConvertError> {
    let pkg = match Package::open(bytes) {
        Ok(p) => p,
        Err(e) => return Err(probe_ole(bytes).unwrap_or(e)),
    };
    let pkg = RefCell::new(pkg);

    // OPC part discovery: the presentation part comes from the package-level
    // officeDocument relationship, with the conventional path as fallback.
    let root_rels = read_rels(&mut pkg.borrow_mut(), "_rels/.rels")?;
    let pres_part = root_rels
        .first_of_type(rel_type::OFFICE_DOCUMENT)
        .and_then(|rel| path::resolve("", &rel.target).ok())
        .map(|t| t.path)
        .unwrap_or_else(|| "ppt/presentation.xml".to_string());
    let pres = pkg.borrow_mut().required_xml_part(&pres_part)?;
    let pres_rels = read_rels(&mut pkg.borrow_mut(), &rels_part_for(&pres_part))?;

    let default_text =
        cascade::parse_level_styles(pres.first_descendant(ns::P, "defaultTextStyle"));

    let slide_paths: Vec<String> = pres
        .first_descendant(ns::P, "sldIdLst")
        .map(|list| {
            list.find_all(ns::P, "sldId")
                .filter_map(|s| s.attr_qualified(ns::R, "id"))
                .filter_map(|rid| pres_rels.internal_target(rid))
                .filter_map(|target| path::resolve(&pres_part, target).ok().map(|t| t.path))
                .collect()
        })
        .unwrap_or_default();
    if slide_paths.is_empty() {
        return Err(ConvertError::malformed_part(
            pres_part.clone(),
            "presentation has no slide list",
        ));
    }

    let assets = RefCell::new(AssetSink::new());
    let mut layouts: HashMap<String, LayoutInfo> = HashMap::new();
    let mut masters: HashMap<String, MasterInfo> = HashMap::new();
    let mut blocks: Vec<Block> = Vec::new();
    let mut failed = 0usize;
    let instance_counter = StdCell::new(0u64);
    // Every slide has a start anchor id so internal slide-to-slide links
    // resolve after concatenation; the anchor node is emitted only on
    // slides some link actually targets.
    let slide_anchors: HashMap<String, String> = slide_paths
        .iter()
        .enumerate()
        .map(|(i, p)| (p.clone(), format!("slide-{}", i + 1)))
        .collect();
    let mut all_rels: Vec<Relationships> = Vec::with_capacity(slide_paths.len());
    for p in &slide_paths {
        all_rels.push(read_rels(&mut pkg.borrow_mut(), &rels_part_for(p))?);
    }
    let targeted: std::collections::HashSet<String> = slide_paths
        .iter()
        .zip(&all_rels)
        .flat_map(|(p, rels)| {
            rels.iter()
                .filter(|(_, r)| r.rel_type == SLIDE_REL && r.mode == TargetMode::Internal)
                .filter_map(move |(_, r)| path::resolve(p, &r.target).ok().map(|t| t.path))
        })
        .filter(|t| slide_anchors.contains_key(t))
        .collect();

    for (slide_index, slide_path) in slide_paths.iter().enumerate() {
        let tree = match pkg.borrow_mut().optional_xml_part(slide_path)? {
            Some(t) => t,
            None => {
                log::warn!("skipping unusable slide {slide_path}");
                failed += 1;
                continue;
            }
        };
        let Some(sp_tree) = tree
            .find(ns::P, "sld")
            .and_then(|s| s.find(ns::P, "cSld"))
            .and_then(|c| c.find(ns::P, "spTree"))
        else {
            log::warn!("skipping slide {slide_path}: no shape tree");
            failed += 1;
            continue;
        };
        let slide_rels = &all_rels[slide_index];

        let layout_path = rel_target_of_type(slide_rels, slide_path, LAYOUT_REL);
        if let Some(lp) = &layout_path
            && !layouts.contains_key(lp)
        {
            let info = load_layout(&pkg, lp)?;
            if let Some(mp) = info.master_path.clone()
                && !masters.contains_key(&mp)
            {
                let m = load_master(&pkg, &mp)?;
                masters.insert(mp, m);
            }
            layouts.insert(lp.clone(), info);
        }
        let layout = layout_path.as_ref().and_then(|lp| layouts.get(lp));
        let master = layout.and_then(|l| l.master_path.as_ref()).and_then(|mp| masters.get(mp));

        let ctx = SlideCtx {
            pkg: &pkg,
            rels: slide_rels,
            base_part: slide_path,
            assets: &assets,
            default_text: &default_text,
            layout,
            master,
            instance_counter: &instance_counter,
            slide_anchors: &slide_anchors,
        };
        if targeted.contains(slide_path)
            && let Some(anchor) = slide_anchors.get(slide_path)
        {
            blocks.push(Block::Paragraph(vec![Inline::Anchor(anchor.clone())]));
        }
        parse_shapes(sp_tree, &ctx, &mut blocks)?;

        // Speaker notes, set off as a quote (fixed policy: included). The
        // tree is loaded before the `if let` so the package borrow is not
        // held across the body.
        let notes = match rel_target_of_type(slide_rels, slide_path, NOTES_REL) {
            Some(notes_path) => {
                let tree = pkg.borrow_mut().optional_xml_part(&notes_path)?;
                tree.map(|t| (notes_path, t))
            }
            None => None,
        };
        if let Some((notes_path, notes_tree)) = notes {
            let notes_rels = read_rels(&mut pkg.borrow_mut(), &rels_part_for(&notes_path))?;
            let notes_ctx = SlideCtx {
                rels: &notes_rels,
                base_part: &notes_path,
                layout: None,
                master: None,
                ..ctx
            };
            let mut notes_blocks = Vec::new();
            for sp in notes_tree.descendants(ns::P, "sp") {
                // Keep note text bodies (real producers use a body
                // placeholder; LibreOffice writes plain text boxes) but skip
                // the slide-image and chrome placeholders.
                if matches!(placeholder_type(sp), Some("sldImg" | "sldNum" | "hdr" | "ftr" | "dt"))
                {
                    continue;
                }
                if let Some(tx) = sp.find(ns::P, "txBody") {
                    parse_text_body(tx, &notes_ctx, None, &mut notes_blocks)?;
                }
            }
            if !notes_blocks.is_empty() {
                blocks.push(Block::BlockQuote(notes_blocks));
            }
        }
    }
    if failed == slide_paths.len() {
        return Err(ConvertError::malformed("no slide in the presentation could be read"));
    }

    let assets = std::mem::take(&mut assets.borrow_mut().assets);
    Ok(Document { blocks, notes: Vec::new(), assets })
}

fn rel_target_of_type(rels: &Relationships, base: &str, rel_type: &str) -> Option<String> {
    // first_of_type picks the lowest id, so duplicate relationships resolve
    // deterministically.
    rels.first_of_type(rel_type)
        .and_then(|rel| path::resolve(base, &rel.target).ok())
        .map(|t| t.path)
}

fn load_layout(pkg: &RefCell<Package>, layout_path: &str) -> Result<LayoutInfo, ConvertError> {
    let placeholders = match pkg.borrow_mut().optional_xml_part(layout_path)? {
        Some(tree) => tree
            .first_descendant(ns::P, "spTree")
            .map(cascade::collect_placeholders)
            .unwrap_or_default(),
        None => Vec::new(),
    };
    let rels = read_rels(&mut pkg.borrow_mut(), &rels_part_for(layout_path))?;
    let master_path = rel_target_of_type(&rels, layout_path, MASTER_REL);
    Ok(LayoutInfo { placeholders, master_path })
}

fn load_master(pkg: &RefCell<Package>, master_path: &str) -> Result<MasterInfo, ConvertError> {
    let mut info = MasterInfo {
        title: LevelStyle::default(),
        body: LevelStyle::default(),
        other: LevelStyle::default(),
        placeholders: Vec::new(),
    };
    if let Some(tree) = pkg.borrow_mut().optional_xml_part(master_path)? {
        if let Some(tx_styles) = tree.first_descendant(ns::P, "txStyles") {
            info.title = cascade::parse_level_styles(tx_styles.find(ns::P, "titleStyle"));
            info.body = cascade::parse_level_styles(tx_styles.find(ns::P, "bodyStyle"));
            info.other = cascade::parse_level_styles(tx_styles.find(ns::P, "otherStyle"));
        }
        if let Some(sp_tree) = tree.first_descendant(ns::P, "spTree") {
            info.placeholders = cascade::collect_placeholders(sp_tree);
        }
    }
    Ok(info)
}

struct SlideCtx<'a, 'b> {
    pkg: &'b RefCell<Package<'a>>,
    rels: &'b Relationships,
    base_part: &'b str,
    assets: &'b RefCell<AssetSink>,
    default_text: &'b LevelStyle,
    layout: Option<&'b LayoutInfo>,
    master: Option<&'b MasterInfo>,
    /// Per-text-body list instance ids, unique document-wide.
    instance_counter: &'b StdCell<u64>,
    /// Slide part path -> the slide's start anchor id, for internal
    /// slide-to-slide links.
    slide_anchors: &'b HashMap<String, String>,
}

impl SlideCtx<'_, '_> {
    /// Slide-to-slide relationships become anchor links to the target
    /// slide's start anchor; everything else classifies as usual.
    fn link_target(&self, rel: &crate::package::relationships::Relationship) -> LinkTarget {
        if rel.mode == TargetMode::Internal
            && let Ok(t) = path::resolve(self.base_part, &rel.target)
            && let Some(anchor) = self.slide_anchors.get(&t.path)
        {
            return LinkTarget::Anchor(anchor.clone());
        }
        classify_rel_target(rel.mode == TargetMode::External, &rel.target)
    }

    /// Fold the cascade for a paragraph, outermost first.
    fn base_props(&self, ph: Option<&PhInfo>, shape_styles: &LevelStyle, lvl: usize) -> TextProps {
        let mut props = self.default_text.level(lvl);
        if let Some(master) = self.master {
            let class_style = match ph.map(|p| cascade::title_class(&p.ph_type)) {
                Some(TitleClass::Title) => &master.title,
                Some(TitleClass::Body) => &master.body,
                None => &master.other,
            };
            props = props.merge(class_style.level(lvl));
            if let Some(ph) = ph
                && let Some(hit) =
                    cascade::match_placeholder(&master.placeholders, &ph.ph_type, ph.idx.as_deref())
            {
                props = props.merge(hit.styles.level(lvl));
            }
        }
        if let Some(layout) = self.layout
            && let Some(ph) = ph
            && let Some(hit) =
                cascade::match_placeholder(&layout.placeholders, &ph.ph_type, ph.idx.as_deref())
        {
            props = props.merge(hit.styles.level(lvl));
        }
        props.merge(shape_styles.level(lvl))
    }

    /// Load an internal relationship target's bytes, resolved against this
    /// part. Failures degrade (log + `None`) per the unified policy;
    /// resource-limit errors always propagate.
    fn rel_part(&self, rel_id: &str) -> Result<Option<RelTarget>, ConvertError> {
        rel_target_bytes(self.pkg, self.rels, self.base_part, rel_id)
    }
}

struct PhInfo {
    ph_type: String,
    idx: Option<String>,
}

fn placeholder_type(sp: &Element) -> Option<&str> {
    sp.first_descendant(ns::P, "ph").map(|ph| ph.attr(ns::P, "type").unwrap_or("body"))
}

fn parse_shapes(
    parent: &Element,
    ctx: &SlideCtx,
    blocks: &mut Vec<Block>,
) -> Result<(), ConvertError> {
    for child in parent.child_elems() {
        if child.is(ns::MC, "AlternateContent") {
            if let Some(branch) = crate::shared::mc::alternate_branch(child, SUPPORTED_NS) {
                parse_shapes(branch, ctx, blocks)?;
            }
            continue;
        }
        if child.ns.as_deref().is_none_or(|n| n != ns::P) {
            continue;
        }
        match child.local.as_str() {
            // Connector shapes carry the same `p:txBody` as plain shapes.
            "sp" | "cxnSp" => parse_shape(child, ctx, blocks)?,
            "grpSp" => parse_shapes(child, ctx, blocks)?,
            "graphicFrame" => parse_graphic_frame(child, ctx, blocks)?,
            "pic" => {
                let descr = child
                    .first_descendant(ns::P, "cNvPr")
                    .and_then(|p| p.attr(ns::P, "descr"))
                    .map(clean_text)
                    .unwrap_or_default();
                let rid = child.first_descendant(ns::A, "blip").and_then(|b| {
                    b.attr_qualified(ns::R, "embed").or_else(|| b.attr_qualified(ns::R, "link"))
                });
                // External-mode targets (`r:link`) become external image
                // sources; embedded targets are retained as assets.
                let source = match rid {
                    Some(rid) => crate::shared::assets::rel_image_source(
                        ctx.pkg,
                        ctx.rels,
                        ctx.base_part,
                        ctx.assets,
                        rid,
                    )?,
                    None => None,
                };
                if source.is_some() || !descr.trim().is_empty() {
                    blocks.push(Block::Paragraph(vec![Inline::Image {
                        alt: descr,
                        source: source.unwrap_or(ImageSource::Unavailable),
                    }]));
                }
            }
            _ => {}
        }
    }
    Ok(())
}

fn parse_shape(sp: &Element, ctx: &SlideCtx, blocks: &mut Vec<Block>) -> Result<(), ConvertError> {
    let ph = sp.first_descendant(ns::P, "ph").map(|p| PhInfo {
        ph_type: p.attr(ns::P, "type").unwrap_or("body").to_string(),
        idx: p.attr(ns::P, "idx").map(str::to_string),
    });
    if let Some(ph) = &ph
        && matches!(ph.ph_type.as_str(), "sldNum" | "dt" | "ftr")
    {
        return Ok(());
    }
    let Some(tx) = sp.find(ns::P, "txBody") else {
        return Ok(());
    };
    // Titles get heading semantics but keep their shape-order position.
    if ph.as_ref().is_some_and(|p| cascade::title_class(&p.ph_type) == TitleClass::Title) {
        push_title_heading(tx, ctx, ph.as_ref(), blocks)?;
    } else {
        parse_text_body(tx, ctx, ph.as_ref(), blocks)?;
    }
    Ok(())
}

/// Collapse a title placeholder's paragraphs into one slide heading. Runs
/// resolve through the full cascade like body text.
fn push_title_heading(
    tx: &Element,
    ctx: &SlideCtx,
    ph: Option<&PhInfo>,
    blocks: &mut Vec<Block>,
) -> Result<(), ConvertError> {
    let shape_styles = cascade::parse_level_styles(tx.find(ns::A, "lstStyle"));
    let mut inlines: Vec<Inline> = Vec::new();
    for p in tx.find_all(ns::A, "p") {
        let ppr = p.find(ns::A, "pPr");
        let lvl: usize =
            ppr.and_then(|pr| pr.attr(ns::A, "lvl")).and_then(|v| v.parse().ok()).unwrap_or(0);
        let mut props = ctx.base_props(ph, &shape_styles, lvl);
        if let Some(ppr) = ppr {
            props = props.merge(cascade::paragraph_props(ppr));
        }
        let base = props.delta.resolve();
        let mut para = parse_para_inlines(p, ctx, base);
        if inlines_are_empty(&para) {
            continue;
        }
        rebase_emphasis(&mut para, base);
        if !inlines.is_empty() {
            inlines.push(Inline::LineBreak);
        }
        inlines.extend(para);
    }
    if !inlines_are_empty(&inlines) {
        let anchor = Some(crate::model::inlines_to_plain_text(&inlines));
        blocks.push(Block::Heading { level: 2, anchor, content: inlines });
    }
    Ok(())
}

fn parse_text_body(
    tx: &Element,
    ctx: &SlideCtx,
    ph: Option<&PhInfo>,
    blocks: &mut Vec<Block>,
) -> Result<(), ConvertError> {
    let shape_styles = cascade::parse_level_styles(tx.find(ns::A, "lstStyle"));
    let instance = {
        let id = ctx.instance_counter.get() + 1;
        ctx.instance_counter.set(id);
        id
    };
    let mut counters = [0u64; cascade::LEVELS];
    let mut started = [false; cascade::LEVELS];
    let mut list_run: Vec<ListEntry> = Vec::new();

    for p in tx.find_all(ns::A, "p") {
        let ppr = p.find(ns::A, "pPr");
        let lvl: usize =
            ppr.and_then(|pr| pr.attr(ns::A, "lvl")).and_then(|v| v.parse().ok()).unwrap_or(0);
        let mut props = ctx.base_props(ph, &shape_styles, lvl);
        if let Some(ppr) = ppr {
            props = props.merge(cascade::paragraph_props(ppr));
        }
        let base = props.delta.resolve();
        let inlines = parse_para_inlines(p, ctx, base);
        if inlines_are_empty(&inlines) {
            flush_list(blocks, &mut list_run);
            continue;
        }
        match props.bullet {
            Bullet::AutoNum { marker, start, wrap } => {
                let lvl_idx = lvl.min(cascade::LEVELS - 1);
                let number = if started[lvl_idx] {
                    counters[lvl_idx].saturating_add(1)
                } else {
                    started[lvl_idx] = true;
                    start
                };
                counters[lvl_idx] = number;
                for deeper in started.iter_mut().skip(lvl_idx + 1) {
                    *deeper = false;
                }
                list_run.push(ListEntry {
                    level: lvl,
                    key: ListKey { instance, marker },
                    number,
                    label: wrap.label(marker, number),
                    blocks: vec![Block::Paragraph(inlines)],
                });
            }
            Bullet::Char => list_run.push(ListEntry {
                level: lvl,
                key: ListKey { instance, marker: MarkerKind::Bullet },
                number: 0,
                label: None,
                blocks: vec![Block::Paragraph(inlines)],
            }),
            Bullet::None | Bullet::Inherit => {
                flush_list(blocks, &mut list_run);
                blocks.push(Block::Paragraph(inlines));
            }
        }
    }
    flush_list(blocks, &mut list_run);
    Ok(())
}

fn parse_para_inlines(p: &Element, ctx: &SlideCtx, base: Style) -> Vec<Inline> {
    let mut out: Vec<Inline> = Vec::new();
    for child in p.child_elems() {
        if child.ns.as_deref().is_none_or(|n| n != ns::A) {
            continue;
        }
        match child.local.as_str() {
            "r" | "fld" => {
                let rpr = child.find(ns::A, "rPr");
                let text =
                    clean_text(&child.find(ns::A, "t").map(|t| t.text()).unwrap_or_default());
                if text.is_empty() {
                    continue;
                }
                let style = match rpr {
                    Some(rpr) => cascade::rpr_delta(rpr).apply(base),
                    None => base,
                };
                let inline = Inline::Text { text, style };
                let target = rpr
                    .and_then(|r| r.find(ns::A, "hlinkClick"))
                    .and_then(|h| h.attr_qualified(ns::R, "id"))
                    .and_then(|rid| ctx.rels.get(rid))
                    .map(|rel| ctx.link_target(rel));
                match target {
                    Some(target) => out.push(Inline::Link { content: vec![inline], target }),
                    None => out.push(inline),
                }
            }
            "br" => out.push(Inline::LineBreak),
            _ => {}
        }
    }
    out
}

fn parse_graphic_frame(
    frame: &Element,
    ctx: &SlideCtx,
    blocks: &mut Vec<Block>,
) -> Result<(), ConvertError> {
    if let Some(tbl) = frame.first_descendant(ns::A, "tbl") {
        parse_table(tbl, ctx, blocks)?;
        return Ok(());
    }
    // Embedded OLE objects: retain identity, media type, and payload.
    if let Some(ole) = frame.first_descendant(ns::P, "oleObj") {
        let prog_id = ole.attr(ns::P, "progId").unwrap_or("object");
        let name = ole.attr(ns::P, "name").unwrap_or("").trim();
        let alt =
            if name.is_empty() { format!("Embedded object: {prog_id}") } else { name.to_string() };
        let source = match ole.attr_qualified(ns::R, "id") {
            Some(rid) => match ctx.rel_part(rid)? {
                Some((part, bytes)) => Some(ImageSource::Asset(ctx.assets.borrow_mut().add(
                    "application/vnd.ms-ole-object".into(),
                    part,
                    &bytes,
                )?)),
                None => None,
            },
            None => None,
        };
        blocks.push(Block::Paragraph(vec![Inline::Image {
            alt,
            source: source.unwrap_or(ImageSource::Unavailable),
        }]));
        return Ok(());
    }
    if let Some(chart_ref) = frame.first_descendant(ns::CHART, "chart")
        && let Some(rid) = chart_ref.attr_qualified(ns::R, "id")
        && let Some((part, bytes)) = ctx.rel_part(rid)?
    {
        match parse_xml(&bytes) {
            Ok(root) => blocks.extend(crate::shared::drawingml::chart_blocks(&root)),
            Err(e) if e.is_fatal() => return Err(e),
            Err(e) => log::warn!("skipping corrupt chart part {part}: {e}"),
        }
        return Ok(());
    }
    if let Some(rel_ids) = frame.first_descendant(ns::DGM, "relIds")
        && let Some(rid) = rel_ids.attr_qualified(ns::R, "dm")
        && let Some((part, bytes)) = ctx.rel_part(rid)?
    {
        match parse_xml(&bytes) {
            Ok(root) => blocks.extend(crate::shared::drawingml::diagram_blocks(&root)),
            Err(e) if e.is_fatal() => return Err(e),
            Err(e) => log::warn!("skipping corrupt diagram part {part}: {e}"),
        }
    }
    Ok(())
}

/// DrawingML slide table: origins carry `gridSpan`/`rowSpan`; merged
/// continuation cells (`hMerge`/`vMerge`) consume covered positions.
fn parse_table(tbl: &Element, ctx: &SlideCtx, blocks: &mut Vec<Block>) -> Result<(), ConvertError> {
    let header_rows = usize::from(
        tbl.find(ns::A, "tblPr")
            .is_some_and(|p| matches!(p.attr(ns::A, "firstRow"), Some("1") | Some("true"))),
    );
    let mut builder = GridBuilder::new();
    for tr in tbl.find_all(ns::A, "tr") {
        builder.next_row();
        for tc in tr.find_all(ns::A, "tc") {
            let merged = matches!(tc.attr(ns::A, "hMerge"), Some("1") | Some("true"))
                || matches!(tc.attr(ns::A, "vMerge"), Some("1") | Some("true"));
            if merged {
                builder.covered();
                continue;
            }
            let col_span: u32 =
                tc.attr(ns::A, "gridSpan").and_then(|v| v.parse().ok()).unwrap_or(1).max(1);
            let row_span: u32 =
                tc.attr(ns::A, "rowSpan").and_then(|v| v.parse().ok()).unwrap_or(1).max(1);
            let mut cell_blocks = Vec::new();
            if let Some(tx) = tc.find(ns::A, "txBody") {
                parse_text_body(tx, ctx, None, &mut cell_blocks)?;
            }
            builder.place(Cell::spanning(cell_blocks, col_span, row_span))?;
        }
    }
    let mut table = builder.finish(TableKind::Data);
    if table.grid.is_empty() {
        return Ok(());
    }
    table.header_rows = resolve_header_rows(&table, header_rows);
    blocks.push(Block::Table(table));
    Ok(())
}