rustc_errors/markdown/
mod.rs

1//! A simple markdown parser that can write formatted text to the terminal
2//!
3//! Entrypoint is `MdStream::parse_str(...)`
4
5use std::io;
6
7use termcolor::{Buffer, BufferWriter, ColorChoice};
8mod parse;
9mod term;
10
11/// An AST representation of a Markdown document
12#[derive(Clone, Debug, Default, PartialEq)]
13pub struct MdStream<'a>(Vec<MdTree<'a>>);
14
15impl<'a> MdStream<'a> {
16    /// Parse a markdown string to a tokenstream
17    #[must_use]
18    pub fn parse_str(s: &str) -> MdStream<'_> {
19        parse::entrypoint(s)
20    }
21
22    /// Write formatted output to a termcolor buffer
23    pub fn write_termcolor_buf(&self, buf: &mut Buffer) -> io::Result<()> {
24        term::entrypoint(self, buf)
25    }
26}
27
28/// Create a termcolor buffer with the `Always` color choice
29pub fn create_stdout_bufwtr() -> BufferWriter {
30    BufferWriter::stdout(ColorChoice::Always)
31}
32
33/// A single tokentree within a Markdown document
34#[derive(Clone, Debug, PartialEq)]
35pub enum MdTree<'a> {
36    /// Leaf types
37    Comment(&'a str),
38    CodeBlock {
39        txt: &'a str,
40        lang: Option<&'a str>,
41    },
42    CodeInline(&'a str),
43    Strong(&'a str),
44    Emphasis(&'a str),
45    Strikethrough(&'a str),
46    PlainText(&'a str),
47    /// [Foo](www.foo.com) or simple anchor <www.foo.com>
48    Link {
49        disp: &'a str,
50        link: &'a str,
51    },
52    /// `[Foo link][ref]`
53    RefLink {
54        disp: &'a str,
55        id: Option<&'a str>,
56    },
57    /// [ref]: www.foo.com
58    LinkDef {
59        id: &'a str,
60        link: &'a str,
61    },
62    /// Break bewtween two paragraphs (double `\n`), not directly parsed but
63    /// added later
64    ParagraphBreak,
65    /// Break bewtween two lines (single `\n`)
66    LineBreak,
67    HorizontalRule,
68    Heading(u8, MdStream<'a>),
69    OrderedListItem(u16, MdStream<'a>),
70    UnorderedListItem(MdStream<'a>),
71}
72
73impl<'a> From<Vec<MdTree<'a>>> for MdStream<'a> {
74    fn from(value: Vec<MdTree<'a>>) -> Self {
75        Self(value)
76    }
77}