minimad/
lib.rs

1/*!
2This crate provides a *very* simple markdown parser.
3
4It's the basis of the [termimad](https://github.com/Canop/termimad) lib, which displays static and dynamic markdown snippets on a terminal without mixing the skin with the code and wrapping the text and tables as needed.
5
6It can be used on its own:
7
8```rust
9use minimad::*;
10
11assert_eq!(
12    parse_line("## a header with some **bold**!"),
13    Line::new_header(
14        2,
15        vec![
16            Compound::raw_str("a header with some "),
17            Compound::raw_str("bold").bold(),
18            Compound::raw_str("!"),
19        ]
20    )
21);
22
23assert_eq!(
24    parse_inline("*Italic then **bold and italic `and some *code*`** and italic*"),
25    Composite::from(vec![
26        Compound::raw_str("Italic then ").italic(),
27        Compound::raw_str("bold and italic ").bold().italic(),
28        Compound::raw_str("and some *code*").bold().italic().code(),
29        Compound::raw_str(" and italic").italic(),
30    ])
31);
32```
33
34The [`mad_inline`] macro is useful for semi-dynamic markdown building: it prevents characters like `'*'` from messing the style:
35
36```
37use minimad::*;
38
39let md1 = mad_inline!(
40    "**$0 formula:** *$1*", // the markdown template, interpreted only once
41    "Disk",  // fills $0
42    "2*π*r", // fills $1. Note that the stars don't mess the markdown
43);
44let md2 = Composite::from(vec![
45    Compound::raw_str("Disk formula:").bold(),
46    Compound::raw_str(" "),
47    Compound::raw_str("2*π*r").italic(),
48]);
49```
50
51Note that Termimad contains macros and tools to deal with templates. If your goal is to print in the console you should use Termimad's functions.
52
53*/
54
55pub mod clean;
56mod markdown;
57pub mod parser;
58mod template;
59
60pub use {
61    clean::*,
62    markdown::*,
63    parser::Options,
64    template::*,
65};
66
67/// reexport so that macros can be used without imports
68pub use once_cell;
69
70/// parse a markdown text
71pub fn parse_text(
72    md: &str,
73    options: Options,
74) -> Text<'_> {
75    parser::parse(md, options)
76}
77
78/// parse a line, which is meant to be part of a markdown text.
79/// This function shouldn't usually be used: if you don't want
80/// a text you probably need `parse_inline`
81pub fn parse_line(md: &str) -> Line<'_> {
82    Line::from(md)
83}
84
85/// parse a monoline markdown snippet which isn't from a text.
86/// Don't produce some types of line: `TableRow`, `Code`, `ListItem`
87///  as they only make sense in a multi-line text.
88pub fn parse_inline(md: &str) -> Composite<'_> {
89    Composite::from_inline(md)
90}