From 7b5a7c52bd9fbd256776ca6737447cf78f80aa57 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Tue, 7 Jan 2025 22:17:52 +1300 Subject: Load default CSS and JavaScript files from site root Instead of linking to a static path for the CSS and JavaScript files in the head of each generated HTML document, dynamically generate a relative path that backtracks to the site root before descending into the chosen directory. This ensures that all pages will link to the same set of CSS/JS files, no matter how deeply nested. This could have been accomplished through the use of absolute paths, but I want the websites generated by this program to be able to be freely mixed and matched with existing websites, which requires the use of relative paths only. --- src/generate_html.rs | 13 +++++++------ src/main.rs | 12 ++++++++++++ 2 files changed, 19 insertions(+), 6 deletions(-) (limited to 'src') diff --git a/src/generate_html.rs b/src/generate_html.rs index 1e3acc7..f14d5f9 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -20,23 +20,24 @@ pub fn generate_html(document: &MarkdownDocument, page: &SourceFile, website: &W \ ", page.name, website.name, - get_html_head(document).trim(), + get_html_head(document, page).trim(), document_to_html(document, page, website).trim() ) } -pub fn get_html_head(document: &MarkdownDocument) -> String { +pub fn get_html_head(document: &MarkdownDocument, page: &SourceFile) -> String { if let Some(Block::Fragment { language, content }) = document.blocks.first() { if language == "embed-html-head" { return content.to_string(); } } - String::from("\ - - - \ + let back = page.back_string(); + format!("\ + + + \ ") } diff --git a/src/main.rs b/src/main.rs index a1b38f6..0a62ec2 100644 --- a/src/main.rs +++ b/src/main.rs @@ -247,6 +247,18 @@ pub struct SourceFile { pub headings: Vec, } +impl SourceFile { + pub fn back_string(&self) -> String { + let mut back = String::new(); + for c in self.full_url.chars() { + if c == '/' { + back.push_str("../"); + } + } + return back; + } +} + pub struct StaticFile { pub full_url: String, // URL full path, with extension pub source_path: PathBuf, -- cgit v1.2.3-70-g09d2