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
use super::*;
use yaml_peg::serde::Optional;
fn slide_title(slide: &Slide) -> &str {
if !slide.title.is_empty() {
&slide.title
} else {
&slide.title_hidden
}
}
pub struct Slides {
pub slides: Vec<ChapterSlide>,
}
impl Slides {
pub fn single<T, D>(title: T, doc: D) -> Self
where
T: ToString,
D: ToString,
{
let slides = vec![ChapterSlide {
slide: Slide {
title: title.to_string(),
content: Content { doc: doc.to_string(), ..Default::default() },
..Default::default()
},
..Default::default()
}];
Self { slides }
}
}
impl ToHtml for Slides {
fn to_html(self, ctx: &Ctx) -> String {
let Self { mut slides } = self;
if !ctx.outline.is_empty() && slides.len() > 1 {
let doc = slides
.iter()
.enumerate()
.skip(1)
.map(|(i, chapter)| {
let title = slide_title(&chapter.slide);
if title.is_empty() {
String::new()
} else {
let id = if chapter.slide.id.is_empty() {
i.to_string()
} else {
chapter.slide.id.clone()
};
format!("+ [{}](#/{})\n", title, id)
+ &chapter
.sub
.iter()
.enumerate()
.map(|(j, slide)| {
let title = slide_title(slide);
if !title.is_empty() {
let id = if slide.id.is_empty() {
format!("{}/{}", i, j + 1)
} else {
slide.id.clone()
};
format!(" + [{}](#/{})\n", title, id)
} else {
String::new()
}
})
.collect::<String>()
}
})
.collect();
slides.first_mut().unwrap().sub.push(Slide {
title: ctx.outline.clone(),
id: "outline".to_string(),
content: Content { doc, ..Default::default() },
..Default::default()
});
}
slides.to_html(ctx)
}
}
#[derive(Default, serde::Deserialize)]
#[serde(default)]
pub struct ChapterSlide {
#[serde(flatten)]
pub slide: Slide,
pub sub: Vec<Slide>,
}
impl ToHtml for ChapterSlide {
fn to_html(self, ctx: &Ctx) -> String {
let Self { slide, sub } = self;
if let Some(header) = &ctx.chapter_header {
header.borrow_mut().clear();
}
let title = slide_title(&slide).to_string();
let slide = slide.to_html(ctx);
if let Some(header) = &ctx.chapter_header {
header.replace(title);
}
format!("<section>\n{}</section>", slide + &sub.to_html(ctx))
}
}
#[derive(Default, serde::Deserialize)]
#[serde(default)]
pub struct Slide {
pub title: String,
#[serde(rename = "title-hidden")]
pub title_hidden: String,
#[serde(rename = "title-only")]
pub title_only: String,
#[serde(flatten)]
pub content: Content,
pub note: String,
pub background: Optional<Background>,
pub class: String,
pub id: String,
pub attr: String,
#[serde(rename = "auto-animate")]
pub auto_animate: bool,
pub trans: String,
#[serde(rename = "bg-trans")]
pub bg_trans: String,
}
impl ToHtml for Slide {
fn to_html(self, ctx: &Ctx) -> String {
let Self {
title,
title_hidden: _,
title_only,
content,
note,
background,
class,
id,
attr,
auto_animate,
trans,
bg_trans,
} = self;
let background = match background {
Optional::Bool(false) => String::new(),
Optional::Bool(true) => ctx.background.clone(),
Optional::Some(bg) => bg.to_html(ctx),
};
let auto_animate = if ctx.auto_animate || auto_animate {
" data-auto-animate"
} else {
""
};
let data = background
+ &class.wrap(" class=\"", "\"")
+ &id.wrap(" id=\"", "\"")
+ &trans.wrap(" data-transition=\"", "\"")
+ &bg_trans.wrap(" data-background-transition=\"", "\"")
+ auto_animate
+ &attr.wrap(" ", "");
let content = md2html(&title.wrap("# ", ""))
+ &md2html(&title_only.wrap("# ", ""))
+ &content.to_html(ctx)
+ &md2html(¬e).wrap("<aside class=\"notes\">", "</aside>\n");
let header = ctx
.chapter_header
.as_ref()
.map(|s| s.borrow().wrap("<div class=\"chapter-header\">", "</div>"))
.unwrap_or_default();
ctx.frag.set(0);
format!("<section{}>\n{}{}</section>", data, content, header)
}
}