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
//! `gen-html` is a templating library for generating HTML from Rust.
//!
//! # Features
//!
//! - **Fast** — [`html!`] macro generates code that is as fast as writing to a string by hand.
//! - **Conditional rendering** — you can use `if`, `for` and `match` inside your templates.
//! - **Automatic escaping**, however you can opt-out using [`Raw<T>`].
//! - **Type safety** — HTML tags and attributes are checked at compile time.
//! - Integration with the rust web ecosystem (`axum`, `actix-web`).
//!
//! # Example
//!
//! ```
//! use gen_html::html;
//!
//! let markup = html! {
//! for i in 1..=3 {
//! span { (i.to_string()) }
//! }
//! };
//!
//! println!("{}", markup);
//! # assert_eq!(markup.to_string(), "<span>1</span><span>2</span><span>3</span>");
//! ```
//!
//! The [`html!`] macro roughly expands to this code.
//!
//! ```
//! use gen_html::{Render, render_fn};
//!
//! let markup = render_fn(|f| {
//! for i in 1..=3 {
//! f.write_str("<span>")?;
//! (&i.to_string()).render_to(f)?;
//! f.write_str("</span>")?;
//! }
//! Ok(())
//! });
//!
//! /* ... */
//! # assert_eq!(markup.render().0, "<span>1</span><span>2</span><span>3</span>");
//! ```
/// The `<!DOCTYPE html>` string literal.
///
/// # Example
///
/// ```
/// use gen_html::{DOCTYPE, html};
///
/// # let markup =
/// html! {
/// (DOCTYPE)
/// h1 { "hello world" }
/// }
/// # ;
/// # assert_eq!(markup.to_string(), "<!DOCTYPE html><h1>hello world</h1>");
/// ```
pub const DOCTYPE: = Raw;
/// Generate HTML with `maud`-like syntax.
///
/// The `html!` macro allows you to write HTML using a `maud`-like syntax while
/// being as efficient as writing to a [`String`] by hand.
///
/// # HTML elements
///
/// Elements are written using their tag name followed by curly braces
/// containing their children.
///
/// ```
/// # use gen_html::html;
/// # let markup =
/// html! {
/// h1 { "Hello world" }
/// p { "This is a paragraph." }
/// }
/// # ;
///
/// # assert_eq!(
/// # markup.to_string(),
/// # "<h1>Hello world</h1><p>This is a paragraph.</p>"
/// # );
/// ```
///
/// Void elements (elements without closing tags) use a trailing semicolon.
///
/// ```
/// # use gen_html::html;
/// # let markup =
/// html! {
/// "First line"
/// br;
/// "Second line"
/// }
/// # ;
/// # assert_eq!(markup.to_string(), "First line<br>Second line");
/// ```
///
/// # Attributes
///
/// Attributes are written using `name: value`.
///
/// ```
/// # use gen_html::html;
/// # let markup =
/// html! {
/// a href: "https://example.com" { "Visit site" }
/// }
/// # ;
/// # assert_eq!(
/// # markup.to_string(),
/// # r#"<a href="https://example.com">Visit site</a>"#
/// # );
/// ```
///
/// Boolean attributes may omit the value.
///
/// ```
/// # use gen_html::html;
/// # let markup =
/// html! {
/// input r#type: "checkbox" checked;
/// }
/// # ;
/// # assert_eq!(
/// # markup.to_string(),
/// # r#"<input type="checkbox" checked>"#
/// # );
/// ```
///
/// # Shorthand syntax
///
/// Instead of writing `id` and `class` you may use `@` and `.` respectively.
///
/// - `@"container"` ==> `id: "container"`
/// - `."flex gap-2"` ==> `class: "flex gap-2"`
///
/// ```
/// # use gen_html::html;
/// # let markup =
/// html! {
/// div @"container" ."flex gap-2" { "content" }
/// }
/// # ;
/// # assert_eq!(
/// # markup.to_string(),
/// # r#"<div id="container" class="flex gap-2">content</div>"#
/// # );
/// ```
///
/// # Inserting expressions
///
/// Use `(expr)` to insert any Rust expression implementing [`Render`].
///
/// ```
/// # use gen_html::html;
/// let name = "Alice";
///
/// # let markup =
/// html! {
/// p { "Hello " (name) "!" }
/// }
/// # ;
/// # assert_eq!(markup.to_string(), "<p>Hello Alice!</p>");
/// ```
///
/// Expressions that implement [`Value`] may be used inside attributes. See its documentation for more details.
///
/// ```
/// # use gen_html::html;
/// let path = "/assets/logo.svg";
/// let description = "Company logo";
///
/// # let markup =
/// html! {
/// img src: (path) alt: (description);
/// }
/// # ;
/// # assert_eq!(
/// # markup.to_string(),
/// # r#"<img src="/assets/logo.svg" alt="Company logo">"#
/// # );
/// ```
///
/// # Control structures
///
/// ## `if`
///
/// ```
/// # use gen_html::html;
/// let logged_in = true;
///
/// # let markup =
/// html! {
/// if logged_in {
/// p { "Welcome back!" }
/// } else {
/// p { "Please log in." }
/// }
/// }
/// # ;
/// # assert_eq!(markup.to_string(), "<p>Welcome back!</p>");
/// ```
///
/// ## `if let`
///
/// ```
/// # use gen_html::html;
/// let name = Some("Damian");
///
/// # let markup =
/// html! {
/// if let Some(name) = name {
/// (name)
/// } else {
/// "stranger"
/// }
/// }
/// # ;
/// # assert_eq!(markup.to_string(), "Damian");
/// ```
///
/// ## `match`
///
/// ```
/// # use gen_html::html;
/// let status = 404;
///
/// # let markup =
/// html! {
/// match status {
/// 200 => p { "OK" },
/// 404 => p { "Not found" },
/// _ => p { "Unknown status" }
/// }
/// }
/// # ;
/// # assert_eq!(markup.to_string(), "<p>Not found</p>");
/// ```
///
/// ## `for`
///
/// ```
/// # use gen_html::html;
/// let shows = ["Breaking Bad", "Planet Earth II", "Chernobyl"];
///
/// # let markup =
/// html! {
/// h1 { "Popular TV shows" }
/// ul {
/// for title in shows {
/// li { (title) }
/// }
/// }
/// }
/// # ;
/// # assert_eq!(
/// # markup.to_string(),
/// # "<h1>Popular TV shows</h1><ul><li>Breaking Bad</li><li>Planet Earth II</li><li>Chernobyl</li></ul>"
/// # );
/// ```
///
/// ## `let`
///
/// ```
/// # use gen_html::html;
/// # let markup =
/// html! {
/// let src = "/assets/welcome.svg";
/// img src: (src);
///
/// // You may also provide the type:
/// let src: &str = "/assets/welcome.svg";
///
/// // let Some(src) = ... else { ... };
/// // ERROR: Only irrefutable patterns are supported
/// }
/// # ;
/// # assert_eq!(
/// # markup.to_string(),
/// # "<img src=\"/assets/welcome.svg\">"
/// # );
/// ```
pub use html;
pub use Escaped;
pub use ;
pub use Value;