-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
105 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,50 @@ | ||
# `hypertext` | ||
|
||
```rs | ||
hypertext::maud! { | ||
"see" a href="https://docs.rs/hypertext" { "docs.rs" } "for documentation." | ||
} | ||
``` | ||
A blazing fast type-checked HTML macro crate. | ||
|
||
## Features | ||
|
||
### Speed | ||
|
||
The macros generate code that is as fast as writing HTML to a string by hand. The macro automatically combines what would be multiple `push_str` calls into one if there is no dynamic content between them. | ||
|
||
The crate gives extreme importance to lazy rendering and minimizing allocation, so it will only render the HTML to a string when you finally call the render function at the end. | ||
|
||
### Type-Checking | ||
|
||
All macros are validated at compile time, so you can't ever misspell an element/attribute or use invalid attributes. All of this validation has absolutely no runtime cost however, and it is just used for developer experience. | ||
|
||
## Multiple Syntaxes | ||
|
||
The crate provides a macro for writing rsx-style code, and another macro for writing [maud](https://maud.lambda.xyz)-style code, and lets you decide whichever one you like more. | ||
- Type checking for element names/attributes | ||
- Completely extensible for use with non-standard elements/attributes | ||
- `#![no_std]` support | ||
- Automatic escaping | ||
- Lazy rendering by default to avoid multiple allocations | ||
- Results in outstanding performance in cases of nested documents, which other libraries may falter in | ||
|
||
## Example | ||
|
||
```rust | ||
use hypertext::{html_elements, GlobalAttributes, RenderIterator}; | ||
|
||
let shopping_list = &vec!["milk", "eggs", "bread"]; | ||
|
||
let shopping_list_maud = hypertext::maud! { | ||
div { | ||
h1 { "Shopping List" } | ||
ul { | ||
@for (&item, i) in shopping_list.iter().zip(1..) { | ||
li.item { | ||
input #{ "item-" (i) } type="checkbox"; | ||
label for={ "item-" (i) } { (item) } | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
// or, alternatively: | ||
|
||
let shopping_list_rsx = hypertext::rsx! { | ||
<div> | ||
<h1>Shopping List</h1> | ||
<ul> | ||
{ shopping_list.iter().zip(1..).map(|(&item, i)| hypertext::rsx! { | ||
<li class="item"> | ||
<input id=format!("item-{i}") type="checkbox"> | ||
<label for=format!("item-{i}")>{ item }</label> | ||
</li> | ||
}).render_all() } | ||
</ul> | ||
</div> | ||
}; | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#[test] | ||
fn readme() { | ||
use hypertext::{html_elements, GlobalAttributes, RenderIterator}; | ||
|
||
let shopping_list = &vec!["milk", "eggs", "bread"]; | ||
|
||
let shopping_list_maud = hypertext::maud! { | ||
div { | ||
h1 { "Shopping List" } | ||
ul { | ||
@for (&item, i) in shopping_list.iter().zip(1..) { | ||
li.item { | ||
input #{ "item-" (i) } type="checkbox"; | ||
label for={ "item-" (i) } { (item) } | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
|
||
// or, alternatively: | ||
|
||
let shopping_list_rsx = hypertext::rsx! { | ||
<div> | ||
<h1>Shopping List</h1> | ||
<ul> | ||
{ shopping_list.iter().zip(1..).map(|(&item, i)| hypertext::rsx! { | ||
<li class="item"> | ||
<input id=format!("item-{i}") type="checkbox"> | ||
<label for=format!("item-{i}")>{ item }</label> | ||
</li> | ||
}).render_all() } | ||
</ul> | ||
</div> | ||
}; | ||
|
||
assert_eq!(shopping_list_maud.render(), shopping_list_rsx.render()); | ||
} |