another-html-builder

Crates.ioanother-html-builder
lib.rsanother-html-builder
version
sourcesrc
created_at2024-11-11 15:12:20.965186
updated_at2024-12-09 09:24:13.08193
descriptionYet another html builder, focused on being a helper for creating elements, escaping attributes, escaping text, but not caring if the html structure is valid. It's up to the developer to test that.
homepage
repositoryhttps://github.com/jdrouet/another-html-builder
max_upload_size
id1443842
Cargo.toml error:TOML parse error at line 18, column 1 | 18 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Jérémie Drouet (jdrouet)

documentation

README

Another HTML builder

Crates.io

codecov

Average time to resolve an issue Percentage of issues still open

The goal of this builder is to be simple, to only rely on the standard library, to avoid copying values and write directly to a buffer. There is no lock involved, the ownership of the buffer is the only thing that will avoid race conditions.

Example

use another_html_builder::attribute::AttributeValue;
use another_html_builder::prelude::WriterExt;
use another_html_builder::{Body, Buffer};

// define your own custom kind of attributes
enum Lang {
    En,
    Fr,
}

impl AttributeValue for Lang {
    fn render(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(match self {
            Self::En => "en",
            Self::Fr => "fr",
        })
    }
}

// create custom components
struct Head {
    title: &'static str,
}

impl Default for Head {
    fn default() -> Self {
        Self {
            title: "Hello world!",
        }
    }
}

impl Head {
    fn render<'a, W: WriterExt>(&self, buf: Buffer<W, Body<'a>>) -> Buffer<W, Body<'a>> {
        buf.node("head")
            .content(|buf| buf.node("title").content(|buf| buf.text(self.title)))
    }
}

let head = Head::default();
let html = Buffer::default()
    .doctype()
    .node("html")
    .attr(("lang", Lang::Fr))
    .content(|buf| head.render(buf))
    .into_inner();
assert_eq!(
    html,
    "<!DOCTYPE html><html lang=\"fr\"><head><title>Hello world!</title></head></html>"
);
Commit count: 31

cargo fmt