pptx_to_md/
types.rs

1#[derive(Debug)]
2pub struct Presentation {
3    pub slides: Vec<Slide>,
4}
5
6#[derive(Debug)]
7pub struct Slide {
8    pub elements: Vec<SlideElement>,
9}
10
11#[derive(Debug, Clone)]
12pub enum SlideElement {
13    Text(TextElement, ElementPosition),
14    Table(TableElement, ElementPosition),
15    Image(ImageReference, ElementPosition),
16    List(ListElement, ElementPosition),
17    Unknown,
18}
19
20impl SlideElement {
21    pub fn position(&self) -> ElementPosition {
22        match self {
23            SlideElement::Text(_, pos)
24            | SlideElement::Image(_, pos)
25            | SlideElement::List(_, pos)
26            | SlideElement::Table(_, pos) => *pos,
27            SlideElement::Unknown => ElementPosition::default(),
28        }
29    }
30}
31
32#[derive(Debug, Clone)]
33pub struct ImageReference {
34    pub id: String,
35    pub target: String,
36}
37
38#[derive(Debug, Clone)]
39pub struct TextElement {
40    pub runs: Vec<Run>,
41}
42
43#[derive(Debug, Default, Clone)]
44pub struct Formatting {
45    pub bold: bool,
46    pub italic: bool,
47    pub underlined: bool,
48    pub lang: String,
49}
50
51#[derive(Debug, Clone)]
52pub struct Run {
53    pub text: String,
54    pub formatting: Formatting,
55}
56
57impl Run {
58    pub fn extract(&self) -> String {
59        self.text.to_string()
60    }
61
62    pub fn render_as_md(&self) -> String {
63        let mut has_new_line = false;
64
65        let mut result = self.extract();
66        if result.ends_with("\n") {
67            has_new_line = true;
68            result = result.replace('\n', "");
69        }
70
71        if self.formatting.bold && self.formatting.italic {
72            result = format!("***{}***", result);
73        } else {
74            if self.formatting.bold {
75                result = format!("**{}**", result);
76            }
77            if self.formatting.italic {
78                result = format!("_{}_", result);
79            }
80        }
81
82        if self.formatting.underlined {
83            result = format!("<u>{}</u>", result);
84        }
85
86        if has_new_line {
87            return format!("{}\n", result)
88        }
89        
90        result
91    }
92}
93
94#[derive(Debug, Clone)]
95pub struct TableElement {
96    pub rows: Vec<TableRow>,
97}
98
99#[derive(Debug, Clone)]
100pub struct TableRow {
101    pub cells: Vec<TableCell>,
102}
103
104#[derive(Debug, Clone)]
105pub struct TableCell {
106    pub runs: Vec<Run>,
107}
108
109#[derive(Debug, Clone)]
110pub struct ListElement {
111    pub items: Vec<ListItem>,
112}
113
114#[derive(Debug, Clone)]
115pub struct ListItem {
116    pub level: u32,
117    pub is_ordered: bool,
118    pub runs: Vec<Run>,
119}
120
121#[derive(Debug, Clone, Copy, Default, PartialEq)]
122pub struct ElementPosition {
123    pub x: i64,
124    pub y: i64,
125}