diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-10-08 09:11:42 +0300 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-10-09 18:28:59 +0300 |
commit | 934b1a53527e83e3d8d3dc4df992956332b512f7 (patch) | |
tree | a439264166e864389b136bd905cf1e737a970d08 /src | |
parent | 3c59880e533af059299a4c5848d32d9d623073b1 (diff) | |
download | subplot-934b1a53527e83e3d8d3dc4df992956332b512f7.tar.gz |
fix: add img alt attributes for diagrams and other images
They're not great, but we can't make them great without forcing the
user to specify a caption for each diagram.
Sponsored-by: author
Diffstat (limited to 'src')
-rw-r--r-- | src/html.rs | 9 | ||||
-rw-r--r-- | src/md.rs | 15 |
2 files changed, 14 insertions, 10 deletions
diff --git a/src/html.rs b/src/html.rs index 23579f9..1e0dafa 100644 --- a/src/html.rs +++ b/src/html.rs @@ -156,6 +156,7 @@ pub fn parse(filename: &Path, markdown: &str) -> Result<Element, HtmlError> { Tag::Image(_, url, title) => { let mut e = Element::new(ElementTag::Img); e.push_attribute(Attribute::new("src", url.as_ref())); + e.push_attribute(Attribute::new("alt", title.as_ref())); if !title.is_empty() { e.push_attribute(Attribute::new("title", title.as_ref())); } @@ -334,9 +335,11 @@ impl Element { fn fix_up_img_alt(&mut self) { if self.tag == ElementTag::Img { - let alt = as_plain_text(self.children()); - self.push_attribute(Attribute::new("alt", &alt)); - self.children.clear(); + if !self.attrs.iter().any(|a| a.name() == "alt") { + let alt = as_plain_text(self.children()); + self.push_attribute(Attribute::new("alt", &alt)); + self.children.clear(); + } } else { for child in self.children.iter_mut() { if let Content::Elt(kid) = child { @@ -382,19 +382,19 @@ mod typeset { fn typeset_dot(e: &Element) -> Result<Element, SubplotError> { let dot = e.content(); let svg = DotMarkup::new(&dot).as_svg()?; - Ok(svg_to_element(svg)) + Ok(svg_to_element(svg, "Dot diagram")) } fn typeset_plantuml(e: &Element) -> Result<Element, SubplotError> { let markup = e.content(); let svg = PlantumlMarkup::new(&markup).as_svg()?; - Ok(svg_to_element(svg)) + Ok(svg_to_element(svg, "UML diagram")) } fn typeset_pikchr(e: &Element) -> Result<Element, SubplotError> { let markup = e.content(); let svg = PikchrMarkup::new(&markup, None).as_svg()?; - Ok(svg_to_element(svg)) + Ok(svg_to_element(svg, "Pikchr diagram")) } fn typeset_roadmap(e: &Element) -> Result<Element, SubplotError> { @@ -404,12 +404,12 @@ mod typeset { let roadmap = roadmap::from_yaml(&yaml)?; let dot = roadmap.format_as_dot(WIDTH)?; let svg = DotMarkup::new(&dot).as_svg()?; - Ok(svg_to_element(svg)) + Ok(svg_to_element(svg, "Road map")) } - fn svg_to_element(svg: Svg) -> Element { + fn svg_to_element(svg: Svg, alt: &str) -> Element { let url = svg_as_data_url(svg); - let img = html_img(&url); + let img = html_img(&url, alt); html_p(vec![Content::Elt(img)]) } @@ -426,9 +426,10 @@ mod typeset { new } - fn html_img(src: &str) -> Element { + fn html_img(src: &str, alt: &str) -> Element { let mut new = Element::new(ElementTag::Img); new.push_attribute(Attribute::new("src", src)); + new.push_attribute(Attribute::new("alt", alt)); new } } |