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
/// Derive [`Renderable`](crate::Renderable) for a type.
///
/// # Examples
///
/// ## [`maud!`]
///
/// ```
/// use hypertext::prelude::*;
///
/// #[derive(Renderable)]
/// #[maud(span { "My name is " (self.name) "!" })]
/// pub struct Person {
/// name: String,
/// }
///
/// assert_eq!(
/// maud! { div { (Person { name: "Alice".into() }) } }
/// .render()
/// .as_inner(),
/// "<div><span>My name is Alice!</span></div>"
/// );
/// ```
///
/// ## [`rsx!`]
///
/// ```
/// use hypertext::prelude::*;
///
/// #[derive(Renderable)]
/// #[rsx(
/// <span>"My name is " (self.name) "!"</span>
/// )]
/// pub struct Person {
/// name: String,
/// }
///
/// assert_eq!(
/// rsx! { <div> (Person { name: "Alice".into() }) </div> }
/// .render()
/// .as_inner(),
/// "<div><span>My name is Alice!</span></div>"
/// );
/// ```
///
/// ## [`attribute!`]
///
/// ```
/// use hypertext::prelude::*;
///
/// #[derive(Renderable)]
/// #[attribute((self.x) "," (self.y))]
/// pub struct Coordinates {
/// x: i32,
/// y: i32,
/// }
///
/// assert_eq!(
/// maud! { div title=(Coordinates { x: 10, y: 20 }) { "Location" } }
/// .render()
/// .as_inner(),
/// r#"<div title="10,20">Location</div>"#
/// );
/// ```
pub use Renderable;
/// Generate an attribute value, returning a
/// [`LazyAttribute`](crate::LazyAttribute).
///
/// # Example
///
/// ```
/// use hypertext::prelude::*;
///
/// let attr = attribute! { "x" @for i in 0..5 { (i) } };
///
/// assert_eq!(
/// maud! { div title=attr { "Hi!" } }.render().as_inner(),
/// "<div title=\"x01234\">Hi!</div>"
/// );
/// ```
pub use attribute;
/// Generate an attribute value, borrowing the environment.
///
/// This is identical to [`attribute!`], except that it does not take
/// ownership of the environment. This is useful when you want to build
/// a [`LazyAttribute`] using some captured variables, but you still
/// want to be able to use the variables after the [`LazyAttribute`] is
/// created.
///
/// [`LazyAttribute`]: crate::LazyAttribute
pub use attribute_borrow;
/// Convert a function returning a [`Renderable`](crate::Renderable) into a
/// component.
///
/// This is a procedural macro that takes a function and generates a
/// struct that holds the function's parameters. The struct implements
/// [`Renderable`] and can be used as a component.
///
/// There are three types of parameters that are supported:
/// - `T`: Stored as `T` in the struct, and will use [`Copy`] to provide the
/// value to the function.
/// - `&T`: Stored as `T` in the struct, and will borrow the value from the
/// struct when calling the function.
/// - `&'a T`: Stored as `&'a T` in the struct, useful for borrowing unsized
/// types such as [`str`] or [`[T]`](slice) without needing to convert them to
/// their owned counterparts.
///
/// The name of the generated struct is derived from the function name by
/// converting it to PascalCase. If you would like to set a different name,
/// you can specify it as `#[component(MyComponentName)]` on the function.
///
/// The visibility of the generated struct is determined by the visibility
/// of the function. If you would like to set a different visibility,
/// you can specify it as `#[component(pub)]`,
/// `#[component(pub(crate))]`, etc. on the function.
///
/// You can combine both of these by setting an attribute like
/// `#[component(pub MyComponentName)]`.
///
/// # Example
///
/// ```
/// use hypertext::prelude::*;
///
/// #[component]
/// fn nav_bar<'a>(title: &'a str, subtitle: &String) -> impl Renderable {
/// maud! {
/// nav {
/// h1 { (title) }
/// h2 { (subtitle) }
/// }
/// }
/// }
///
/// assert_eq!(
/// maud! {
/// div {
/// NavBar title="My Nav Bar" subtitle=("My Subtitle".to_owned());
/// }
/// }
/// .render()
/// .as_inner(),
/// "<div><nav><h1>My Nav Bar</h1><h2>My Subtitle</h2></nav></div>",
/// );
/// ```
pub use component;
/// Generate HTML using [`maud`] syntax, returning a [`Lazy`](crate::Lazy).
///
/// Note that this is not a complete 1:1 port of [`maud`]'s syntax as it is
/// stricter in some places to prevent anti-patterns.
///
/// Some key differences are:
/// - Attribute keys must be simple punctuation-separated identifiers.
/// - [`id`]'s shorthand (`#`), if specified, must be the first attribute.
/// - [`class`]'s shorthand (`.`), if specified must be the second group of
/// attributes.
///
/// Additionally, adding `!DOCTYPE` at the beginning of the invocation will
/// render `"<!DOCTYPE html>"`.
///
/// For more details, see the [maud book](https://maud.lambda.xyz).
///
/// # Example
///
/// ```
/// use hypertext::prelude::*;
///
/// assert_eq!(
/// maud! {
/// div #profile title="Profile" {
/// h1 { "Alice" }
/// }
/// }
/// .render()
/// .as_inner(),
/// r#"<div id="profile" title="Profile"><h1>Alice</h1></div>"#
/// );
/// ```
///
/// [`maud`]: https://docs.rs/maud
/// [`id`]: crate::validation::GlobalAttributes::id
/// [`class`]: crate::validation::GlobalAttributes::class
pub use maud;
/// Generate HTML using [`maud!`] syntax, borrowing the environment.
///
/// This is identical to [`maud!`], except that it does not take ownership
/// of the environment. This is useful when you want to build a [`Lazy`]
/// using some captured variables, but you still want to be able to use
/// the variables after the [`Lazy`] is created.
///
/// [`Lazy`]: crate::Lazy
pub use maud_borrow;
/// Generate HTML using rsx syntax, returning a [`Lazy`](crate::Lazy).
///
/// # Example
///
/// ```
/// use hypertext::prelude::*;
///
/// assert_eq!(
/// rsx! {
/// <div id="profile" title="Profile">
/// <h1>Alice</h1>
/// </div>
/// }
/// .render()
/// .as_inner(),
/// r#"<div id="profile" title="Profile"><h1>Alice</h1></div>"#
/// );
/// ```
pub use rsx;
/// Generate HTML using [`rsx!`] syntax, borrowing the environment.
///
/// This is identical to [`rsx!`], except that it does not take ownership of
/// the environment. This is useful when you want to build a [`Lazy`] using
/// some captured variables, but you still want to be able to use the
/// variables after the [`Lazy`] is created.
///
/// [`Lazy`]: crate::Lazy
pub use rsx_borrow;