From 35dbab0ceb33a7ed3053bc9edf1919db5b4491e7 Mon Sep 17 00:00:00 2001 From: Tom Alexander Date: Sun, 17 Dec 2023 14:28:27 -0500 Subject: [PATCH] Create a page header struct. --- src/config/full.rs | 4 ++++ src/config/raw.rs | 4 ++-- src/context/mod.rs | 2 ++ src/context/page_header.rs | 19 +++++++++++++++++++ src/intermediate/convert.rs | 9 ++++++--- src/render/duster_renderer.rs | 2 +- 6 files changed, 34 insertions(+), 6 deletions(-) create mode 100644 src/context/page_header.rs diff --git a/src/config/full.rs b/src/config/full.rs index 0f962aa..4978e8d 100644 --- a/src/config/full.rs +++ b/src/config/full.rs @@ -67,4 +67,8 @@ impl Config { pub(crate) fn get_web_root(&self) -> Option<&str> { self.raw.web_root.as_deref() } + + pub(crate) fn get_site_title(&self) -> Option<&str> { + self.raw.site_title.as_deref() + } } diff --git a/src/config/raw.rs b/src/config/raw.rs index b0d95cf..ce34997 100644 --- a/src/config/raw.rs +++ b/src/config/raw.rs @@ -4,7 +4,7 @@ use serde::Serialize; /// This is the struct for the writer.toml config file that ends up in each site's root directory. #[derive(Deserialize, Serialize)] pub(crate) struct RawConfig { - site_title: String, + pub(super) site_title: Option, author: Option, email: Option, pub(super) use_relative_paths: Option, @@ -14,7 +14,7 @@ pub(crate) struct RawConfig { impl Default for RawConfig { fn default() -> Self { RawConfig { - site_title: "My super awesome website".to_owned(), + site_title: None, author: None, email: None, use_relative_paths: None, diff --git a/src/context/mod.rs b/src/context/mod.rs index 2c412b3..f3a8311 100644 --- a/src/context/mod.rs +++ b/src/context/mod.rs @@ -35,6 +35,7 @@ mod line_break; mod macros; mod object; mod org_macro; +mod page_header; mod paragraph; mod plain_link; mod plain_list; @@ -69,4 +70,5 @@ pub(crate) use footnote_definition::RenderRealFootnoteDefinition; pub(crate) use global_settings::GlobalSettings; pub(crate) use heading::RenderHeading; pub(crate) use object::RenderObject; +pub(crate) use page_header::PageHeader; pub(crate) use section::RenderSection; diff --git a/src/context/page_header.rs b/src/context/page_header.rs new file mode 100644 index 0000000..0115e9c --- /dev/null +++ b/src/context/page_header.rs @@ -0,0 +1,19 @@ +use serde::Serialize; + +/// The header that goes above the content of the page. +/// +/// This header will be mostly the same on every page. +#[derive(Debug, Serialize)] +pub(crate) struct PageHeader { + website_title: Option, + home_link: Option, +} + +impl PageHeader { + pub(crate) fn new(website_title: Option, home_link: Option) -> PageHeader { + PageHeader { + website_title, + home_link, + } + } +} diff --git a/src/intermediate/convert.rs b/src/intermediate/convert.rs index 747fce8..72e11bb 100644 --- a/src/intermediate/convert.rs +++ b/src/intermediate/convert.rs @@ -4,6 +4,7 @@ use std::path::PathBuf; use crate::config::Config; use crate::context::GlobalSettings; +use crate::context::PageHeader; use crate::context::RenderBlogPostPage; use crate::context::RenderDocumentElement; use crate::context::RenderRealFootnoteDefinition; @@ -37,6 +38,10 @@ pub(crate) fn convert_blog_post_page_to_render_context, F: AsRef< "blog_post.js", )?]; let global_settings = GlobalSettings::new(page.title.clone(), css_files, js_files); + let page_header = PageHeader::new( + config.get_site_title().map(str::to_string), + Some(get_web_path(config, output_directory, output_file, "")?), + ); let link_to_blog_post = get_web_path( config, output_directory, @@ -100,9 +105,7 @@ fn get_web_path, F: AsRef, P: AsRef>( containing_file_relative_to_output_directory .parent() .ok_or("File should exist in a folder.")?, - path_from_web_root - .parent() - .ok_or("File should exist in a folder.")?, + path_from_web_root.parent().unwrap_or(&Path::new("")), ) .collect::(); // Subtracting 1 from the depth to "remove" the file name. diff --git a/src/render/duster_renderer.rs b/src/render/duster_renderer.rs index e5078a9..2208077 100644 --- a/src/render/duster_renderer.rs +++ b/src/render/duster_renderer.rs @@ -34,7 +34,7 @@ impl<'a> RendererIntegration<'a> for DusterRenderer<'a> { } // TODO: This is horribly inefficient. I am converting from a serialize type to json and back again so I can use the existing implementation of IntoContextElement. Honestly, I probably need to rework a lot of duster now that I've improved in rust over the years. let json_context = serde_json::to_string(&context)?; - println!("{}", json_context); + // println!("{}", json_context); let parsed_context: serde_json::Value = serde_json::from_str(json_context.as_str())?; let rendered_output = dust_renderer.render("main", Some(&parsed_context))?; Ok(rendered_output)