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() { for entry in fs::read_dir(&file_path).unwrap() {
let entry_file = entry.unwrap(); let entry_file = entry.unwrap();
let child = entry_file.file_name().into_string().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); attachments.push(child);
} else if !(child.starts_with(".") || child == self.index_filename || child == DEFAULT_TOML_FILENAME) { } 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 url: String,
pub is_directory: bool, pub is_directory: bool,
pub parent: Option<Box<Page>>, pub parent: Option<Box<Page>>,
pub children: Vec<String>, pub children: Vec<Child>,
pub attachments: Vec<String> pub attachments: Vec<String>
} }
#[derive(Serialize)]
pub struct Child {
pub title: String,
pub full_name: String,
pub name: String,
pub path: PathBuf
}
#[derive(Serialize)] #[derive(Serialize)]
pub struct Edit { pub struct Edit {
pub author: Option<String>, 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)) 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 { impl PageRenderer {
pub fn new() -> PageRenderer { pub fn new() -> PageRenderer {
Self::with_template_path(DEFAULT_TEMPLATES_PATH) Self::with_template_path(DEFAULT_TEMPLATES_PATH)
@@ -337,7 +348,6 @@ impl PageRenderer {
pub fn with_template_path(template_path: &str) -> PageRenderer { pub fn with_template_path(template_path: &str) -> PageRenderer {
let mut tera = tera::Tera::new(&format!("{template_path}/**/*.html")).unwrap(); let mut tera = tera::Tera::new(&format!("{template_path}/**/*.html")).unwrap();
tera.register_filter("markdown", render_markdown); tera.register_filter("markdown", render_markdown);
tera.register_filter("file_stem", file_stem);
PageRenderer { PageRenderer {
template_root: template_path.into(), template_root: template_path.into(),
tera tera
@@ -535,8 +545,8 @@ fn export_wiki_page(historian: &Historian, renderer: &PageRenderer, name: &str,
} }
for child in page.children { for child in page.children {
let child_path = page_path.join(child); // let child_path = page_path.join(child);
export_wiki_page(historian, renderer, child_path.to_str().unwrap(), output_path); 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> { pub fn resolve_link(&self, link: &str) -> Option<String> {
let root = self.historian.resolve_to_page("")?; 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 { loop {
let mut next_page_names: Vec<String> = vec![]; let mut next_page_names: Vec<String> = vec![];
@@ -572,7 +582,7 @@ impl<'a> Linker<'a> {
for child in page.children { for child in page.children {
let mut child_path = page.full_name.to_owned(); let mut child_path = page.full_name.to_owned();
child_path.push('/'); child_path.push('/');
child_path.push_str(&child); child_path.push_str(&child.name);
next_page_names.push(child_path); next_page_names.push(child_path);
} }
} }

View File

@@ -82,12 +82,12 @@
<p class="title"><a href="{{ relative_root | safe }}{{ page.parent.url }}">{{ page.parent.name }}</a></p> <p class="title"><a href="{{ relative_root | safe }}{{ page.parent.url }}">{{ page.parent.name }}</a></p>
<ul> <ul>
{% for child in page.parent.children %} {% for child in page.parent.children %}
<li{% if child == page.name %} class="highlight"{% endif %}> <li{% if child.name == page.name %} class="highlight"{% endif %}>
<a href="{% if page.is_directory %}../{% endif %}{{ child }}">{{ child | file_stem }}</a> <a href="{% if page.is_directory %}../{% endif %}{{ child.name }}">{{ child.title }}</a>
{% if page.is_directory and child == page.name %} {% if page.is_directory and child.name == page.name %}
<ul> <ul>
{% for child in page.children %} {% for child in page.children %}
<li><a href="{{ child }}">{{ child | file_stem }}</a></li> <li><a href="{{ child.name }}">{{ child.title }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
{% endif %} {% endif %}
@@ -100,7 +100,7 @@
<p class="title"><a href="{{ relative_root | safe }}{{ page.url }}">{{ page.name }}</a></p> <p class="title"><a href="{{ relative_root | safe }}{{ page.url }}">{{ page.name }}</a></p>
<ul> <ul>
{% for child in page.children %} {% for child in page.children %}
<li><a href="{{ child }}">{{ child | file_stem }}</a></li> <li><a href="{{ child.name }}">{{ child.title }}</a></li>
{% endfor %} {% endfor %}
</ul> </ul>
</div> </div>