From 4c24c0354cb145750093e5228023b172077a6bba Mon Sep 17 00:00:00 2001 From: liquidev Date: Thu, 8 Feb 2024 11:34:09 +0100 Subject: [PATCH] add support for thumbnails --- content/programming/unreal-engine/fixes.tree | 3 +- crates/treehouse/src/cli/generate.rs | 35 +++++++++++-- crates/treehouse/src/tree/attributes.rs | 13 ++++- crates/treehouse/src/tree/mod.rs | 49 ++++++++++++++++++- static/pic/404.png | Bin 0 -> 1897 bytes template/tree.hbs | 4 ++ 6 files changed, 96 insertions(+), 8 deletions(-) create mode 100644 static/pic/404.png diff --git a/content/programming/unreal-engine/fixes.tree b/content/programming/unreal-engine/fixes.tree index 836c737..6d51a86 100644 --- a/content/programming/unreal-engine/fixes.tree +++ b/content/programming/unreal-engine/fixes.tree @@ -1,5 +1,6 @@ %% title = "new in Unreal Engine 5.4: data validation quick fixes" - thumbnail = "01HP1G5WC29GP4KQY1NV1F1RR1" + thumbnail.id = "01HP1G5WC29GP4KQY1NV1F1RR1" + thumbnail.alt = "a screenshot with a validation error in it; beneath the error there's a hint that it may be fixed automatically, coupled with a link you can click on to fix the issue" % id = "01HP1FESY3H9K1QVSM1XMNC8NS" - a few days ago I got a really cool change into Unreal, which allows you to add quick fixes to any data validation warning/error you emit: diff --git a/crates/treehouse/src/cli/generate.rs b/crates/treehouse/src/cli/generate.rs index ac75fa5..a72cb21 100644 --- a/crates/treehouse/src/cli/generate.rs +++ b/crates/treehouse/src/cli/generate.rs @@ -24,7 +24,7 @@ use crate::{ tree::branches_to_html, }, state::Source, - tree::{attributes::RootAttributes, SemaRoots}, + tree::SemaRoots, }; use crate::state::{FileId, Treehouse}; @@ -107,7 +107,11 @@ impl Generator { Ok(()) } - fn parse_trees(&self, paths: &Paths<'_>) -> anyhow::Result<(Treehouse, Vec)> { + fn parse_trees( + &self, + config: &Config, + paths: &Paths<'_>, + ) -> anyhow::Result<(Treehouse, Vec)> { let mut treehouse = Treehouse::new(); let mut parsed_trees = vec![]; @@ -148,7 +152,7 @@ impl Generator { ); if let Ok(roots) = parse_tree_with_diagnostics(&mut treehouse, file_id) { - let roots = SemaRoots::from_roots(&mut treehouse, file_id, roots); + let roots = SemaRoots::from_roots(&mut treehouse, config, file_id, roots); treehouse.roots.insert(tree_path.clone(), roots); parsed_trees.push(ParsedTree { tree_path, @@ -197,11 +201,18 @@ impl Generator { #[derive(Serialize)] pub struct Page { pub title: String, + pub thumbnail: Option, pub breadcrumbs: String, pub tree_path: Option, pub tree: String, } + #[derive(Serialize)] + pub struct Thumbnail { + pub url: String, + pub alt: Option, + } + #[derive(Serialize)] pub struct TemplateData<'a> { pub config: &'a Config, @@ -211,6 +222,22 @@ impl Generator { config, page: Page { title: roots.attributes.title.clone(), + thumbnail: roots + .attributes + .thumbnail + .as_ref() + .map(|thumbnail| Thumbnail { + url: format!( + "{}/static/pic/{}", + config.site, + config + .pics + .get(&thumbnail.id) + .map(|x| &**x) + .unwrap_or("404.png") + ), + alt: thumbnail.alt.clone(), + }), breadcrumbs, tree_path: treehouse .tree_path(parsed_tree.file_id) @@ -268,7 +295,7 @@ pub fn generate(paths: &Paths<'_>) -> anyhow::Result { info!("parsing tree"); let mut generator = Generator::default(); generator.add_directory_rec(paths.content_dir)?; - let (mut treehouse, parsed_trees) = generator.parse_trees(paths)?; + let (mut treehouse, parsed_trees) = generator.parse_trees(&config, paths)?; info!("generating navigation map"); let navigation_map = build_navigation_map(&treehouse, "index"); diff --git a/crates/treehouse/src/tree/attributes.rs b/crates/treehouse/src/tree/attributes.rs index ce88408..d4181f4 100644 --- a/crates/treehouse/src/tree/attributes.rs +++ b/crates/treehouse/src/tree/attributes.rs @@ -15,7 +15,18 @@ pub struct RootAttributes { /// ID of picture attached to the page, to be used as a thumbnail. #[serde(default)] - pub thumbnail: Option, + pub thumbnail: Option, +} + +/// A picture reference. +#[derive(Debug, Default, Clone, PartialEq, Eq, Deserialize, Serialize)] +pub struct Picture { + /// ID of the picture. + pub id: String, + + /// Optional alt text. + #[serde(default)] + pub alt: Option, } /// Branch attributes. diff --git a/crates/treehouse/src/tree/mod.rs b/crates/treehouse/src/tree/mod.rs index 35569f5..01ecf88 100644 --- a/crates/treehouse/src/tree/mod.rs +++ b/crates/treehouse/src/tree/mod.rs @@ -9,6 +9,7 @@ use treehouse_format::{ }; use crate::{ + config::Config, state::{toml_error_to_diagnostic, FileId, Source, TomlError, Treehouse}, tree::attributes::{Attributes, Content}, }; @@ -42,9 +43,14 @@ pub struct SemaRoots { } impl SemaRoots { - pub fn from_roots(treehouse: &mut Treehouse, file_id: FileId, roots: Roots) -> Self { + pub fn from_roots( + treehouse: &mut Treehouse, + config: &Config, + file_id: FileId, + roots: Roots, + ) -> Self { Self { - attributes: Self::parse_attributes(treehouse, file_id, &roots), + attributes: Self::parse_attributes(treehouse, config, file_id, &roots), branches: roots .branches .into_iter() @@ -55,6 +61,7 @@ impl SemaRoots { fn parse_attributes( treehouse: &mut Treehouse, + config: &Config, file_id: FileId, roots: &Roots, ) -> RootAttributes { @@ -88,6 +95,44 @@ impl SemaRoots { } } + if let Some(thumbnail) = &attributes.thumbnail { + if thumbnail.alt.is_none() { + treehouse.diagnostics.push(Diagnostic { + severity: Severity::Warning, + code: Some("sema".into()), + message: "thumbnail without alt text".into(), + labels: vec![Label { + style: LabelStyle::Primary, + file_id, + range: roots.attributes.as_ref().unwrap().percent.clone(), + message: "".into(), + }], + notes: vec![ + "note: alt text is important for people using screen readers".into(), + "help: add alt text using the thumbnail.alt key".into(), + ], + }) + } + + if !config.pics.contains_key(&thumbnail.id) { + treehouse.diagnostics.push(Diagnostic { + severity: Severity::Warning, + code: Some("sema".into()), + message: format!( + "thumbnail picture with id '{}' does not exist", + thumbnail.id + ), + labels: vec![Label { + style: LabelStyle::Primary, + file_id, + range: roots.attributes.as_ref().unwrap().percent.clone(), + message: "".into(), + }], + notes: vec!["note: check your id for typos".into()], + }) + } + } + attributes } } diff --git a/static/pic/404.png b/static/pic/404.png new file mode 100644 index 0000000000000000000000000000000000000000..9282dd1f6f9d2c80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;!X*>J;x;BGz&&#JP|vrlYA0()tSHp)sA2Irx&ZP?xFTuiVDL)v0Y$a`h7( zLNxn)Zk@4hM8^_%+6Kks4KP`+MRE-MP~!K&2F_r*sl1Pq*%m0=QWOkti2li9SL~lp zQIfH19tSSrJ5$5dB{}@tNlu5)y=v75dza;%GZeg{h&7rpk0Cz%V6+e!&lW7Cj&*Jigd$d z_ygw-iH-H?d&e2>-!8n(b{X&zT|9=JJElwJYd zuV?;=(3t^`i^MK?@W_L&r;0yVdjI-c%eAxBB+szP)@Lue^5v~BGA)=)zGb6dOs=SU zy4uY%O#8UNINxw>7R2*2eHdABu5r9r-n1n#RweU*hAxogI>d4pZ(_{e!z{#rma5@k zd$!r}r-FX_j}$vDoo-!Azb#evzTkZSB&e+5xPH;~pap)KjRWBfsvdi@v(lDX$+r}- zgzx7F7qUbXi+CDiO7CL8-CaCe6MTn#M!t3fkFHp$EK+Q|48f=xP(Q6w3z=*XAqMAKyT4}Wm&`Mv zoE*qFGDe`|{8n-T%>W_W2)9~_$%vj5GwRjnwHw*%?sa4PnF$a&9uUK-DTKQEIaq zXe(@2na=?+|7?IJ+BcRbz5=SCGxKdAv54>M_re^o&fp3(IU=xF$>-!pEU32~vKc;i z2l@`zwmGodu69QA)&38DJ%K?%Z1S5>Y1sbu%9c+Ikk;s^?X-Ika+f<~PSP=U#o+=| zusBJG;{p|*X{yG*Pe7{ikR#XmaWmA+n{{o{aVPV7r$5N8FVY59xO{)mr R3-NgbaJDYk#uI_5{{hzGc$okI literal 0 HcmV?d00001 diff --git a/template/tree.hbs b/template/tree.hbs index 0312931..1293eb9 100644 --- a/template/tree.hbs +++ b/template/tree.hbs @@ -18,6 +18,10 @@ It just needs to be a string replacement. --}} + {{#if page.thumbnail}} + + + {{/if}}