From 96fc77dc3ed2493b696c6d35daa6c7b67c7b199f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E3=83=AA=E3=82=AD=E8=90=8C?= Date: Sun, 31 Aug 2025 12:06:28 +0200 Subject: [PATCH] img width="" height="" generation for djot renderer --- src/html/djot.rs | 55 ++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 53 insertions(+), 2 deletions(-) diff --git a/src/html/djot.rs b/src/html/djot.rs index d473d76..c1fcd17 100644 --- a/src/html/djot.rs +++ b/src/html/djot.rs @@ -19,7 +19,9 @@ use crate::dirs::Dirs; use crate::state::FileId; use crate::state::Treehouse; use crate::vfs; +use crate::vfs::Dir; use crate::vfs::ImageSize; +use crate::vfs::VPathBuf; use super::highlight::highlight; @@ -476,9 +478,28 @@ impl<'a> Writer<'a> { Container::Image(src, link_type) => { if self.img_alt_text == 1 { if !src.is_empty() { - out.push_str(r#"" src=""#); + out.push_str(r#"" "#); if let SpanLinkType::Unresolved = link_type { // TODO: Image size. + let resolved_image = + resolve_image_link(self.renderer.config, src); + let size = if let Some(ResolvedImageLink::VPath(vpath)) = + &resolved_image + { + vfs::query::(&self.renderer.dirs.pic, vpath) + } else { + None + }; + + if let Some(size) = size { + write!( + out, + r#" width="{}" height="{}""#, + size.width, size.height + )?; + } + + out.push_str(r#" src=""#); if let Some(resolved) = resolve_link( self.renderer.config, self.renderer.treehouse, @@ -489,11 +510,14 @@ impl<'a> Writer<'a> { } else { write_attr(src, out); } + out.push('"'); } else { + out.push_str(r#" src=""#); write_attr(src, out); + out.push('"'); } } - out.push_str(r#"">"#); + out.push('>'); } self.img_alt_text -= 1; } @@ -672,3 +696,30 @@ pub fn resolve_link( _ => None, }) } + +#[derive(Debug, Clone)] +pub enum ResolvedImageLink { + VPath(VPathBuf), + Url(String), +} + +impl ResolvedImageLink { + pub fn into_url(self, config: &Config, pics_dir: &dyn Dir) -> Option { + match self { + ResolvedImageLink::VPath(vpath) => vfs::url(&config.site, pics_dir, &vpath), + ResolvedImageLink::Url(url) => Some(url), + } + } +} + +pub fn resolve_image_link(config: &Config, link: &str) -> Option { + link.split_once(':').and_then(|(kind, linked)| match kind { + "def" => config.defs.get(linked).cloned().map(ResolvedImageLink::Url), + "pic" => config + .pics + .get(linked) + .cloned() + .map(ResolvedImageLink::VPath), + _ => None, + }) +}