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