add support for thumbnails

This commit is contained in:
liquidex 2024-02-08 11:34:09 +01:00
parent d7e42667fe
commit 4c24c0354c
6 changed files with 96 additions and 8 deletions

View file

@ -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:

View file

@ -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<ParsedTree>)> {
fn parse_trees(
&self,
config: &Config,
paths: &Paths<'_>,
) -> anyhow::Result<(Treehouse, Vec<ParsedTree>)> {
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<Thumbnail>,
pub breadcrumbs: String,
pub tree_path: Option<String>,
pub tree: String,
}
#[derive(Serialize)]
pub struct Thumbnail {
pub url: String,
pub alt: Option<String>,
}
#[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<Treehouse> {
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");

View file

@ -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<String>,
pub thumbnail: Option<Picture>,
}
/// 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<String>,
}
/// Branch attributes.

View file

@ -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
}
}

BIN
static/pic/404.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.9 KiB

View file

@ -18,6 +18,10 @@
It just needs to be a string replacement.
--}}
<!-- treehouse-ca37057a-cff5-45b3-8415-3b02dbf6c799-per-branch-metadata -->
{{#if page.thumbnail}}
<meta property="og:image" content="{{ page.thumbnail.url }}">
<meta property="og:image:alt" content="{{ page.thumbnail.alt }}">
{{/if}}
<link rel="stylesheet" href="{{ config.site }}/static/css/main.css">
<link rel="stylesheet" href="{{ config.site }}/static/css/tree.css">