#[rustfmt::skip]
pub mod no_rustfmt_block {
use crate::*;
use constants::*;
use nom::{
branch::*, bytes::complete::*, character::complete::*, combinator::*, multi::*,
sequence::*, IResult,
};
pub fn parse_element_bold_italic(input: &str) -> IResult<&str, &str> {
alt((
delimited( tag(BITALIC_1), is_not(BITALIC_1), tag(BITALIC_1)),
delimited( tag(BITALIC_2), is_not(BITALIC_2), tag(BITALIC_2)),
))(input)
}
pub fn parse_element_bold(input: &str) -> IResult<&str, &str> {
alt((
delimited( tag(BOLD_1), is_not(BOLD_1), tag(BOLD_1)),
delimited( tag(BOLD_2), is_not(BOLD_2), tag(BOLD_2)),
))(input)
}
pub fn parse_element_italic(input: &str) -> IResult<&str, &str> {
alt((
delimited( tag(ITALIC_1), is_not(ITALIC_1), tag(ITALIC_1)),
delimited( tag(ITALIC_2), is_not(ITALIC_2), tag(ITALIC_2)),
))(input)
}
pub fn parse_element_code(input: &str) -> IResult<&str, &str> {
delimited( tag(BACKTICK), is_not(BACKTICK), tag(BACKTICK))(input)
}
pub fn parse_element_link(input: &str) -> IResult<&str, (&str, &str)> {
pair(
delimited( tag(LEFT_BRACKET), is_not(RIGHT_BRACKET), tag(RIGHT_BRACKET)),
delimited( tag(LEFT_PAREN), is_not(RIGHT_PAREN), tag(RIGHT_PAREN)),
)(input)
}
pub fn parse_element_image(input: &str) -> IResult<&str, (&str, &str)> {
pair(
delimited( tag(LEFT_IMG), is_not(RIGHT_IMG), tag(RIGHT_IMG)),
delimited( tag(LEFT_PAREN), is_not(RIGHT_PAREN), tag(RIGHT_PAREN)),
)(input)
}
pub fn parse_element_plaintext(input: &str) -> IResult<&str, &str> {
recognize(
many1(
preceded(
not(
alt((
tag(BITALIC_1),
tag(BITALIC_2),
tag(BOLD_1),
tag(BOLD_2),
tag(ITALIC_1),
tag(ITALIC_2),
tag(BACKTICK),
tag(LEFT_BRACKET),
tag(LEFT_IMG),
tag(NEW_LINE),
))
),
anychar,
)
)
)(input)
}
pub fn parse_element_markdown_inline(input: &str) -> IResult<&str, Fragment> {
alt((
map(parse_element_italic, Fragment::Italic),
map(parse_element_bold, Fragment::Bold),
map(parse_element_bold_italic, Fragment::BoldItalic),
map(parse_element_code, Fragment::InlineCode),
map(parse_element_image, Fragment::Image),
map(parse_element_link, Fragment::Link),
map(parse_element_plaintext, Fragment::Plain),
))(input)
}
}
pub use no_rustfmt_block::*;
#[cfg(test)]
mod tests {
use nom::{error::{Error, ErrorKind},
Err as NomErr};
use super::*;
use crate::*;
#[test]
fn test_parse_element_italic() {
assert_eq!(
parse_element_italic("*here is italic*"),
Ok(("", "here is italic"))
);
assert_eq!(
parse_element_italic("_here is italic_"),
Ok(("", "here is italic"))
);
assert_eq!(
parse_element_italic("*here is italic"),
Err(NomErr::Error(Error {
input: "*here is italic",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_italic("here is italic*"),
Err(NomErr::Error(Error {
input: "here is italic*",
code: ErrorKind::Tag,
}))
);
assert_eq!(
parse_element_italic("here is italic"),
Err(NomErr::Error(Error {
input: "here is italic",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_italic("*"),
Err(NomErr::Error(Error {
input: "*",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_italic("**"),
Err(NomErr::Error(Error {
input: "**",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_italic(""),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_italic("**we are doing bold**"),
Err(NomErr::Error(Error {
input: "**we are doing bold**",
code: ErrorKind::Tag
}))
);
}
#[test]
fn test_parse_element_bold_italic() {
assert_eq!(
parse_element_bold_italic("***here is bitalic***"),
Ok(("", "here is bitalic"))
);
assert_eq!(
parse_element_bold("***here is bitalic"),
Err(NomErr::Error(Error {
input: "***here is bitalic",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_bold("here is bitalic***"),
Err(NomErr::Error(Error {
input: "here is bitalic***",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_bold_italic("___here is bitalic___"),
Ok(("", "here is bitalic"))
);
assert_eq!(
parse_element_bold_italic("___here is bitalic"),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_bold_italic("here is bitalic___"),
Err(NomErr::Error(Error {
input: "here is bitalic___",
code: ErrorKind::Tag
}))
);
}
#[test]
fn test_parse_element_bold() {
assert_eq!(
parse_element_bold("**here is bold**"),
Ok(("", "here is bold"))
);
assert_eq!(
parse_element_bold("__here is bold__"),
Ok(("", "here is bold"))
);
assert_eq!(
parse_element_bold("**here is bold"),
Err(NomErr::Error(Error {
input: "**here is bold",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_bold("here is bold**"),
Err(NomErr::Error(Error {
input: "here is bold**",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_bold("here is bold"),
Err(NomErr::Error(Error {
input: "here is bold",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_bold("****"),
Err(NomErr::Error(Error {
input: "****",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_bold("**"),
Err(NomErr::Error(Error {
input: "**",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_bold("*"),
Err(NomErr::Error(Error {
input: "*",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_bold(""),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_bold("*this is italic*"),
Err(NomErr::Error(Error {
input: "*this is italic*",
code: ErrorKind::Tag
}))
);
}
#[test]
fn test_parse_element_code() {
assert_eq!(
parse_element_code("`here is code"),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_code("here is code`"),
Err(NomErr::Error(Error {
input: "here is code`",
code: ErrorKind::Tag
}))
);
assert_eq!(
parse_element_code("``"),
Err(NomErr::Error(Error {
input: "`",
code: ErrorKind::IsNot
}))
);
assert_eq!(
parse_element_code("`"),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::IsNot
}))
);
assert_eq!(
parse_element_code(""),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Tag
}))
);
}
#[test]
fn test_parse_element_link() {
assert_eq!(
parse_element_link("[title](https://www.example.com)"),
Ok(("", ("title", "https://www.example.com")))
);
assert_eq!(
parse_element_code(""),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Tag
}))
);
}
#[test]
fn test_parse_element_image() {
assert_eq!(
parse_element_image(""),
Ok(("", ("alt text", "image.jpg")))
);
assert_eq!(
parse_element_code(""),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Tag
}))
);
}
#[test]
fn test_parse_element_plaintext() {
assert_eq!(
parse_element_plaintext("1234567890"),
Ok(("", "1234567890"))
);
assert_eq!(
parse_element_plaintext("oh my gosh!"),
Ok(("", "oh my gosh!"))
);
assert_eq!(
parse_element_plaintext("oh my gosh"),
Err(NomErr::Error(Error {
input: "[link baby](and then somewhat)",
code: ErrorKind::Not
}))
);
assert_eq!(
parse_element_plaintext("`codeblock for bums`"),
Err(NomErr::Error(Error {
input: "`codeblock for bums`",
code: ErrorKind::Not
}))
);
assert_eq!(
parse_element_plaintext(""),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Not
}))
);
assert_eq!(
parse_element_plaintext("here is plaintext"),
Ok(("", "here is plaintext"))
);
assert_eq!(
parse_element_plaintext("here is plaintext!"),
Ok(("", "here is plaintext!"))
);
assert_eq!(
parse_element_plaintext("here is plaintext"),
Err(NomErr::Error(Error {
input: "[title](https://www.example.com)",
code: ErrorKind::Not
}))
);
assert_eq!(
parse_element_plaintext(""),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Not
}))
);
assert_eq!(
parse_element_plaintext(""),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Eof
}))
);
}
#[test]
fn test_parse_element_markdown_inline() {
assert_eq!(
parse_element_markdown_inline("*here is italic*"),
Ok(("", Fragment::Italic("here is italic")))
);
assert_eq!(
parse_element_markdown_inline("**here is bold**"),
Ok(("", Fragment::Bold("here is bold")))
);
assert_eq!(
parse_element_markdown_inline("`here is code`"),
Ok(("", Fragment::InlineCode("here is code")))
);
assert_eq!(
parse_element_markdown_inline("[title](https://www.example.com)"),
Ok(("", (Fragment::Link(("title", "https://www.example.com")))))
);
assert_eq!(
parse_element_markdown_inline(""),
Ok(("", (Fragment::Image(("alt text", "image.jpg")))))
);
assert_eq!(
parse_element_markdown_inline("here is plaintext!"),
Ok(("", Fragment::Plain("here is plaintext!")))
);
assert_eq!(
parse_element_markdown_inline("here is some plaintext *but what if we italicize?"),
Ok((
"*but what if we italicize?",
Fragment::Plain("here is some plaintext ")
))
);
assert_eq!(
parse_element_markdown_inline("here is some plaintext \n*but what if we italicize?"),
Ok((
"\n*but what if we italicize?",
Fragment::Plain("here is some plaintext ")
))
);
assert_eq!(
parse_element_markdown_inline("\n"),
Err(NomErr::Error(Error {
input: "\n",
code: ErrorKind::Not
}))
);
assert_eq!(
parse_element_markdown_inline(""),
Err(NomErr::Error(Error {
input: "",
code: ErrorKind::Eof
}))
);
}
}