raw
1use nom::{
2 IResult, Parser,
3 bytes::complete::take_while1,
4 character::complete::{char, multispace0},
5 combinator::opt,
6 sequence::{delimited, preceded},
7};
8
9use crate::{
10 ast::{Image, Inline},
11 parser::link_util::{link_destination, link_title},
12};
13
14pub(super) fn image<'a>() -> impl FnMut(&'a str) -> IResult<&'a str, Inline> {
16 move |input: &'a str| {
17 let (input, alt) = preceded(
18 char('!'),
19 delimited(char('['), take_while1(|c| c != ']'), char(']')),
20 )
21 .parse(input)?;
22
23 let (input, (destination, title)) = delimited(
24 char('('),
25 (
26 preceded(multispace0, link_destination),
27 opt(preceded(multispace0, link_title)),
28 ),
29 preceded(multispace0, char(')')),
30 )
31 .parse(input)?;
32
33 Ok((
34 input,
35 Inline::Image(Image {
36 destination,
37 title,
38 alt: alt.to_owned(),
39 }),
40 ))
41 }
42}
43