2023-12-17 15:23:40 -05:00
|
|
|
use std::path::Path;
|
|
|
|
|
|
|
|
use serde::Serialize;
|
|
|
|
|
|
|
|
use crate::config::Config;
|
|
|
|
use crate::context::RenderDocumentElement;
|
|
|
|
use crate::context::RenderRealFootnoteDefinition;
|
|
|
|
use crate::error::CustomError;
|
2023-12-19 10:47:03 -05:00
|
|
|
use crate::intermediate::get_web_path;
|
2023-12-17 15:23:40 -05:00
|
|
|
use crate::intermediate::BlogPost;
|
|
|
|
|
|
|
|
use super::GlobalSettings;
|
|
|
|
use super::PageHeader;
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
#[serde(tag = "type")]
|
|
|
|
#[serde(rename = "blog_stream")]
|
|
|
|
pub(crate) struct RenderBlogStream {
|
|
|
|
global_settings: GlobalSettings,
|
|
|
|
page_header: Option<PageHeader>,
|
|
|
|
children: Vec<RenderBlogStreamEntry>,
|
2023-12-19 14:13:29 -05:00
|
|
|
stream_pagination: Option<RenderBlogStreamPagination>,
|
2023-12-17 15:23:40 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RenderBlogStream {
|
|
|
|
pub(crate) fn new(
|
|
|
|
config: &Config,
|
|
|
|
output_directory: &Path,
|
|
|
|
output_file: &Path,
|
2023-12-17 17:16:26 -05:00
|
|
|
original: &[&BlogPost],
|
2023-12-17 17:26:15 -05:00
|
|
|
older_link: Option<String>,
|
|
|
|
newer_link: Option<String>,
|
2023-12-17 15:23:40 -05:00
|
|
|
) -> Result<RenderBlogStream, CustomError> {
|
2023-12-19 10:47:03 -05:00
|
|
|
let css_files = vec![
|
|
|
|
// get_web_path(
|
|
|
|
// config,
|
|
|
|
// output_directory,
|
|
|
|
// output_file,
|
|
|
|
// "stylesheet/reset.css",
|
|
|
|
// )?,
|
|
|
|
get_web_path(config, output_directory, output_file, "stylesheet/main.css")?,
|
|
|
|
];
|
|
|
|
let js_files = vec![get_web_path(
|
|
|
|
config,
|
|
|
|
output_directory,
|
|
|
|
output_file,
|
|
|
|
"blog_post.js",
|
|
|
|
)?];
|
|
|
|
let global_settings = GlobalSettings::new(
|
|
|
|
config.get_site_title().map(str::to_string),
|
|
|
|
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 children = original
|
|
|
|
.into_iter()
|
|
|
|
.map(|blog_post| {
|
|
|
|
RenderBlogStreamEntry::new(config, output_directory, output_file, blog_post)
|
|
|
|
})
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
|
2023-12-19 14:13:29 -05:00
|
|
|
let stream_pagination = if older_link.is_some() || newer_link.is_some() {
|
|
|
|
Some(RenderBlogStreamPagination::new(older_link, newer_link)?)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
};
|
|
|
|
|
2023-12-19 10:47:03 -05:00
|
|
|
Ok(RenderBlogStream {
|
|
|
|
global_settings,
|
|
|
|
page_header: Some(page_header),
|
|
|
|
children,
|
2023-12-19 14:13:29 -05:00
|
|
|
stream_pagination,
|
2023-12-19 10:47:03 -05:00
|
|
|
})
|
2023-12-17 15:23:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub(crate) struct RenderBlogStreamEntry {
|
|
|
|
/// The title that will be shown visibly on the page.
|
|
|
|
title: Option<String>,
|
|
|
|
|
|
|
|
self_link: Option<String>,
|
|
|
|
|
|
|
|
children: Vec<RenderDocumentElement>,
|
|
|
|
|
|
|
|
footnotes: Vec<RenderRealFootnoteDefinition>,
|
|
|
|
}
|
2023-12-19 10:47:03 -05:00
|
|
|
|
|
|
|
impl RenderBlogStreamEntry {
|
2023-12-19 14:13:29 -05:00
|
|
|
fn new(
|
2023-12-19 10:47:03 -05:00
|
|
|
config: &Config,
|
|
|
|
output_directory: &Path,
|
|
|
|
output_file: &Path,
|
|
|
|
original: &BlogPost,
|
|
|
|
) -> Result<RenderBlogStreamEntry, CustomError> {
|
2023-12-19 10:59:34 -05:00
|
|
|
// TODO: This link is probably wrong.
|
|
|
|
let link_to_blog_post = get_web_path(
|
|
|
|
config,
|
|
|
|
output_directory,
|
|
|
|
output_file,
|
|
|
|
output_file.strip_prefix(output_directory)?,
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// TODO: Should I guess an index page instead of erroring out?
|
|
|
|
let index_page = original
|
|
|
|
.get_index_page()
|
|
|
|
.ok_or_else(|| format!("Blog post {} needs an index page.", original.id))?;
|
|
|
|
|
|
|
|
let title = index_page.title.clone();
|
|
|
|
|
|
|
|
// TODO: Handle footnotes.
|
|
|
|
let children = index_page
|
|
|
|
.children
|
|
|
|
.iter()
|
|
|
|
.map(|child| RenderDocumentElement::new(config, output_directory, output_file, child))
|
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
|
|
|
|
Ok(RenderBlogStreamEntry {
|
|
|
|
title,
|
|
|
|
self_link: Some(link_to_blog_post),
|
|
|
|
children,
|
|
|
|
footnotes: Vec::new(),
|
|
|
|
})
|
2023-12-19 10:47:03 -05:00
|
|
|
}
|
|
|
|
}
|
2023-12-19 14:13:29 -05:00
|
|
|
|
|
|
|
#[derive(Debug, Serialize)]
|
|
|
|
pub(crate) struct RenderBlogStreamPagination {
|
|
|
|
older_link: Option<String>,
|
|
|
|
newer_link: Option<String>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl RenderBlogStreamPagination {
|
|
|
|
fn new(
|
|
|
|
older_link: Option<String>,
|
|
|
|
newer_link: Option<String>,
|
|
|
|
) -> Result<RenderBlogStreamPagination, CustomError> {
|
|
|
|
Ok(RenderBlogStreamPagination {
|
|
|
|
older_link,
|
|
|
|
newer_link,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|