Skip to content

Commit fcee950

Browse files
let rustdoc print the crate version into docs
1 parent 1db1144 commit fcee950

File tree

5 files changed

+44
-0
lines changed

5 files changed

+44
-0
lines changed

src/librustdoc/clean/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ impl<T: Clean<U>, U> Clean<Vec<U>> for P<[T]> {
112112
#[derive(Clone, Debug)]
113113
pub struct Crate {
114114
pub name: String,
115+
pub version: Option<String>,
115116
pub src: PathBuf,
116117
pub module: Option<Item>,
117118
pub externs: Vec<(CrateNum, ExternalCrate)>,
@@ -183,6 +184,7 @@ impl<'a, 'tcx> Clean<Crate> for visit_ast::RustdocVisitor<'a, 'tcx> {
183184

184185
Crate {
185186
name,
187+
version: None,
186188
src,
187189
module: Some(module),
188190
externs,

src/librustdoc/html/render.rs

+14
Original file line numberDiff line numberDiff line change
@@ -256,6 +256,9 @@ pub struct Cache {
256256
// the access levels from crateanalysis.
257257
pub access_levels: Arc<AccessLevels<DefId>>,
258258

259+
/// The version of the crate being documented, if given fron the `--crate-version` flag.
260+
pub crate_version: Option<String>,
261+
259262
// Private fields only used when initially crawling a crate to build a cache
260263

261264
stack: Vec<String>,
@@ -534,6 +537,7 @@ pub fn run(mut krate: clean::Crate,
534537
primitive_locations: FxHashMap(),
535538
stripped_mod: false,
536539
access_levels: krate.access_levels.clone(),
540+
crate_version: krate.version.take(),
537541
orphan_impl_items: Vec::new(),
538542
traits: mem::replace(&mut krate.external_traits, FxHashMap()),
539543
deref_trait_did,
@@ -3422,6 +3426,16 @@ impl<'a> fmt::Display for Sidebar<'a> {
34223426
write!(fmt, "{}", it.name.as_ref().unwrap())?;
34233427
write!(fmt, "</p>")?;
34243428

3429+
if it.is_crate() {
3430+
if let Some(ref version) = cache().crate_version {
3431+
write!(fmt,
3432+
"<div class='block version'>\
3433+
<p>Version {}</p>\
3434+
</div>",
3435+
version)?;
3436+
}
3437+
}
3438+
34253439
match it.inner {
34263440
clean::StructItem(ref s) => sidebar_struct(fmt, it, s)?,
34273441
clean::TraitItem(ref t) => sidebar_trait(fmt, it, t)?,

src/librustdoc/html/static/rustdoc.css

+9
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,15 @@ nav.sub {
203203
word-wrap: break-word;
204204
}
205205

206+
.sidebar .version {
207+
font-size: 15px;
208+
text-align: center;
209+
border-bottom: #DDDDDD 1px solid;
210+
overflow-wrap: break-word;
211+
word-wrap: break-word; /* deprecated */
212+
word-break: break-word; /* Chrome, non-standard */
213+
}
214+
206215
.location:empty {
207216
border: none;
208217
}

src/librustdoc/lib.rs

+6
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ pub fn opts() -> Vec<RustcOptGroup> {
243243
unstable("display-warnings", |o| {
244244
o.optflag("", "display-warnings", "to print code warnings when testing doc")
245245
}),
246+
unstable("crate-version", |o| {
247+
o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
248+
}),
246249
]
247250
}
248251

@@ -460,6 +463,7 @@ where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R {
460463
let triple = matches.opt_str("target");
461464
let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
462465
let crate_name = matches.opt_str("crate-name");
466+
let crate_version = matches.opt_str("crate-version");
463467
let plugin_path = matches.opt_str("plugin-path");
464468

465469
let cr = PathBuf::from(cratefile);
@@ -484,6 +488,8 @@ where R: 'static + Send, F: 'static + Send + FnOnce(Output) -> R {
484488
krate.name = name
485489
}
486490

491+
krate.version = crate_version;
492+
487493
// Process all of the crate attributes, extracting plugin metadata along
488494
// with the passes which we are supposed to run.
489495
for attr in krate.module.as_ref().unwrap().attrs.lists("doc") {

src/test/rustdoc/crate-version.rs

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
// compile-flags: --crate-version=1.3.37 -Z unstable-options
12+
13+
// @has 'crate_version/index.html' '//div[@class="block version"]/p' 'Version 1.3.37'

0 commit comments

Comments
 (0)