iterate captures

This commit is contained in:
KitsuneCafe 2024-02-05 15:37:56 -05:00
parent 2b2171c61d
commit 06c9e87041

View file

@ -5,8 +5,9 @@ use regex::Regex;
use roxy_core::roxy::Parse;
const DEFAULT_CONTEXT: Lazy<tera::Context> = Lazy::new(|| tera::Context::default());
const EXPANSION_RE: Lazy<Regex> =
Lazy::new(|| Regex::new("\\{% (extends|include) \"?(.+?)\"? %\\}").expect("couldn't load regex"));
const EXPANSION_RE: Lazy<Regex> = Lazy::new(|| {
Regex::new("\\{% (extends|include) \"?(.+?)\"? %\\}").expect("couldn't load regex")
});
#[derive(Debug)]
pub struct TeraParserOptions {
@ -43,16 +44,17 @@ impl<'a> TeraParser<'a> {
fn load_template(&mut self, path: &str, src: &[u8]) -> Result<(), tera::Error> {
let str = String::from_utf8_lossy(src).to_string();
while let Some(captures) = EXPANSION_RE.captures(&str.as_str()) {
if let Some(layout_path) = captures.get(2) {
let layout_path = layout_path.as_str();
let path = PathBuf::from(path).parent().map(|p| p.join(layout_path)).unwrap();
for (_, [_, layout_path]) in EXPANSION_RE
.captures_iter(&str.as_str())
.map(|c| c.extract())
{
let path = PathBuf::from(path)
.parent()
.map(|p| p.join(layout_path))
.unwrap();
self.tera.add_template_file(
&path.canonicalize().unwrap(),
Some(layout_path)
)?;
}
self.tera
.add_template_file(&path.canonicalize().unwrap(), Some(layout_path))?;
}
Ok(())
@ -60,7 +62,12 @@ impl<'a> TeraParser<'a> {
}
impl<'a> Parse for TeraParser<'a> {
fn parse(&mut self, path: &str, src: &[u8], dst: &mut Vec<u8>) -> Result<(), roxy_core::error::Error> {
fn parse(
&mut self,
path: &str,
src: &[u8],
dst: &mut Vec<u8>,
) -> Result<(), roxy_core::error::Error> {
// TODO: This error is a hack
let err = |e: tera::Error| {
println!("{e:?}");
@ -90,5 +97,14 @@ mod tests {
use roxy_core::roxy::Parse;
use crate::{TeraParser, TeraParserOptions};
}
#[test]
fn capture_test() {
let mut tera = tera::Tera::default();
let mut parser = TeraParser::new(&mut tera, TeraParserOptions::default());
let data = b"<p>{% include \"test.html\" %}</p>";
let mut dst = Vec::new();
parser.parse("test.html", data, &mut dst);
println!("{dst:?}");
}
}