Giter Site home page Giter Site logo

html's Introduction

html

Type-safe HTML support for Rust

Philosophy

HTML is easy to get started with, but hard to get right. There are several hundred element kinds, element attributes, and deeply nested hierachies - with some relationships even being conditional on each other. Remembering all of this is difficult and error-prone, but luckily we don't have to remember any of this by using the type system! Rust's type system enables us to model the entire HTML spec, allowing us to catch all errors ahead of time during compilation.

This project comes in layers. The bottom-most layer is the HTML spec itself. We download it, and parse it into definition files. We then take these definitions, and use it to generate the html-sys crate. This crate is semantically correct, and knows how to render itself to string representations. We then combine html-sys with web-sys (wip) to create a higher-level HTML interface, complete with support for events. This can be used to manipulate HTML both in browser (wip) and non-browser contexts.

Examples

We can create HTML structures one-by-one:

#![recursion_limit = "512"]

use html::text_content::OrderedList;
let tree = OrderedList::builder()
    .list_item(|li| li.text("nori").class("cat"))
    .list_item(|li| li.text("chashu").class("cat"))
    .build();
let string = tree.to_string();

But we can also use Rust's native control flow structures such as loops to iterate over items and create HTML:

#![recursion_limit = "512"]

use html::text_content::OrderedList;
let mut ol = OrderedList::builder();
for name in ["hello", "world"] {
    ol.list_item(|li| li.text(name));
}
let tree = ol.build();

We can also create elements separately and append them later:

#![recursion_limit = "512"]

use html::text_content::{OrderedList, ListItem};
let mut ol = OrderedList::builder();
let li = ListItem::builder().text("hello").build();
ol.push(li);
let tree = ol.build();

Building the project

To fetch, scrape, parse, and generate code; clone the project and then run:

$ cargo xtask all

This will run all stages of the html-bindgen crate, and create a fresh copy of all generated code. If you make a change to any of the codegen stages, please re-run xtask to update the generated code.

References

Installation

$ cargo add html

Safety

This crate uses #![deny(unsafe_code)] to ensure everything is implemented in 100% Safe Rust.

Contributing

Want to join us? Check out our "Contributing" guide and take a look at some of these issues:

License

Licensed under either of Apache License, Version 2.0 or MIT license at your option.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this crate by you, as defined in the Apache-2.0 license, shall be dual licensed as above, without any additional terms or conditions.

html's People

Contributors

yoshuawuyts avatar alexmoon avatar tepperson2 avatar henkkuli avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.