mirror of
https://github.com/zyllian/webdog.git
synced 2025-05-09 01:56:42 -07:00
refactoring and new additions wheee
This commit is contained in:
parent
94ba05daf2
commit
3a274932a8
10 changed files with 192 additions and 91 deletions
|
@ -1,3 +1,7 @@
|
|||
---
|
||||
extra: index
|
||||
---
|
||||
|
||||
# zyl's website
|
||||
|
||||
hi! i'm zyl, a trans doggirlthing <abbr title="therian">ΘΔ</abbr> in the pnw.
|
||||
|
|
|
@ -35,8 +35,14 @@ a {
|
|||
}
|
||||
}
|
||||
|
||||
main.page {
|
||||
margin: 8px;
|
||||
#content {
|
||||
main.page {
|
||||
margin: 0 8px 8px 8px;
|
||||
}
|
||||
|
||||
.index-info {
|
||||
margin: 8px;
|
||||
}
|
||||
}
|
||||
|
||||
abbr {
|
||||
|
|
|
@ -2,8 +2,10 @@
|
|||
<html lang="en">
|
||||
|
||||
<head>
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="referrer" content="no-referrer">
|
||||
<link rel="stylesheet" href="/styles/index.css">
|
||||
<title>{{title}}</title>
|
||||
{{{head}}}
|
||||
</head>
|
||||
|
||||
|
@ -18,9 +20,11 @@
|
|||
<a href="/images/">images</a> |
|
||||
<a href="https://github.com/zyllian/zyllian.github.io" rel="noopener noreferrer">source</a>
|
||||
</header>
|
||||
<main class="page">
|
||||
{{{page}}}
|
||||
</main>
|
||||
<div id="content">
|
||||
<main class="page">
|
||||
{{{page}}}
|
||||
</main>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
|
|
12
site/templates/extras/index-injection.hbs
Normal file
12
site/templates/extras/index-injection.hbs
Normal file
|
@ -0,0 +1,12 @@
|
|||
<hr />
|
||||
<div class="index-info">
|
||||
<h3>most recent blog posts</h3>
|
||||
<div class="blog-post-list">
|
||||
{{#each resources}}
|
||||
<div class="post">
|
||||
<p class="title"><a href="/blog/{{id}}">{{title}}</a></p>
|
||||
<p class="short-desc">{{desc}}</p>
|
||||
</div>
|
||||
{{/each}}
|
||||
</div>
|
||||
</div>
|
27
src/blog.rs
27
src/blog.rs
|
@ -1,16 +1,15 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
builder::SiteBuilder,
|
||||
resource::{ResourceBuilder, ResourceBuilderConfig, ResourceMetadata, ResourceMethods},
|
||||
SiteConfig,
|
||||
resource::{ResourceBuilderConfig, ResourceMetadata, ResourceMethods},
|
||||
Site, SiteConfig,
|
||||
};
|
||||
|
||||
pub const BLOG_PATH: &str = "blog";
|
||||
|
||||
/// Builds the blog.
|
||||
pub fn build_blog(site_builder: &SiteBuilder) -> anyhow::Result<()> {
|
||||
let config = ResourceBuilderConfig {
|
||||
/// Gets the blog's resource configuration.
|
||||
pub fn get_blog_resource_config(site: &Site) -> ResourceBuilderConfig {
|
||||
ResourceBuilderConfig {
|
||||
source_path: BLOG_PATH.to_string(),
|
||||
output_path_short: BLOG_PATH.to_string(),
|
||||
output_path_long: BLOG_PATH.to_string(),
|
||||
|
@ -19,17 +18,11 @@ pub fn build_blog(site_builder: &SiteBuilder) -> anyhow::Result<()> {
|
|||
rss_template: "rss/blog-post".to_string(),
|
||||
rss_title: "zyl's blog".to_string(),
|
||||
rss_description: "Feed of recent blog posts on zyl's website.".to_string(),
|
||||
list_title: "Blog".to_string(),
|
||||
tag_list_title: "Blog Tags".to_string(),
|
||||
resource_name_plural: "Blog posts".to_string(),
|
||||
resources_per_page: site_builder.site.config.blog_posts_per_page,
|
||||
};
|
||||
|
||||
let mut builder = ResourceBuilder::<BlogPostMetadata, BlogPostTemplateData>::new(config);
|
||||
builder.load_all(site_builder)?;
|
||||
builder.build_all(site_builder)?;
|
||||
|
||||
Ok(())
|
||||
list_title: "blog".to_string(),
|
||||
tag_list_title: "blog tags".to_string(),
|
||||
resource_name_plural: "blog posts".to_string(),
|
||||
resources_per_page: site.config.blog_posts_per_page,
|
||||
}
|
||||
}
|
||||
|
||||
/// Metadata for a blog post.
|
||||
|
|
112
src/builder.rs
112
src/builder.rs
|
@ -10,16 +10,20 @@ use pulldown_cmark::{Options, Parser};
|
|||
use serde::Serialize;
|
||||
use url::Url;
|
||||
|
||||
use crate::{util, PageMetadata, Site, ROOT_PATH, SASS_PATH};
|
||||
use crate::{
|
||||
extras::Extra, resource::ResourceBuilder, util, PageMetadata, Site, ROOT_PATH, SASS_PATH,
|
||||
};
|
||||
|
||||
/// Struct containing data to be sent to templates when rendering them.
|
||||
#[derive(Debug, Serialize)]
|
||||
struct TemplateData<'a, T> {
|
||||
/// The rendered page.
|
||||
pub page: &'a str,
|
||||
/// The page's title.
|
||||
pub title: &'a str,
|
||||
/// Custom template data.
|
||||
#[serde(flatten)]
|
||||
pub extra: T,
|
||||
pub extra_data: T,
|
||||
}
|
||||
|
||||
/// Struct used to build the site.
|
||||
|
@ -34,6 +38,13 @@ pub struct SiteBuilder<'a> {
|
|||
pub build_path: PathBuf,
|
||||
/// Whether the site is going to be served locally with the dev server.
|
||||
serving: bool,
|
||||
|
||||
/// Resource builder for the site's images section.
|
||||
pub images_builder:
|
||||
ResourceBuilder<crate::images::ImageMetadata, crate::images::ImageTemplateData>,
|
||||
/// Resource builder for the site's blog section.
|
||||
pub blog_builder:
|
||||
ResourceBuilder<crate::blog::BlogPostMetadata, crate::blog::BlogPostTemplateData>,
|
||||
}
|
||||
|
||||
impl<'a> SiteBuilder<'a> {
|
||||
|
@ -50,6 +61,8 @@ impl<'a> SiteBuilder<'a> {
|
|||
Self {
|
||||
matter: Matter::new(),
|
||||
reg: Handlebars::new(),
|
||||
images_builder: ResourceBuilder::new(crate::images::get_images_resource_config(&site)),
|
||||
blog_builder: ResourceBuilder::new(crate::blog::get_blog_resource_config(&site)),
|
||||
site,
|
||||
build_path,
|
||||
serving,
|
||||
|
@ -94,48 +107,24 @@ impl<'a> SiteBuilder<'a> {
|
|||
std::fs::create_dir(images_path).context("Failed to create images path")?;
|
||||
}
|
||||
|
||||
self.images_builder
|
||||
.load_all(&self)
|
||||
.context("Failed to load images metadata")?;
|
||||
self.blog_builder
|
||||
.load_all(&self)
|
||||
.context("Failed to load blog metadata")?;
|
||||
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
pub fn build_page_raw(
|
||||
&self,
|
||||
page_metadata: PageMetadata,
|
||||
page_html: &str,
|
||||
) -> anyhow::Result<String> {
|
||||
self.build_page_raw_extra(page_metadata, page_html, ())
|
||||
}
|
||||
|
||||
/// Helper to build a page without writing it to disk.
|
||||
pub fn build_page_raw_extra<T>(
|
||||
&self,
|
||||
page_metadata: PageMetadata,
|
||||
page_html: &str,
|
||||
extra: T,
|
||||
) -> anyhow::Result<String>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let out = self.reg.render(
|
||||
&page_metadata.template.unwrap_or_else(|| "base".to_string()),
|
||||
&TemplateData {
|
||||
page: page_html,
|
||||
extra,
|
||||
},
|
||||
)?;
|
||||
|
||||
let title = match &page_metadata.title {
|
||||
Some(page_title) => format!("{} / {}", self.site.config.title, page_title),
|
||||
_ => self.site.config.title.clone(),
|
||||
};
|
||||
|
||||
// Modify HTML output
|
||||
/// Function to rewrite HTML wow.
|
||||
pub fn rewrite_html(&self, html: String) -> anyhow::Result<String> {
|
||||
let mut output = Vec::new();
|
||||
let mut rewriter = HtmlRewriter::new(
|
||||
Settings {
|
||||
element_content_handlers: vec![
|
||||
element!("head", |el| {
|
||||
el.prepend(r#"<meta charset="utf-8">"#, ContentType::Html);
|
||||
el.append(&format!("<title>{}</title>", title), ContentType::Html);
|
||||
if self.serving {
|
||||
el.append(r#"<script src="/_dev.js"></script>"#, ContentType::Html);
|
||||
}
|
||||
|
@ -180,10 +169,55 @@ impl<'a> SiteBuilder<'a> {
|
|||
|c: &[u8]| output.extend_from_slice(c),
|
||||
);
|
||||
|
||||
rewriter.write(out.as_bytes())?;
|
||||
rewriter.write(html.as_bytes())?;
|
||||
rewriter.end()?;
|
||||
|
||||
let mut out = String::from_utf8(output)?;
|
||||
Ok(String::from_utf8(output)?)
|
||||
}
|
||||
|
||||
pub fn build_page_raw(
|
||||
&self,
|
||||
page_metadata: PageMetadata,
|
||||
page_html: &str,
|
||||
) -> anyhow::Result<String> {
|
||||
self.build_page_raw_with_extra_data(page_metadata, page_html, ())
|
||||
}
|
||||
|
||||
/// Helper to build a page without writing it to disk.
|
||||
pub fn build_page_raw_with_extra_data<T>(
|
||||
&self,
|
||||
page_metadata: PageMetadata,
|
||||
page_html: &str,
|
||||
extra_data: T,
|
||||
) -> anyhow::Result<String>
|
||||
where
|
||||
T: Serialize,
|
||||
{
|
||||
let extra = page_metadata
|
||||
.extra
|
||||
.and_then(|extra| crate::extras::get_extra(&extra));
|
||||
|
||||
let title = match &page_metadata.title {
|
||||
Some(page_title) => format!("{} / {}", self.site.config.title, page_title),
|
||||
_ => self.site.config.title.clone(),
|
||||
};
|
||||
|
||||
let out = self.reg.render(
|
||||
&page_metadata.template.unwrap_or_else(|| "base".to_string()),
|
||||
&TemplateData {
|
||||
page: page_html,
|
||||
title: &title,
|
||||
extra_data,
|
||||
},
|
||||
)?;
|
||||
|
||||
// Modify HTML output
|
||||
let mut out = self.rewrite_html(out)?;
|
||||
|
||||
if let Some(Extra::HtmlModification(f)) = extra {
|
||||
out = f(out, self)?;
|
||||
}
|
||||
|
||||
if !self.serving {
|
||||
out = minifier::html::minify(&out);
|
||||
}
|
||||
|
@ -269,11 +303,11 @@ impl<'a> SiteBuilder<'a> {
|
|||
|
||||
/// Builds the site's various image pages.
|
||||
pub fn build_images(&self) -> anyhow::Result<()> {
|
||||
crate::images::build_images(self)
|
||||
self.images_builder.build_all(self)
|
||||
}
|
||||
|
||||
/// Builds the site's blog.
|
||||
pub fn build_blog(&self) -> anyhow::Result<()> {
|
||||
crate::blog::build_blog(self)
|
||||
self.blog_builder.build_all(self)
|
||||
}
|
||||
}
|
||||
|
|
45
src/extras.rs
Normal file
45
src/extras.rs
Normal file
|
@ -0,0 +1,45 @@
|
|||
use lol_html::{element, RewriteStrSettings};
|
||||
use serde::Serialize;
|
||||
|
||||
use crate::{blog::BlogPostMetadata, builder::SiteBuilder, resource::ResourceMetadata};
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Extra {
|
||||
HtmlModification(fn(page: String, builder: &SiteBuilder) -> anyhow::Result<String>),
|
||||
}
|
||||
|
||||
/// Gets the extra for the given value.
|
||||
pub fn get_extra(extra: &str) -> Option<Extra> {
|
||||
match extra {
|
||||
"index" => Some(Extra::HtmlModification(index)),
|
||||
_ => None,
|
||||
}
|
||||
}
|
||||
|
||||
/// Extra to add a sidebar to the index page with recent blog posts on it.
|
||||
fn index(page: String, builder: &SiteBuilder) -> anyhow::Result<String> {
|
||||
#[derive(Debug, Serialize)]
|
||||
struct SidebarTemplateData<'r> {
|
||||
resources: Vec<&'r ResourceMetadata<BlogPostMetadata>>,
|
||||
}
|
||||
|
||||
let lmd = builder.blog_builder.loaded_metadata.borrow();
|
||||
|
||||
let sidebar = builder.reg.render(
|
||||
"extras/index-injection",
|
||||
&SidebarTemplateData {
|
||||
resources: lmd.iter().take(3).map(|(_, v)| v).collect(),
|
||||
},
|
||||
)?;
|
||||
|
||||
Ok(lol_html::rewrite_str(
|
||||
&page,
|
||||
RewriteStrSettings {
|
||||
element_content_handlers: vec![element!("#content", |el| {
|
||||
el.append(&sidebar, lol_html::html_content::ContentType::Html);
|
||||
Ok(())
|
||||
})],
|
||||
..Default::default()
|
||||
},
|
||||
)?)
|
||||
}
|
|
@ -1,17 +1,16 @@
|
|||
use serde::{Deserialize, Serialize};
|
||||
|
||||
use crate::{
|
||||
builder::SiteBuilder,
|
||||
resource::{ResourceBuilder, ResourceBuilderConfig, ResourceMetadata, ResourceMethods},
|
||||
SiteConfig,
|
||||
resource::{ResourceBuilderConfig, ResourceMetadata, ResourceMethods},
|
||||
Site, SiteConfig,
|
||||
};
|
||||
|
||||
pub(crate) const IMAGES_PATH: &str = "images";
|
||||
pub(crate) const IMAGES_OUT_PATH: &str = "i";
|
||||
|
||||
/// Builds the site's image pages.
|
||||
pub fn build_images(site_builder: &SiteBuilder) -> anyhow::Result<()> {
|
||||
let config = ResourceBuilderConfig {
|
||||
/// Gets the resource configuration for images.
|
||||
pub fn get_images_resource_config(site: &Site) -> ResourceBuilderConfig {
|
||||
ResourceBuilderConfig {
|
||||
source_path: IMAGES_PATH.to_string(),
|
||||
output_path_short: IMAGES_OUT_PATH.to_string(),
|
||||
output_path_long: "images".to_string(),
|
||||
|
@ -19,18 +18,12 @@ pub fn build_images(site_builder: &SiteBuilder) -> anyhow::Result<()> {
|
|||
resource_list_template: "images".to_string(),
|
||||
rss_template: "rss/image".to_string(),
|
||||
rss_title: "zyl's images".to_string(),
|
||||
rss_description: "Feed of newly uploaded images from zyl's website.".to_string(),
|
||||
list_title: "Images".to_string(),
|
||||
tag_list_title: "Image Tags".to_string(),
|
||||
resource_name_plural: "Images".to_string(),
|
||||
resources_per_page: site_builder.site.config.images_per_page,
|
||||
};
|
||||
|
||||
let mut builder = ResourceBuilder::<ImageMetadata, ImageTemplateData>::new(config);
|
||||
builder.load_all(site_builder)?;
|
||||
builder.build_all(site_builder)?;
|
||||
|
||||
Ok(())
|
||||
rss_description: "feed of newly uploaded images from zyl's website.".to_string(),
|
||||
list_title: "images".to_string(),
|
||||
tag_list_title: "image tags".to_string(),
|
||||
resource_name_plural: "images".to_string(),
|
||||
resources_per_page: site.config.images_per_page,
|
||||
}
|
||||
}
|
||||
|
||||
/// Definition for a remote image.
|
||||
|
@ -46,7 +39,7 @@ pub struct ImageMetadata {
|
|||
|
||||
/// Template data for a specific image.
|
||||
#[derive(Debug, Serialize)]
|
||||
struct ImageTemplateData {
|
||||
pub struct ImageTemplateData {
|
||||
/// Direct URL to the image's CDN location.
|
||||
/// TODO: link to smaller versions on list pages
|
||||
src: String,
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
mod blog;
|
||||
mod builder;
|
||||
mod extras;
|
||||
mod images;
|
||||
mod link_list;
|
||||
mod resource;
|
||||
|
@ -65,6 +66,8 @@ pub struct PageMetadata {
|
|||
pub title: Option<String>,
|
||||
/// The template to use for the page. If not specified, it defaults to "base".
|
||||
pub template: Option<String>,
|
||||
/// The extra stuff to run for the page, if any.
|
||||
pub extra: Option<String>,
|
||||
}
|
||||
|
||||
/// Struct containing information about the site.
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
use std::{
|
||||
cell::RefCell,
|
||||
collections::BTreeMap,
|
||||
marker::PhantomData,
|
||||
path::{Path, PathBuf},
|
||||
|
@ -13,6 +14,10 @@ use time::{format_description::well_known::Rfc2822, OffsetDateTime};
|
|||
|
||||
use crate::{builder::SiteBuilder, link_list::Link, PageMetadata, SiteConfig};
|
||||
|
||||
pub trait ResourceConfig<R> {
|
||||
fn new(builder: &SiteBuilder) -> Self;
|
||||
}
|
||||
|
||||
/// Metadata for resources.
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
pub struct ResourceMetadata<T> {
|
||||
|
@ -126,9 +131,9 @@ pub struct ResourceBuilderConfig {
|
|||
#[derive(Debug)]
|
||||
pub struct ResourceBuilder<M, E> {
|
||||
/// The builder's config.
|
||||
config: ResourceBuilderConfig,
|
||||
pub config: ResourceBuilderConfig,
|
||||
/// The currently loaded resource metadata.
|
||||
loaded_metadata: Vec<(String, ResourceMetadata<M>)>,
|
||||
pub loaded_metadata: RefCell<Vec<(String, ResourceMetadata<M>)>>,
|
||||
_extra: PhantomData<E>,
|
||||
}
|
||||
|
||||
|
@ -176,8 +181,9 @@ where
|
|||
}
|
||||
|
||||
/// Loads all resource metadata from the given config.
|
||||
pub fn load_all(&mut self, builder: &SiteBuilder) -> anyhow::Result<()> {
|
||||
self.loaded_metadata.clear();
|
||||
pub fn load_all(&self, builder: &SiteBuilder) -> anyhow::Result<()> {
|
||||
let mut lmd = self.loaded_metadata.borrow_mut();
|
||||
lmd.clear();
|
||||
for e in builder
|
||||
.site
|
||||
.site_path
|
||||
|
@ -190,11 +196,10 @@ where
|
|||
if cfg!(not(debug_assertions)) && metadata.draft {
|
||||
continue;
|
||||
}
|
||||
self.loaded_metadata.push((id, metadata));
|
||||
lmd.push((id, metadata));
|
||||
}
|
||||
}
|
||||
self.loaded_metadata
|
||||
.sort_by(|a, b| b.1.timestamp.cmp(&a.1.timestamp));
|
||||
lmd.sort_by(|a, b| b.1.timestamp.cmp(&a.1.timestamp));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -224,7 +229,7 @@ where
|
|||
builder.reg.render(&self.config.resource_template, &data)?
|
||||
};
|
||||
|
||||
let out = builder.build_page_raw_extra(
|
||||
let out = builder.build_page_raw_with_extra_data(
|
||||
PageMetadata {
|
||||
title: Some(resource.title.clone()),
|
||||
..Default::default()
|
||||
|
@ -250,12 +255,14 @@ where
|
|||
std::fs::create_dir_all(&out_long)?;
|
||||
}
|
||||
|
||||
for (id, resource) in &self.loaded_metadata {
|
||||
let lmd = self.loaded_metadata.borrow();
|
||||
|
||||
for (id, resource) in lmd.iter() {
|
||||
self.build(builder, id.clone(), resource)?;
|
||||
}
|
||||
|
||||
let mut data = Vec::with_capacity(self.loaded_metadata.len());
|
||||
for (id, resource) in &self.loaded_metadata {
|
||||
let mut data = Vec::with_capacity(lmd.len());
|
||||
for (id, resource) in lmd.iter() {
|
||||
let extra = resource.get_extra_resource_template_data(&builder.site.config)?;
|
||||
data.push(ResourceTemplateData {
|
||||
resource,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue