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 a32cb15
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ struct PageLayout<'a> {
display_krate_version_extra: &'a str,
}

pub(crate) use crate::html::render::sidebar::filters;

pub(crate) fn render<T: Print, S: Print>(
layout: &Layout,
page: &Page<'_>,
Expand Down
2 changes: 1 addition & 1 deletion src/librustdoc/html/render/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ mod tests;

mod context;
mod print_item;
mod sidebar;
pub(crate) mod sidebar;
mod span_map;
mod type_layout;
mod write_shared;
Expand Down
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
2 changes: 1 addition & 1 deletion src/librustdoc/html/templates/page.html
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@
</a> {# #}
{% endif %}
<h2> {# #}
<a href="{{page.root_path|safe}}{{display_krate_with_trailing_slash|safe}}index.html">{{display_krate}}</a> {# #}
<a href="{{page.root_path|safe}}{{display_krate_with_trailing_slash|safe}}index.html">{{display_krate|wrapped|safe}}</a> {# #}
{% if !display_krate_version_number.is_empty() %}
<span class="version">{{+ display_krate_version_number}}</span>
{% endif %}
Expand Down
8 changes: 5 additions & 3 deletions src/librustdoc/html/templates/sidebar.html
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{% if !title.is_empty() %}
<h2 class="location"> {# #}
<a href="#">{{title_prefix}}{{title}}</a> {# #}
<a href="#">{{title_prefix}}{{title|wrapped|safe}}</a> {# #}
</h2>
{% endif %}
<div class="sidebar-elems">
Expand All @@ -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 a32cb15

Please sign in to comment.