2021-01-09 18:01:17 +13:00
|
|
|
#![doc(html_root_url = "https://docs.rs/maud_macros/0.22.2")]
|
2018-04-16 20:27:41 +12:00
|
|
|
// TokenStream values are reference counted, and the mental overhead of tracking
|
|
|
|
// lifetimes outweighs the marginal gains from explicit borrowing
|
2018-09-03 19:43:53 +12:00
|
|
|
#![allow(clippy::needless_pass_by_value)]
|
2018-04-16 20:27:41 +12:00
|
|
|
|
2017-07-07 22:59:20 +12:00
|
|
|
extern crate proc_macro;
|
|
|
|
|
2018-05-17 20:38:44 +12:00
|
|
|
mod ast;
|
|
|
|
mod generate;
|
2014-12-18 18:57:55 +13:00
|
|
|
mod parse;
|
2014-12-17 21:11:56 +13:00
|
|
|
|
2021-01-09 17:15:43 +13:00
|
|
|
use proc_macro2::{Ident, Span, TokenStream, TokenTree};
|
2020-09-27 21:36:27 +10:00
|
|
|
use proc_macro_error::proc_macro_error;
|
2020-07-12 11:42:14 +00:00
|
|
|
use quote::quote;
|
2016-01-01 11:43:59 +13:00
|
|
|
|
2017-07-07 22:59:20 +12:00
|
|
|
#[proc_macro]
|
2020-08-30 23:28:49 +03:00
|
|
|
#[proc_macro_error]
|
2020-07-12 11:42:14 +00:00
|
|
|
pub fn html(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
|
|
expand(input.into()).into()
|
2016-08-15 20:32:39 +12:00
|
|
|
}
|
|
|
|
|
2017-07-07 22:59:20 +12:00
|
|
|
#[proc_macro]
|
2020-08-30 23:28:49 +03:00
|
|
|
#[proc_macro_error]
|
2020-07-12 11:42:14 +00:00
|
|
|
pub fn html_debug(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
|
|
|
|
let expr = expand(input.into());
|
2017-08-12 16:21:13 +12:00
|
|
|
println!("expansion:\n{}", expr);
|
2020-07-12 11:42:14 +00:00
|
|
|
expr.into()
|
2017-08-12 16:21:13 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expand(input: TokenStream) -> TokenStream {
|
2021-01-09 17:15:43 +13:00
|
|
|
let output_ident = TokenTree::Ident(Ident::new("__maud_output", Span::mixed_site()));
|
2017-08-12 16:21:13 +12:00
|
|
|
// Heuristic: the size of the resulting markup tends to correlate with the
|
|
|
|
// code size of the template itself
|
2018-04-16 01:06:19 -07:00
|
|
|
let size_hint = input.to_string().len();
|
2020-08-31 09:51:21 +03:00
|
|
|
let markups = parse::parse(input);
|
2018-05-17 20:38:44 +12:00
|
|
|
let stmts = generate::generate(markups, output_ident.clone());
|
2017-08-12 16:21:13 +12:00
|
|
|
quote!({
|
|
|
|
extern crate maud;
|
2020-07-12 11:42:14 +00:00
|
|
|
let mut #output_ident = ::std::string::String::with_capacity(#size_hint);
|
|
|
|
#stmts
|
|
|
|
maud::PreEscaped(#output_ident)
|
2017-08-12 16:21:13 +12:00
|
|
|
})
|
2015-04-13 20:20:02 +12:00
|
|
|
}
|