Render playlist banners in build, make image base name generic

This commit is contained in:
Simon Repp
2024-03-19 19:31:11 +01:00
parent 60f9f5a549
commit bd4e751a94
4 changed files with 49 additions and 10 deletions
+10
View File
@@ -4,6 +4,7 @@ use indoc::formatdoc;
use crate::{Collection, Context, Site};
use crate::build::Breadcrumb;
use crate::build::playlist::banner_html;
use crate::build::render::{layout, thumbnail};
pub fn collection_html(
@@ -64,7 +65,16 @@ pub fn collection_html(
let slug = playlist.slug();
let title = playlist.title.as_deref().unwrap_or_else(|| playlist.slug());
let playlist_prefix = format!("{slug}/");
let banner = if let Some(banner) = &playlist.banner {
banner_html(banner, &playlist_prefix)
} else {
String::new()
};
formatdoc!(r#"
{banner}
<a href="{slug}{index_suffix}">{title}</a>
"#)
})
+26 -1
View File
@@ -2,10 +2,29 @@ use std::fs;
use indoc::formatdoc;
use crate::{Context, Playlist, Site};
use crate::{Banner, Context, Playlist, Site};
use crate::build::Breadcrumb;
use crate::build::render::{layout, thumbnail};
pub fn banner_html(banner: &Banner, playlist_prefix: &str) -> String {
let alt = if let Some(description) = &banner.description {
format!(r#"alt="{description}" "#)
} else {
String::new()
};
if let Some(poster_assets) = &banner.assets {
// TODO: Adjust up_to_xxx (depends on final content width we're using)
let image_attributes = poster_assets.img_attributes_up_to_1280("banner", playlist_prefix);
let src = image_attributes.src;
let srcset = image_attributes.srcset;
formatdoc!(r#"<img {alt}src="{src}" srcset="{srcset}">"#)
} else {
formatdoc!(r#"<img {alt}src="{playlist_prefix}banner.jpg">"#)
}
}
pub fn playlist_html(
context: &Context,
playlist: &Playlist,
@@ -21,6 +40,12 @@ pub fn playlist_html(
playlist.title.as_deref().unwrap_or_else(|| playlist.slug())
};
if let Some(banner) = &playlist.banner {
let playlist_prefix = "";
let img = banner_html(banner, playlist_prefix);
body.push_str(&img);
}
body.push_str(&format!("<h1>{title}</h1>\n\n"));
if let Some(description) = &playlist.description {
+1 -1
View File
@@ -366,7 +366,7 @@ pub fn thumbnail(video: &Video, video_prefix: &str) -> String {
match &video.poster {
Some(poster) => {
if let Some(poster_assets) = &poster.assets {
let image_attributes = poster_assets.img_attributes_up_to_480(video_prefix);
let image_attributes = poster_assets.img_attributes_up_to_480("poster", video_prefix);
let src = image_attributes.src;
let srcset = image_attributes.srcset;
+12 -8
View File
@@ -60,6 +60,7 @@ impl ImgAttributes {
/// Assets MUST be passed in ascending size
pub fn new(
assets_ascending_by_size: Vec<&PosterAsset>,
base_name: &str,
prefix: &str
) -> ImgAttributes {
let mut src = String::new();
@@ -68,10 +69,13 @@ impl ImgAttributes {
let mut asset_peek_iter = assets_ascending_by_size.iter().peekable();
while let Some(asset) = asset_peek_iter.next() {
srcset.push(format!("{}poster_{}x{}.jpg {}w", prefix, asset.width, asset.height, asset.width));
let height = asset.height;
let width = asset.width;
srcset.push(format!("{prefix}{base_name}_{width}x{height}.jpg {width}w"));
if asset_peek_iter.peek().is_none() {
src = format!("{}poster_{}x{}.jpg", prefix, asset.width, asset.height);
src = format!("{prefix}{base_name}_{width}x{height}.jpg");
}
}
@@ -95,16 +99,16 @@ impl PosterAssets {
result
}
pub fn img_attributes_up_to_320(&self, prefix: &str) -> ImgAttributes {
pub fn img_attributes_up_to_320(&self, base_name: &str, prefix: &str) -> ImgAttributes {
let assets = match &self.max_320 {
Some(max_320) => vec![&self.max_160, max_320],
None => vec![&self.max_160]
};
ImgAttributes::new(assets, prefix)
ImgAttributes::new(assets, base_name, prefix)
}
pub fn img_attributes_up_to_480(&self, prefix: &str) -> ImgAttributes {
pub fn img_attributes_up_to_480(&self, base_name: &str, prefix: &str) -> ImgAttributes {
let assets = match &self.max_320 {
Some(max_320) => match &self.max_480 {
Some(max_480) => vec![&self.max_160, max_320, max_480],
@@ -113,10 +117,10 @@ impl PosterAssets {
None => vec![&self.max_160]
};
ImgAttributes::new(assets, prefix)
ImgAttributes::new(assets, base_name, prefix)
}
pub fn img_attributes_up_to_1280(&self, prefix: &str) -> ImgAttributes {
pub fn img_attributes_up_to_1280(&self, base_name: &str, prefix: &str) -> ImgAttributes {
let mut assets = Vec::with_capacity(4);
assets.push(&self.max_160);
@@ -125,7 +129,7 @@ impl PosterAssets {
if let Some(asset) = &self.max_800 { assets.push(asset); }
if let Some(asset) = &self.max_1280 { assets.push(asset); }
ImgAttributes::new(assets, prefix)
ImgAttributes::new(assets, base_name, prefix)
}
pub fn largest(&self) -> &PosterAsset {