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
use crate::{
traits::{AsFooter, MarkdownElement},
types::{header::Header, link::Link, list::List, paragraph::Paragraph},
Image,
};
use std::fmt;
use tousize::ToUsize;
/// A markdown document.
#[derive(Default)]
pub struct Markdown {
/// The markdown elements.
pub elements: Vec<Box<dyn MarkdownElement>>,
/// The markdown footer elements.
pub footers: Vec<Box<dyn MarkdownElement>>,
}
impl Markdown {
/// Creates a new default `Markdown` instance.
pub fn new() -> Self {
Self::default()
}
/// Creates a new `Markdown` instance with the given elements and footers.
pub fn with(
elements: Vec<Box<dyn MarkdownElement>>,
footers: Vec<Box<dyn MarkdownElement>>,
) -> Self {
Self { elements, footers }
}
/// Adds a header to the document.
///
/// # Arguments
///
/// - `text`: The header's text.
/// - `level`: The header's level.
///
/// # Panics
///
/// Panics if the header level is not valid (one to six inclusive).
pub fn header(&mut self, text: impl Into<String>, level: impl ToUsize) -> &mut Self {
let header = Header::from(text, level);
self.elements.push(Box::new(header));
self
}
/// Adds a header with level 1 to the document.
///
/// # Arguments
///
/// - `text`: The header's text.
pub fn header1(&mut self, text: impl Into<String>) -> &mut Self {
self.header(text, 1usize);
self
}
/// Adds a header with level 2 to the document.
///
/// # Arguments
///
/// - `text`: The header's text.
pub fn header2(&mut self, text: impl Into<String>) -> &mut Self {
self.header(text, 2usize);
self
}
/// Adds a header with level 3 to the document.
///
/// # Arguments
///
/// - `text`: The header's text.
pub fn header3(&mut self, text: impl Into<String>) -> &mut Self {
self.header(text, 3usize);
self
}
/// Adds a header with level 4 to the document.
///
/// # Arguments
///
/// - `text`: The header's text.
pub fn header4(&mut self, text: impl Into<String>) -> &mut Self {
self.header(text, 4usize);
self
}
/// Adds a header with level 5 to the document.
///
/// # Arguments
///
/// - `text`: The header's text.
pub fn header5(&mut self, text: impl Into<String>) -> &mut Self {
self.header(text, 5usize);
self
}
/// Adds a header with level 6 to the document.
///
/// # Arguments
///
/// - `text`: The header's text.
pub fn header6(&mut self, text: impl Into<String>) -> &mut Self {
self.header(text, 6usize);
self
}
/// Adds a list to the document.
///
/// # Arguments
///
/// - `list`: The list instance to add.
pub fn list(&mut self, list: List) -> &mut Self {
self.elements.push(Box::new(list));
self
}
/// Adds a link to the document.
///
/// # Arguments
///
/// - `link`: The link instance to add.
///
/// # Note
///
/// The associated footer element is added as well if the passed link is
/// marked as footer.
pub fn link(&mut self, link: Link) -> &mut Self {
if link.footer {
self.footers.push(link.as_footer());
}
self.elements.push(Box::new(link));
self
}
/// Adds an image to the document.
///
/// ### Argument
///
/// - `image`: The image instance to add.
///
/// # Note
///
/// The associated footer element is added as well if the passed link is
/// marked as footer.
pub fn image(&mut self, image: Image) -> &mut Self {
if image.footer {
self.footers.push(image.as_footer());
}
self.elements.push(Box::new(image));
self
}
/// Adds a paragraph to the document.
///
/// # Arguments
///
/// - `text`: The paragraph's text.
pub fn paragraph(&mut self, text: impl Into<String>) -> &mut Self {
self.elements.push(Box::new(Paragraph::from(text)));
self
}
/// Renders the markdown document to a `String`.
///
/// The method does render each
/// [element](struct.Markdown.structfield.elements) in order, followed by
/// each [footer](struct.Markdown.structfield.footers).
pub fn render(&self) -> String {
self.to_string()
}
}
impl fmt::Display for Markdown {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for (index, element) in self.elements.iter().enumerate() {
if index == self.elements.len() - 1 {
write!(f, "{}", element.render())?;
} else {
writeln!(f, "{}", element.render())?;
}
}
if !self.footers.is_empty() {
writeln!(f, "")?;
}
for footer in &self.footers {
writeln!(f, "{}", footer.render())?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::ImageBuilder;
#[test]
fn document_with_one_paragraph() {
assert_eq!(
Markdown::new().paragraph("Hello World").render(),
"Hello World\n"
);
}
#[test]
fn document_with_two_paragraphs() {
assert_eq!(
Markdown::new()
.paragraph("Hello World")
.paragraph("Two paragraphs")
.render(),
"Hello World\n\nTwo paragraphs\n"
);
}
#[test]
fn document_with_image() {
let mut doc = Markdown::new();
doc.image(
ImageBuilder::new()
.url("https://example.com/picture.png")
.text("A cute picture of a sandcat")
.build(),
);
assert_eq!(
doc.render(),
"\n"
);
}
#[test]
fn document_with_image_footer() {
let mut doc = Markdown::new();
doc.image(
ImageBuilder::new()
.url("https://example.com/picture.png")
.text("A cute picture of a sandcat")
.footer()
.build(),
);
assert_eq!(doc.render(), "![A cute picture of a sandcat][A cute picture of a sandcat]\n\n[A cute picture of a sandcat]: https://example.com/picture.png\n");
}
}