]> git.lizzy.rs Git - rust.git/commitdiff
let rustdoc print the crate version into docs
authorQuietMisdreavus <grey@quietmisdreavus.net>
Mon, 2 Oct 2017 23:29:03 +0000 (18:29 -0500)
committerQuietMisdreavus <grey@quietmisdreavus.net>
Mon, 9 Oct 2017 14:56:17 +0000 (09:56 -0500)
src/librustdoc/clean/mod.rs
src/librustdoc/html/render.rs
src/librustdoc/html/static/rustdoc.css
src/librustdoc/lib.rs
src/test/rustdoc/crate-version.rs [new file with mode: 0644]

index c9afa3646b2da0ff260fafb2cc7bd84d22d5357f..6cf8ea3c57dd0911fe820837c526312a38f37544 100644 (file)
@@ -112,6 +112,7 @@ fn clean(&self, cx: &DocContext) -> Vec<U> {
 #[derive(Clone, Debug)]
 pub struct Crate {
     pub name: String,
+    pub version: Option<String>,
     pub src: PathBuf,
     pub module: Option<Item>,
     pub externs: Vec<(CrateNum, ExternalCrate)>,
@@ -183,6 +184,7 @@ fn clean(&self, cx: &DocContext) -> Crate {
 
         Crate {
             name,
+            version: None,
             src,
             module: Some(module),
             externs,
index a3f446885f96e7270668ef5497bb17b6a4c96594..9bc94eff14223925e0fd8fa9ca581795654f20b4 100644 (file)
@@ -256,6 +256,9 @@ pub struct Cache {
     // the access levels from crateanalysis.
     pub access_levels: Arc<AccessLevels<DefId>>,
 
+    /// The version of the crate being documented, if given fron the `--crate-version` flag.
+    pub crate_version: Option<String>,
+
     // Private fields only used when initially crawling a crate to build a cache
 
     stack: Vec<String>,
@@ -534,6 +537,7 @@ pub fn run(mut krate: clean::Crate,
         primitive_locations: FxHashMap(),
         stripped_mod: false,
         access_levels: krate.access_levels.clone(),
+        crate_version: krate.version.take(),
         orphan_impl_items: Vec::new(),
         traits: mem::replace(&mut krate.external_traits, FxHashMap()),
         deref_trait_did,
@@ -3422,6 +3426,16 @@ fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
             write!(fmt, "{}", it.name.as_ref().unwrap())?;
             write!(fmt, "</p>")?;
 
+            if it.is_crate() {
+                if let Some(ref version) = cache().crate_version {
+                    write!(fmt,
+                           "<div class='block version'>\
+                            <p>Version {}</p>\
+                            </div>",
+                           version)?;
+                }
+            }
+
             match it.inner {
                 clean::StructItem(ref s) => sidebar_struct(fmt, it, s)?,
                 clean::TraitItem(ref t) => sidebar_trait(fmt, it, t)?,
index 27574e67bc87bef27ac9641103f56d4308cf82f2..61a3902098ffa6eab4c6139143f19e747880fbd7 100644 (file)
@@ -203,6 +203,15 @@ nav.sub {
        word-wrap: break-word;
 }
 
+.sidebar .version {
+       font-size: 15px;
+       text-align: center;
+       border-bottom: #DDDDDD 1px solid;
+       overflow-wrap: break-word;
+       word-wrap: break-word; /* deprecated */
+       word-break: break-word; /* Chrome, non-standard */
+}
+
 .location:empty {
        border: none;
 }
index 9563ccfcc65fd9e9617a4c29e2226ccb82e0eb72..f8bf00ad73fcca1fb9fc417e80978d6a47e1e619 100644 (file)
@@ -243,6 +243,9 @@ pub fn opts() -> Vec<RustcOptGroup> {
         unstable("display-warnings", |o| {
             o.optflag("", "display-warnings", "to print code warnings when testing doc")
         }),
+        unstable("crate-version", |o| {
+            o.optopt("", "crate-version", "crate version to print into documentation", "VERSION")
+        }),
     ]
 }
 
@@ -460,6 +463,7 @@ fn rust_input<R, F>(cratefile: &str, externs: Externs, matches: &getopts::Matche
     let triple = matches.opt_str("target");
     let maybe_sysroot = matches.opt_str("sysroot").map(PathBuf::from);
     let crate_name = matches.opt_str("crate-name");
+    let crate_version = matches.opt_str("crate-version");
     let plugin_path = matches.opt_str("plugin-path");
 
     let cr = PathBuf::from(cratefile);
@@ -484,6 +488,8 @@ fn rust_input<R, F>(cratefile: &str, externs: Externs, matches: &getopts::Matche
             krate.name = name
         }
 
+        krate.version = crate_version;
+
         // Process all of the crate attributes, extracting plugin metadata along
         // with the passes which we are supposed to run.
         for attr in krate.module.as_ref().unwrap().attrs.lists("doc") {
diff --git a/src/test/rustdoc/crate-version.rs b/src/test/rustdoc/crate-version.rs
new file mode 100644 (file)
index 0000000..07ab5ce
--- /dev/null
@@ -0,0 +1,13 @@
+// Copyright 2016 The Rust Project Developers. See the COPYRIGHT
+// file at the top-level directory of this distribution and at
+// http://rust-lang.org/COPYRIGHT.
+//
+// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
+// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
+// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
+// option. This file may not be copied, modified, or distributed
+// except according to those terms.
+
+// compile-flags: --crate-version=1.3.37 -Z unstable-options
+
+// @has 'crate_version/index.html' '//div[@class="block version"]/p' 'Version 1.3.37'