#html #html-macro #jsx #syn #rsx #proc-macro #macro

rstml

Rust templating for XML-based formats (HTML, SVG, MathML) implemented on top of proc-macro::TokenStreams

4 releases

0.11.2 Aug 14, 2023
0.11.1 Aug 14, 2023
0.11.0 Jul 18, 2023
0.10.6 May 17, 2023
0.9.0 May 12, 2023

#28 in Procedural macros

Download history 5926/week @ 2023-12-09 6358/week @ 2023-12-16 4314/week @ 2023-12-23 5121/week @ 2023-12-30 7530/week @ 2024-01-06 7216/week @ 2024-01-13 7429/week @ 2024-01-20 7532/week @ 2024-01-27 8033/week @ 2024-02-03 7165/week @ 2024-02-10 7519/week @ 2024-02-17 7060/week @ 2024-02-24 8468/week @ 2024-03-02 8718/week @ 2024-03-09 7481/week @ 2024-03-16 8770/week @ 2024-03-23

34,527 downloads per month
Used in 134 crates (16 directly)

MIT license

96KB
1.5K SLoC

rstml

crates.io page docs.rs page codecov build license: MIT Rust templating for XML-based formats (HTML, SVG, MathML) implemented on top of proc-macro::TokenStreams. Similar to JSX but for Rust (commonly named RSX).The parsed result is a nested Node structure, similar to the browser DOM, where node name and value are syn expressions to support building proc macros.

The fork of original syn-rsx repo. It was created because of various reasons:

See comparsion for more detail.

use std::convert::TryFrom;

use eyre::bail;
use quote::quote;
use rstml::{parse2, Node, NodeAttribute, NodeElement, NodeText};

// Create HTML `TokenStream`.
let tokens = quote! { <hello world>"hi"</hello> };

// Parse the tokens into a tree of `Node`s.
let nodes = parse2(tokens)?;

// Extract some specific nodes from the tree.
let Node::Element(element) = &nodes[0] else { bail!("element") };
let Node::Attribute(attribute) = &element.attributes[0] else { bail!("attribute") };
let Node::Text(text) = &element.children[0] else { bail!("text") };

// Work with the nodes.
assert_eq!(element.name.to_string(), "hello");
assert_eq!(attribute.key.to_string(), "world");
assert_eq!(String::try_from(&text.value)?, "hi");

Powered by rstml

  • html-to-string-macro - basic example of rstml usage that uses format! macro to stringify html.
  • html-node - more powerfull version of html-to-string macro that convert html representation to Rust types, which can be used to runtime introspection. Each of this type has Display and Debug implementation and therefore can be used to pretty print html node.
  • leptos - framework for web application. Rstml is used inside view/template macros and for hot-reload feature.
  • leptosfmt - a wrapper of rustfmt tool for leptos which can format html/xml code inside view macros.
  • sauron - a versatile web framework and library for building client-side and/or server-side web application.

Features

  • Not opinionated

    Every tag or attribute name is valid

    <hello world />
    
  • Text nodes

    <div>"String literal"</div>
    
  • Unquoted text nodes

    Unquoted text is supported with few limitations:

    • Only valid Rust TokenStream can be unquoted text (no single quote text is supported, no unclosed braces, etc.)
    • Unquoted text not always can save spaces. It uses Span::source_text and Span::join to retrive info about spaces, and it is not always available.
    • Quoted text near unquoted treated as diferent Node, end library user should decide whenever to preserve quotation.
    <div> Some string that is valid rust token stream </div>
    
  • Node names separated by dash, colon or double colon

    <tag-name some:attribute-key="value" />
    <tag::name attribute::key="value" />
    
  • Node names with reserved keywords

    <input type="submit" />
    
  • Doctypes, Comments and Fragments

    <!DOCTYPE html>
    <!-- "comment" -->
    <></>
    
  • Braced blocks are parsed as arbitrary Rust code

    <{ let block = "in node name position"; } />
    <div>{ let block = "in node position"; }</div>
    <div { let block = "in attribute position"; } />
    <div key={ let block = "in attribute value position"; } />
    
  • Attribute values can be any valid syn expression without requiring braces

    <div key=some::value() />
    
  • Helpful error reporting out of the box

    error: open tag has no corresponding close tag and is not self-closing
    --> examples/html-to-string-macro/tests/lib.rs:5:24
      |
    5 |     html_to_string! { <div> };
      |                        ^^^
    
  • Possibility to get the span for a whole node

    This can be used to improve error reporting, e.g.

    error: Invalid element
    --> examples/src/main.rs:14:13
       |
    14 | /             <div>
    15 | |                 "invalid node for some consumer specific reason"
    16 | |             </div>
       | |__________________^
       
    
  • Recoverable parser

    Can parse html with multiple mistakes. As result library user get array of errors that can be reported, and tree of nodes that was parsed.

     <div hello={world.} /> <!-- dot after world is invalid syn expression -->
      <>
          <div>"1"</x> <!-- incorrect closed tag -->
          <div>"2"</div>
          <div>"3"</div>
          <div {"some-attribute-from-rust-block"}/>
      </>
    

    Using this feature one can write macro in IDE friendly way. This macro will work faster (because on invalid syntax it change output slightly, instead of removing it completely, so IDE can check diff quicly). And give completion (goto definition, and other semantic related feature) more often.

  • Customization

    A ParserConfig to customize parsing behavior is available, so if you have slightly different requirements for parsing and it's not yet customizable feel free to open an issue or pull request to extend the configuration.

    One highlight with regards to customization is the transform_block configuration, which takes a closure that receives raw block content as ParseStream and lets you optionally convert it to a TokenStream. That makes it possible to have custom syntax in blocks. More details in #9

Dependencies

~0.4–0.9MB
~20K SLoC