Skip to content

Commit

Permalink
expose the clean::lines utility
Browse files Browse the repository at this point in the history
  • Loading branch information
Canop committed Dec 26, 2019
1 parent 32cfa1f commit 01ef21c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 13 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.6.2"
version = "0.6.3"
authors = ["dystroy <denys.seguret@gmail.com>"]
repository = "https://github.com/Canop/minimad"
description = "light Markdown parser"
Expand Down
16 changes: 8 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,14 @@ assert_eq!(
assert_eq!(
Line::from("Hello ~~wolrd~~ **World**. *Code*: `sqrt(π/2)`"),
Line::new_paragraph(vec![
Compound::raw_str("Hello "),
Compound::raw_str("wolrd").strikeout(),
Compound::raw_str(" "),
Compound::raw_str("World").bold(),
Compound::raw_str(". "),
Compound::raw_str("Code").italic(),
Compound::raw_str(": "),
Compound::raw_str("sqrt(π/2)").code(),
Compound::raw_str("Hello "),
Compound::raw_str("wolrd").strikeout(),
Compound::raw_str(" "),
Compound::raw_str("World").bold(),
Compound::raw_str(". "),
Compound::raw_str("Code").italic(),
Compound::raw_str(": "),
Compound::raw_str("sqrt(π/2)").code(),
])
);
```
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ assert_eq!(
```
*/

mod clean;
pub mod clean;
mod composite;
mod compound;
mod line;
Expand Down
61 changes: 58 additions & 3 deletions src/text.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::line::Line;
use crate::line_parser::LineParser;
use crate::{
line::Line,
line_parser::LineParser,
};

/// a text, that is just a collection of lines
#[derive(Debug, Default, PartialEq, Eq, Clone)]
Expand All @@ -8,10 +10,33 @@ pub struct Text<'a> {
}

impl<'s> From<&'s str> for Text<'s> {
/// build a text from a multi-line string interpreted as markdown
fn from(md: &str) -> Text<'_> {
Text::from_md_lines(md.lines())
}
}

impl<'s> Text<'s> {
/// parse a text from markdown lines.
///
/// The main reason to use this one is to use
/// the `clean::lines` function which is useful with
/// raw literals:
/// ```
/// use minimad::{clean, Text};
/// let md = clean::lines(r#"
/// * some bullet item
/// some text
/// some_code();
/// "#);
/// let text = Text::from_md_lines(md.into_iter());
/// ```
pub fn from_md_lines<I>(md_lines: I) -> Self
where I: Iterator<Item=&'s str>
{
let mut lines = Vec::new();
let mut between_fences = false;
for md_line in md.lines() {
for md_line in md_lines {
let parser = LineParser::from(md_line);
let line = if between_fences {
parser.as_code()
Expand All @@ -30,3 +55,33 @@ impl<'s> From<&'s str> for Text<'s> {
Text { lines }
}
}

/// Tests of text parsing
#[cfg(test)]
mod tests {
use crate::{
compound::*,
text::Text,
line::*,
clean,
};

#[test]
fn indented_code_between_fences() {
let md = clean::lines(r#"
outside
```code
a
b
```
"#);
assert_eq!(
Text::from_md_lines(md.into_iter()),
Text{ lines: vec![
Line::new_paragraph(vec![Compound::raw_str("outside")]),
Line::new_code(Compound::raw_str("a").code()),
Line::new_code(Compound::raw_str(" b").code()),
]},
);
}
}

0 comments on commit 01ef21c

Please sign in to comment.