Add a simple markdown parser for formatting `rustc --explain`
Currently, the output of `rustc --explain foo` displays the raw markdown in a
pager. This is acceptable, but using actual formatting makes it easier to
understand.
This patch consists of three major components:
1. A markdown parser. This is an extremely simple non-backtracking recursive
implementation that requires normalization of the final token stream
2. A utility to write the token stream to an output buffer
3. Configuration within rustc_driver_impl to invoke this combination for
`--explain`. Like the current implementation, it first attempts to print to
a pager with a fallback colorized terminal, and standard print as a last
resort.
If color is disabled, or if the output does not support it, or if printing
with color fails, it will write the raw markdown (which matches current
behavior).
Pagers known to support color are: `less` (with `-r`), `bat` (aka `catbat`),
and `delta`.
The markdown parser does not support the entire markdown specification, but
should support the following with reasonable accuracy:
- Headings, including formatting
- Comments
- Code, inline and fenced block (no indented block)
- Strong, emphasis, and strikethrough formatted text
- Links, anchor, inline, and reference-style
- Horizontal rules
- Unordered and ordered list items, including formatting
This parser and writer should be reusable by other systems if ever needed.
2022-12-19 12:09:40 -06:00
|
|
|
//! A simple markdown parser that can write formatted text to the terminal
|
|
|
|
//!
|
|
|
|
//! Entrypoint is `MdStream::parse_str(...)`
|
2024-06-20 05:04:30 +10:00
|
|
|
|
Add a simple markdown parser for formatting `rustc --explain`
Currently, the output of `rustc --explain foo` displays the raw markdown in a
pager. This is acceptable, but using actual formatting makes it easier to
understand.
This patch consists of three major components:
1. A markdown parser. This is an extremely simple non-backtracking recursive
implementation that requires normalization of the final token stream
2. A utility to write the token stream to an output buffer
3. Configuration within rustc_driver_impl to invoke this combination for
`--explain`. Like the current implementation, it first attempts to print to
a pager with a fallback colorized terminal, and standard print as a last
resort.
If color is disabled, or if the output does not support it, or if printing
with color fails, it will write the raw markdown (which matches current
behavior).
Pagers known to support color are: `less` (with `-r`), `bat` (aka `catbat`),
and `delta`.
The markdown parser does not support the entire markdown specification, but
should support the following with reasonable accuracy:
- Headings, including formatting
- Comments
- Code, inline and fenced block (no indented block)
- Strong, emphasis, and strikethrough formatted text
- Links, anchor, inline, and reference-style
- Horizontal rules
- Unordered and ordered list items, including formatting
This parser and writer should be reusable by other systems if ever needed.
2022-12-19 12:09:40 -06:00
|
|
|
use std::io;
|
|
|
|
|
|
|
|
use termcolor::{Buffer, BufferWriter, ColorChoice};
|
|
|
|
mod parse;
|
|
|
|
mod term;
|
|
|
|
|
|
|
|
/// An AST representation of a Markdown document
|
|
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
|
|
pub struct MdStream<'a>(Vec<MdTree<'a>>);
|
|
|
|
|
|
|
|
impl<'a> MdStream<'a> {
|
|
|
|
/// Parse a markdown string to a tokenstream
|
|
|
|
#[must_use]
|
|
|
|
pub fn parse_str(s: &str) -> MdStream<'_> {
|
|
|
|
parse::entrypoint(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Write formatted output to a termcolor buffer
|
|
|
|
pub fn write_termcolor_buf(&self, buf: &mut Buffer) -> io::Result<()> {
|
|
|
|
term::entrypoint(self, buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Create a termcolor buffer with the `Always` color choice
|
|
|
|
pub fn create_stdout_bufwtr() -> BufferWriter {
|
|
|
|
BufferWriter::stdout(ColorChoice::Always)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A single tokentree within a Markdown document
|
|
|
|
#[derive(Clone, Debug, PartialEq)]
|
|
|
|
pub enum MdTree<'a> {
|
|
|
|
/// Leaf types
|
|
|
|
Comment(&'a str),
|
|
|
|
CodeBlock {
|
|
|
|
txt: &'a str,
|
|
|
|
lang: Option<&'a str>,
|
|
|
|
},
|
|
|
|
CodeInline(&'a str),
|
|
|
|
Strong(&'a str),
|
|
|
|
Emphasis(&'a str),
|
|
|
|
Strikethrough(&'a str),
|
|
|
|
PlainText(&'a str),
|
|
|
|
/// [Foo](www.foo.com) or simple anchor <www.foo.com>
|
|
|
|
Link {
|
|
|
|
disp: &'a str,
|
|
|
|
link: &'a str,
|
|
|
|
},
|
|
|
|
/// `[Foo link][ref]`
|
|
|
|
RefLink {
|
|
|
|
disp: &'a str,
|
|
|
|
id: Option<&'a str>,
|
|
|
|
},
|
|
|
|
/// [ref]: www.foo.com
|
|
|
|
LinkDef {
|
|
|
|
id: &'a str,
|
|
|
|
link: &'a str,
|
|
|
|
},
|
|
|
|
/// Break bewtween two paragraphs (double `\n`), not directly parsed but
|
|
|
|
/// added later
|
|
|
|
ParagraphBreak,
|
|
|
|
/// Break bewtween two lines (single `\n`)
|
|
|
|
LineBreak,
|
|
|
|
HorizontalRule,
|
|
|
|
Heading(u8, MdStream<'a>),
|
|
|
|
OrderedListItem(u16, MdStream<'a>),
|
|
|
|
UnorderedListItem(MdStream<'a>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> From<Vec<MdTree<'a>>> for MdStream<'a> {
|
|
|
|
fn from(value: Vec<MdTree<'a>>) -> Self {
|
|
|
|
Self(value)
|
|
|
|
}
|
|
|
|
}
|