Skip to main content

askama_minify/
lib.rs

1use proc_macro::TokenStream;
2use syn::parse_macro_input;
3
4mod args;
5mod expand;
6mod item;
7mod loader;
8mod minifier;
9
10use args::MacroArgs;
11use expand::expand_template_minify;
12use item::TemplateItem;
13
14/// Reads an Askama template at compile time, minifies it, and injects it as
15/// `#[template(source = "...", ext = "...")]`.
16///
17/// Use it with Askama's derive macro:
18///
19/// ```ignore
20/// use askama::Template;
21/// use askama_minify::template_minify;
22///
23/// #[template_minify(path = "index.html")]
24/// #[derive(Template)]
25/// struct IndexTemplate<'a> {
26///     title: &'a str,
27/// }
28/// ```
29///
30/// Template paths are resolved relative to `CARGO_MANIFEST_DIR`; if that file
31/// does not exist, `templates/<path>` is tried to match Askama's default layout.
32#[proc_macro_attribute]
33pub fn template_minify(attr: TokenStream, item: TokenStream) -> TokenStream {
34    let args = parse_macro_input!(attr with MacroArgs::parse);
35    let item = parse_macro_input!(item as TemplateItem).0;
36
37    match expand_template_minify(args, item) {
38        Ok(tokens) => tokens.into(),
39        Err(error) => error.to_compile_error().into(),
40    }
41}