-
Notifications
You must be signed in to change notification settings - Fork 12.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #112697 - tgross35:explain-markdown, r=oli-obk
Add simple markdown formatting to `rustc --explain` output This is a second attempt at #104540, which is #63128 without dependencies. This PR adds basic markdown formatting to `rustc --explain` output when available. Currently, the output just displays raw markdown: this works of course, but it really doesn't look very elegant. (output is `rustc --explain E0038`) <img width="583" alt="image" src="https://github.com/rust-lang/rust/assets/13724985/ea418117-47af-455b-83c0-6fc59276efee"> After this patch, sample output from the same file: <img width="693" alt="image" src="https://github.com/rust-lang/rust/assets/13724985/12f7bf9b-a3fe-4104-b74b-c3e5227f3de9"> This also obeys the `--color always/auto/never` command option. Behavior: - If pager is available and supports color, print with formatting to the pager - If pager is not available or fails print with formatting to stdout - otherwise without formatting - Follow `--color always/never` if suppied - If everything fails, just print plain text to stdout r? `@oli-obk` cc `@estebank` (since the two of you were involved in the previous discussion)
- Loading branch information
Showing
14 changed files
with
1,406 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
//! A simple markdown parser that can write formatted text to the terminal | ||
//! | ||
//! Entrypoint is `MdStream::parse_str(...)` | ||
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) | ||
} | ||
} |
Oops, something went wrong.