ppt-rs 0.2.37

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
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
//! HTML to PowerPoint conversion
//!
//! Parses HTML content and converts it into PowerPoint slide structures.
//! No external dependencies required - uses a lightweight state-machine parser.
//!
//! # This is the Basic HTML Parser
//!
//! This parser is designed for simple, fast HTML-to-PowerPoint conversion
//! with minimal dependencies. For advanced web scraping and content extraction,
//! see the `web2ppt::parser` module (requires scraper crate).
//!
//! ## When to use this parser:
//! - Converting simple HTML strings
//! - Processing well-structured HTML files
//! - When you want zero external dependencies
//! - For embedded applications
//!
//! ## When to use web2ppt::parser instead:
//! - Processing live web pages with navigation/ads
//! - When you need intelligent content extraction
//! - For automatic content cleaning and detection
//! - When you need to handle complex web page structures
//!
//! # Supported HTML elements
//!
//! - `<h1>` → New slide title
//! - `<h2>` through `<h6>` → Bold section headers on current slide
//! - `<p>` → Bullet points / paragraphs
//! - `<ul>/<ol>` with `<li>` → List items
//! - `<table>` with `<tr>/<th>/<td>` → Table objects (styled header row)
//! - `<pre>/<code>` → Code blocks
//! - `<img>` → Image placeholders (with alt text)
//! - `<blockquote>` → Speaker notes
//! - `<strong>/<b>` → Bold text (via markdown-style `**`)
//! - `<em>/<i>` → Italic text (via markdown-style `*`)
//! - `<a href="...">` → Hyperlink text
//! - `<hr>` → Slide break
//! - `<br>` → Line break within text
//! - `<title>` → Presentation title (falls back to first `<h1>`)

mod parser;
mod style;
mod tokenizer;

use crate::generator::SlideContent;

/// Options for HTML parsing
#[derive(Clone, Debug)]
pub struct HtmlParseOptions {
    /// Maximum slides to generate
    pub max_slides: usize,
    /// Maximum bullet points per slide
    pub max_bullets: usize,
    /// Include code blocks
    pub include_code: bool,
    /// Include tables
    pub include_tables: bool,
    /// Include image placeholders
    pub include_images: bool,
}

impl Default for HtmlParseOptions {
    fn default() -> Self {
        Self {
            max_slides: 50,
            max_bullets: 10,
            include_code: true,
            include_tables: true,
            include_images: true,
        }
    }
}

impl HtmlParseOptions {
    pub fn new() -> Self {
        Self::default()
    }

    pub fn max_slides(mut self, n: usize) -> Self {
        self.max_slides = n;
        self
    }

    pub fn max_bullets(mut self, n: usize) -> Self {
        self.max_bullets = n;
        self
    }

    pub fn include_code(mut self, include: bool) -> Self {
        self.include_code = include;
        self
    }

    pub fn include_tables(mut self, include: bool) -> Self {
        self.include_tables = include;
        self
    }

    pub fn include_images(mut self, include: bool) -> Self {
        self.include_images = include;
        self
    }
}

/// Parse HTML content into slides with default options
pub fn parse_html(html: &str) -> Result<Vec<SlideContent>, String> {
    Html2Ppt::with_options(HtmlParseOptions::default()).parse(html)
}

/// Parse HTML content into slides with custom options
pub fn parse_html_with_options(
    html: &str,
    options: HtmlParseOptions,
) -> Result<Vec<SlideContent>, String> {
    Html2Ppt::with_options(options).parse(html)
}


// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// HTML to PowerPoint converter
pub struct Html2Ppt {
    options: HtmlParseOptions,
}

impl Html2Ppt {
    pub fn new() -> Self {
        Self::with_options(HtmlParseOptions::default())
    }

    pub fn with_options(options: HtmlParseOptions) -> Self {
        Self { options }
    }

    /// Parse an HTML string into slide content
    pub fn parse(&self, html: &str) -> Result<Vec<SlideContent>, String> {
        let events = tokenizer::tokenize_html(html);
        parser::HtmlSlideParser::new(self.options.clone()).parse(&events)
    }

    /// Parse HTML from a file path
    pub fn parse_file(&self, path: &str) -> Result<Vec<SlideContent>, String> {
        let html =
            std::fs::read_to_string(path).map_err(|e| format!("Failed to read HTML file: {e}"))?;
        self.parse(&html)
    }
}

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

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

    #[test]
    fn test_simple_headings() {
        let html = "<h1>First Slide</h1><p>Some content</p><h1>Second Slide</h1>";
        let slides = parse_html(html).unwrap();
        assert_eq!(slides.len(), 2);
        assert_eq!(slides[0].title, "First Slide");
        assert_eq!(slides[1].title, "Second Slide");
        assert_eq!(slides[1].content.len(), 0);
    }

    #[test]
    fn test_table() {
        let html = r#"
            <html><body>
                <h1>Data</h1>
                <table>
                    <tr><th>Name</th><th>Value</th></tr>
                    <tr><td>A</td><td>1</td></tr>
                    <tr><td>B</td><td>2</td></tr>
                </table>
            </body></html>
        "#;
        let slides = parse_html(html).unwrap();
        assert!(slides[0].table.is_some());
    }

    #[test]
    fn test_code_block() {
        let html = r#"
            <html><body>
                <h1>Code Example</h1>
                <pre><code>fn main() { println!("hello"); }</code></pre>
            </body></html>
        "#;
        let slides = parse_html(html).unwrap();
        assert!(!slides[0].code_blocks.is_empty());
        assert!(slides[0].code_blocks[0].code.contains("fn main()"));
    }

    #[test]
    fn test_blockquote_notes() {
        let html = r#"
            <html><body>
                <h1>Slide</h1>
                <p>Content</p>
                <blockquote>Speaker note here</blockquote>
            </body></html>
        "#;
        let slides = parse_html(html).unwrap();
        assert_eq!(slides[0].notes, Some("Speaker note here".to_string()));
    }

    #[test]
    fn test_hr_slide_break() {
        let html = "<h1>Slide 1</h1><p>Content</p><hr><h1>Slide 2</h1><p>More content</p>";
        let slides = parse_html(html).unwrap();
        assert_eq!(slides.len(), 2);
    }

    #[test]
    fn test_entity_decoding() {
        let html = r#"
            <html><body>
                <h1>Test</h1>
                <p>AT&amp;T &lt;test&gt; &quot;quote&quot;</p>
            </body></html>
        "#;
        let slides = parse_html(html).unwrap();
        assert!(slides[0].content[0].contains("AT&T"));
        assert!(slides[0].content[0].contains("<test>"));
    }

    #[test]
    fn test_img_placeholder() {
        let html = r#"
            <html><body>
                <h1>Images</h1>
                <img src="photo.jpg" alt="A photo">
            </body></html>
        "#;
        let slides = parse_html(html).unwrap();
        assert!(
            slides[0]
                .content
                .iter()
                .any(|c| c.contains("[Image: A photo]"))
        );
    }

    #[test]
    fn test_skip_script_style() {
        let html = r#"
            <html><body>
                <h1>Real Content</h1>
                <p>Visible text</p>
                <script>var x = "should not appear";</script>
                <style>.hidden { color: red; }</style>
            </body></html>
        "#;
        let slides = parse_html(html).unwrap();
        assert_eq!(slides.len(), 1);
        assert_eq!(slides[0].content.len(), 1);
        assert!(slides[0].content[0].contains("Visible"));
    }

    #[test]
    fn test_no_h1_fallback() {
        let html = r#"<html><body><p>Just a paragraph.</p></body></html>"#;
        let slides = parse_html(html).unwrap();
        assert_eq!(slides.len(), 1);
    }

    #[test]
    fn test_empty_input() {
        let result = parse_html("<html><body></body></html>");
        assert!(result.is_err());
    }

    #[test]
    fn test_br_tag() {
        let html = r#"<html><body><h1>Title</h1><p>Line 1<br>Line 2</p></body></html>"#;
        let slides = parse_html(html).unwrap();
        assert!(!slides[0].content.is_empty());
    }

    #[test]
    fn test_bold_italic() {
        let html = r#"
            <html><body>
                <h1>Formatting</h1>
                <p><strong>Bold</strong> and <em>italic</em> text</p>
            </body></html>
        "#;
        let slides = parse_html(html).unwrap();
        let c = &slides[0].content[0];
        assert!(c.contains("**Bold**"));
    }

    #[test]
    fn test_complex_nested() {
        let html = r#"
            <html><body>
                <h1>Welcome</h1>
                <p>Introduction paragraph.</p>
                <h2>Section A</h2>
                <ul>
                    <li>First item</li>
                    <li>Second item</li>
                </ul>
                <h1>Details</h1>
                <table><tr><th>Col1</th><th>Col2</th></tr>
                       <tr><td>A</td><td>B</td></tr></table>
                <pre><code>let x = 1;</code></pre>
            </body></html>
        "#;
        let slides = parse_html(html).unwrap();
        assert_eq!(slides.len(), 2);
        assert_eq!(slides[0].title, "Welcome");
        assert!(!slides[1].code_blocks.is_empty());
        assert!(slides[1].table.is_some());
    }

    #[test]
    fn test_html2ppt_options() {
        let options = HtmlParseOptions::new()
            .max_slides(3)
            .max_bullets(5)
            .include_images(false);
        assert_eq!(options.max_slides, 3);
        assert_eq!(options.max_bullets, 5);
        assert!(!options.include_images);
    }

    #[test]
    fn test_html2ppt_struct() {
        let converter = Html2Ppt::new();
        let html = "<h1>Test</h1><p>Content</p>";
        let slides = converter.parse(html).unwrap();
        assert_eq!(slides.len(), 1);
    }

    #[test]
    fn test_nested_elements() {
        let html = r#"
            <div><div><div><div><div>
                <h1>Deep Nesting</h1>
                <p>Still works</p>
            </div></div></div></div></div>
        "#;
        let slides = parse_html(html).unwrap();
        assert_eq!(slides[0].title, "Deep Nesting");
    }

    #[test]
    fn test_link_with_href() {
        let html = r#"
            <html><body>
                <h1>Links</h1>
                <p>Visit <a href="https://example.com">Example</a> website</p>
            </body></html>
        "#;
        let slides = parse_html(html).unwrap();
        assert!(slides[0].content[0].contains("Example"));
    }

    #[test]
    fn test_paragraph_inline_color() {
        let html = r#"<h1>Test</h1><p style="color:red">Red text</p>"#;
        let slides = parse_html(html).unwrap();
        assert_eq!(slides[0].bullets.len(), 1);
        let fmt = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("Should have format");
        assert_eq!(fmt.color, Some("FF0000".to_string()));
    }

    #[test]
    fn test_paragraph_inline_font_size() {
        let html = r#"<h1>Test</h1><p style="font-size:20px">Bigger text</p>"#;
        let slides = parse_html(html).unwrap();
        let fmt = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("Should have format");
        assert_eq!(fmt.font_size, Some(15));
    }

    #[test]
    fn test_paragraph_inline_bold() {
        let html = r#"<h1>Test</h1><p style="font-weight:bold">Bold paragraph</p>"#;
        let slides = parse_html(html).unwrap();
        let fmt = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("Should have format");
        assert!(fmt.bold);
    }

    #[test]
    fn test_paragraph_inline_italic() {
        let html = r#"<h1>Test</h1><p style="font-style:italic">Italic paragraph</p>"#;
        let slides = parse_html(html).unwrap();
        let fmt = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("Should have format");
        assert!(fmt.italic);
    }

    #[test]
    fn test_paragraph_inline_underline() {
        let html = r#"<h1>Test</h1><p style="text-decoration:underline">Underlined</p>"#;
        let slides = parse_html(html).unwrap();
        let fmt = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("Should have format");
        assert!(fmt.underline);
    }

    #[test]
    fn test_paragraph_inline_multiple_styles() {
        let html =
            r#"<h1>Test</h1><p style="color:blue; font-size:18pt; font-weight:bold">Styled</p>"#;
        let slides = parse_html(html).unwrap();
        let fmt = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("Should have format");
        assert_eq!(fmt.color, Some("0000FF".to_string()));
        assert_eq!(fmt.font_size, Some(18));
        assert!(fmt.bold);
    }

    #[test]
    fn test_paragraph_no_style_no_format() {
        let html = "<h1>Test</h1><p>Plain text</p>";
        let slides = parse_html(html).unwrap();
        assert!(slides[0].bullets[0].format.is_none());
    }

    #[test]
    fn test_h1_inline_color() {
        let html = r#"<h1 style="color:green">Green Title</h1>"#;
        let slides = parse_html(html).unwrap();
        assert_eq!(slides[0].title_color, Some("008000".to_string()));
    }

    #[test]
    fn test_h1_inline_font_size() {
        let html = r#"<h1 style="font-size:36pt">Big Title</h1>"#;
        let slides = parse_html(html).unwrap();
        assert_eq!(slides[0].title_size, Some(36));
    }

    #[test]
    fn test_h1_inline_bold_true() {
        let html = r#"<h1 style="font-weight:bold">Bold Title</h1>"#;
        let slides = parse_html(html).unwrap();
        assert!(slides[0].title_bold); // True because css bold=True; default is also true
    }

    #[test]
    fn test_h1_inline_italic() {
        let html = r#"<h1 style="font-style:italic">Italic Title</h1>"#;
        let slides = parse_html(html).unwrap();
        assert!(slides[0].title_italic);
    }

    #[test]
    fn test_h1_underline_from_style() {
        let html = r#"<h1 style="text-decoration:underline">Underlined Title</h1>"#;
        let slides = parse_html(html).unwrap();
        assert!(slides[0].title_underline);
    }

    #[test]
    fn test_list_item_with_inline_style() {
        let html =
            r#"<h1>List</h1><ul><li style="color:red">Red item</li><li>Normal item</li></ul>"#;
        let slides = parse_html(html).unwrap();
        let fmt0 = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("First item should have format");
        assert_eq!(fmt0.color, Some("FF0000".to_string()));
        assert!(slides[0].bullets[1].format.is_none()); // second item has no style
    }

    #[test]
    fn test_nested_style_inheritance() {
        let html =
            r#"<div style="color:red"><p>Red text</p><p style="color:blue">Blue text</p></div>"#;
        let slides = parse_html(html).unwrap();
        // Both paragraphs ended up as bullets on the same slide (if first h1 existed or auto-title)
        assert_eq!(slides[0].bullets.len(), 2);
        let fmt0 = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("First should have format");
        assert_eq!(fmt0.color, Some("FF0000".to_string())); // inherits red from div
        let fmt1 = slides[0].bullets[1]
            .format
            .as_ref()
            .expect("Second should have format");
        assert_eq!(fmt1.color, Some("0000FF".to_string())); // overrides to blue
    }

    #[test]
    fn test_style_on_container_div() {
        let html =
            r#"<h1>Styled Container</h1><div style="color:purple"><p>Purple paragraph</p></div>"#;
        let slides = parse_html(html).unwrap();
        let fmt = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("Should have format");
        assert_eq!(fmt.color, Some("800080".to_string()));
    }

    #[test]
    fn test_void_tag_br_does_not_affect_style() {
        let html = r#"<h1>Test</h1><p style="color:red">First<br style="color:blue">Second</p>"#;
        let slides = parse_html(html).unwrap();
        // The <br> should not push/pop style, so the paragraph should have color:red
        let fmt = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("Should have format");
        assert_eq!(fmt.color, Some("FF0000".to_string()));
    }

    #[test]
    fn test_style_content_size_default() {
        let html = "<h1>Test</h1><p>Default size</p>";
        let slides = parse_html(html).unwrap();
        assert_eq!(slides[0].content_size, Some(28));
    }

    #[test]
    fn test_background_color_as_highlight() {
        let html = r#"<h1>Test</h1><p style="background-color:yellow">Highlighted</p>"#;
        let slides = parse_html(html).unwrap();
        let fmt = slides[0].bullets[0]
            .format
            .as_ref()
            .expect("Should have format");
        assert_eq!(fmt.highlight, Some("FFFF00".to_string()));
    }
}