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
//! Defines the `HtmlContainer` Trait

use crate::attributes::Attributes;
use crate::content::BodyContent;
use crate::Container;
use crate::Html;
use crate::Table;
use std::collections::HashMap;
use std::fmt::Display;

/// An HTML element that can contain other HTML elements
///
/// This trait implements the majority of the specific "add x" methods, requiring implementors
/// to add only one method: [`add_html()`](crate::HtmlContainer::add_html)
pub trait HtmlContainer: Html + Sized {
    /// Adds the specified HTML element to this container
    ///
    /// This method should *probably* not be used directly by client programs. For most
    /// applications, the `add_x()` methods will provide more safety.
    fn add_html(self, html: Box<dyn Html>) -> Self;

    /// Nest the specified container within this container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// let content = Container::default()
    ///     .add_header(1, "Content Outside")
    ///     .add_container(
    ///         Container::new(ContainerType::Main)
    ///             .add_paragraph("Content Inside")
    ///     )
    ///     .to_html_string();
    ///
    /// assert_eq!(
    ///     content,
    ///     "<div><h1>Content Outside</h1><main><p>Content Inside</p></main></div>"
    /// );
    /// ```
    fn add_container(self, container: Container) -> Self {
        self.add_html(Box::new(container))
    }

    /// Nest the specified `Table` within this container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// let content = Container::default()
    ///     .add_table(
    ///         Table::from(&[
    ///             [1, 2, 3],
    ///             [4, 5, 6]
    ///         ])
    ///         .add_header_row(&['A', 'B', 'C'])
    ///     )
    ///     .to_html_string();
    ///
    /// assert_eq!(
    ///     content,
    ///     concat!(
    ///         "<div><table><thead>",
    ///         "<tr><th>A</th><th>B</th><th>C</th></tr>",
    ///         "</thead><tbody>",
    ///         "<tr><td>1</td><td>2</td><td>3</td></tr>",
    ///         "<tr><td>4</td><td>5</td><td>6</td></tr>",
    ///         "</tbody></table></div>"
    ///     )
    /// );
    /// ```
    fn add_table(self, table: Table) -> Self {
        self.add_html(Box::new(table))
    }

    /// Adds a header tag with the designated level to this container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// let content = Container::default()
    ///     .add_header(1, "Header Text")
    ///     .to_html_string();
    ///
    /// assert_eq!(content, r#"<div><h1>Header Text</h1></div>"#)
    /// ```
    fn add_header<T: Display>(self, level: u8, text: T) -> Self {
        let content = BodyContent::Header {
            level,
            content: format!("{}", text),
            attr: Attributes::default(),
        };
        self.add_html(Box::new(content))
    }

    /// Adds a header tag with the designated level and attributes to this container.
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// # use maplit::hashmap;
    /// let content = Container::default()
    ///     .add_header_attr(1, "Header Text", hashmap! {"id" => "main-header"})
    ///     .to_html_string();
    ///
    /// assert_eq!(content, r#"<div><h1 id="main-header">Header Text</h1></div>"#)
    /// ```
    fn add_header_attr<T: Display>(self, level: u8, text: T, attr: HashMap<&str, &str>) -> Self {
        let content = BodyContent::Header {
            level,
            content: format!("{}", text),
            attr: attr.into(),
        };
        self.add_html(Box::new(content))
    }

    /// Adds an `<img>` tag to this container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// let content = Container::default()
    ///     .add_image("myimage.png", "a test image")
    ///     .to_html_string();
    ///
    /// assert_eq!(content, r#"<div><img src="myimage.png" alt="a test image"></div>"#)
    /// ```
    fn add_image(self, src: &str, alt: &str) -> Self {
        let content = BodyContent::Image {
            src: src.into(),
            alt: alt.into(),
            attr: Attributes::default(),
        };
        self.add_html(Box::new(content))
    }

    /// Adds an `<img>` tag with the specified attributes to this container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// # use maplit::hashmap;
    /// let content = Container::default()
    ///     .add_image_attr("myimage.png", "a test image", hashmap! {"id" => "sample-image"})
    ///     .to_html_string();
    ///
    /// assert_eq!(
    ///     content,
    ///     r#"<div><img src="myimage.png" alt="a test image" id="sample-image"></div>"#
    /// )
    /// ```
    fn add_image_attr(self, src: &str, alt: &str, attr: HashMap<&str, &str>) -> Self {
        let content = BodyContent::Image {
            src: src.into(),
            alt: alt.into(),
            attr: attr.into(),
        };
        self.add_html(Box::new(content))
    }

    /// Adds an `<a>` tag to this container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// let content = Container::default()
    ///     .add_link("https://rust-lang.org/", "Rust Homepage")
    ///     .to_html_string();
    ///
    /// assert_eq!(content, r#"<div><a href="https://rust-lang.org/">Rust Homepage</a></div>"#)
    /// ```
    fn add_link<T: Display>(self, href: &str, text: T) -> Self {
        let content = BodyContent::Link {
            href: href.into(),
            content: format!("{}", text),
            attr: Attributes::default(),
        };
        self.add_html(Box::new(content))
    }

    /// Adds an `<a>` tag with the specified attributes to this container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// # use maplit::hashmap;
    /// let content = Container::default()
    ///     .add_link_attr("https://rust-lang.org/", "Rust Homepage", hashmap! {"class" => "links"})
    ///     .to_html_string();
    ///
    /// assert_eq!(
    ///     content,
    ///     r#"<div><a href="https://rust-lang.org/" class="links">Rust Homepage</a></div>"#
    /// )
    /// ```
    fn add_link_attr<T: Display>(self, href: &str, text: T, attr: HashMap<&str, &str>) -> Self {
        let content = BodyContent::Link {
            href: href.into(),
            content: format!("{}", text),
            attr: attr.into(),
        };
        self.add_html(Box::new(content))
    }

    /// Adds a `<p>` tag element to this Container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// let content = Container::default()
    ///     .add_paragraph("This is sample paragraph text")
    ///     .to_html_string();
    ///
    /// assert_eq!(content, r#"<div><p>This is sample paragraph text</p></div>"#)
    /// ```
    fn add_paragraph<T: Display>(self, text: T) -> Self {
        let content = BodyContent::Paragraph {
            content: format!("{}", text),
            attr: Attributes::default(),
        };
        self.add_html(Box::new(content))
    }

    /// Adds a `<p>` tag element with the specified attributes to this Container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// # use maplit::hashmap;
    /// let content = Container::default()
    ///     .add_paragraph_attr("This is sample paragraph text", hashmap! {"class" => "text"})
    ///     .to_html_string();
    ///
    /// assert_eq!(content, r#"<div><p class="text">This is sample paragraph text</p></div>"#)
    /// ```
    fn add_paragraph_attr<T: Display>(self, text: T, attr: HashMap<&str, &str>) -> Self {
        let content = BodyContent::Paragraph {
            content: format!("{}", text),
            attr: attr.into(),
        };
        self.add_html(Box::new(content))
    }

    /// Adds a `<pre>` tag element to this container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// let content = Container::default()
    ///     .add_preformatted("This | is   preformatted => text")
    ///     .to_html_string();
    ///
    /// assert_eq!(content, r#"<div><pre>This | is   preformatted => text</pre></div>"#)
    /// ```
    fn add_preformatted<T: Display>(self, text: T) -> Self {
        let content = BodyContent::Preformatted {
            content: format!("{}", text),
            attr: Attributes::default(),
        };
        self.add_html(Box::new(content))
    }

    /// Adds a `<pre>` tag element with the specified attributes to this container
    ///
    /// # Example
    /// ```
    /// # use build_html::*;
    /// # use maplit::hashmap;
    /// let content = Container::default()
    ///     .add_preformatted_attr("This | is   preformatted => text", hashmap! {"id" => "code"})
    ///     .to_html_string();
    ///
    /// assert_eq!(content, r#"<div><pre id="code">This | is   preformatted => text</pre></div>"#)
    /// ```
    fn add_preformatted_attr<T: Display>(self, text: T, attr: HashMap<&str, &str>) -> Self {
        let content = BodyContent::Preformatted {
            content: format!("{}", text),
            attr: attr.into(),
        };
        self.add_html(Box::new(content))
    }
}