From a84c2e234e30b0a0f1cbf690bec060dc68588db6 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Mon, 9 Jun 2025 11:41:01 +1200 Subject: Qualify page names in log messages The name of each page is now prefixed with the name of the parent in log messages, to disambiguate between pages that share the same name. --- src/collect_files.rs | 29 ++++++++++++++++++++++++++--- src/generate_html.rs | 6 +++--- 2 files changed, 29 insertions(+), 6 deletions(-) diff --git a/src/collect_files.rs b/src/collect_files.rs index 18d75f1..ccdfc49 100644 --- a/src/collect_files.rs +++ b/src/collect_files.rs @@ -4,6 +4,7 @@ use highlight::*; use vagabond::*; use std::collections::HashMap; +use std::fmt::Debug; pub struct Website { @@ -49,7 +50,7 @@ pub struct Redirect { pub last_modified: Option, // last-modified time of source file } -pub trait LinkFrom { +pub trait LinkFrom: Debug { fn name(&self) -> &str; fn parent_url(&self) -> &str; fn parents(&self) -> &[String]; @@ -60,6 +61,12 @@ pub trait LinkFrom { } return root; } + fn qualified_name(&self) -> String { + match self.parents().last() { + Some(parent) => format!("{parent}/{}", self.name()), + None => format!("/{}", self.name()), + } + } } pub struct Highlighters { @@ -77,6 +84,18 @@ impl Page { } } +impl Debug for Page { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "\"{}\"", self.qualified_name()) + } +} + +impl Debug for Redirect { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + write!(f, "\"{}\"", self.qualified_name()) + } +} + impl LinkFrom for Page { fn name(&self) -> &str { &self.name } fn parent_url(&self) -> &str { &self.parent_url } @@ -174,6 +193,10 @@ impl Website { self.config.insert(key, std::mem::take(&mut value)); } } else { + let full_name = match parents.last() { + Some(parent) => format!("{parent}/{name}"), + None => name.to_string(), + }; match extension.as_str() { "md" => { let markdown = std::fs::read_to_string(&source_path).unwrap(); @@ -193,7 +216,7 @@ impl Website { None }).collect(); for url in duplicates { - warn!("Page {name:?} contains multiple headings with ID \"#{url}\""); + warn!("Page {full_name:?} contains multiple headings with ID \"#{url}\""); } if name_url == "+index" { if parents.is_empty() { @@ -339,7 +362,7 @@ impl Website { if let Some(heading) = heading { let heading = make_url_safe(strip_appendix(heading)); if !page.headings.iter().any(|h| h.url == heading) { - warn!("Page {:?} contains link to nonexistent heading {heading:?} on page {path:?}", from.name()); + warn!("Page {from:?} contains link to nonexistent heading {heading:?} on page {path:?}"); } return Some(format!("{root}{path}.{ext}#{heading}")); } else { diff --git a/src/generate_html.rs b/src/generate_html.rs index 5abddf2..2aab900 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -94,7 +94,7 @@ pub fn get_table_of_contents(page: &Page) -> String { pub fn document_to_html(document: &MarkdownDocument, page: &Page, website: &Website) -> String { - let from = &page.name; + let from = &page; let mut html = String::new(); macro_rules! line_to_html { @@ -340,7 +340,7 @@ struct ParsedLink { } fn parse_internal_link(name: &str, page: &Page, website: &Website) -> ParsedLink { - let from = &page.name; + let from = &page; let (class, label, path) = match name.split_once('#') { Some(("", heading)) => ("heading", heading, format!("#{}", strip_appendix(heading))), Some((page, heading)) => ("page", heading, format!("{page}.html#{}", strip_appendix(heading))), @@ -370,7 +370,7 @@ fn parse_internal_link(name: &str, page: &Page, website: &Website) -> ParsedLink } fn parse_external_link(label: &str, path: &str, page: &Page, website: &Website) -> ParsedLink { - let from = &page.name; + let from = &page; let mut path = path.to_owned(); let mut label = label.to_string(); let mut is_internal = true; -- cgit v1.2.3-70-g09d2