search for nested includes
This commit is contained in:
parent
38e161ae3d
commit
13868747c2
43
src/lib.rs
43
src/lib.rs
|
@ -1,4 +1,8 @@
|
||||||
use std::{path::PathBuf, io::BufReader, fs::{File, read, self}};
|
use std::{
|
||||||
|
fs::{self, read, File},
|
||||||
|
io::BufReader,
|
||||||
|
path::{Path, PathBuf},
|
||||||
|
};
|
||||||
|
|
||||||
use once_cell::sync::Lazy;
|
use once_cell::sync::Lazy;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
|
@ -42,23 +46,24 @@ impl<'a> TeraParser<'a> {
|
||||||
self.context = Some(context);
|
self.context = Some(context);
|
||||||
}
|
}
|
||||||
|
|
||||||
fn load_template(&mut self, path: &str, src: &[u8]) -> Result<(), tera::Error> {
|
fn load_template<P: AsRef<Path>>(&mut self, path: &P, src: &[u8]) -> Result<(), tera::Error> {
|
||||||
|
let path = path.as_ref().canonicalize()?;
|
||||||
let str = String::from_utf8_lossy(src).to_string();
|
let str = String::from_utf8_lossy(src).to_string();
|
||||||
for (_, [_, layout_path]) in EXPANSION_RE
|
for (_, [_, layout_path]) in EXPANSION_RE
|
||||||
.captures_iter(&str.as_str())
|
.captures_iter(&str.as_str())
|
||||||
.map(|c| c.extract())
|
.map(|c| c.extract())
|
||||||
{
|
{
|
||||||
let path = PathBuf::from(path)
|
let path = path
|
||||||
|
.canonicalize()?
|
||||||
.parent()
|
.parent()
|
||||||
.map(|p| p.join(layout_path))
|
.map(|p| p.join(layout_path))
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
self.tera
|
|
||||||
.add_template_file(&path.canonicalize().unwrap(), Some(layout_path))?;
|
|
||||||
|
|
||||||
let next_template = fs::read(&path)?;
|
let next_template = fs::read(&path)?;
|
||||||
let path_str = path.to_string_lossy().to_string();
|
self.load_template(&path, &next_template)?;
|
||||||
self.load_template(path_str.as_str(), &next_template)?;
|
|
||||||
|
self.tera
|
||||||
|
.add_template_file(&path, Some(layout_path))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
@ -79,7 +84,7 @@ impl<'a> Parse for TeraParser<'a> {
|
||||||
};
|
};
|
||||||
|
|
||||||
if self.options.auto_load_layouts {
|
if self.options.auto_load_layouts {
|
||||||
self.load_template(path, src).map_err(err)?;
|
self.load_template(&path, src).map_err(err)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
let template = String::from_utf8_lossy(src).to_string();
|
let template = String::from_utf8_lossy(src).to_string();
|
||||||
|
@ -98,17 +103,19 @@ impl<'a> Parse for TeraParser<'a> {
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
|
use std::fs;
|
||||||
|
|
||||||
use roxy_core::roxy::Parse;
|
use roxy_core::roxy::Parse;
|
||||||
|
|
||||||
use crate::{TeraParser, TeraParserOptions};
|
use crate::{TeraParser, TeraParserOptions};
|
||||||
|
|
||||||
#[test]
|
//#[test]
|
||||||
fn capture_test() {
|
//fn capture_test() {
|
||||||
let mut tera = tera::Tera::default();
|
// let mut tera = tera::Tera::default();
|
||||||
let mut parser = TeraParser::new(&mut tera, TeraParserOptions::default());
|
// let mut parser = TeraParser::new(&mut tera, TeraParserOptions::default());
|
||||||
let data = b"<p>{% include \"test.html\" %}</p>";
|
// let data = fs::read("./tests/index.md").unwrap();
|
||||||
let mut dst = Vec::new();
|
// let mut dst = Vec::new();
|
||||||
parser.parse("test.html", data, &mut dst);
|
// parser.parse("./tests/index.md", &data, &mut dst);
|
||||||
println!("{dst:?}");
|
// println!("{dst:?}");
|
||||||
}
|
//}
|
||||||
}
|
}
|
||||||
|
|
1
tests/base.html
Normal file
1
tests/base.html
Normal file
|
@ -0,0 +1 @@
|
||||||
|
:3
|
2
tests/index.md
Normal file
2
tests/index.md
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
{% extends "middle.html" %}
|
||||||
|
|
2
tests/middle.html
Normal file
2
tests/middle.html
Normal file
|
@ -0,0 +1,2 @@
|
||||||
|
{% extends "base.html" %}
|
||||||
|
|
Loading…
Reference in a new issue