Skip to content

rustdoc codeblock hash escape #51803

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 4, 2018
Merged

rustdoc codeblock hash escape #51803

merged 2 commits into from
Jul 4, 2018

Conversation

lorepozo
Copy link
Contributor

So that docstring text such as the following (in a code block) can be created ergonomically:

let s = "
    foo
    # bar
    baz
";

Such code in a docstring hide the     # bar line.

Previously, using two consecutive hashes     ## bar would turn the line into shown # bar, losing the leading whitespace. A line of code like     # bar (such as in the example above) could not be represented in the docstring text.

This commit makes the two consecutive hashes not also trim the leading whitespace — the two hashes simply escape into a single hash and do not hide the line, leaving the rest of that line unaffected. The new docstring text to achieve the above code block is:

/// ```
/// let s = "
///     foo
///     ## bar
///     baz
/// ";
/// ```

@rust-highfive
Copy link
Contributor

r? @QuietMisdreavus

(rust_highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 26, 2018
@killercup killercup added the T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. label Jun 26, 2018
@@ -91,15 +91,15 @@ impl<'a> Line<'a> {
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)))
Copy link
Member

@killercup killercup Jun 26, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Two things:

  1. Wait, there is no Cow-returning replacen yet? Might be a nice thing to have, since it's pretty concise. Not relevant here, though.
  2. Given that we are in the branch where the string starts with ##, isn't replacing the two hash signs with one the same as &s[1..]?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. Agreed.
  2. It is not the same, because of leading whitespace. We check if trimmed starts with ##, not s.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh right, my bad

@TimNN
Copy link
Contributor

TimNN commented Jul 3, 2018

Ping from triage, @QuietMisdreavus / @rust-lang/rustdoc: This PR requires your review.

@GuillaumeGomez
Copy link
Member

I think it should be documented in the rustdoc book. Can you add it in there please?

@lorepozo
Copy link
Contributor Author

lorepozo commented Jul 4, 2018

@GuillaumeGomez done — however, it will not render correctly until rust-lang/mdBook#719 gets merged.

@rust-highfive
Copy link
Contributor

The job x86_64-gnu-llvm-3.9 of your PR failed on Travis (raw log). Through arcane magic we have determined that the following fragments from the build log may contain information about the problem.

Click to expand the log.
[00:43:34] travis_fold:start:stage1-test
travis_time:start:stage1-test
Building stage1 test artifacts (x86_64-unknown-linux-gnu -> x86_64-unknown-linux-gnu)
[00:43:34]     Finished release [optimized] target(s) in 0.26s
[00:43:34] command did not execute successfully: "/checkout/obj/build/x86_64-unknown-linux-gnu/stage0/bin/cargo" "build" "--target" "x86_64-unknown-linux-gnu" "-j" "4" "--release" "--locked" "--color" "always" "--manifest-path" "/checkout/src/libtest/Cargo.toml" "--message-format" "json"
[00:43:34] expected success, got: signal: 9
[00:43:34] thread 'main' panicked at 'cargo must succeed', bootstrap/compile.rs:1114:9
[00:43:34] travis_fold:end:stage1-test

[00:43:34] travis_time:end:stage1-test:start=1530711748658475458,finish=1530711749257516580,duration=599041122


[00:43:34] failed to run: /checkout/obj/build/bootstrap/debug/bootstrap test
[00:43:34] Build completed unsuccessfully in 0:00:13
[00:43:34] make: *** [check] Error 1
[00:43:34] Makefile:58: recipe for target 'check' failed

The command "stamp sh -x -c "$RUN_SCRIPT"" exited with 2.
travis_time:start:05d0c637
$ date && (curl -fs --head https://google.com | grep ^Date: | sed 's/Date: //g' || true)

I'm a bot! I can only do what humans tell me to, so if this was not helpful or you have suggestions for improvements, please ping or otherwise contact @TimNN. (Feature Requests)

@GuillaumeGomez
Copy link
Member

I think the sentence is badly turned. It'd be better to precise that only the first # needs to be escaped. In case you don't want to write it, just show it into an example that only the first # needs to be escaped.

@lorepozo
Copy link
Contributor Author

lorepozo commented Jul 4, 2018

@GuillaumeGomez good point — I've updated it.

@GuillaumeGomez
Copy link
Member

Thanks!

@bors: r+

@bors
Copy link
Collaborator

bors commented Jul 4, 2018

📌 Commit ff2ff2b has been approved by GuillaumeGomez

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jul 4, 2018
@bors
Copy link
Collaborator

bors commented Jul 4, 2018

⌛ Testing commit ff2ff2b with merge afaa406...

bors added a commit that referenced this pull request Jul 4, 2018
…omez

rustdoc codeblock hash escape

So that docstring text such as the following (in a code block) can be created ergonomically:

```rust
let s = "
    foo
    # bar
    baz
";
```

Such code in a docstring hide the <code>&nbsp;&nbsp;&nbsp;&nbsp;# bar</code> line.

Previously, using two consecutive hashes <code>&nbsp;&nbsp;&nbsp;&nbsp;## bar</code> would turn the line into _shown_ `# bar`, losing the leading whitespace. A line of code like <code>&nbsp;&nbsp;&nbsp;&nbsp;# bar</code> (such as in the example above) **could not be represented** in the docstring text.

This commit makes the two consecutive hashes not also trim the leading whitespace — the two hashes simply **escape** into a single hash and do not hide the line, leaving the rest of that line unaffected. The new docstring text to achieve the above code block is:

```rust
/// ```
/// let s = "
///     foo
///     ## bar
///     baz
/// ";
/// ```
```
@bors
Copy link
Collaborator

bors commented Jul 4, 2018

☀️ Test successful - status-appveyor, status-travis
Approved by: GuillaumeGomez
Pushing afaa406 to master...

@bors bors merged commit ff2ff2b into rust-lang:master Jul 4, 2018
Ruin0x11 pushed a commit to Ruin0x11/mdBook that referenced this pull request Aug 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants