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
use crate::markdown::{render_runs, MarkdownContext};
use crate::parser_config::ImageHandlingMode;
use crate::{ElementPosition, ImageReference, ParserConfig, SlideElement};
use base64::{engine::general_purpose, Engine as _};
use image::ImageOutputFormat;
use std::collections::HashMap;
use std::fs;
use std::io::Cursor;
use std::path::{Path, PathBuf};
/// Encapsulates images for manual extraction of images from slides
#[derive(Debug)]
pub struct ManualImage {
pub base64_content: String,
pub img_ref: ImageReference,
}
impl ManualImage {
pub fn new(base64_content: String, img_ref: ImageReference) -> ManualImage {
Self {
base64_content,
img_ref,
}
}
}
/// Represents a single slide extracted from a PowerPoint (pptx) file.
///
/// Contains structured slide data including slide number, parsed content elements
/// (text, tables, images, lists), speaker notes, and associated image references.
///
/// A `Slide` can be converted into other formats, such as Markdown, or its
/// contained images can be extracted in base64 representation.
///
/// Typically, you retrieve instances of `Slide` through [`PptxContainer::parse()`].
#[derive(Debug)]
pub struct Slide {
pub rel_path: String,
pub slide_number: u32,
pub elements: Vec<SlideElement>,
pub speaker_notes: Vec<crate::TextElement>,
pub comments: Vec<crate::TextElement>,
pub images: Vec<ImageReference>,
pub image_data: HashMap<String, Vec<u8>>,
pub config: ParserConfig,
}
impl Slide {
#[allow(clippy::too_many_arguments)]
pub fn new(
rel_path: String,
slide_number: u32,
elements: Vec<SlideElement>,
speaker_notes: Vec<crate::TextElement>,
comments: Vec<crate::TextElement>,
images: Vec<ImageReference>,
image_data: HashMap<String, Vec<u8>>,
config: ParserConfig,
) -> Self {
Self {
rel_path,
slide_number,
elements,
speaker_notes,
comments,
images,
image_data,
config,
}
}
/// Converts slide contents into a Markdown formatted string.
///
/// Translates internal slide elements (text, tables, lists, images) to valid
/// and readable Markdown. Embedded images will be encoded as base64 inline images.
///
/// # Returns
///
/// Returns an `Option<String>`:
/// - `Some(String)`: Markdown representation of slide if conversion succeeds.
/// - `None`: If a conversion error occurs during image encoding.
pub fn convert_to_md(&self) -> Option<String> {
let mut slide_txt = String::new();
if self.config.include_slide_number_as_comment {
slide_txt.push_str(format!("<!-- Slide {} -->\n\n", self.slide_number).as_str());
}
let mut image_count = 0;
let mut sorted_elements = self.elements.clone();
sorted_elements.sort_by_key(|element| {
let ElementPosition { y, x } = element.position();
(y, x)
});
for element in sorted_elements {
match element {
SlideElement::Text(text, _pos) => {
slide_txt.push_str(&render_runs(&text.runs, MarkdownContext::Flow));
slide_txt.push('\n');
}
SlideElement::Table(table, _pos) => {
let mut is_header = true;
for row in &table.rows {
let mut row_texts = Vec::new();
for cell in &row.cells {
let cell_text = render_runs(&cell.runs, MarkdownContext::TableCell);
row_texts.push(cell_text);
}
let row_line = format!("| {} |", row_texts.join(" | "));
slide_txt.push_str(&row_line);
slide_txt.push('\n');
if is_header {
let separator_line = format!(
"|{}|",
row_texts
.iter()
.map(|_| " --- ")
.collect::<Vec<_>>()
.join("|")
);
slide_txt.push_str(&separator_line);
slide_txt.push('\n');
is_header = false;
}
}
slide_txt.push('\n');
}
SlideElement::Image(image_ref, _pos) => {
match self.config.image_handling_mode {
ImageHandlingMode::InMarkdown => {
if let Some(image_data) = self.image_data.get(&image_ref.id) {
let image_data = if self.config.compress_images {
self.compress_image(image_data)
} else {
Some(image_data.clone())
};
let base64_string = general_purpose::STANDARD.encode(image_data?);
let image_name = image_ref.target.split('/').next_back()?;
let file_ext = image_name.split('.').next_back()?;
slide_txt.push_str(
format!(
"",
image_name, file_ext, base64_string
)
.as_str(),
);
}
}
ImageHandlingMode::Save => {
if let Some(image_data) = self.image_data.get(&image_ref.id) {
let image_data = if self.config.compress_images {
self.compress_image(image_data)
} else {
Some(image_data.clone())
};
let ext = if self.config.compress_images {
"jpg".to_string()
} else {
self.get_image_extension(&image_ref.target)
};
let output_dir = self
.config
.image_output_path
.clone()
.unwrap_or_else(|| PathBuf::from("."));
let _ = fs::create_dir_all(&output_dir);
let mut image_path = output_dir.clone();
let file_name = format!(
"slide{}_image{}_{}.{}",
self.slide_number,
image_count + 1,
&image_ref.id,
ext
);
image_path.push(&file_name);
let _ = fs::write(&image_path, image_data?);
let abs_file_url = self.path_to_file_url(&image_path);
let html_link =
format!(r#"<a href={:?}>{file_name}</a>"#, abs_file_url?);
image_count += 1;
slide_txt.push_str(&html_link);
slide_txt.push('\n');
}
}
ImageHandlingMode::Manually => {
slide_txt.push('\n');
continue;
}
}
slide_txt.push('\n');
}
SlideElement::List(list_element, _pos) => {
let mut counters: Vec<usize> = Vec::new();
let mut previous_level = 0;
for item in &list_element.items {
let item_text = render_runs(&item.runs, MarkdownContext::ListItem);
let level = item.level as usize;
if level >= counters.len() {
counters.resize(level + 1, 0);
}
match level.cmp(&previous_level) {
std::cmp::Ordering::Greater => counters[level] = 0,
std::cmp::Ordering::Less => counters.truncate(level + 1),
std::cmp::Ordering::Equal => {}
}
counters[level] += 1;
previous_level = level;
let indent = "\t".repeat(level);
let marker = if item.is_ordered {
format!("{}{}. ", indent, counters[level])
} else {
format!("{}- ", indent)
};
slide_txt.push_str(&format!("{}{}\n", marker, item_text));
}
}
_ => (),
}
}
if self.config.include_speaker_notes && !self.speaker_notes.is_empty() {
append_quoted_section(&mut slide_txt, "Speaker Notes", &self.speaker_notes);
}
if self.config.include_comments && !self.comments.is_empty() {
append_quoted_section(&mut slide_txt, "Comments", &self.comments);
}
Some(slide_txt)
}
/// Extracts the numeric slide identifier from a slide path.
///
/// Helper method to parse slide numbers from internal pptx
/// slide paths (e.g., "ppt/slides/slide1.xml" → `1`).
pub fn extract_slide_number(path: &str) -> Option<u32> {
path.split('/')
.next_back()
.and_then(|filename| {
filename
.strip_prefix("slide")
.and_then(|s| s.strip_suffix(".xml"))
})
.and_then(|num_str| num_str.parse::<u32>().ok())
}
/// Links slide images references with their corresponding targets.
///
/// Ensures that each image referenced by its ID is correctly
/// linked to the actual internal resource paths stored in the slide.
/// This method is typically used internally after parsing a slide
///
/// # Notes
///
/// Internally those are the values image references are holding
///
/// | Parameter | Example value |
/// |---------- |---------------------- |
/// | `id` | *rId2* |
/// | `target` | *../media/image2.png* |
///
pub fn link_images(&mut self) {
let id_to_target: HashMap<String, String> = self
.images
.iter()
.map(|img_ref| (img_ref.id.clone(), img_ref.target.clone()))
.collect();
for element in &mut self.elements {
if let SlideElement::Image(ref mut img_ref, _pos) = element {
if let Some(target) = id_to_target.get(&img_ref.id) {
img_ref.target = target.clone();
}
}
}
}
/// Extracts the file extension from image paths
pub fn get_image_extension(&self, path: &str) -> String {
Path::new(path)
.extension()
.and_then(|ext| ext.to_str())
.unwrap_or("bin")
.to_string()
}
/// Compresses the image data and returning it as a `jpg` byte slice
///
/// # Parameter
///
/// - `image_data`: The raw image data as a byte array
///
/// # Returns
///
/// - `Vec<u8>`: Returns the compressed and converted jpg byte array
///
/// # Notes
///
/// All images will be converted to `jpg`
pub fn compress_image(&self, image_data: &[u8]) -> Option<Vec<u8>> {
let img = match image::load_from_memory(image_data) {
Ok(image) => image,
Err(_) => return None,
};
let mut output = Vec::new();
let quality = self.config.quality;
if img
.write_to(
&mut Cursor::new(&mut output),
ImageOutputFormat::Jpeg(quality),
)
.is_ok()
{
Some(output)
} else {
None
}
}
pub fn load_images_manually(&self) -> Option<Vec<ManualImage>> {
let mut images: Vec<ManualImage> = Vec::new();
let image_refs: Vec<&ImageReference> = self
.elements
.iter()
.filter_map(|element| match element {
SlideElement::Image(ref img, _pos) => Some(img),
_ => None,
})
.collect();
for image_ref in image_refs {
if let Some(image_data) = self.image_data.get(&image_ref.id) {
let image_data = if self.config.compress_images {
self.compress_image(image_data)
} else {
Some(image_data.clone())
};
let base64_str = general_purpose::STANDARD.encode(image_data?);
let image = ManualImage::new(base64_str, image_ref.clone());
images.push(image);
}
}
Some(images)
}
fn path_to_file_url(&self, path: &Path) -> Option<String> {
let abs_path = path.canonicalize().ok()?;
let mut path_str = abs_path.to_string_lossy().replace('\\', "/");
// remove windows unc prefix
if cfg!(windows) {
if let Some(stripped) = path_str.strip_prefix("//?/") {
path_str = stripped.to_string();
}
Some(format!("file:///{}", path_str))
} else {
Some(format!("file://{}", path_str))
}
}
}
fn append_quoted_section(output: &mut String, title: &str, elements: &[crate::TextElement]) {
if !output.is_empty() && !output.ends_with("\n\n") {
output.push('\n');
}
output.push_str(&format!("> **{}**\n>\n", title));
for (index, element) in elements.iter().enumerate() {
let content = render_runs(&element.runs, MarkdownContext::Quote);
for line in content.lines() {
output.push_str("> ");
output.push_str(line);
output.push('\n');
}
if index + 1 < elements.len() {
output.push_str(">\n");
}
}
}
#[cfg(test)]
#[path = "../tests/unit/slide.rs"]
mod tests;