Expand description
an html rendering crate. you probably shouldn’t use it, but if you do want to use it it would look something like this:
use supertext::{HtmlWriter, HtmlSink, a, tr, td, th, table, p, h1, style, meta};
use supertext::Sink;
pub fn example(items: &[(String, String)]) -> Result<String, std::fmt::Error> {
let mut out = String::new();
let mut writer = HtmlWriter::new(&mut out);
let mut html = HtmlSink::root(&mut writer);
let mut head = html.head();
{
let mut style = head.open_tag(style());
style.rule(".table { font-family: monospace; border: 1px solid black; }")?;
style.rule(".row-item { padding-left: 4px; padding-right: 4px; border-right: 1px solid black; }")?;
style.rule(".odd-row { background: #eee; }")?;
style.rule(".even-row { background: #ddd; }")?;
}
head.append(meta().property("og:type").content("website"));
head.close();
let mut body = html.body();
{
let mut header = body.open_tag(h1());
header.text("an example page that is super cool")?;
}
{
let mut description = body.open_tag(p());
match items.len() {
0 => description.text("no items")?,
1 => description.text("one item")?,
o => description.text(&format!("{} items", o))?,
};
}
let mut table = body.open_tag(table().class("table"));
{
let mut header = table.header();
let th = th().class("row-item");
let mut add_cell = |name: &'static str| header.open_tag(th.clone()).text(name);
add_cell("key")?;
add_cell("value")?;
}
let td = td().class("row-item");
for (i, item) in items.iter().enumerate() {
let mut row = table.row(tr().class(["even-row", "odd-row"][i % 2]));
{
let mut elem = row.open_tag(td.clone());
elem.open_tag(a().href(&format!("/{}", item.0))).text(&item.0)?;
}
{
row.open_tag(td.clone()).text(&item.1)?;
}
}
table.close();
body.close();
html.close();
Ok(out)
}Structs§
- Body
Sink - Head
Sink - Html
Mini Writer - a general adapter of
std::fmt::Writeimplementations for HTML formatting. this does not add unnecessary whitespace. - Html
Sink - Html
Writer - a general adapter of
std::fmt::Writeimplementations for HTML formatting. this produces text that uses four space indentation, and tracks indentation levels based on subtag nesting.