Skip to content

Commit d3b4cb1

Browse files
committed
rustdoc codeblock hash escape
1 parent fdd9cdc commit d3b4cb1

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

src/librustdoc/html/markdown.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [(String, String)]);
6464
/// All lines are used in documentation tests.
6565
enum Line<'a> {
6666
Hidden(&'a str),
67-
Shown(&'a str),
67+
Shown(Cow<'a, str>),
6868
}
6969

7070
impl<'a> Line<'a> {
71-
fn for_html(self) -> Option<&'a str> {
71+
fn for_html(self) -> Option<Cow<'a, str>> {
7272
match self {
7373
Line::Shown(l) => Some(l),
7474
Line::Hidden(_) => None,
7575
}
7676
}
7777

78-
fn for_code(self) -> &'a str {
78+
fn for_code(self) -> Cow<'a, str> {
7979
match self {
80-
Line::Shown(l) |
81-
Line::Hidden(l) => l,
80+
Line::Shown(l) => l,
81+
Line::Hidden(l) => Cow::Borrowed(l),
8282
}
8383
}
8484
}
@@ -91,15 +91,15 @@ impl<'a> Line<'a> {
9191
fn map_line(s: &str) -> Line {
9292
let trimmed = s.trim();
9393
if trimmed.starts_with("##") {
94-
Line::Shown(&trimmed[1..])
94+
Line::Shown(Cow::Owned(s.replacen("##", "#", 1)))
9595
} else if trimmed.starts_with("# ") {
9696
// # text
9797
Line::Hidden(&trimmed[2..])
9898
} else if trimmed == "#" {
9999
// We cannot handle '#text' because it could be #[attr].
100100
Line::Hidden("")
101101
} else {
102-
Line::Shown(s)
102+
Line::Shown(Cow::Borrowed(s))
103103
}
104104
}
105105

@@ -168,7 +168,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
168168
}
169169
}
170170
let lines = origtext.lines().filter_map(|l| map_line(l).for_html());
171-
let text = lines.collect::<Vec<&str>>().join("\n");
171+
let text = lines.collect::<Vec<Cow<str>>>().join("\n");
172172
PLAYGROUND.with(|play| {
173173
// insert newline to clearly separate it from the
174174
// previous block so we can shorten the html output
@@ -179,7 +179,7 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
179179
}
180180
let test = origtext.lines()
181181
.map(|l| map_line(l).for_code())
182-
.collect::<Vec<&str>>().join("\n");
182+
.collect::<Vec<Cow<str>>>().join("\n");
183183
let krate = krate.as_ref().map(|s| &**s);
184184
let (test, _) = test::make_test(&test, krate, false,
185185
&Default::default());
@@ -477,7 +477,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Sp
477477
}
478478
if let Some(offset) = offset {
479479
let lines = test_s.lines().map(|l| map_line(l).for_code());
480-
let text = lines.collect::<Vec<&str>>().join("\n");
480+
let text = lines.collect::<Vec<Cow<str>>>().join("\n");
481481
nb_lines += doc[prev_offset..offset].lines().count();
482482
let line = tests.get_line() + (nb_lines - 1);
483483
let filename = tests.get_filename();

0 commit comments

Comments
 (0)