Skip to content

Commit

Permalink
raw_str function to generate non parsed text
Browse files Browse the repository at this point in the history
  • Loading branch information
Canop committed Mar 9, 2023
1 parent b7a4b0e commit e857aec
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 20 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "minimad"
version = "0.11.0"
version = "0.12.0"
authors = ["dystroy <denys.seguret@gmail.com>"]
repository = "https://github.com/Canop/minimad"
description = "light Markdown parser"
Expand Down
8 changes: 7 additions & 1 deletion src/markdown/composite.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::*;
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
pub enum CompositeStyle {
Paragraph,
Header(u8), // never 0, and <= MAX_HEADER_DEPTH
Header(u8), // never 0, and <= MAX_HEADER_DEPTH
ListItem(u8), // can't be built > 3 by parsing
Code,
Quote,
Expand Down Expand Up @@ -48,6 +48,12 @@ impl<'a> Composite<'a> {
pub fn from_inline(md: &'a str) -> Composite<'a> {
parser::LineParser::from(md).inline()
}
pub fn raw_str(s: &'a str) -> Composite<'a> {
Self {
style: CompositeStyle::Paragraph,
compounds: vec![Compound::raw_str(s)],
}
}
#[inline(always)]
pub fn is_code(&self) -> bool {
matches!(self.style, CompositeStyle::Code { .. })
Expand Down
12 changes: 9 additions & 3 deletions src/markdown/line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@ pub enum Line<'a> {
CodeFence(Composite<'a>),
}

impl Line<'_> {
pub fn from(md: &str) -> Line<'_> {
impl<'a> Line<'a> {
pub fn from(md: &'a str) -> Self {
parser::LineParser::from(md).line()
}
pub fn raw_str(s: &'a str) -> Self {
Self::Normal(Composite::raw_str(s))
}
#[inline(always)]
pub fn char_length(&self) -> usize {
match self {
Expand Down Expand Up @@ -54,7 +57,10 @@ impl Line<'_> {
compounds,
})
}
pub fn new_list_item(depth: u8, compounds: Vec<Compound<'_>>) -> Line<'_> {
pub fn new_list_item(
depth: u8,
compounds: Vec<Compound<'_>>,
) -> Line<'_> {
Line::Normal(Composite {
style: CompositeStyle::ListItem(depth),
compounds,
Expand Down
4 changes: 4 additions & 0 deletions src/markdown/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,8 @@ impl<'s> Text<'s> {
{
crate::parser::parse_lines(md_lines, Options::default())
}
pub fn raw_str(s: &'s str) -> Self {
let lines = s.lines().map(|line| Line::raw_str(line)).collect();
Self { lines }
}
}
37 changes: 22 additions & 15 deletions src/parser/line_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -494,34 +494,41 @@ mod tests {
fn list_item() {
assert_eq!(
Line::from("* *list* item"),
Line::new_list_item(0, vec![
Compound::raw_str("list").italic(),
Compound::raw_str(" item"),
])
Line::new_list_item(
0,
vec![
Compound::raw_str("list").italic(),
Compound::raw_str(" item"),
]
)
);
}

#[test]
fn deep_list_items() {
assert_eq!(
Line::from(" * *list* item"),
Line::new_list_item(1, vec![
Compound::raw_str("list").italic(),
Compound::raw_str(" item"),
])
Line::new_list_item(
1,
vec![
Compound::raw_str("list").italic(),
Compound::raw_str(" item"),
]
)
);
assert_eq!(
Line::from(" * deeper"),
Line::new_list_item(2, vec![
Compound::raw_str("deeper"),
])
Line::new_list_item(2, vec![Compound::raw_str("deeper"),])
);
assert_eq!(
Line::from(" * even **deeper**"),
Line::new_list_item(3, vec![
Compound::raw_str("even "),
Compound::raw_str("deeper").bold(),
])
Line::new_list_item(
3,
vec![
Compound::raw_str("even "),
Compound::raw_str("deeper").bold(),
]
)
);
assert_eq!(
Line::from(" * but not this one..."),
Expand Down

0 comments on commit e857aec

Please sign in to comment.