diff --git a/src/context.rs b/src/context.rs index 5b3dbb3..8d167cb 100644 --- a/src/context.rs +++ b/src/context.rs @@ -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, b: &'a Value) -> Option { 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, b: &'a Value) -> Option { } 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, b: &'a Value) -> Option { 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); } } } diff --git a/src/functions.rs b/src/functions.rs new file mode 100644 index 0000000..f5534c2 --- /dev/null +++ b/src/functions.rs @@ -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) -> Result { + println!("value {value:?}"); + let arr = try_get_value!("values", "value", Map, value) + .into_iter() + .map(|(_, x)| x) + .collect(); + + Ok(to_value::(arr)?) +} + +pub fn register_functions(tera: &mut Tera) { + tera.register_filter("values", values); +} diff --git a/src/main.rs b/src/main.rs index cb70c56..b4786aa 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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>( 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> { }); 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> { } 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);