Add new filter; fixing context (as always xwx)

This commit is contained in:
KitsuneCafe 2024-02-11 06:45:28 -05:00
parent 90ac6e07c9
commit 09ffd82157
3 changed files with 28 additions and 1 deletions

View file

@ -2,12 +2,16 @@ use std::path::Path;
use tera::{Map, Value};
// FIXME: first processed entry in a map gets pushed to parent. see note below
fn inner_merge<'a>(a: &mut Map<String, Value>, b: &'a Value) -> Option<Value> {
match b {
Value::Object(o) => {
let mut map = Map::new();
for (key, entry) in o.iter() {
println!("{key} {entry:?}");
let mut child_map = if a.contains_key(key) {
// FIXME: this is probably the culprit. it doesn't pick up changes made during
// this iteration
a[key]
.as_object()
.map(|m| m.to_owned())
@ -15,6 +19,7 @@ fn inner_merge<'a>(a: &mut Map<String, Value>, b: &'a Value) -> Option<Value> {
} else {
Map::new()
};
println!("map {child_map:?}");
if let Some(value) = inner_merge(&mut child_map, entry) {
child_map.insert(key.into(), value);
@ -23,6 +28,7 @@ fn inner_merge<'a>(a: &mut Map<String, Value>, b: &'a Value) -> Option<Value> {
map.extend(child_map);
}
println!("final map {map:?}");
Some(map.into())
}
value => Some(value.to_owned()),
@ -40,7 +46,7 @@ impl Merge for tera::Context {
if let Some(value) = inner_merge(map, &other.into_json()) {
if let Ok(new_map) = tera::Context::from_value(value) {
self.extend(new_map)
self.extend(new_map);
}
}
}

16
src/functions.rs Normal file
View file

@ -0,0 +1,16 @@
use std::collections::HashMap;
use tera::{to_value, try_get_value, Map, Result, Tera, Value};
pub fn values(value: &Value, _args: &HashMap<String, Value>) -> Result<Value> {
println!("value {value:?}");
let arr = try_get_value!("values", "value", Map<String, Value>, value)
.into_iter()
.map(|(_, x)| x)
.collect();
Ok(to_value::<Value>(arr)?)
}
pub fn register_functions(tera: &mut Tera) {
tera.register_filter("values", values);
}

View file

@ -1,10 +1,12 @@
pub mod config;
pub mod context;
mod file_path;
pub mod functions;
use config::{Config, Merge};
use context::Context;
use file_path::FilePath;
use functions::register_functions;
use roxy_core::error::Error;
use roxy_markdown_parser::MarkdownParser;
@ -80,6 +82,7 @@ fn context_from_meta_files<'a, T: AsRef<Path>>(
let mut str = String::from_utf8(buf).map_err(handle_err)?;
let toml: Table = toml::from_str(&mut str).map_err(handle_err)?;
println!("{:?}", &file_path.strip_root(path)?);
context.insert(
&file_path.strip_root(path)?,
&tera::to_value(toml).map_err(handle_err)?,
@ -123,6 +126,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
});
let mut context = context_from_meta_files(&meta, &file_path)?;
println!(":3 {context:?}\n");
let theme = config.syntect.theme.unwrap_or(DEFAULT_THEME.to_string());
let syntax_set = SyntaxSet::load_defaults_newlines();
@ -156,6 +160,7 @@ fn main() -> Result<(), Box<dyn std::error::Error>> {
}
let mut tera = tera::Tera::default();
register_functions(&mut tera);
let mut html = TeraParser::new(&mut tera, TeraParserOptions::default());
html.add_context(context.to_inner());
parser.push(&mut html);