added 'this' helper for layouts

This commit is contained in:
KitsuneCafe 2024-02-02 18:19:57 -05:00
parent 86b1222005
commit 2bebd0e540
2 changed files with 41 additions and 20 deletions

6
Cargo.lock generated
View file

@ -834,7 +834,7 @@ dependencies = [
[[package]]
name = "roxy_core"
version = "0.1.0"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-core.git#72d23383d68a344f8d5d976cca1ce0f777d936ec"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-core.git#8121a30b0c306ac5e5b48dcee5ea0b7ec12003df"
[[package]]
name = "roxy_markdown_parser"
@ -848,7 +848,6 @@ dependencies = [
[[package]]
name = "roxy_markdown_tera_rewriter"
version = "0.1.0"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-markdown-tera-rewriter.git#b7960123ad2680266395d3d45102ae58a91701b9"
dependencies = [
"once_cell",
"regex",
@ -858,8 +857,9 @@ dependencies = [
[[package]]
name = "roxy_tera_parser"
version = "0.1.0"
source = "git+https://fem.mint.lgbt/kitsunecafe/roxy-tera-parser.git#abb1178657285b96216230b31ec429088d25e141"
dependencies = [
"once_cell",
"regex",
"roxy_core",
"tera",
]

View file

@ -1,7 +1,7 @@
use clap::Parser as Clap;
use roxy_markdown_parser::MarkdownParser;
use roxy_markdown_tera_rewriter::MarkdownTeraRewriter;
use roxy_tera_parser::TeraParser;
use roxy_markdown_tera_rewriter::{MarkdownTeraRewriter, MarkdownTeraPreformatter};
use roxy_tera_parser::{TeraParser, TeraParserOptions};
use std::{
fs::File,
io::{BufReader, Error, ErrorKind, Read},
@ -69,7 +69,7 @@ fn get_files<P: AsRef<Path> + std::fmt::Debug>(path: &P) -> Result<Vec<PathBuf>,
Ok(files)
}
#[derive(Debug)]
#[derive(Debug, Clone)]
struct FilePath<'a, P: AsRef<Path>> {
input: PathBuf,
root_dir: PathBuf,
@ -109,6 +109,10 @@ impl<'a, P: AsRef<Path> + 'a> FilePath<'a, P> {
.map(|path| self.output.as_ref().join(path))
.map_err(RoxyError::from)
}
pub fn strip_root<P2: AsRef<Path>>(&self, value: &'a P2) -> Result<&Path, StripPrefixError> {
value.as_ref().strip_prefix(&self.root_dir)
}
}
#[derive(Debug)]
@ -127,13 +131,16 @@ impl Context {
path.as_ref()
.with_extension("")
.to_string_lossy()
.trim()
.split(path::MAIN_SEPARATOR_STR)
.fold(String::new(), |a, b| format!("{a}.{b}"))
.trim_matches('.')
.to_string()
}
fn insert<P: AsRef<Path>>(&mut self, path: &P, meta: Table) {
fn insert<P: AsRef<Path>>(&mut self, path: &P, value: &tera::Value) {
self.inner
.insert(Self::normalize_path(path).trim_start_matches('.'), &meta);
.insert(Self::normalize_path(path).trim_start_matches('.'), &value);
}
fn get<P: AsRef<Path>>(&self, path: &P) -> Option<&tera::Value> {
@ -150,6 +157,7 @@ fn main() -> Result<(), RoxyError> {
files.iter().partition(|f| f.extension().unwrap() == "toml");
let mut context = Context::new();
for path in meta {
let mut buf = Vec::new();
@ -158,25 +166,38 @@ fn main() -> Result<(), RoxyError> {
let mut str = String::from_utf8(buf).unwrap();
let toml: Table = toml::from_str(&mut str).unwrap();
context.insert(&path.strip_prefix(&file_path.root_dir).unwrap(), toml);
context.insert(
&file_path.strip_root(path).unwrap(),
&tera::to_value(toml).unwrap(),
);
}
let mut parser = Parser::new();
parser.push(MarkdownParser::new());
let rewriter = MarkdownTeraRewriter::new();
parser.push(rewriter);
let tera = tera::Tera::default();
let html = TeraParser::new(tera, context.inner);
parser.push(html);
for file in files {
let mut parser = Parser::new();
let mut preformatter = MarkdownTeraPreformatter::new();
parser.push(&mut preformatter);
let mut md = MarkdownParser::new();
parser.push(&mut md);
let mut rewriter = MarkdownTeraRewriter::new();
parser.push(&mut rewriter);
let file_name = file.with_extension("html");
let output_path = file_path.to_output(&file_name)?;
if let Ok(path) = &file_path.strip_root(&file_name) {
if let Some(current_context) = context.get(path) {
context.insert(&"this", &current_context.clone());
}
}
let mut tera = tera::Tera::default();
let mut html = TeraParser::new(&mut tera, TeraParserOptions::default());
html.add_context(&context.inner);
parser.push(&mut html);
Roxy::process_file(&file, &output_path, &mut parser).unwrap();
}
Ok(())
}