Skip to content

Commit 36da26d

Browse files
committed
feat: new RenderWithAlt trait
1 parent 0005223 commit 36da26d

File tree

16 files changed

+83
-33
lines changed

16 files changed

+83
-33
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
maudit: patch
3+
---
4+
5+
Add a new RenderWithAlt trait for rendering images, enforcing the usage of alt when trying to render to a String

crates/maudit/src/assets.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ pub mod image_cache;
1010
mod script;
1111
mod style;
1212
mod tailwind;
13-
pub use image::{Image, ImageFormat, ImageOptions};
13+
pub use image::{Image, ImageFormat, ImageOptions, ImagePlaceholder, RenderWithAlt};
1414
pub use script::Script;
1515
pub use style::{Style, StyleOptions};
1616
pub use tailwind::TailwindPlugin;

crates/maudit/src/assets/image.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,3 +432,19 @@ fn thumbhash_to_png(thumbhash_rgba: &(usize, usize, Vec<u8>)) -> Vec<u8> {
432432

433433
bytes
434434
}
435+
436+
/// Trait to render an image with an alt text.
437+
pub trait RenderWithAlt {
438+
fn render(&self, alt: &str) -> String;
439+
}
440+
441+
impl RenderWithAlt for Image {
442+
fn render(&self, alt: &str) -> String {
443+
let (width, height) = self.dimensions();
444+
445+
format!(
446+
r#"<img src="{}" width="{}" height="{}" loading="lazy" decoding="async" alt="{}"/>"#,
447+
self.url, width, height, alt
448+
)
449+
}
450+
}

crates/maudit/src/route.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -737,7 +737,10 @@ pub mod prelude {
737737
CachedRoute, DynamicRouteContext, FullRoute, Page, PageContext, PageParams, Pages,
738738
PaginatedContentPage, PaginationPage, RenderResult, Route, RouteExt, paginate,
739739
};
740-
pub use crate::assets::{Asset, Image, ImageFormat, ImageOptions, Script, Style, StyleOptions};
740+
pub use crate::assets::{
741+
Asset, Image, ImageFormat, ImageOptions, ImagePlaceholder, RenderWithAlt, Script, Style,
742+
StyleOptions,
743+
};
741744
pub use crate::content::{
742745
ContentContext, ContentEntry, Entry, EntryInner, MarkdownContent, RouteContent,
743746
};

crates/maudit/src/templating/maud_ext.rs

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use maud::{Markup, Render, html};
22

33
use crate::{
44
GENERATOR,
5-
assets::{Asset, Image, Script, Style},
5+
assets::{Asset, Script, Style},
66
route::RenderResult,
77
};
88

@@ -22,15 +22,6 @@ impl Render for Script {
2222
}
2323
}
2424

25-
impl Render for Image {
26-
fn render(&self) -> Markup {
27-
let (width, height) = self.dimensions();
28-
html! {
29-
img src=(self.url()) width=(width) height=(height) loading="lazy" decoding="async";
30-
}
31-
}
32-
}
33-
3425
/// Can be used to create a generator tag in the output HTML. See [`GENERATOR`](crate::GENERATOR).
3526
pub fn generator() -> Markup {
3627
html! {

examples/basics/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod layout;
22

3-
use maudit::{BuildOptions, BuildOutput, coronate, routes};
3+
use maudit::{BuildOptions, BuildOutput, content_sources, coronate, routes};
44

55
mod routes {
66
mod index;
@@ -10,5 +10,5 @@ mod routes {
1010
pub use routes::Index;
1111

1212
fn main() -> Result<BuildOutput, Box<dyn std::error::Error>> {
13-
coronate(routes![Index], vec![].into(), BuildOptions::default())
13+
coronate(routes![Index], content_sources![], BuildOptions::default())
1414
}

examples/basics/src/routes/index.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ impl Route for Index {
1010
let logo = ctx.assets.add_image("images/logo.svg");
1111

1212
layout(html! {
13-
(logo)
13+
(logo.render("Maudit logo, a crudely drawn crown"))
1414
h1 { "Hello World" }
1515
})
1616
}
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
mod layout;
22

3-
use maudit::{BuildOptions, BuildOutput, coronate, routes};
3+
use maudit::{BuildOptions, BuildOutput, content_sources, coronate, routes};
44

55
mod routes {
66
mod index;
@@ -10,5 +10,5 @@ mod routes {
1010
pub use routes::Index;
1111

1212
fn main() -> Result<BuildOutput, Box<dyn std::error::Error>> {
13-
coronate(routes![Index], vec![].into(), BuildOptions::default())
13+
coronate(routes![Index], content_sources![], BuildOptions::default())
1414
}

examples/image-processing/src/routes/index.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@ impl Route for Index {
1818
);
1919

2020
layout(html! {
21-
(logo)
21+
(logo.render("Maudit logo, a crudely drawn crown"))
2222
h1 { "Hello World" }
2323
h2 { "Here's a 200x200 walrus:" }
24-
(walrus)
24+
(walrus.render("A walrus with tusks"))
2525
})
2626
}
2727
}

examples/kitchen-sink/src/main.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use maudit::{AssetsOptions, BuildOptions, BuildOutput, coronate, routes};
1+
use maudit::{AssetsOptions, BuildOptions, BuildOutput, content_sources, coronate, routes};
22

33
mod routes {
44
mod dynamic;
@@ -12,7 +12,7 @@ mod routes {
1212
fn main() -> Result<BuildOutput, Box<dyn std::error::Error>> {
1313
coronate(
1414
routes![routes::Index, routes::DynamicExample, routes::Endpoint],
15-
vec![].into(),
15+
content_sources![],
1616
BuildOptions {
1717
assets: AssetsOptions {
1818
tailwind_binary_path: "../../node_modules/.bin/tailwindcss".into(),

0 commit comments

Comments
 (0)