|
| 1 | +use comrak::html::{ChildRendering, Context, dangerous_url}; |
| 2 | +use comrak::nodes::NodeLink; |
| 3 | +use nom::branch::alt; |
| 4 | +use nom::bytes::tag; |
| 5 | + |
| 6 | +use nom::Parser; |
| 7 | +use nom::character::complete::digit1; |
| 8 | +use nom::combinator::{eof, map_res, opt}; |
| 9 | +use std::fmt; |
| 10 | +use std::fmt::{Display, Write}; |
| 11 | + |
| 12 | +enum ImgDimension { |
| 13 | + Px(u32), |
| 14 | + Percent(u32), |
| 15 | +} |
| 16 | + |
| 17 | +impl Display for ImgDimension { |
| 18 | + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
| 19 | + match self { |
| 20 | + Self::Px(n) => write!(f, "{n}px"), |
| 21 | + Self::Percent(n) => write!(f, "{n}%"), |
| 22 | + } |
| 23 | + } |
| 24 | +} |
| 25 | + |
| 26 | +struct SithImg<'a> { |
| 27 | + url: &'a str, |
| 28 | + width: Option<ImgDimension>, |
| 29 | + height: Option<ImgDimension>, |
| 30 | +} |
| 31 | + |
| 32 | +struct ParseDimensionError; |
| 33 | + |
| 34 | +/// Given a querystring that may contain image dimensions, |
| 35 | +/// parse it and return the result |
| 36 | +/// |
| 37 | +/// ## Note |
| 38 | +/// |
| 39 | +/// If the dimension string is valid, it will always contain the image width. |
| 40 | +/// However, the height is optional. |
| 41 | +fn parse_dimensions(s: &str) -> Result<(ImgDimension, Option<ImgDimension>), ParseDimensionError> { |
| 42 | + fn parse_dim(dim: &str) -> Result<(&str, ImgDimension), ParseDimensionError> { |
| 43 | + let parsed = ( |
| 44 | + map_res(digit1::<_, (_, _)>, str::parse), |
| 45 | + opt(alt((eof, tag("%"), tag("px")))), |
| 46 | + ) |
| 47 | + .parse(dim); |
| 48 | + let Ok((remaining, (val, unit))) = parsed else { |
| 49 | + return Err(ParseDimensionError); |
| 50 | + }; |
| 51 | + match unit { |
| 52 | + Some("%") => Ok((remaining, ImgDimension::Percent(val))), |
| 53 | + None | Some("px") | Some("") => Ok((remaining, ImgDimension::Px(val))), |
| 54 | + _ => Err(ParseDimensionError), |
| 55 | + } |
| 56 | + } |
| 57 | + let (remaining, width) = parse_dim(s)?; |
| 58 | + let height = if let Some(stripped) = remaining.strip_prefix("x") { |
| 59 | + Some(parse_dim(stripped)?.1) |
| 60 | + } else { |
| 61 | + None |
| 62 | + }; |
| 63 | + |
| 64 | + Ok((width, height)) |
| 65 | +} |
| 66 | + |
| 67 | +impl<'a> From<&'a str> for SithImg<'a> { |
| 68 | + fn from(s: &'a str) -> Self { |
| 69 | + // if the url contained dimension instructions, remove the query part |
| 70 | + // else, leave the url untouched |
| 71 | + if let Some((url, query)) = s.rsplit_once('?') |
| 72 | + && let Ok((width, height)) = parse_dimensions(query) |
| 73 | + { |
| 74 | + Self { |
| 75 | + url, |
| 76 | + width: Some(width), |
| 77 | + height, |
| 78 | + } |
| 79 | + } else { |
| 80 | + Self { |
| 81 | + url: s, |
| 82 | + width: None, |
| 83 | + height: None, |
| 84 | + } |
| 85 | + } |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +/// Render an image, with eventual size modifiers. |
| 90 | +/// |
| 91 | +/// This is basically a copy-paste of the comrack `render_image` function, |
| 92 | +/// with some code added |
| 93 | +pub(crate) fn render_image<T>( |
| 94 | + context: &mut Context<T>, |
| 95 | + entering: bool, |
| 96 | + nl: &NodeLink, |
| 97 | +) -> Result<ChildRendering, fmt::Error> { |
| 98 | + if entering { |
| 99 | + let img_data = SithImg::from(nl.url.as_str()); |
| 100 | + if context.options.render.figure_with_caption { |
| 101 | + context.write_str("<figure>")?; |
| 102 | + } |
| 103 | + context.write_str("<img src=\"")?; |
| 104 | + if !dangerous_url(img_data.url) { |
| 105 | + context.escape_href(img_data.url)?; |
| 106 | + } |
| 107 | + if img_data.width.is_some() || img_data.height.is_some() { |
| 108 | + context.write_str("\" style=\"")?; |
| 109 | + if let Some(width) = img_data.width { |
| 110 | + write!(context, "width:{}", width)?; |
| 111 | + } |
| 112 | + if let Some(height) = img_data.height { |
| 113 | + write!(context, ";height:{}", height)?; |
| 114 | + } |
| 115 | + } |
| 116 | + context.write_str("\" alt=\"")?; |
| 117 | + return Ok(ChildRendering::Plain); |
| 118 | + } else { |
| 119 | + if !nl.title.is_empty() { |
| 120 | + context.write_str("\" title=\"")?; |
| 121 | + context.escape(&nl.title)?; |
| 122 | + } |
| 123 | + context.write_str("\" />")?; |
| 124 | + if context.options.render.figure_with_caption { |
| 125 | + if !nl.title.is_empty() { |
| 126 | + context.write_str("<figcaption>")?; |
| 127 | + context.escape(&nl.title)?; |
| 128 | + context.write_str("</figcaption>")?; |
| 129 | + } |
| 130 | + context.write_str("</figure>")?; |
| 131 | + } |
| 132 | + } |
| 133 | + |
| 134 | + Ok(ChildRendering::HTML) |
| 135 | +} |
0 commit comments