ppt-rs 0.2.14

Create, read, and update PowerPoint 2007+ (.pptx) files with rich formatting, bullet styles, themes, and templates.
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
//! Converter from web content to PowerPoint

use super::{Web2PptError, Result, Web2PptConfig, WebContent, ContentType};
use crate::{create_pptx_with_content, SlideContent, SlideLayout};

/// Safely truncate text at char boundary
fn truncate_text(text: &str, max_len: usize) -> String {
    if text.len() <= max_len {
        return text.to_string();
    }
    
    // Find a safe truncation point
    let mut end = max_len;
    while end > 0 && !text.is_char_boundary(end) {
        end -= 1;
    }
    
    // Try to break at word boundary
    if let Some(last_space) = text[..end].rfind(' ') {
        if last_space > max_len / 2 {
            end = last_space;
        }
    }
    
    format!("{}...", &text[..end].trim_end())
}

/// Options for conversion
#[derive(Clone, Debug)]
pub struct ConversionOptions {
    /// Presentation title (overrides page title)
    pub title: Option<String>,
    /// Author name
    pub author: Option<String>,
    /// Add source URL to slides
    pub include_source_url: bool,
    /// Add page numbers
    pub add_page_numbers: bool,
}

impl Default for ConversionOptions {
    fn default() -> Self {
        ConversionOptions {
            title: None,
            author: None,
            include_source_url: true,
            add_page_numbers: false,
        }
    }
}

impl ConversionOptions {
    /// Create new options
    pub fn new() -> Self {
        Self::default()
    }

    /// Set custom title
    pub fn title(mut self, title: &str) -> Self {
        self.title = Some(title.to_string());
        self
    }

    /// Set author
    pub fn author(mut self, author: &str) -> Self {
        self.author = Some(author.to_string());
        self
    }

    /// Include source URL
    pub fn with_source_url(mut self, include: bool) -> Self {
        self.include_source_url = include;
        self
    }

    /// Add page numbers
    pub fn with_page_numbers(mut self, add: bool) -> Self {
        self.add_page_numbers = add;
        self
    }
}

/// Web to PowerPoint converter
pub struct Web2Ppt {
    config: Web2PptConfig,
}

impl Web2Ppt {
    /// Create a new converter with default config
    pub fn new() -> Self {
        Self::with_config(Web2PptConfig::default())
    }

    /// Create a new converter with custom config
    pub fn with_config(config: Web2PptConfig) -> Self {
        Web2Ppt { config }
    }

    /// Convert web content to PowerPoint bytes
    pub fn convert(&self, content: &WebContent, options: &ConversionOptions) -> Result<Vec<u8>> {
        let slides = self.build_slides(content, options)?;
        let title = options.title.as_ref().unwrap_or(&content.title);

        create_pptx_with_content(title, slides)
            .map_err(|e| Web2PptError::GenerationError(e.to_string()))
    }

    /// Build slides from web content
    fn build_slides(&self, content: &WebContent, options: &ConversionOptions) -> Result<Vec<SlideContent>> {
        let mut slides = Vec::new();

        // Title slide
        let title = options.title.as_ref().unwrap_or(&content.title);
        let mut title_slide = SlideContent::new(title)
            .layout(SlideLayout::CenteredTitle);

        if let Some(desc) = &content.description {
            title_slide = title_slide.add_bullet(desc);
        }

        if options.include_source_url {
            title_slide = title_slide.add_bullet(&format!("Source: {}", content.url));
        }

        slides.push(title_slide);

        // Content slides
        if self.config.group_by_headings {
            self.build_grouped_slides(content, &mut slides)?;
        } else {
            self.build_linear_slides(content, &mut slides)?;
        }

        // Limit slides
        if slides.len() > self.config.max_slides {
            slides.truncate(self.config.max_slides);
        }

        Ok(slides)
    }

    /// Build slides grouped by headings
    fn build_grouped_slides(&self, content: &WebContent, slides: &mut Vec<SlideContent>) -> Result<()> {
        let groups = content.grouped_by_headings();

        // If no groups found, fall back to linear mode
        if groups.is_empty() {
            return self.build_linear_slides(content, slides);
        }

        for (heading, blocks) in groups {
            if slides.len() >= self.config.max_slides {
                break;
            }

            let mut slide = SlideContent::new(&heading.text)
                .layout(SlideLayout::TitleAndContent);

            let mut bullet_count = 0;

            for block in blocks {
                if bullet_count >= self.config.max_bullets_per_slide {
                    // Start a new slide for overflow
                    slides.push(slide);
                    slide = SlideContent::new(&format!("{} (cont.)", heading.text))
                        .layout(SlideLayout::TitleAndContent);
                    bullet_count = 0;

                    if slides.len() >= self.config.max_slides {
                        break;
                    }
                }

                match &block.content_type {
                    ContentType::Paragraph => {
                        // Truncate long paragraphs - use char_indices for safe slicing
                        let text = truncate_text(&block.text, 200);
                        slide = slide.add_bullet(&text);
                        bullet_count += 1;
                    }
                    ContentType::ListItem => {
                        let text = truncate_text(&block.text, 180);
                        slide = slide.add_bullet(&format!("• {}", text));
                        bullet_count += 1;
                    }
                    ContentType::Quote => {
                        let text = truncate_text(&block.text, 180);
                        slide = slide.add_bullet(&format!("\"{}\"", text));
                        bullet_count += 1;
                    }
                    ContentType::Code => {
                        if self.config.include_code {
                            let code = truncate_text(&block.text, 150);
                            slide = slide.add_bullet(&format!("[Code] {}", code));
                            bullet_count += 1;
                        }
                    }
                    ContentType::Table(rows) => {
                        if self.config.include_tables && !rows.is_empty() {
                            let summary = format!("[Table: {} rows × {} cols]", 
                                rows.len(), 
                                rows.first().map(|r| r.len()).unwrap_or(0)
                            );
                            slide = slide.add_bullet(&summary);
                            bullet_count += 1;
                        }
                    }
                    ContentType::Image { alt, .. } => {
                        if self.config.include_images && !alt.is_empty() {
                            slide = slide.add_bullet(&format!("[Image: {}]", alt));
                            bullet_count += 1;
                        }
                    }
                    _ => {}
                }
            }

            // Only add slide if it has content
            if bullet_count > 0 {
                slides.push(slide);
            }
        }

        Ok(())
    }

    /// Build slides linearly (not grouped)
    fn build_linear_slides(&self, content: &WebContent, slides: &mut Vec<SlideContent>) -> Result<()> {
        let mut current_slide: Option<SlideContent> = None;
        let mut bullet_count = 0;

        // If no content blocks, create a slide with description
        if content.blocks.is_empty() {
            if let Some(desc) = &content.description {
                let slide = SlideContent::new("Content")
                    .layout(SlideLayout::TitleAndContent)
                    .add_bullet(desc);
                slides.push(slide);
            }
            return Ok(());
        }

        for block in &content.blocks {
            if slides.len() >= self.config.max_slides {
                break;
            }

            match &block.content_type {
                ContentType::Title | ContentType::Heading(_) => {
                    // Save current slide if it has content
                    if let Some(slide) = current_slide.take() {
                        if bullet_count > 0 {
                            slides.push(slide);
                        }
                    }

                    // Start new slide
                    current_slide = Some(
                        SlideContent::new(&block.text)
                            .layout(SlideLayout::TitleAndContent)
                    );
                    bullet_count = 0;
                }
                ContentType::Paragraph => {
                    // If no current slide, create one
                    if current_slide.is_none() {
                        current_slide = Some(
                            SlideContent::new("Overview")
                                .layout(SlideLayout::TitleAndContent)
                        );
                    }
                    
                    if let Some(ref mut slide) = current_slide {
                        if bullet_count < self.config.max_bullets_per_slide {
                            let text = truncate_text(&block.text, 200);
                            *slide = slide.clone().add_bullet(&text);
                            bullet_count += 1;
                        } else {
                            // Start new continuation slide
                            slides.push(slide.clone());
                            let title = slide.title.clone();
                            *slide = SlideContent::new(&format!("{} (cont.)", title))
                                .layout(SlideLayout::TitleAndContent);
                            let text = truncate_text(&block.text, 200);
                            *slide = slide.clone().add_bullet(&text);
                            bullet_count = 1;
                        }
                    }
                }
                ContentType::ListItem => {
                    if current_slide.is_none() {
                        current_slide = Some(
                            SlideContent::new("Key Points")
                                .layout(SlideLayout::TitleAndContent)
                        );
                    }
                    
                    if let Some(ref mut slide) = current_slide {
                        if bullet_count < self.config.max_bullets_per_slide {
                            let text = truncate_text(&block.text, 180);
                            *slide = slide.clone().add_bullet(&format!("• {}", text));
                            bullet_count += 1;
                        }
                    }
                }
                ContentType::Quote => {
                    if let Some(ref mut slide) = current_slide {
                        if bullet_count < self.config.max_bullets_per_slide {
                            let text = truncate_text(&block.text, 180);
                            *slide = slide.clone().add_bullet(&format!("\"{}\"", text));
                            bullet_count += 1;
                        }
                    }
                }
                _ => {}
            }
        }

        // Save last slide
        if let Some(slide) = current_slide {
            if bullet_count > 0 {
                slides.push(slide);
            }
        }

        Ok(())
    }

    /// Get config
    pub fn config(&self) -> &Web2PptConfig {
        &self.config
    }
}

impl Default for Web2Ppt {
    fn default() -> Self {
        Self::new()
    }
}

/// High-level function to convert a URL to PPTX bytes
#[cfg(feature = "web2ppt")]
pub fn url_to_pptx(url: &str) -> Result<Vec<u8>> {
    url_to_pptx_with_options(url, Web2PptConfig::default(), ConversionOptions::default())
}

/// High-level function to convert a URL to PPTX bytes with options
#[cfg(feature = "web2ppt")]
pub fn url_to_pptx_with_options(
    url: &str,
    config: Web2PptConfig,
    options: ConversionOptions,
) -> Result<Vec<u8>> {
    use super::{WebFetcher, WebParser};

    // Fetch
    let fetcher = WebFetcher::with_config(config.clone())?;
    let html = fetcher.fetch(url)?;

    // Parse
    let parser = WebParser::with_config(config.clone());
    let content = parser.parse(&html, url)?;

    // Convert
    let converter = Web2Ppt::with_config(config);
    converter.convert(&content, &options)
}

/// Convert HTML string to PPTX bytes
pub fn html_to_pptx(html: &str, url: &str) -> Result<Vec<u8>> {
    html_to_pptx_with_options(html, url, Web2PptConfig::default(), ConversionOptions::default())
}

/// Convert HTML string to PPTX bytes with options
pub fn html_to_pptx_with_options(
    html: &str,
    url: &str,
    config: Web2PptConfig,
    options: ConversionOptions,
) -> Result<Vec<u8>> {
    use super::WebParser;

    // Parse
    let parser = WebParser::with_config(config.clone());
    let content = parser.parse(html, url)?;

    // Convert
    let converter = Web2Ppt::with_config(config);
    converter.convert(&content, &options)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_conversion_options() {
        let options = ConversionOptions::new()
            .title("Custom Title")
            .author("Test Author")
            .with_source_url(false);

        assert_eq!(options.title, Some("Custom Title".to_string()));
        assert_eq!(options.author, Some("Test Author".to_string()));
        assert!(!options.include_source_url);
    }

    #[test]
    fn test_html_to_pptx() {
        let html = r#"
            <!DOCTYPE html>
            <html>
            <head><title>Test Page</title></head>
            <body>
                <h1>Main Title</h1>
                <p>This is a paragraph with enough text to be included in the presentation.</p>
                <h2>Section 1</h2>
                <p>Section 1 content with enough text to be included in the presentation.</p>
                <ul>
                    <li>Item 1</li>
                    <li>Item 2</li>
                </ul>
            </body>
            </html>
        "#;

        let result = html_to_pptx(html, "https://example.com");
        assert!(result.is_ok());

        let pptx = result.unwrap();
        assert!(!pptx.is_empty());
    }

    #[test]
    fn test_web2ppt_config() {
        let config = Web2PptConfig::new()
            .max_slides(5)
            .max_bullets(3);

        let converter = Web2Ppt::with_config(config);
        assert_eq!(converter.config().max_slides, 5);
        assert_eq!(converter.config().max_bullets_per_slide, 3);
    }
}