maud_macros/
lib.rs

1#![doc(html_root_url = "https://docs.rs/maud_macros/0.26.0")]
2// TokenStream values are reference counted, and the mental overhead of tracking
3// lifetimes outweighs the marginal gains from explicit borrowing
4#![allow(clippy::needless_pass_by_value)]
5
6extern crate proc_macro;
7
8mod ast;
9mod escape;
10mod generate;
11mod parse;
12
13use proc_macro2::{Ident, Span, TokenStream, TokenTree};
14use proc_macro_error::proc_macro_error;
15use quote::quote;
16
17#[proc_macro]
18#[proc_macro_error]
19pub fn html(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
20    expand(input.into()).into()
21}
22
23fn expand(input: TokenStream) -> TokenStream {
24    let output_ident = TokenTree::Ident(Ident::new("__maud_output", Span::mixed_site()));
25    // Heuristic: the size of the resulting markup tends to correlate with the
26    // code size of the template itself
27    let size_hint = input.to_string().len();
28    let markups = parse::parse(input);
29    let stmts = generate::generate(markups, output_ident.clone());
30    quote!({
31        extern crate alloc;
32        let mut #output_ident = alloc::string::String::with_capacity(#size_hint);
33        #stmts
34        maud::PreEscaped(#output_ident)
35    })
36}