Skip to content

Commit

Permalink
rustdoc: use <wbr> in sidebar
Browse files Browse the repository at this point in the history
This also improves sidebar layout, so instead of

    BTreeM
    ap

you get this

    BTree
    Map
  • Loading branch information
notriddle committed Jul 7, 2024
1 parent 0411d58 commit b7f202a
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 2 deletions.
16 changes: 16 additions & 0 deletions src/librustdoc/html/render/sidebar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ impl<'a> Link<'a> {
}
}

pub(crate) mod filters {
use crate::{html::escape::EscapeBodyTextWithWbr, html::render::display_fn};
use askama::{Html, MarkupDisplay};
use std::fmt::Display;
pub(crate) fn wrapped<T>(v: T) -> askama::Result<MarkupDisplay<Html, impl Display>>
where
T: Display,
{
let string = v.to_string();
Ok(MarkupDisplay::new_safe(
display_fn(move |f| EscapeBodyTextWithWbr(&string).fmt(f)),
Html,
))
}
}

pub(super) fn print_sidebar(cx: &Context<'_>, it: &clean::Item, buffer: &mut Buffer) {
let blocks: Vec<LinkBlock<'_>> = match *it.kind {
clean::StructItem(ref s) => sidebar_struct(cx, it, s),
Expand Down
6 changes: 4 additions & 2 deletions src/librustdoc/html/templates/sidebar.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@ <h2 class="location"> {# #}
{% for block in blocks %}
{% if block.should_render() %}
{% if !block.heading.name.is_empty() %}
<h3><a href="#{{block.heading.href|safe}}">{{block.heading.name}}</a></h3>
<h3><a href="#{{block.heading.href|safe}}"> {# #}
{{block.heading.name|wrapped|safe}} {# #}
</a></h3> {# #}
{% endif %}
{% if !block.links.is_empty() %}
<ul class="block{% if !block.class.is_empty() +%} {{+block.class}}{% endif %}">
{% for link in block.links %}
<li><a href="#{{link.href|safe}}">{{link.name}}</a></li>
<li><a href="#{{link.href|safe}}">{{link.name|wrapped|safe}}</a></li>
{% endfor %}
</ul>
{% endif %}
Expand Down

0 comments on commit b7f202a

Please sign in to comment.