Skip to content

Commit

Permalink
Rollup merge of rust-lang#105539 - GuillaumeGomez:hashtag-prepended-l…
Browse files Browse the repository at this point in the history
…ines-non-rust, r=notriddle

rustdoc: Only hide lines starting with `#` in rust code blocks

Fixes rust-lang#105527.

So before approving, this is a big question: in rust code blocks, in a line starts with a `#`, we hide it in the output. However, should we do the same for non-rust code blocks too? I think it's a bit problematic to do it because `#` can be used for many things but I prefer to check first with everyone (might also be worth updating documentation too).

cc `@rust-lang/rustdoc`
r? `@notriddle`
  • Loading branch information
Dylan-DPC committed Dec 11, 2022
2 parents 790c528 + bc63c0e commit 6ff107e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/librustdoc/html/markdown.rs
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,6 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
_ => {}
}
}
let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
let text = lines.intersperse("\n".into()).collect::<String>();

let parse_result = match kind {
CodeBlockKind::Fenced(ref lang) => {
Expand All @@ -260,7 +258,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
<pre class=\"language-{}\"><code>{}</code></pre>\
</div>",
lang,
Escape(&text),
Escape(&origtext),
)
.into(),
));
Expand All @@ -270,6 +268,9 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'_, 'a, I> {
CodeBlockKind::Indented => Default::default(),
};

let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
let text = lines.intersperse("\n".into()).collect::<String>();

compile_fail = parse_result.compile_fail;
should_panic = parse_result.should_panic;
ignore = parse_result.ignore;
Expand Down
37 changes: 37 additions & 0 deletions src/librustdoc/html/markdown/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,3 +309,40 @@ fn test_find_testable_code_line() {
t("```rust\n```\n```rust\n```", &[1, 3]);
t("```rust\n```\n ```rust\n```", &[1, 3]);
}

#[test]
fn test_ascii_with_prepending_hashtag() {
fn t(input: &str, expect: &str) {
let mut map = IdMap::new();
let output = Markdown {
content: input,
links: &[],
ids: &mut map,
error_codes: ErrorCodes::Yes,
edition: DEFAULT_EDITION,
playground: &None,
heading_offset: HeadingOffset::H2,
}
.into_string();
assert_eq!(output, expect, "original: {}", input);
}

t(
r#"```ascii
#..#.####.#....#.....##..
#..#.#....#....#....#..#.
####.###..#....#....#..#.
#..#.#....#....#....#..#.
#..#.#....#....#....#..#.
#..#.####.####.####..##..
```"#,
"<div class=\"example-wrap\"><pre class=\"language-ascii\"><code>\
#..#.####.#....#.....##..
#..#.#....#....#....#..#.
####.###..#....#....#..#.
#..#.#....#....#....#..#.
#..#.#....#....#....#..#.
#..#.####.####.####..##..
</code></pre></div>",
);
}

0 comments on commit 6ff107e

Please sign in to comment.