diff --git a/CHANGELOG.md b/CHANGELOG.md index becfe36..0e700a2 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,7 @@ a section - The table of content for a page/section is now only available as the `toc` variable when rendering it and not anymore on the `page`/`section` variable +- Default directory for `load_data` is now the root of the site instead of the `content` directory ### Other - Add support for content in multiple languages diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index 634664d..60d9afc 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -349,7 +349,7 @@ impl Site { ); self.tera.register_function( "load_data", - global_fns::LoadData::new(self.content_path.clone(), self.base_path.clone()), + global_fns::LoadData::new(self.base_path.clone()), ); self.tera.register_function("trans", global_fns::Trans::new(self.config.clone())); self.tera.register_function( diff --git a/components/templates/src/global_fns/load_data.rs b/components/templates/src/global_fns/load_data.rs index 48b7176..5bc506f 100644 --- a/components/templates/src/global_fns/load_data.rs +++ b/components/templates/src/global_fns/load_data.rs @@ -174,22 +174,21 @@ fn get_output_format_from_args( /// Currently the supported formats are json, toml, csv and plain text #[derive(Debug)] pub struct LoadData { - content_path: PathBuf, base_path: PathBuf, client: Arc>, result_cache: Arc>>, } impl LoadData { - pub fn new(content_path: PathBuf, base_path: PathBuf) -> Self { + pub fn new(base_path: PathBuf) -> Self { let client = Arc::new(Mutex::new(Client::builder().build().expect("reqwest client build"))); let result_cache = Arc::new(Mutex::new(HashMap::new())); - Self { content_path, base_path, client, result_cache } + Self { base_path, client, result_cache } } } impl TeraFn for LoadData { fn call(&self, args: &HashMap) -> Result { - let data_source = get_data_source_from_args(&self.content_path, &args)?; + let data_source = get_data_source_from_args(&self.base_path, &args)?; let file_format = get_output_format_from_args(&args, &data_source)?; let cache_key = data_source.get_cache_key(&file_format); @@ -334,7 +333,7 @@ mod tests { #[test] fn fails_when_missing_file() { let static_fn = - LoadData::new(PathBuf::from("../utils/test-files"), PathBuf::from("../utils")); + LoadData::new(PathBuf::from("../utils")); let mut args = HashMap::new(); args.insert("path".to_string(), to_value("../../../READMEE.md").unwrap()); let result = static_fn.call(&args); @@ -345,9 +344,9 @@ mod tests { #[test] fn cant_load_outside_content_dir() { let static_fn = - LoadData::new(PathBuf::from("../utils/test-files"), PathBuf::from("../utils")); + LoadData::new(PathBuf::from(PathBuf::from("../utils"))); let mut args = HashMap::new(); - args.insert("path".to_string(), to_value("../../../README.md").unwrap()); + args.insert("path".to_string(), to_value("../../README.md").unwrap()); args.insert("format".to_string(), to_value("plain").unwrap()); let result = static_fn.call(&args); assert!(result.is_err()); @@ -395,7 +394,7 @@ mod tests { #[test] fn can_load_remote_data() { - let static_fn = LoadData::new(PathBuf::new(), PathBuf::new()); + let static_fn = LoadData::new(PathBuf::new()); let mut args = HashMap::new(); args.insert("url".to_string(), to_value("https://httpbin.org/json").unwrap()); args.insert("format".to_string(), to_value("json").unwrap()); @@ -408,7 +407,7 @@ mod tests { #[test] fn fails_when_request_404s() { - let static_fn = LoadData::new(PathBuf::new(), PathBuf::new()); + let static_fn = LoadData::new(PathBuf::new()); let mut args = HashMap::new(); args.insert("url".to_string(), to_value("https://httpbin.org/status/404/").unwrap()); args.insert("format".to_string(), to_value("json").unwrap()); @@ -424,7 +423,6 @@ mod tests { fn can_load_toml() { let static_fn = LoadData::new( PathBuf::from("../utils/test-files"), - PathBuf::from("../utils/test-files"), ); let mut args = HashMap::new(); args.insert("path".to_string(), to_value("test.toml").unwrap()); @@ -446,7 +444,6 @@ mod tests { fn can_load_csv() { let static_fn = LoadData::new( PathBuf::from("../utils/test-files"), - PathBuf::from("../utils/test-files"), ); let mut args = HashMap::new(); args.insert("path".to_string(), to_value("test.csv").unwrap()); @@ -469,7 +466,6 @@ mod tests { fn bad_csv_should_result_in_error() { let static_fn = LoadData::new( PathBuf::from("../utils/test-files"), - PathBuf::from("../utils/test-files"), ); let mut args = HashMap::new(); args.insert("path".to_string(), to_value("uneven_rows.csv").unwrap()); @@ -492,7 +488,6 @@ mod tests { fn can_load_json() { let static_fn = LoadData::new( PathBuf::from("../utils/test-files"), - PathBuf::from("../utils/test-files"), ); let mut args = HashMap::new(); args.insert("path".to_string(), to_value("test.json").unwrap());