1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#![feature(proc_macro_diagnostic)]
#![feature(proc_macro_hygiene)]
#![feature(proc_macro_quote)]
#![feature(proc_macro_span)]
#![doc(html_root_url = "https://docs.rs/maud_macros/0.22.0")]
#![allow(clippy::needless_pass_by_value)]
extern crate proc_macro;
mod ast;
mod generate;
mod parse;
use proc_macro::{Literal, Span, Ident, TokenStream, TokenTree};
use proc_macro::quote;
type ParseResult<T> = Result<T, ()>;
#[proc_macro]
pub fn html(input: TokenStream) -> TokenStream {
expand(input)
}
#[proc_macro]
pub fn html_debug(input: TokenStream) -> TokenStream {
let expr = expand(input);
println!("expansion:\n{}", expr);
expr
}
fn expand(input: TokenStream) -> TokenStream {
let output_ident = TokenTree::Ident(Ident::new("__maud_output", Span::mixed_site()));
let size_hint = input.to_string().len();
let size_hint = TokenTree::Literal(Literal::u64_unsuffixed(size_hint as u64));
let markups = match parse::parse(input) {
Ok(markups) => markups,
Err(()) => Vec::new(),
};
let stmts = generate::generate(markups, output_ident.clone());
quote!({
extern crate maud;
let mut $output_ident = ::std::string::String::with_capacity($size_hint);
$stmts
maud::PreEscaped($output_ident)
})
}