rustdoc codeblock hash escape

This commit is contained in:
Lucas Morales 2018-06-25 21:26:31 -04:00
parent fdd9cdc879
commit d3b4cb1730
No known key found for this signature in database
GPG Key ID: AB31E959F7559E19

View File

@ -64,21 +64,21 @@ pub struct MarkdownSummaryLine<'a>(pub &'a str, pub &'a [(String, String)]);
/// All lines are used in documentation tests. /// All lines are used in documentation tests.
enum Line<'a> { enum Line<'a> {
Hidden(&'a str), Hidden(&'a str),
Shown(&'a str), Shown(Cow<'a, str>),
} }
impl<'a> Line<'a> { impl<'a> Line<'a> {
fn for_html(self) -> Option<&'a str> { fn for_html(self) -> Option<Cow<'a, str>> {
match self { match self {
Line::Shown(l) => Some(l), Line::Shown(l) => Some(l),
Line::Hidden(_) => None, Line::Hidden(_) => None,
} }
} }
fn for_code(self) -> &'a str { fn for_code(self) -> Cow<'a, str> {
match self { match self {
Line::Shown(l) | Line::Shown(l) => l,
Line::Hidden(l) => l, Line::Hidden(l) => Cow::Borrowed(l),
} }
} }
} }
@ -91,7 +91,7 @@ impl<'a> Line<'a> {
fn map_line(s: &str) -> Line { fn map_line(s: &str) -> Line {
let trimmed = s.trim(); let trimmed = s.trim();
if trimmed.starts_with("##") { if trimmed.starts_with("##") {
Line::Shown(&trimmed[1..]) Line::Shown(Cow::Owned(s.replacen("##", "#", 1)))
} else if trimmed.starts_with("# ") { } else if trimmed.starts_with("# ") {
// # text // # text
Line::Hidden(&trimmed[2..]) Line::Hidden(&trimmed[2..])
@ -99,7 +99,7 @@ fn map_line(s: &str) -> Line {
// We cannot handle '#text' because it could be #[attr]. // We cannot handle '#text' because it could be #[attr].
Line::Hidden("") Line::Hidden("")
} else { } else {
Line::Shown(s) Line::Shown(Cow::Borrowed(s))
} }
} }
@ -168,7 +168,7 @@ 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 lines = origtext.lines().filter_map(|l| map_line(l).for_html());
let text = lines.collect::<Vec<&str>>().join("\n"); let text = lines.collect::<Vec<Cow<str>>>().join("\n");
PLAYGROUND.with(|play| { PLAYGROUND.with(|play| {
// insert newline to clearly separate it from the // insert newline to clearly separate it from the
// previous block so we can shorten the html output // 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> {
} }
let test = origtext.lines() let test = origtext.lines()
.map(|l| map_line(l).for_code()) .map(|l| map_line(l).for_code())
.collect::<Vec<&str>>().join("\n"); .collect::<Vec<Cow<str>>>().join("\n");
let krate = krate.as_ref().map(|s| &**s); let krate = krate.as_ref().map(|s| &**s);
let (test, _) = test::make_test(&test, krate, false, let (test, _) = test::make_test(&test, krate, false,
&Default::default()); &Default::default());
@ -477,7 +477,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Sp
} }
if let Some(offset) = offset { if let Some(offset) = offset {
let lines = test_s.lines().map(|l| map_line(l).for_code()); let lines = test_s.lines().map(|l| map_line(l).for_code());
let text = lines.collect::<Vec<&str>>().join("\n"); let text = lines.collect::<Vec<Cow<str>>>().join("\n");
nb_lines += doc[prev_offset..offset].lines().count(); nb_lines += doc[prev_offset..offset].lines().count();
let line = tests.get_line() + (nb_lines - 1); let line = tests.get_line() + (nb_lines - 1);
let filename = tests.get_filename(); let filename = tests.get_filename();