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
//! Slide XML -> markdown block-stream parser.
use crate::entities::read_event_folding_entities;
use quick_xml::events::Event as XmlEvent;
use quick_xml::Reader as XmlReader;
use super::md_blocks::{
attr_local_name, collapse_ws, decode_attr, push_text, BlockKind, SlideBlock,
SlideMarkdownContent,
};
use std::collections::HashMap;
use std::io::Cursor;
pub(super) fn parse_slide_for_markdown(
xml_bytes: &[u8],
rels: &HashMap<String, String>,
) -> Result<SlideMarkdownContent, String> {
let mut reader = XmlReader::from_reader(Cursor::new(xml_bytes));
let mut buf = Vec::new();
// Shape tracking
let mut sp_depth: i32 = 0;
let mut sp_is_title = false;
let mut sp_ph_checked = false;
let mut in_pic = false;
// Slide-background fill: <p:bg><p:bgPr><a:blipFill><a:blip r:embed>. It is
// never inside a <p:pic>, so the `in_pic` gate below skipped it and a
// background-only slide rendered with no image at all — while the chunk
// path now extracts it, leaving the two disagreeing (TECH_DEBT #17).
let mut in_bg = false;
let mut bg_rid: Option<String> = None;
let mut pic_alt: Option<String> = None;
let mut pic_rid: Option<String> = None;
// Text body / paragraph tracking (inside sp)
let mut in_txbody = false;
let mut in_para = false;
let mut para_level: u8 = 0;
let mut para_has_bullet = false;
let mut para_is_numbered = false;
let mut para_explicit_bu_none = false;
let mut para_in_ppr = false;
let mut para_text = String::new();
// Run tracking (inside a:r)
let mut in_run = false;
let mut cur_bold = false;
let mut cur_italic = false;
let mut cur_run_text = String::new();
let mut cur_hlink_rid = String::new(); // r:id from <a:hlinkClick> in current rPr
// <a:t> tracking
let mut in_t = false;
// Table tracking
let mut in_tbl = false;
let mut tbl_has_header = false;
let mut in_tbl_ppr = false;
let mut tbl_rows: Vec<Vec<String>> = Vec::new();
let mut tbl_current_row: Vec<String> = Vec::new();
let mut in_tc = false;
let mut tc_text = String::new();
let mut in_tc_body = false;
let mut in_tc_para = false;
let mut tc_para_text = String::new();
let mut in_tc_t = false;
// Shape paragraphs accumulation
let mut shape_paragraphs: Vec<SlideBlock> = Vec::new();
let mut slide = SlideMarkdownContent::default();
loop {
// Entity references arrive as their own event; fold them back into text.
let mut spill = String::new();
let mut is_entity = false;
match read_event_folding_entities!(reader, &mut buf, &mut spill, &mut is_entity) {
Ok(XmlEvent::Start(ref e)) => {
let ename = e.name();
let ebytes = ename.as_ref();
let local: &[u8] = ebytes.rsplit(|b| *b == b':').next().unwrap_or(ebytes);
match local {
// Group shapes are traversed transparently; the tag itself
// needs no state — shapes inside are handled as normal.
b"grpSp" => {}
b"sp" => {
sp_depth += 1;
if sp_depth == 1 {
sp_is_title = false;
sp_ph_checked = false;
shape_paragraphs.clear();
}
}
b"ph" if sp_depth > 0 && !sp_ph_checked => {
sp_ph_checked = true;
let mut ph_type: Option<String> = None;
let mut ph_idx: Option<String> = None;
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
let val = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.to_string();
match key_local {
b"type" => ph_type = Some(val),
b"idx" => ph_idx = Some(val),
_ => {}
}
}
if let Some(t) = ph_type {
let t = t.to_ascii_lowercase();
sp_is_title = matches!(t.as_str(), "title" | "ctrtitle" | "subtitle");
} else if ph_idx.as_deref() == Some("0") {
sp_is_title = true;
}
}
b"txBody" if in_tc => {
in_tc_body = true;
}
b"txBody" if sp_depth > 0 && !in_tbl => {
in_txbody = true;
}
b"p" if in_txbody => {
in_para = true;
para_text.clear();
para_level = 0;
para_has_bullet = false;
para_is_numbered = false;
para_explicit_bu_none = false;
para_in_ppr = false;
cur_hlink_rid.clear();
}
b"pPr" if in_para => {
para_in_ppr = true;
for attr in e.attributes().flatten() {
if attr_local_name(attr.key.as_ref()) == b"lvl" {
let v = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.parse::<u8>()
.unwrap_or(0);
para_level = v;
break;
}
}
}
b"buChar" if para_in_ppr => {
para_has_bullet = true;
para_is_numbered = false;
}
b"buAutoNum" if para_in_ppr => {
para_has_bullet = true;
para_is_numbered = true;
}
b"buNone" if para_in_ppr => {
para_has_bullet = false;
para_is_numbered = false;
para_explicit_bu_none = true;
}
b"r" if in_para => {
in_run = true;
cur_bold = false;
cur_italic = false;
cur_run_text.clear();
}
b"rPr" if in_run => {
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
let val =
String::from_utf8_lossy(attr.value.as_ref()).to_ascii_lowercase();
match key_local {
b"b" => cur_bold = val == "1" || val == "true",
b"i" => cur_italic = val == "1" || val == "true",
_ => {}
}
}
}
b"hlinkClick" if in_run => {
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
if key_local == b"id" {
cur_hlink_rid = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.to_string();
break;
}
}
}
b"t" if in_para && !in_tc_para => {
in_t = true;
}
b"pic" => {
in_pic = true;
pic_alt = None;
pic_rid = None;
}
b"bg" => {
in_bg = true;
bg_rid = None;
}
b"cNvPr" if in_pic => {
let mut descr: Option<String> = None;
let mut name: Option<String> = None;
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
// descr/name are human-authored, so they carry real
// entities — a raw byte read leaks "
" into the
// alt text. Decode, then flatten the newlines that
// decoding reveals: this renders inline.
let val = collapse_ws(&decode_attr(&attr));
if val.is_empty() {
continue;
}
match key_local {
b"descr" => descr = Some(val),
b"name" => name = Some(val),
_ => {}
}
}
if let Some(d) = descr {
pic_alt = Some(d);
} else if let Some(n) = name {
let lower = n.to_ascii_lowercase();
let generic = lower.starts_with("picture ")
|| lower.starts_with("image ")
|| lower.starts_with("graphic ")
|| lower.starts_with("chart ");
if !generic {
pic_alt = Some(n);
}
}
}
b"blip" if in_pic => {
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
if key_local == b"embed" {
let rid = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.to_string();
if !rid.is_empty() {
pic_rid = Some(rid);
}
break;
}
}
}
b"blip" if in_bg => {
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
if key_local == b"embed" {
let rid = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.to_string();
if !rid.is_empty() {
bg_rid = Some(rid);
}
break;
}
}
}
b"tbl" => {
in_tbl = true;
tbl_has_header = false;
tbl_rows.clear();
}
b"tblPr" if in_tbl => {
in_tbl_ppr = true;
for attr in e.attributes().flatten() {
if attr_local_name(attr.key.as_ref()) == b"firstRow" {
let v = String::from_utf8_lossy(attr.value.as_ref());
tbl_has_header = v.trim() == "1";
break;
}
}
}
b"tr" if in_tbl => {
tbl_current_row.clear();
}
b"tc" if in_tbl => {
in_tc = true;
tc_text.clear();
}
b"p" if in_tc_body => {
in_tc_para = true;
tc_para_text.clear();
}
b"t" if in_tc_para => {
in_tc_t = true;
}
_ => {}
}
}
Ok(XmlEvent::Empty(ref e)) => {
let ename = e.name();
let ebytes = ename.as_ref();
let local: &[u8] = ebytes.rsplit(|b| *b == b':').next().unwrap_or(ebytes);
match local {
// SmartArt: the slide only points at the part holding the text.
// `<c:chart r:id=…/>` — the pointer to the chart part.
b"chart" => {
for attr in e.attributes().flatten() {
if attr_local_name(attr.key.as_ref()) == b"id" {
let v = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.to_string();
if !v.is_empty() {
slide.chart_rids.push(v);
}
break;
}
}
}
b"relIds" => {
for attr in e.attributes().flatten() {
if attr_local_name(attr.key.as_ref()) == b"dm" {
let v = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.to_string();
if !v.is_empty() {
slide.diagram_rids.push(v);
}
break;
}
}
}
b"pPr" if in_para => {
for attr in e.attributes().flatten() {
if attr_local_name(attr.key.as_ref()) == b"lvl" {
let v = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.parse::<u8>()
.unwrap_or(0);
para_level = v;
break;
}
}
}
b"hlinkClick" if in_run => {
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
if key_local == b"id" {
cur_hlink_rid = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.to_string();
break;
}
}
}
b"ph" if sp_depth > 0 && !sp_ph_checked => {
sp_ph_checked = true;
let mut ph_type: Option<String> = None;
let mut ph_idx: Option<String> = None;
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
let val = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.to_string();
match key_local {
b"type" => ph_type = Some(val),
b"idx" => ph_idx = Some(val),
_ => {}
}
}
if let Some(t) = ph_type {
let t = t.to_ascii_lowercase();
sp_is_title = matches!(t.as_str(), "title" | "ctrtitle" | "subtitle");
} else if ph_idx.as_deref() == Some("0") {
sp_is_title = true;
}
}
b"buChar" if para_in_ppr => {
para_has_bullet = true;
para_is_numbered = false;
}
b"buAutoNum" if para_in_ppr => {
para_has_bullet = true;
para_is_numbered = true;
}
b"buNone" if para_in_ppr => {
para_has_bullet = false;
para_is_numbered = false;
para_explicit_bu_none = true;
}
b"rPr" if in_run => {
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
let val =
String::from_utf8_lossy(attr.value.as_ref()).to_ascii_lowercase();
match key_local {
b"b" => cur_bold = val == "1" || val == "true",
b"i" => cur_italic = val == "1" || val == "true",
_ => {}
}
}
}
b"pic" => {
in_pic = false;
slide.blocks.push(SlideBlock::image(None, None));
}
b"cNvPr" if in_pic => {
let mut descr: Option<String> = None;
let mut name: Option<String> = None;
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
// descr/name are human-authored, so they carry real
// entities — a raw byte read leaks "
" into the
// alt text. Decode, then flatten the newlines that
// decoding reveals: this renders inline.
let val = collapse_ws(&decode_attr(&attr));
if val.is_empty() {
continue;
}
match key_local {
b"descr" => descr = Some(val),
b"name" => name = Some(val),
_ => {}
}
}
if let Some(d) = descr {
pic_alt = Some(d);
} else if let Some(n) = name {
let lower = n.to_ascii_lowercase();
let generic = lower.starts_with("picture ")
|| lower.starts_with("image ")
|| lower.starts_with("graphic ")
|| lower.starts_with("chart ");
if !generic {
pic_alt = Some(n);
}
}
}
b"blip" if in_pic => {
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
if key_local == b"embed" {
let rid = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.to_string();
if !rid.is_empty() {
pic_rid = Some(rid);
}
break;
}
}
}
b"blip" if in_bg => {
for attr in e.attributes().flatten() {
let key_local = attr_local_name(attr.key.as_ref());
if key_local == b"embed" {
let rid = String::from_utf8_lossy(attr.value.as_ref())
.trim()
.to_string();
if !rid.is_empty() {
bg_rid = Some(rid);
}
break;
}
}
}
b"tblPr" if in_tbl => {
for attr in e.attributes().flatten() {
if attr_local_name(attr.key.as_ref()) == b"firstRow" {
let v = String::from_utf8_lossy(attr.value.as_ref());
tbl_has_header = v.trim() == "1";
break;
}
}
}
_ => {}
}
}
Ok(XmlEvent::Text(ref e)) => {
if in_t && in_para && !in_tc_para {
let txt = e.decode().unwrap_or_default().to_string();
if in_run {
push_text(&mut cur_run_text, &txt);
} else {
push_text(&mut para_text, &txt);
}
}
if in_tc_t {
let txt = e.decode().unwrap_or_default().to_string();
push_text(&mut tc_para_text, &txt);
}
}
Ok(XmlEvent::CData(ref e)) => {
if in_t && in_para && !in_tc_para {
let txt = String::from_utf8_lossy(e.as_ref()).to_string();
if in_run {
push_text(&mut cur_run_text, &txt);
} else {
push_text(&mut para_text, &txt);
}
}
if in_tc_t {
let txt = String::from_utf8_lossy(e.as_ref()).to_string();
push_text(&mut tc_para_text, &txt);
}
}
Ok(XmlEvent::End(ref e)) => {
let ename = e.name();
let ebytes = ename.as_ref();
let local: &[u8] = ebytes.rsplit(|b| *b == b':').next().unwrap_or(ebytes);
match local {
b"grpSp" => {}
b"r" if in_para => {
if !cur_run_text.is_empty() {
let formatted = match (cur_bold, cur_italic) {
(true, true) => format!("***{}***", cur_run_text),
(true, false) => format!("**{}**", cur_run_text),
(false, true) => format!("*{}*", cur_run_text),
_ => cur_run_text.clone(),
};
let final_text = if !cur_hlink_rid.is_empty() {
if let Some(url) = rels.get(&cur_hlink_rid) {
format!("[{}]({})", formatted, url)
} else {
formatted
}
} else {
formatted
};
push_text(&mut para_text, &final_text);
}
in_run = false;
cur_bold = false;
cur_italic = false;
cur_run_text.clear();
cur_hlink_rid.clear();
}
b"t" if in_t => {
in_t = false;
}
b"t" if in_tc_t => {
in_tc_t = false;
}
b"pPr" if para_in_ppr => {
para_in_ppr = false;
}
b"p" if in_para => {
in_para = false;
let trimmed = para_text.trim().to_string();
if !trimmed.is_empty() {
let inferred_bullet = !sp_is_title && !para_explicit_bu_none;
if para_has_bullet || (inferred_bullet && !para_is_numbered) {
shape_paragraphs.push(SlideBlock::list_item(
trimmed,
para_level,
para_is_numbered,
));
} else {
shape_paragraphs.push(SlideBlock::paragraph(trimmed));
}
}
para_text.clear();
}
b"txBody" if in_txbody && !in_tbl => {
in_txbody = false;
}
b"sp" if sp_depth > 0 => {
sp_depth -= 1;
if sp_depth == 0 {
if sp_is_title && slide.title.is_none() {
let title_parts: Vec<String> = shape_paragraphs
.iter()
.filter_map(|b| {
if matches!(
b.kind,
BlockKind::Paragraph | BlockKind::ListItem
) {
Some(b.text.clone())
} else {
None
}
})
.collect();
if !title_parts.is_empty() {
slide.title = Some(title_parts.join(" "));
}
} else {
slide.blocks.extend(shape_paragraphs.drain(..));
}
shape_paragraphs.clear();
sp_is_title = false;
sp_ph_checked = false;
}
}
b"pic" if in_pic => {
in_pic = false;
slide
.blocks
.push(SlideBlock::image(pic_alt.take(), pic_rid.take()));
}
b"bg" if in_bg => {
in_bg = false;
if let Some(rid) = bg_rid.take() {
slide.blocks.push(SlideBlock::image(None, Some(rid)));
}
}
b"p" if in_tc_para => {
in_tc_para = false;
let t = tc_para_text.trim().to_string();
if !t.is_empty() {
if !tc_text.is_empty() {
tc_text.push(' ');
}
tc_text.push_str(&t);
}
tc_para_text.clear();
}
b"txBody" if in_tc_body => {
in_tc_body = false;
}
b"tc" if in_tc => {
in_tc = false;
tbl_current_row.push(tc_text.trim().to_string());
tc_text.clear();
}
b"tr" if in_tbl => {
if !tbl_current_row.is_empty() {
tbl_rows.push(std::mem::take(&mut tbl_current_row));
}
}
b"tbl" if in_tbl => {
in_tbl = false;
if !tbl_rows.is_empty() {
let has_hdr = tbl_has_header || tbl_rows.len() > 1;
slide
.blocks
.push(SlideBlock::table(std::mem::take(&mut tbl_rows), has_hdr));
}
}
b"tblPr" if in_tbl_ppr => {
in_tbl_ppr = false;
}
_ => {}
}
}
Ok(XmlEvent::Eof) => break,
Err(e) => return Err(format!("PPTX slide XML parse error: {e}")),
_ => {}
}
buf.clear();
}
Ok(slide)
}