Merge branch 'header'

This commit is contained in:
Tom Alexander 2023-12-17 14:47:45 -05:00
commit 6968a5b02c
Signed by: talexander
GPG Key ID: D3A179C9A53C0EDE
9 changed files with 45 additions and 7 deletions

View File

@ -7,7 +7,7 @@
{?global_settings.page_title}<title>{global_settings.page_title}</title>{/global_settings.page_title} {?global_settings.page_title}<title>{global_settings.page_title}</title>{/global_settings.page_title}
</head> </head>
<body> <body>
{! TODO: Header bar with links? !} {#.page_header}{>page_header/}{/.page_header}
<div class="main_content"> <div class="main_content">
{@select key=.type} {@select key=.type}
{@eq value="blog_post_page"}{>blog_post_page/}{/eq} {@eq value="blog_post_page"}{>blog_post_page/}{/eq}

View File

@ -0,0 +1,4 @@
<div class="page_header">
<a href="{.home_link}">{.website_title}</a>
{! TODO: Additional links? !}
</div>

View File

@ -67,4 +67,8 @@ impl Config {
pub(crate) fn get_web_root(&self) -> Option<&str> { pub(crate) fn get_web_root(&self) -> Option<&str> {
self.raw.web_root.as_deref() self.raw.web_root.as_deref()
} }
pub(crate) fn get_site_title(&self) -> Option<&str> {
self.raw.site_title.as_deref()
}
} }

View File

@ -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. /// This is the struct for the writer.toml config file that ends up in each site's root directory.
#[derive(Deserialize, Serialize)] #[derive(Deserialize, Serialize)]
pub(crate) struct RawConfig { pub(crate) struct RawConfig {
site_title: String, pub(super) site_title: Option<String>,
author: Option<String>, author: Option<String>,
email: Option<String>, email: Option<String>,
pub(super) use_relative_paths: Option<bool>, pub(super) use_relative_paths: Option<bool>,
@ -14,7 +14,7 @@ pub(crate) struct RawConfig {
impl Default for RawConfig { impl Default for RawConfig {
fn default() -> Self { fn default() -> Self {
RawConfig { RawConfig {
site_title: "My super awesome website".to_owned(), site_title: None,
author: None, author: None,
email: None, email: None,
use_relative_paths: None, use_relative_paths: None,

View File

@ -2,6 +2,7 @@ use serde::Serialize;
use super::footnote_definition::RenderRealFootnoteDefinition; use super::footnote_definition::RenderRealFootnoteDefinition;
use super::GlobalSettings; use super::GlobalSettings;
use super::PageHeader;
use super::RenderDocumentElement; use super::RenderDocumentElement;
#[derive(Debug, Serialize)] #[derive(Debug, Serialize)]
@ -10,6 +11,8 @@ use super::RenderDocumentElement;
pub(crate) struct RenderBlogPostPage { pub(crate) struct RenderBlogPostPage {
global_settings: GlobalSettings, global_settings: GlobalSettings,
page_header: Option<PageHeader>,
/// The title that will be shown visibly on the page. /// The title that will be shown visibly on the page.
title: Option<String>, title: Option<String>,
@ -24,6 +27,7 @@ impl RenderBlogPostPage {
// TODO: Maybe these settings should be moved into a common struct so this can have the same type signature as the others. // TODO: Maybe these settings should be moved into a common struct so this can have the same type signature as the others.
pub(crate) fn new( pub(crate) fn new(
global_settings: GlobalSettings, global_settings: GlobalSettings,
page_header: Option<PageHeader>,
title: Option<String>, title: Option<String>,
self_link: Option<String>, self_link: Option<String>,
children: Vec<RenderDocumentElement>, children: Vec<RenderDocumentElement>,
@ -31,6 +35,7 @@ impl RenderBlogPostPage {
) -> RenderBlogPostPage { ) -> RenderBlogPostPage {
RenderBlogPostPage { RenderBlogPostPage {
global_settings, global_settings,
page_header,
title, title,
self_link, self_link,
children, children,

View File

@ -35,6 +35,7 @@ mod line_break;
mod macros; mod macros;
mod object; mod object;
mod org_macro; mod org_macro;
mod page_header;
mod paragraph; mod paragraph;
mod plain_link; mod plain_link;
mod plain_list; mod plain_list;
@ -69,4 +70,5 @@ pub(crate) use footnote_definition::RenderRealFootnoteDefinition;
pub(crate) use global_settings::GlobalSettings; pub(crate) use global_settings::GlobalSettings;
pub(crate) use heading::RenderHeading; pub(crate) use heading::RenderHeading;
pub(crate) use object::RenderObject; pub(crate) use object::RenderObject;
pub(crate) use page_header::PageHeader;
pub(crate) use section::RenderSection; pub(crate) use section::RenderSection;

View File

@ -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<String>,
home_link: Option<String>,
}
impl PageHeader {
pub(crate) fn new(website_title: Option<String>, home_link: Option<String>) -> PageHeader {
PageHeader {
website_title,
home_link,
}
}
}

View File

@ -4,6 +4,7 @@ use std::path::PathBuf;
use crate::config::Config; use crate::config::Config;
use crate::context::GlobalSettings; use crate::context::GlobalSettings;
use crate::context::PageHeader;
use crate::context::RenderBlogPostPage; use crate::context::RenderBlogPostPage;
use crate::context::RenderDocumentElement; use crate::context::RenderDocumentElement;
use crate::context::RenderRealFootnoteDefinition; use crate::context::RenderRealFootnoteDefinition;
@ -37,6 +38,10 @@ pub(crate) fn convert_blog_post_page_to_render_context<D: AsRef<Path>, F: AsRef<
"blog_post.js", "blog_post.js",
)?]; )?];
let global_settings = GlobalSettings::new(page.title.clone(), css_files, js_files); 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( let link_to_blog_post = get_web_path(
config, config,
output_directory, output_directory,
@ -76,6 +81,7 @@ pub(crate) fn convert_blog_post_page_to_render_context<D: AsRef<Path>, F: AsRef<
let ret = RenderBlogPostPage::new( let ret = RenderBlogPostPage::new(
global_settings, global_settings,
Some(page_header),
page.title.clone(), page.title.clone(),
Some(link_to_blog_post), Some(link_to_blog_post),
children, children,
@ -100,9 +106,7 @@ fn get_web_path<D: AsRef<Path>, F: AsRef<Path>, P: AsRef<Path>>(
containing_file_relative_to_output_directory containing_file_relative_to_output_directory
.parent() .parent()
.ok_or("File should exist in a folder.")?, .ok_or("File should exist in a folder.")?,
path_from_web_root path_from_web_root.parent().unwrap_or(&Path::new("")),
.parent()
.ok_or("File should exist in a folder.")?,
) )
.collect::<PathBuf>(); .collect::<PathBuf>();
// Subtracting 1 from the depth to "remove" the file name. // Subtracting 1 from the depth to "remove" the file name.

View File

@ -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. // 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)?; 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 parsed_context: serde_json::Value = serde_json::from_str(json_context.as_str())?;
let rendered_output = dust_renderer.render("main", Some(&parsed_context))?; let rendered_output = dust_renderer.render("main", Some(&parsed_context))?;
Ok(rendered_output) Ok(rendered_output)