summaryrefslogtreecommitdiff
path: root/src/generate_html.rs
diff options
context:
space:
mode:
authorBen Bridle <bridle.benjamin@gmail.com>2025-01-07 22:17:52 +1300
committerBen Bridle <bridle.benjamin@gmail.com>2025-01-07 22:17:52 +1300
commit7b5a7c52bd9fbd256776ca6737447cf78f80aa57 (patch)
treef21c2be91e7f96762579a7d2077d1f463c8426d7 /src/generate_html.rs
parent02946f8f8ecb95349a8cc3af51be2df0256e196e (diff)
downloadtoaster-7b5a7c52bd9fbd256776ca6737447cf78f80aa57.zip
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.
Diffstat (limited to 'src/generate_html.rs')
-rw-r--r--src/generate_html.rs13
1 files changed, 7 insertions, 6 deletions
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
</html> \
",
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("\
-<link rel='stylesheet' type='text/css' media='screen' href='/static/screen.css'>
-<link rel='stylesheet' type='text/css' media='print' href='/static/print.css'>
-<script src='/static/render_math.js' defer></script> \
+ let back = page.back_string();
+ format!("\
+<link rel='stylesheet' type='text/css' media='screen' href='{back}static/screen.css'>
+<link rel='stylesheet' type='text/css' media='print' href='{back}static/print.css'>
+<script src='{back}static/render_math.js' defer></script> \
")
}