Skip to content

rustdoc: Add a custom callback for codespan to collapse whitespace #24116

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
Apr 7, 2015

Conversation

zaeleus
Copy link
Contributor

@zaeleus zaeleus commented Apr 6, 2015

Because the current style for code in rustdoc is to prewrap whitespace, code spans that are hard wrapped in the source documentation are prematurely wrapped when rendered in HTML. For example,

/// ...
/// type can be borrowed as multiple different types. In particular, `Vec<T>:
/// Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`.

renders as

screen shot 2015-04-06 at 12 11 21

because "Vec<T>: Borrow<Vec<T>>" wraps to the next line in the source.

CommonMark 0.18 [1] specifies "interior spaces and line endings are collapsed into single spaces" for code spans, which would actually prevent this issue, but hoedown does not currently conform to the
CommonMark spec.

The added span-level callback attempts to adhere to how whitespace is handled as described by CommonMark, fixing the issue of early, unintentional wrapping of code spans in rendered HTML.

Because the current style for `code` in rustdoc is to prewrap
whitespace, code spans that are hard wrapped in the source
documentation are prematurely wrapped when rendered in HTML.

CommonMark 0.18 [[1]] specifies "interior spaces and line endings are
collapsed into single spaces" for code spans, which would actually
prevent this issue, but hoedown does not currently conform to the
CommonMark spec.

The added span-level callback attempts to adhere to how whitespace is
handled as described by CommonMark, fixing the issue of early,
unintentional wrapping of code spans in rendered HTML.

[1]: http://spec.commonmark.org/0.18/
@zaeleus zaeleus force-pushed the rustdoc-codespan branch from c26b66e to b6c2e82 Compare April 6, 2015 18:03
@steveklabnik
Copy link
Member

Ouch! We don't strictly follow CommonMark, but I like this.

}
}

buffer
Copy link
Member

Choose a reason for hiding this comment

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

Perhaps this would be a little clearer leveraging iterators?

s.split(|c: char| c.is_whitespace()).filter(|s| {
    !s.is_empty()
}).collect::<Vec<_>>().connect(" ")

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's clean, thanks! I didn't know str::split could take a closure.

@alexcrichton
Copy link
Member

Nice fix!

@alexcrichton alexcrichton assigned alexcrichton and unassigned huonw Apr 6, 2015
@zaeleus zaeleus force-pushed the rustdoc-codespan branch from 4a78e81 to 46cc6e5 Compare April 6, 2015 19:07
@alexcrichton
Copy link
Member

@bors: r+ 46cc6e5

Thanks!

bors added a commit that referenced this pull request Apr 7, 2015
Because the current style for `code` in rustdoc is to prewrap whitespace, code spans that are hard wrapped in the source documentation are prematurely wrapped when rendered in HTML. [For example][2],

```
/// ...
/// type can be borrowed as multiple different types. In particular, `Vec<T>:
/// Borrow<Vec<T>>` and `Vec<T>: Borrow<[T]>`.
```

renders as

![screen shot 2015-04-06 at 12 11 21](https://cloud.githubusercontent.com/assets/191331/7008216/2706b3b0-dc56-11e4-941e-1b0154fcbc5c.png)

because "`Vec<T>: Borrow<Vec<T>>`" wraps to the next line in the source.

CommonMark 0.18 [[1]] specifies "interior spaces and line endings are collapsed into single spaces" for code spans, which would actually prevent this issue, but hoedown does not currently conform to the
CommonMark spec.

The added span-level callback attempts to adhere to how whitespace is handled as described by CommonMark, fixing the issue of early, unintentional wrapping of code spans in rendered HTML.

[1]: http://spec.commonmark.org/0.18/
[2]: https://doc.rust-lang.org/std/borrow/trait.Borrow.html
@bors
Copy link
Collaborator

bors commented Apr 7, 2015

⌛ Testing commit 46cc6e5 with merge de51bbe...

@bors bors merged commit 46cc6e5 into rust-lang:master Apr 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants