#html-xml #xml #svg #markup #html #tags

tagger

Write SVG / HTML / XML programmatically

33 releases (15 stable)

4.3.5 Jul 8, 2023
4.3.4 Nov 26, 2022
4.3.3 Mar 18, 2022
3.3.2 Dec 31, 2021
0.7.1 Mar 8, 2021

#229 in Encoding

Download history 402/week @ 2023-12-13 180/week @ 2023-12-20 216/week @ 2023-12-27 419/week @ 2024-01-03 337/week @ 2024-01-10 507/week @ 2024-01-17 728/week @ 2024-01-24 798/week @ 2024-01-31 768/week @ 2024-02-07 362/week @ 2024-02-14 346/week @ 2024-02-21 962/week @ 2024-02-28 476/week @ 2024-03-06 592/week @ 2024-03-13 305/week @ 2024-03-20 688/week @ 2024-03-27

2,204 downloads per month
Used in 4 crates (2 directly)

MIT license

17KB
342 lines

Before you try

I recommend you use the tagu crate instead of this crate.

Overview

Build xml / html / svg programmatically using element building blocks. Instead of using a templating engine, write data/markup that 'looks like' rust.

Find it on github and crates.io. Docs at docs.rs.

Tagger also provides functionality to build svg paths and polyline attribute data.

Tagger additionally protects against XML escaping by replacing xml escape characters with its encoded value.

Example

fn main() -> std::fmt::Result {
    let width = 100.0;
    let height = 100.0;

    let mut w = tagger::new(tagger::upgrade_write(std::io::stdout()));

    w.elem("svg", |d| {
        d.attr("xmlns", "http://www.w3.org/2000/svg")?;
        d.attr("viewBox", format_args!("0 0 {} {}", width, height))
    })?
    .build(|w| {
        w.single("rect", |d| {
            d.attr("x1", 0)?;
            d.attr("y1", 0)?;
            d.attr("rx", 20)?;
            d.attr("ry", 20)?;
            d.attr("width", width)?;
            d.attr("height", height)?;
            d.attr("style", "fill:blue")
        })?;

        w.elem("style", tagger::no_attr())?
            .build(|w| w.put_raw(".test{fill:none;stroke:white;stroke-width:3}"))?;

        w.elem("g", |d| d.attr("class", "test"))?.build(|w| {
            for r in (0..50).step_by(10) {
                w.single("circle", |w| {
                    w.attr("cx", 50.0)?;
                    w.attr("cy", 50.0)?;
                    w.attr("r", r)
                })?;
            }
            Ok(())
        })
    })
}

Output

demo

No runtime deps