From d3b4cb1730cb18bd42e3f985bdc155a3c5565e1c Mon Sep 17 00:00:00 2001 From: Lucas Morales Date: Mon, 25 Jun 2018 21:26:31 -0400 Subject: [PATCH 1/2] rustdoc codeblock hash escape --- src/librustdoc/html/markdown.rs | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs index 7088104cd7a..c77a720b16b 100644 --- a/src/librustdoc/html/markdown.rs +++ b/src/librustdoc/html/markdown.rs @@ -64,21 +64,21 @@ /// All lines are used in documentation tests. enum Line<'a> { Hidden(&'a str), - Shown(&'a str), + Shown(Cow<'a, str>), } impl<'a> Line<'a> { - fn for_html(self) -> Option<&'a str> { + fn for_html(self) -> Option> { match self { Line::Shown(l) => Some(l), Line::Hidden(_) => None, } } - fn for_code(self) -> &'a str { + fn for_code(self) -> Cow<'a, str> { match self { - Line::Shown(l) | - Line::Hidden(l) => l, + Line::Shown(l) => l, + Line::Hidden(l) => Cow::Borrowed(l), } } } @@ -91,7 +91,7 @@ fn for_code(self) -> &'a str { fn map_line(s: &str) -> Line { let trimmed = s.trim(); if trimmed.starts_with("##") { - Line::Shown(&trimmed[1..]) + Line::Shown(Cow::Owned(s.replacen("##", "#", 1))) } else if trimmed.starts_with("# ") { // # text Line::Hidden(&trimmed[2..]) @@ -99,7 +99,7 @@ fn map_line(s: &str) -> Line { // We cannot handle '#text' because it could be #[attr]. Line::Hidden("") } else { - Line::Shown(s) + Line::Shown(Cow::Borrowed(s)) } } @@ -168,7 +168,7 @@ fn next(&mut self) -> Option { } } let lines = origtext.lines().filter_map(|l| map_line(l).for_html()); - let text = lines.collect::>().join("\n"); + let text = lines.collect::>>().join("\n"); PLAYGROUND.with(|play| { // insert newline to clearly separate it from the // previous block so we can shorten the html output @@ -179,7 +179,7 @@ fn next(&mut self) -> Option { } let test = origtext.lines() .map(|l| map_line(l).for_code()) - .collect::>().join("\n"); + .collect::>>().join("\n"); let krate = krate.as_ref().map(|s| &**s); let (test, _) = test::make_test(&test, krate, false, &Default::default()); @@ -477,7 +477,7 @@ pub fn find_testable_code(doc: &str, tests: &mut ::test::Collector, position: Sp } if let Some(offset) = offset { let lines = test_s.lines().map(|l| map_line(l).for_code()); - let text = lines.collect::>().join("\n"); + let text = lines.collect::>>().join("\n"); nb_lines += doc[prev_offset..offset].lines().count(); let line = tests.get_line() + (nb_lines - 1); let filename = tests.get_filename(); From ff2ff2b2b88f5948e1326809e70fbdf4db167097 Mon Sep 17 00:00:00 2001 From: Lucas Morales Date: Wed, 4 Jul 2018 08:56:31 -0400 Subject: [PATCH 2/2] rustdoc book on codeblock hash escaping --- src/doc/rustdoc/src/documentation-tests.md | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/src/doc/rustdoc/src/documentation-tests.md b/src/doc/rustdoc/src/documentation-tests.md index 7639537fc55..e4af122d0cb 100644 --- a/src/doc/rustdoc/src/documentation-tests.md +++ b/src/doc/rustdoc/src/documentation-tests.md @@ -170,6 +170,23 @@ By repeating all parts of the example, you can ensure that your example still compiles, while only showing the parts that are relevant to that part of your explanation. +The `#`-hiding of lines can be prevented by using two consecutive hashes +`##`. This only needs to be done with with the first `#` which would've +otherwise caused hiding. If we have a string literal like the following, +which has a line that starts with a `#`: + +```rust +let s = "foo +## bar # baz"; +``` + +We can document it by escaping the initial `#`: + +```text +/// let s = "foo +/// ## bar # baz"; +``` + ## Using `?` in doc tests