syn-rsx
syn
-powered parser for JSX-like TokenStream
s, aka 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.
use TryFrom;
use bail;
use quote;
use ;
// Create HTML `TokenStream`.
let tokens = quote! ;
// Parse the tokens into a tree of `Node`s.
let nodes = parse2?;
// Extract some specific nodes from the tree.
let Element = &nodes else ;
let Attribute = &element.attributes else ;
let Text = &element.children else ;
// Work with the nodes.
assert_eq!;
assert_eq!;
assert_eq!;
You might want to check out the html-to-string-macro example as well.
Features
-
Not opinionated
Every tag or attribute name is valid
-
Text nodes
Support for unquoted text is planned.
"String literal"
-
Node names separated by dash, colon or double colon
-
Node names with reserved keywords
-
Doctypes, Comments and Fragments
<!-- "comment" --> <></>
-
Braced blocks are parsed as arbitrary Rust code
<{ let block = "in node name position"; } /> { let block = "in node position"; }
-
Attribute values can be any valid syn expression without requiring braces
-
Helpful error reporting out of the box
error: open tag has no corresponding close tag and is not self-closing -/html-to-string-macro/tests/lib.rs:5:24 | 5 | html_to_string! ;
-
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 asParseStream
and lets you optionally convert it to aTokenStream
. That makes it possible to have custom syntax in blocks. More details in #9