Introduce a struct for child members and use that instead of string names.

This commit is contained in:
2025-05-04 16:14:47 -05:00
parent 2473fdd24b
commit a6ea9ed048
2 changed files with 32 additions and 22 deletions

View File

@@ -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<Box<Page>>,
pub children: Vec<String>,
pub children: Vec<Child>,
pub attachments: Vec<String>
}
#[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<String>,
@@ -320,15 +340,6 @@ fn render_markdown (content: &tera::Value, args: &HashMap<String, tera::Value>)
Ok(tera::Value::String(html_output))
}
fn file_stem (content: &tera::Value, args: &HashMap<String, tera::Value>) -> tera::Result<tera::Value> {
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<String> {
let root = self.historian.resolve_to_page("")?;
let mut page_names = root.children;
let mut page_names: Vec<String> = root.children.iter().map(|child| child.name.to_owned()).collect();
loop {
let mut next_page_names: Vec<String> = 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);
}
}