added html generation
All checks were successful
Test the running changes / Test (push) Successful in 40s
All checks were successful
Test the running changes / Test (push) Successful in 40s
This commit is contained in:
@@ -1,41 +1,33 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use crate::{ast::Inline, parser::inline::inline};
|
||||
use crate::{ast::Block, parser::inline::inline};
|
||||
use nom::{
|
||||
IResult, Parser,
|
||||
bytes::complete::{tag, take_until},
|
||||
multi::{many_m_n, many1, many0},
|
||||
sequence::{terminated, delimited},
|
||||
branch::alt,
|
||||
bytes::complete::{tag, take_until},
|
||||
multi::{many_m_n, many0, many1},
|
||||
sequence::{delimited, terminated},
|
||||
};
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
pub enum Block {
|
||||
Heading { inner: Vec<Inline>, level: u8 },
|
||||
Code { content: String, lang: String },
|
||||
Quote { inner: Box<Block> },
|
||||
Paragraph { inner: Vec<Inline> },
|
||||
}
|
||||
use super::MarkdownParseError;
|
||||
|
||||
pub fn blocks(input: &str) -> IResult<&str, Vec<Block>> {
|
||||
pub fn blocks(input: &str) -> IResult<&str, Vec<Block>, MarkdownParseError> {
|
||||
many0(block).parse(input)
|
||||
}
|
||||
|
||||
|
||||
pub fn block(input: &str) -> IResult<&str, Block> {
|
||||
pub fn block(input: &str) -> IResult<&str, Block, MarkdownParseError> {
|
||||
terminated(
|
||||
alt((heading_block, code_block, quote_block, paragraph_block)),
|
||||
tag("\n"),
|
||||
).parse(input)
|
||||
)
|
||||
.parse(input)
|
||||
}
|
||||
|
||||
fn paragraph_block(input: &str) -> IResult<&str, Block> {
|
||||
fn paragraph_block(input: &str) -> IResult<&str, Block, MarkdownParseError> {
|
||||
(inline)
|
||||
.parse(input)
|
||||
.map(|(rem, inl)| (rem, Block::Paragraph { inner: inl }))
|
||||
}
|
||||
|
||||
fn heading_block(input: &str) -> IResult<&str, Block> {
|
||||
fn heading_block(input: &str) -> IResult<&str, Block, MarkdownParseError> {
|
||||
(many_m_n(1, 6, tag("#")), many1(tag(" ")), inline)
|
||||
.parse(input)
|
||||
.map(|(rem, (head, _, title))| {
|
||||
@@ -49,7 +41,7 @@ fn heading_block(input: &str) -> IResult<&str, Block> {
|
||||
})
|
||||
}
|
||||
|
||||
fn code_block(input: &str) -> IResult<&str, Block> {
|
||||
fn code_block(input: &str) -> IResult<&str, Block, MarkdownParseError> {
|
||||
delimited(
|
||||
tag("```"),
|
||||
(take_until("\n"), tag("\n"), take_until("```\n")),
|
||||
@@ -67,15 +59,17 @@ fn code_block(input: &str) -> IResult<&str, Block> {
|
||||
})
|
||||
}
|
||||
|
||||
fn quote_block(input: &str) -> IResult<&str, Block> {
|
||||
(tag(">"), many0(tag(" ")), block).parse(input).map(|(rem, (_, _, inner))| {
|
||||
(
|
||||
rem,
|
||||
Block::Quote {
|
||||
inner: Box::new(inner),
|
||||
},
|
||||
)
|
||||
})
|
||||
fn quote_block(input: &str) -> IResult<&str, Block, MarkdownParseError> {
|
||||
(tag(">"), many0(tag(" ")), block)
|
||||
.parse(input)
|
||||
.map(|(rem, (_, _, inner))| {
|
||||
(
|
||||
rem,
|
||||
Block::Quote {
|
||||
inner: Box::new(inner),
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
//|-------------------------------------------------------------------------------|
|
||||
@@ -206,9 +200,9 @@ echo hello
|
||||
block,
|
||||
Block::Quote {
|
||||
inner: Box::new(Block::Paragraph {
|
||||
inner: vec![
|
||||
Inline::Text { content: "sun tzu".to_string() }
|
||||
]
|
||||
inner: vec![Inline::Text {
|
||||
content: "sun tzu".to_string()
|
||||
}]
|
||||
})
|
||||
}
|
||||
);
|
||||
@@ -216,8 +210,7 @@ echo hello
|
||||
|
||||
#[test]
|
||||
fn heading_and_paragraph() {
|
||||
let md =
|
||||
"## Heading
|
||||
let md = "## Heading
|
||||
Hello MD
|
||||
";
|
||||
let (rem, blocks) = blocks(md).unwrap();
|
||||
@@ -227,15 +220,15 @@ Hello MD
|
||||
blocks,
|
||||
vec![
|
||||
Block::Heading {
|
||||
inner: vec![
|
||||
Inline::Text { content: "Heading".to_string() }
|
||||
],
|
||||
inner: vec![Inline::Text {
|
||||
content: "Heading".to_string()
|
||||
}],
|
||||
level: 2
|
||||
},
|
||||
Block::Paragraph {
|
||||
inner: vec![
|
||||
Inline::Text { content: "Hello MD".to_string() }
|
||||
]
|
||||
inner: vec![Inline::Text {
|
||||
content: "Hello MD".to_string()
|
||||
}]
|
||||
}
|
||||
]
|
||||
);
|
||||
|
||||
@@ -1,23 +1,28 @@
|
||||
#![allow(dead_code)]
|
||||
|
||||
use nom::IResult;
|
||||
use nom::{
|
||||
Parser,
|
||||
branch::alt,
|
||||
bytes::complete::{is_not, tag},
|
||||
error::context,
|
||||
multi::many0,
|
||||
sequence::delimited,
|
||||
};
|
||||
|
||||
use crate::ast::{Inline, Href};
|
||||
use crate::ast::{Href, Inline};
|
||||
|
||||
use super::MarkdownParseError;
|
||||
|
||||
pub fn inline(input: &str) -> IResult<&str, Vec<Inline>> {
|
||||
many0(alt((text_inline, bold_inline, italic_inline, code_inline, link_inline))).parse(input)
|
||||
pub fn inline(input: &str) -> IResult<&str, Vec<Inline>, MarkdownParseError> {
|
||||
many0(alt((
|
||||
text_inline,
|
||||
bold_inline,
|
||||
italic_inline,
|
||||
code_inline,
|
||||
link_inline,
|
||||
)))
|
||||
.parse(input)
|
||||
}
|
||||
|
||||
fn text_inline(input: &str) -> IResult<&str, Inline> {
|
||||
fn text_inline(input: &str) -> IResult<&str, Inline, MarkdownParseError> {
|
||||
is_not("*_`[]\n").parse(input).map(|(rem, con)| {
|
||||
(
|
||||
rem,
|
||||
@@ -28,54 +33,48 @@ fn text_inline(input: &str) -> IResult<&str, Inline> {
|
||||
})
|
||||
}
|
||||
|
||||
fn bold_inline(input: &str) -> IResult<&str, Inline> {
|
||||
delimited(
|
||||
context("opening bold tag", tag("*")),
|
||||
inline,
|
||||
context("closing bold tag", tag("*")),
|
||||
)
|
||||
fn bold_inline(input: &str) -> IResult<&str, Inline, MarkdownParseError> {
|
||||
delimited(tag("*"), inline, tag("*"))
|
||||
.parse(input)
|
||||
.map(|(rem, inl)| (rem, Inline::Bold { inner: inl }))
|
||||
}
|
||||
|
||||
fn italic_inline(input: &str) -> IResult<&str, Inline> {
|
||||
delimited(
|
||||
context("opening italics tag", tag("_")),
|
||||
inline,
|
||||
context("closing italics tag", tag("_")),
|
||||
)
|
||||
fn italic_inline(input: &str) -> IResult<&str, Inline, MarkdownParseError> {
|
||||
delimited(tag("_"), inline, tag("_"))
|
||||
.parse(input)
|
||||
.map(|(rem, inl)| (rem, Inline::Italic { inner: inl }))
|
||||
}
|
||||
|
||||
fn code_inline(input: &str) -> IResult<&str, Inline> {
|
||||
delimited(
|
||||
context("opening code tag", tag("`")),
|
||||
context("inline code", is_not("`\n")),
|
||||
context("closing code tag", tag("`")),
|
||||
)
|
||||
fn code_inline(input: &str) -> IResult<&str, Inline, MarkdownParseError> {
|
||||
delimited(tag("`"), is_not("`\n"), tag("`"))
|
||||
.parse(input)
|
||||
.map(|(rem, inl)| (rem, Inline::Code { content: inl.to_string() }))
|
||||
.map(|(rem, inl)| {
|
||||
(
|
||||
rem,
|
||||
Inline::Code {
|
||||
content: inl.to_string(),
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
fn link_inline(input: &str) -> IResult<&str, Inline> {
|
||||
fn link_inline(input: &str) -> IResult<&str, Inline, MarkdownParseError> {
|
||||
(
|
||||
delimited(
|
||||
context("opening link tag", tag("[")),
|
||||
context("link name", inline),
|
||||
context("closing link tag", tag("]")),
|
||||
),
|
||||
delimited(
|
||||
context("opening href tag", tag("(")),
|
||||
context("link href", is_not(")\n")),
|
||||
context("closing href tag", tag(")")),
|
||||
)
|
||||
delimited(tag("["), inline, tag("]")),
|
||||
delimited(tag("("), is_not(")\n"), tag(")")),
|
||||
)
|
||||
.parse(input)
|
||||
.map(|(rem, (name, href))| (rem, Inline::Link { inner: name, href: Href::new(href) }))
|
||||
.map(|(rem, (name, href))| {
|
||||
(
|
||||
rem,
|
||||
Inline::Link {
|
||||
inner: name,
|
||||
href: Href::new(href),
|
||||
},
|
||||
)
|
||||
})
|
||||
}
|
||||
|
||||
|
||||
//|-------------------------------------------------------------------------------|
|
||||
//| TESTS TESTS TESTS TESTS TESTS TESTS TESTS TESTS TESTS TESTS TESTS TESTS TESTS |
|
||||
//|-------------------------------------------------------------------------------|
|
||||
@@ -254,9 +253,15 @@ mod test {
|
||||
assert_eq!(
|
||||
parsed,
|
||||
vec![
|
||||
Inline::Text { content: "take some ".to_string() },
|
||||
Inline::Code { content: "code and *bold* and _italics_".to_string() },
|
||||
Inline::Text { content: " lmao".to_string() }
|
||||
Inline::Text {
|
||||
content: "take some ".to_string()
|
||||
},
|
||||
Inline::Code {
|
||||
content: "code and *bold* and _italics_".to_string()
|
||||
},
|
||||
Inline::Text {
|
||||
content: " lmao".to_string()
|
||||
}
|
||||
]
|
||||
);
|
||||
}
|
||||
@@ -273,7 +278,9 @@ mod test {
|
||||
parsed,
|
||||
Inline::Link {
|
||||
inner: vec![
|
||||
Inline::Text { content: "this link is ".to_string() },
|
||||
Inline::Text {
|
||||
content: "this link is ".to_string()
|
||||
},
|
||||
Inline::Bold {
|
||||
inner: vec![Inline::Text {
|
||||
content: "important".to_string()
|
||||
|
||||
@@ -1,3 +1,32 @@
|
||||
pub mod inline;
|
||||
pub mod block;
|
||||
use std::fmt::{Debug, Display};
|
||||
|
||||
pub mod block;
|
||||
pub mod inline;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct MarkdownParseError {
|
||||
kind: nom::error::ErrorKind,
|
||||
message: String,
|
||||
}
|
||||
|
||||
impl Display for MarkdownParseError {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{:?}, {}", self.kind, self.message)
|
||||
}
|
||||
}
|
||||
|
||||
impl nom::error::ParseError<&str> for MarkdownParseError {
|
||||
fn from_error_kind(input: &str, kind: nom::error::ErrorKind) -> Self {
|
||||
Self {
|
||||
kind,
|
||||
message: format!("at: {}", input),
|
||||
}
|
||||
}
|
||||
|
||||
fn append(input: &str, kind: nom::error::ErrorKind, other: Self) -> Self {
|
||||
Self {
|
||||
kind,
|
||||
message: format!("{}\nat: {}", other, input),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user