mirror of
https://github.com/askama-rs/askama.git
synced 2025-10-01 15:00:57 +00:00
Expose less granular API in askama crate
This commit is contained in:
parent
66dd21c857
commit
0bc54d366a
@ -11,8 +11,14 @@ pub trait Template {
|
||||
}
|
||||
}
|
||||
|
||||
pub mod filters;
|
||||
pub mod generator;
|
||||
pub mod parser;
|
||||
mod generator;
|
||||
mod parser;
|
||||
mod path;
|
||||
|
||||
pub mod filters;
|
||||
pub use path::rerun_if_templates_changed;
|
||||
pub fn build_template(path: &str, ast: &syn::DeriveInput) -> String {
|
||||
let src = path::get_template_source(path);
|
||||
let nodes = parser::parse(&src);
|
||||
generator::generate(ast, path, nodes)
|
||||
}
|
||||
|
@ -1,7 +1,18 @@
|
||||
use std::env;
|
||||
use std::fs::{self, DirEntry};
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
use std::fs::{self, DirEntry, File};
|
||||
use std::io::{self, Read};
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
pub fn get_template_source(tpl_file: &str) -> String {
|
||||
let root = env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||
let mut path = PathBuf::from(root);
|
||||
path.push("templates");
|
||||
path.push(Path::new(tpl_file));
|
||||
let mut f = File::open(path).unwrap();
|
||||
let mut s = String::new();
|
||||
f.read_to_string(&mut s).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn visit_dirs(dir: &Path, cb: &Fn(&DirEntry)) -> io::Result<()> {
|
||||
if dir.is_dir() {
|
||||
|
@ -3,9 +3,6 @@ extern crate proc_macro;
|
||||
extern crate syn;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use std::fs::File;
|
||||
use std::io::Read;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
fn get_path_from_attrs(attrs: &Vec<syn::Attribute>) -> String {
|
||||
for attr in attrs {
|
||||
@ -35,17 +32,6 @@ fn get_path_from_attrs(attrs: &Vec<syn::Attribute>) -> String {
|
||||
panic!("template annotation not found");
|
||||
}
|
||||
|
||||
fn get_template_source(tpl_file: &str) -> String {
|
||||
let root = ::std::env::var("CARGO_MANIFEST_DIR").unwrap();
|
||||
let mut path = PathBuf::from(root);
|
||||
path.push("templates");
|
||||
path.push(Path::new(tpl_file));
|
||||
let mut f = File::open(path).unwrap();
|
||||
let mut s = String::new();
|
||||
f.read_to_string(&mut s).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
#[proc_macro_derive(Template, attributes(template))]
|
||||
pub fn derive_template(input: TokenStream) -> TokenStream {
|
||||
let source = input.to_string();
|
||||
@ -57,7 +43,5 @@ pub fn derive_template(input: TokenStream) -> TokenStream {
|
||||
};
|
||||
|
||||
let path = get_path_from_attrs(&ast.attrs);
|
||||
let src = get_template_source(&path);
|
||||
let nodes = askama::parser::parse(&src);
|
||||
askama::generator::generate(&ast, &path, nodes).parse().unwrap()
|
||||
askama::build_template(&path, &ast).parse().unwrap()
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user