Skip to content
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

rustdoc: Strip blank lines from start and end of code blocks #78695

Open
camelid opened this issue Nov 3, 2020 · 2 comments
Open

rustdoc: Strip blank lines from start and end of code blocks #78695

camelid opened this issue Nov 3, 2020 · 2 comments
Labels
A-markdown-parsing Area: Markdown parsing for doc-comments C-enhancement Category: An issue proposing an enhancement or a PR with one. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.

Comments

@camelid
Copy link
Member

camelid commented Nov 3, 2020

People will often use the # character to hide lines that aren't relevant to the example, but rustdoc will display blank lines you leave between #-prefixed lines and the visible lines. For example, this source:

//! ```
//! # #[cfg(any(feature = "alloc", feature = "std"))]
//! # mod test {
//!
//! use num::FromPrimitive;
//! use num::bigint::BigInt;
//! use num::rational::{Ratio, BigRational};
//!
//! # pub
//! fn approx_sqrt(number: u64, iterations: usize) -> BigRational {
//!     let start: Ratio<BigInt> = Ratio::from_integer(FromPrimitive::from_u64(number).unwrap());
//!     let mut approx = start.clone();
//!
//!     for _ in 0..iterations {
//!         approx = (&approx + (&start / &approx)) /
//!             Ratio::from_integer(FromPrimitive::from_u64(2).unwrap());
//!     }
//!
//!     approx
//! }
//! # }
//! # #[cfg(not(any(feature = "alloc", feature = "std")))]
//! # mod test { pub fn approx_sqrt(n: u64, _: usize) -> u64 { n } }
//! # use crate::test::approx_sqrt;
//!
//! fn main() {
//!     println!("{}", approx_sqrt(10, 4)); // prints 4057691201/1283082416
//! }
//!
//! ```

leads to this output:

image

I propose that rustdoc strips blank lines from the start and end of code blocks to avoid this not-great-looking output. This would happen after the # lines were stripped out.

@camelid camelid added T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue. C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Nov 3, 2020
@jyn514 jyn514 added the A-markdown-parsing Area: Markdown parsing for doc-comments label Nov 12, 2020
@camelid
Copy link
Member Author

camelid commented Jan 20, 2021

I feel like I remember Guillaume or someone else working on something to fix this recently, but I can't find the PR. Am I mistaken or do you know which PR it is?

@ojeda
Copy link
Contributor

ojeda commented Oct 13, 2022

The start/end behaves differently, which is confusing, e.g. this:

/// ```
/// # use c::f;
///
///
/// f()?;
///
///
/// # Ok::<(), i32>(())
/// ```

will render 2 blank lines on top, but only 1 on the bottom.

Edit: since this may be considered a bug (inconsistent number of blank lines), and this issue is a feature request (stripping all blank lines), I have created a separate issue: #102996.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-markdown-parsing Area: Markdown parsing for doc-comments C-enhancement Category: An issue proposing an enhancement or a PR with one. T-rustdoc Relevant to the rustdoc team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

4 participants