#![allow(dead_code)]
use super::markdown::md_link_destination;
use crate::parser::markdown::md_link_destination_enclosed;
use crate::parser::markdown::md_link_text;
use crate::parser::Link;
use crate::take_until_unbalanced;
use html_escape::decode_html_entities;
use nom::combinator::*;
use nom::{bytes::complete::tag, sequence::tuple};
use std::borrow::Cow;
pub fn md_img_link(i: &str) -> nom::IResult<&str, Link> {
let (i, (alt, src)) = md_img(i)?;
Ok((i, Link::Image(alt, src)))
}
pub fn md_img(i: &str) -> nom::IResult<&str, (Cow<str>, Cow<str>)> {
nom::sequence::preceded(
tag("!"),
nom::sequence::tuple((md_link_text, md_img_link_destination_enclosed)),
)(i)
}
fn md_img_link_destination_enclosed(i: &str) -> nom::IResult<&str, Cow<str>> {
map_parser(
nom::sequence::delimited(tag("("), take_until_unbalanced('(', ')'), tag(")")),
md_link_destination,
)(i)
}
pub fn md_img2dest_link(i: &str) -> nom::IResult<&str, Link> {
let (i, (text1, img_alt, img_src, text2, dest, title)) = md_img2dest(i)?;
Ok((
i,
Link::Image2Dest(text1, img_alt, img_src, text2, dest, title),
))
}
pub fn md_img2dest(
i: &str,
) -> nom::IResult<&str, (Cow<str>, Cow<str>, Cow<str>, Cow<str>, Cow<str>, Cow<str>)> {
map(
nom::sequence::tuple((
map_parser(
nom::sequence::delimited(tag("["), take_until_unbalanced('[', ']'), tag("]")),
tuple((
nom::bytes::complete::take_until("!["),
md_img,
nom::combinator::rest,
)),
),
md_link_destination_enclosed,
)),
|((a, (b, c), d), (e, f))| (decode_html_entities(a), b, c, decode_html_entities(d), e, f),
)(i)
}