mirror of
https://github.com/askama-rs/askama.git
synced 2025-09-30 22:41:13 +00:00
Move get_template_source() into askama_derive
This commit is contained in:
parent
c8d9a6326d
commit
f9f4f57221
@ -317,8 +317,6 @@
|
||||
extern crate askama_derive;
|
||||
extern crate askama_shared as shared;
|
||||
|
||||
use shared::path;
|
||||
|
||||
use std::fs::{self, DirEntry};
|
||||
use std::io;
|
||||
use std::path::Path;
|
||||
|
@ -1,7 +1,7 @@
|
||||
use super::Context;
|
||||
use super::{get_template_source, Context};
|
||||
use input::TemplateInput;
|
||||
use parser::{self, Cond, Expr, MatchParameter, MatchVariant, Node, Target, When, WS};
|
||||
use shared::{filters, path};
|
||||
use shared::filters;
|
||||
|
||||
use proc_macro2::Span;
|
||||
|
||||
@ -494,7 +494,7 @@ impl<'a> Generator<'a> {
|
||||
let path = self.input
|
||||
.config
|
||||
.find_template(path, Some(&self.input.path));
|
||||
let src = path::get_template_source(&path);
|
||||
let src = get_template_source(&path);
|
||||
let nodes = parser::parse(&src);
|
||||
let nested = {
|
||||
let mut gen = self.child();
|
||||
|
@ -14,9 +14,10 @@ mod parser;
|
||||
use input::{Print, Source, TemplateInput};
|
||||
use parser::{Expr, Macro, Node};
|
||||
use proc_macro::TokenStream;
|
||||
use shared::{path, Config};
|
||||
use shared::Config;
|
||||
|
||||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
#[proc_macro_derive(Template, attributes(template))]
|
||||
@ -36,7 +37,7 @@ fn build_template(ast: &syn::DeriveInput) -> String {
|
||||
let input = TemplateInput::new(ast);
|
||||
let source: String = match input.source {
|
||||
Source::Source(ref s) => s.clone(),
|
||||
Source::Path(_) => path::get_template_source(&input.path),
|
||||
Source::Path(_) => get_template_source(&input.path),
|
||||
};
|
||||
|
||||
let mut sources = HashMap::new();
|
||||
@ -70,12 +71,12 @@ fn find_used_templates(input: &TemplateInput, map: &mut HashMap<PathBuf, String>
|
||||
match n {
|
||||
Node::Extends(Expr::StrLit(extends)) => {
|
||||
let extends = input.config.find_template(extends, Some(&path));
|
||||
let source = path::get_template_source(&extends);
|
||||
let source = get_template_source(&extends);
|
||||
check.push((extends, source));
|
||||
}
|
||||
Node::Import(_, import, _) => {
|
||||
let import = input.config.find_template(import, Some(&path));
|
||||
let source = path::get_template_source(&import);
|
||||
let source = get_template_source(&import);
|
||||
check.push((import, source));
|
||||
}
|
||||
_ => {}
|
||||
@ -158,3 +159,30 @@ impl<'a> Context<'a> {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn get_template_source(tpl_path: &Path) -> String {
|
||||
match fs::read_to_string(tpl_path) {
|
||||
Err(_) => panic!(
|
||||
"unable to open template file '{}'",
|
||||
tpl_path.to_str().unwrap()
|
||||
),
|
||||
Ok(mut source) => {
|
||||
if source.ends_with('\n') {
|
||||
let _ = source.pop();
|
||||
}
|
||||
source
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::get_template_source;
|
||||
use Config;
|
||||
|
||||
#[test]
|
||||
fn get_source() {
|
||||
let path = Config::new().find_template("b.html", None);
|
||||
assert_eq!(get_template_source(&path), "bar");
|
||||
}
|
||||
}
|
||||
|
1
askama_derive/templates/b.html
Normal file
1
askama_derive/templates/b.html
Normal file
@ -0,0 +1 @@
|
||||
bar
|
@ -17,7 +17,6 @@ mod escaping;
|
||||
pub use error::{Error, Result};
|
||||
pub use escaping::MarkupDisplay;
|
||||
pub mod filters;
|
||||
pub mod path;
|
||||
|
||||
pub struct Config {
|
||||
pub dirs: Vec<PathBuf>,
|
||||
|
@ -1,29 +0,0 @@
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
|
||||
pub fn get_template_source(tpl_path: &Path) -> String {
|
||||
match fs::read_to_string(tpl_path) {
|
||||
Err(_) => panic!(
|
||||
"unable to open template file '{}'",
|
||||
tpl_path.to_str().unwrap()
|
||||
),
|
||||
Ok(mut source) => {
|
||||
if source.ends_with('\n') {
|
||||
let _ = source.pop();
|
||||
}
|
||||
source
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::get_template_source;
|
||||
use Config;
|
||||
|
||||
#[test]
|
||||
fn get_source() {
|
||||
let path = Config::new().find_template("sub/b.html", None);
|
||||
assert_eq!(get_template_source(&path), "bar");
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user