Compare commits

..

No commits in common. "13868747c22d09c6cd61bebb4dbb69e299be6bdb" and "d7a364b1af1c2ec400d951fcd41560f929feb12c" have entirely different histories.

4 changed files with 17 additions and 51 deletions

View file

@ -1,17 +1,12 @@
use std::{ use std::path::PathBuf;
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;
use roxy_core::roxy::Parse; use roxy_core::roxy::Parse;
const DEFAULT_CONTEXT: Lazy<tera::Context> = Lazy::new(|| tera::Context::default()); const DEFAULT_CONTEXT: Lazy<tera::Context> = Lazy::new(|| tera::Context::default());
const EXPANSION_RE: Lazy<Regex> = Lazy::new(|| { const EXPANSION_RE: Lazy<Regex> =
Regex::new("\\{% (extends|include) \"?(.+?)\"? %\\}").expect("couldn't load regex") Lazy::new(|| Regex::new("\\{% extends \"?(.+?)\"? %\\}").expect("couldn't load regex"));
});
#[derive(Debug)] #[derive(Debug)]
pub struct TeraParserOptions { pub struct TeraParserOptions {
@ -46,24 +41,18 @@ impl<'a> TeraParser<'a> {
self.context = Some(context); self.context = Some(context);
} }
fn load_template<P: AsRef<Path>>(&mut self, path: &P, src: &[u8]) -> Result<(), tera::Error> { fn load_template(&mut self, path: &str, 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 if let Some(captures) = EXPANSION_RE.captures(&str.as_str()) {
.captures_iter(&str.as_str()) if let Some(layout_path) = captures.get(1) {
.map(|c| c.extract()) let layout_path = layout_path.as_str();
{ let path = PathBuf::from(path).parent().map(|p| p.join(layout_path)).unwrap();
let path = path
.canonicalize()?
.parent()
.map(|p| p.join(layout_path))
.unwrap();
let next_template = fs::read(&path)?; self.tera.add_template_file(
self.load_template(&path, &next_template)?; &path.canonicalize().unwrap(),
Some(layout_path)
self.tera )?;
.add_template_file(&path, Some(layout_path))?; }
} }
Ok(()) Ok(())
@ -71,12 +60,7 @@ impl<'a> TeraParser<'a> {
} }
impl<'a> Parse for TeraParser<'a> { impl<'a> Parse for TeraParser<'a> {
fn parse( fn parse(&mut self, path: &str, src: &[u8], dst: &mut Vec<u8>) -> Result<(), roxy_core::error::Error> {
&mut self,
path: &str,
src: &[u8],
dst: &mut Vec<u8>,
) -> Result<(), roxy_core::error::Error> {
// TODO: This error is a hack // TODO: This error is a hack
let err = |e: tera::Error| { let err = |e: tera::Error| {
println!("{e:?}"); println!("{e:?}");
@ -84,7 +68,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();
@ -103,19 +87,6 @@ impl<'a> Parse for TeraParser<'a> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use std::fs; #[test]
fn it_works() {}
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 = fs::read("./tests/index.md").unwrap();
// let mut dst = Vec::new();
// parser.parse("./tests/index.md", &data, &mut dst);
// println!("{dst:?}");
//}
} }

View file

@ -1 +0,0 @@
:3

View file

@ -1,2 +0,0 @@
{% extends "middle.html" %}

View file

@ -1,2 +0,0 @@
{% extends "base.html" %}