Git Product home page Git Product logo

build_html's Introduction

Rust

build_html: Rust HTML Generation

This crate allows HTML strings to be generated from within Rust code using the Builder pattern. In general, this library will attempt to create a 'minimal' version of the HTML; tags are concatinated without additional spaces being added. For this reason, this library may not be optimal for use in applications intending to display the raw HTML.

The main application for this crate, and its impetus, is providing simple server-side rendering for a web application. However, it could also be used to generate HTML reports from within other applications.

This crate is written in purely safe Rust with no production dependencies.

Use

Everything you need to use this crate has been exported from the crate root. This means that you can get easy access to every element using the import: use build_html::*.

While the easiest way to use the library is with a root import, you can also import items piecemeal and prefix types with the package name. Note that the traits Html and HtmlContainer must be in scope for the library to be useful:

use build_html::{self, Html, HtmlContainer};

let page = build_html::HtmlPage::new()
    .with_paragraph("Some Text")
    .to_html_string();

This project was created with the builder pattern in mind. To create an HTML document, start with an HtmlPage, and build up the document with chained method calls. Once the document is built up, convert it to a String using to_html_string(). The Display trait is also implemented for all Html elements, meaning these elements can be converted into HTML using format!() as well.

While adding content, required attributes are specified as method parameters. These parameters are generic on the ToString trait, allowing many useful types to be used. Additional optional parameters (such as id or class attributes) can be added by passing in some type implementing the IntoIterator trait which has items which are 2-tuples of objects implementing ToString. This means that you can use anything from a HashMap<String, String> to a Vec<(&str, &str)> to (new with Rust 1.53) arrays of 2-tuples of static strings.

use build_html::*;

let html: String = HtmlPage::new()
    .with_title("My Page")
    .with_header(1, "Main Content:")
    .with_container(
        Container::new(ContainerType::Article)
            .with_attributes([("id", "article1")])
            .with_header_attr(2, "Hello, World", [("id", "article-head"), ("class", "header")])
            .with_paragraph("This is a simple HTML demo")
    )
    .to_html_string();

produces a string equivalent to:

<!DOCTYPE html>
<html>
    <head>
        <title>My Page</title>
    </head>
    <body>
        <h1>Main Content:</h1>
        <article id="article1">
            <h2 id="article-head" class="header">Hello World</h2>
            <p>This is a simple HTML demo</p>
        </article>
    </body>
</html>

Supported Features

This library does not intend to support all tags and features from the HTML specification. However, an effort has been made to provide the most common features for a majority of use cases.

Currently, this library supports adding the following HTML features / tags:

Body Elements

  • Containers (<article>, <div>, <main>)
  • Headers (<h1>, <h2>, <h3>, ... )
  • Images (<img>)
  • Links (<a>)
  • Lists (<li>, <ol>)
  • Paragraphs (<p>)
  • Preformatted Text (<pre>)
  • Tables (<table>)

Header Elements

  • Links (<link>)
  • Meta (<meta>)
  • Scripts (<script>)
  • Style (<style>)
  • Title (<title>)

Extensibility

In the event that you require additional tags or types not implemented in this library, you can achieve this using one of two escape hatches.

  1. You can implement your own HTML types and add them using HtmlContainer::add_html
  2. You can add one-off raw content using HtmlContainer::add_raw

Acknowledgment

This project was made possible thanks to the following great projects:

License

This project is licensed under the MIT license.

Copyright (C) 2020-22 Joseph Skubal

build_html's People

Contributors

skubalj 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.