Skip to content

Commit 6d6fa79

Browse files
committed
rustdoc: clean up the layout for annotated version numbers
This should result in a layout for the actual standard library, when built on CI, that looks like this: _____ / \ std | R | 1.74.0-nightly \_____/ (203c57d 2023-09-17) Having the whole version as one string caused it to flex wrap, because the sidebar isn't wide enough to fit the whole thing.
1 parent 957c5db commit 6d6fa79

File tree

6 files changed

+23
-6
lines changed

6 files changed

+23
-6
lines changed

src/librustdoc/html/layout.rs

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ pub(crate) struct Layout {
1818
pub(crate) default_settings: FxHashMap<String, String>,
1919
pub(crate) krate: String,
2020
pub(crate) krate_version: String,
21+
pub(crate) krate_version_extra: String,
2122
/// The given user css file which allow to customize the generated
2223
/// documentation theme.
2324
pub(crate) css_file_extension: Option<PathBuf>,

src/librustdoc/html/render/context.rs

+5-1
Original file line numberDiff line numberDiff line change
@@ -528,13 +528,17 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
528528
if let Some(url) = playground_url {
529529
playground = Some(markdown::Playground { crate_name: Some(krate.name(tcx)), url });
530530
}
531+
let krate_version = cache.crate_version.as_deref().unwrap_or_default();
532+
let (krate_version, krate_version_extra) =
533+
krate_version.split_once(" ").unwrap_or((krate_version, ""));
531534
let mut layout = layout::Layout {
532535
logo: String::new(),
533536
favicon: String::new(),
534537
external_html,
535538
default_settings,
536539
krate: krate.name(tcx).to_string(),
537-
krate_version: cache.crate_version.as_deref().unwrap_or_default().to_string(),
540+
krate_version: krate_version.to_string(),
541+
krate_version_extra: krate_version_extra.to_string(),
538542
css_file_extension: extension_css,
539543
scrape_examples_extension: !call_locations.is_empty(),
540544
};

src/librustdoc/html/static/css/rustdoc.css

+1
Original file line numberDiff line numberDiff line change
@@ -492,6 +492,7 @@ ul.block, .block li {
492492
}
493493

494494
.sidebar-elems,
495+
.sidebar > .version,
495496
.sidebar > h2 {
496497
padding-left: 24px;
497498
}

src/librustdoc/html/static/js/main.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,13 @@ function setMobileTopbar() {
5151
// but with the current code it's hard to get the right information in the right place.
5252
const mobileTopbar = document.querySelector(".mobile-topbar");
5353
const locationTitle = document.querySelector(".sidebar h2.location");
54-
if (mobileLocationTitle) {
54+
if (mobileTopbar) {
5555
const mobileTitle = document.createElement("h2");
56+
mobileTitle.className = "location";
5657
if (hasClass(document.body, "crate")) {
57-
mobileLocationTitle.innerText = `Crate ${window.currentCrate}`;
58+
mobileTitle.innerText = `Crate ${window.currentCrate}`;
5859
} else if (locationTitle) {
59-
mobileLocationTitle.innerHTML = locationTitle.innerHTML;
60+
mobileTitle.innerHTML = locationTitle.innerHTML;
6061
}
6162
mobileTopbar.appendChild(mobileTitle);
6263
}

src/librustdoc/html/templates/page.html

+5-2
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,11 @@ <h2> {# #}
101101
{% if !layout.krate_version.is_empty() %}
102102
<span class="version">{{+ layout.krate_version}}</span>
103103
{% endif %}
104-
</h2>
105-
</div>
104+
</h2> {# #}
105+
</div> {# #}
106+
{% if !layout.krate_version_extra.is_empty() %}
107+
<div class="version">{{+ layout.krate_version_extra}}</div> {# #}
108+
{% endif %}
106109
{% endif %}
107110
{{ sidebar|safe }}
108111
</nav> {# #}

tests/rustdoc/crate-version-extra.rs

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// compile-flags: '--crate-version=1.3.37-nightly (203c57dbe 2023-09-17)'
2+
3+
#![crate_name="foo"]
4+
5+
// main version next to logo, extra version data below it
6+
// @has 'foo/index.html' '//h2/span[@class="version"]' '1.3.37-nightly'
7+
// @has 'foo/index.html' '//nav[@class="sidebar"]/div[@class="version"]' '(203c57dbe 2023-09-17)'

0 commit comments

Comments
 (0)