diff --git a/src/lib.rs b/src/lib.rs index 9855eaf..a6426d0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -69,10 +69,22 @@ impl Historian { for entry in fs::read_dir(&file_path).unwrap() { let entry_file = entry.unwrap(); let child = entry_file.file_name().into_string().unwrap(); - if entry_file.metadata().unwrap().is_file() && !child.ends_with(MD_EXTENSION) { + let entry_metadata = entry_file.metadata().unwrap(); + let entry_path = entry_file.path(); + + if entry_metadata.is_file() && !child.ends_with(MD_EXTENSION) { attachments.push(child); } else if !(child.starts_with(".") || child == self.index_filename || child == DEFAULT_TOML_FILENAME) { - children.push(child); + children.push(Child { + name: child.to_owned(), + full_name: format!("{}/{}", name, child), + title: if entry_metadata.is_file() { + entry_path.file_stem().unwrap().to_str().unwrap().to_owned() + } else { + child.to_owned() + }, + path: entry_path + }); } } @@ -289,10 +301,18 @@ pub struct Page { pub url: String, pub is_directory: bool, pub parent: Option>, - pub children: Vec, + pub children: Vec, pub attachments: Vec } +#[derive(Serialize)] +pub struct Child { + pub title: String, + pub full_name: String, + pub name: String, + pub path: PathBuf +} + #[derive(Serialize)] pub struct Edit { pub author: Option, @@ -320,15 +340,6 @@ fn render_markdown (content: &tera::Value, args: &HashMap) Ok(tera::Value::String(html_output)) } -fn file_stem (content: &tera::Value, args: &HashMap) -> tera::Result { - let content_as_str = content.as_str().expect("as_str"); - if !content_as_str.ends_with(MD_EXTENSION) { - Ok(content.clone()) - } else { - Ok(tera::Value::String(Path::new(content_as_str).file_stem().expect("file_stem").to_str().expect("to_str").to_owned())) - } -} - impl PageRenderer { pub fn new() -> PageRenderer { Self::with_template_path(DEFAULT_TEMPLATES_PATH) @@ -337,7 +348,6 @@ impl PageRenderer { pub fn with_template_path(template_path: &str) -> PageRenderer { let mut tera = tera::Tera::new(&format!("{template_path}/**/*.html")).unwrap(); tera.register_filter("markdown", render_markdown); - tera.register_filter("file_stem", file_stem); PageRenderer { template_root: template_path.into(), tera @@ -535,8 +545,8 @@ fn export_wiki_page(historian: &Historian, renderer: &PageRenderer, name: &str, } for child in page.children { - let child_path = page_path.join(child); - export_wiki_page(historian, renderer, child_path.to_str().unwrap(), output_path); + // let child_path = page_path.join(child); + export_wiki_page(historian, renderer, child.path.to_str().unwrap(), output_path); } } } @@ -556,7 +566,7 @@ impl<'a> Linker<'a> { pub fn resolve_link(&self, link: &str) -> Option { let root = self.historian.resolve_to_page("")?; - let mut page_names = root.children; + let mut page_names: Vec = root.children.iter().map(|child| child.name.to_owned()).collect(); loop { let mut next_page_names: Vec = vec![]; @@ -572,7 +582,7 @@ impl<'a> Linker<'a> { for child in page.children { let mut child_path = page.full_name.to_owned(); child_path.push('/'); - child_path.push_str(&child); + child_path.push_str(&child.name); next_page_names.push(child_path); } } diff --git a/templates/base.html b/templates/base.html index 88c3c0d..42ce8c4 100644 --- a/templates/base.html +++ b/templates/base.html @@ -82,12 +82,12 @@

{{ page.parent.name }}