Skip to content

Commit 810a514

Browse files
committed
Add crates to search-index
1 parent 9a79137 commit 810a514

File tree

3 files changed

+28
-9
lines changed

3 files changed

+28
-9
lines changed

src/librustdoc/html/render.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ use std::sync::Arc;
5252

5353
use externalfiles::ExternalHtml;
5454

55-
use serialize::json::{self, ToJson};
55+
use serialize::json::as_json;
5656
use syntax::{abi, ast};
5757
use syntax::feature_gate::UnstableFeatures;
5858
use rustc::middle::cstore::LOCAL_CRATE;
@@ -534,8 +534,8 @@ pub fn run(mut krate: clean::Crate,
534534
cx.krate(krate)
535535
}
536536

537+
/// Build the search index from the collected metadata
537538
fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
538-
// Build the search index from the collected metadata
539539
let mut nodeid_to_pathid = HashMap::new();
540540
let mut pathid_to_nodeid = Vec::new();
541541
{
@@ -582,7 +582,13 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
582582

583583
// Collect the index into a string
584584
let mut w = io::Cursor::new(Vec::new());
585-
write!(&mut w, r#"searchIndex['{}'] = {{"items":["#, krate.name).unwrap();
585+
let krate_doc = krate.module.as_ref().map(|module| {
586+
Escape(&shorter(module.doc_value())).to_string()
587+
}).unwrap_or("".to_owned());
588+
589+
write!(&mut w, r#"searchIndex[{}] = {{doc: {}, "items":["#,
590+
as_json(&krate.name),
591+
as_json(&krate_doc)).unwrap();
586592

587593
let mut lastpath = "".to_string();
588594
for (i, item) in cache.search_index.iter().enumerate() {
@@ -598,9 +604,9 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
598604
if i > 0 {
599605
write!(&mut w, ",").unwrap();
600606
}
601-
write!(&mut w, r#"[{},"{}","{}",{}"#,
602-
item.ty as usize, item.name, path,
603-
item.desc.to_json().to_string()).unwrap();
607+
write!(&mut w, "[{},{},{},{}",
608+
item.ty as usize,
609+
as_json(&item.name), as_json(&path), as_json(&item.desc)).unwrap();
604610
match item.parent {
605611
Some(nodeid) => {
606612
let pathid = *nodeid_to_pathid.get(&nodeid).unwrap();
@@ -693,7 +699,7 @@ fn write_shared(cx: &Context,
693699
if !line.starts_with(key) {
694700
continue
695701
}
696-
if line.starts_with(&format!("{}['{}']", key, krate)) {
702+
if line.starts_with(&format!(r#"{}["{}"]"#, key, krate)) {
697703
continue
698704
}
699705
ret.push(line.to_string());
@@ -1387,7 +1393,7 @@ impl Context {
13871393
let js_dst = this.dst.join("sidebar-items.js");
13881394
let mut js_out = BufWriter::new(try_err!(File::create(&js_dst), &js_dst));
13891395
try_err!(write!(&mut js_out, "initSidebarItems({});",
1390-
json::as_json(&items)), &js_dst);
1396+
as_json(&items)), &js_dst);
13911397
}
13921398

13931399
for item in m.items {

src/librustdoc/html/static/main.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -580,6 +580,9 @@
580580
displayPath = "";
581581
href = rootPath + item.path.replace(/::/g, '/') +
582582
'/' + type + '.' + name + '.html';
583+
} else if (type === "externcrate") {
584+
displayPath = "";
585+
href = rootPath + name + '/index.html';
583586
} else if (item.parent !== undefined) {
584587
var myparent = item.parent;
585588
var anchor = '#' + type + '.' + name;
@@ -678,6 +681,16 @@
678681
for (var crate in rawSearchIndex) {
679682
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
680683

684+
searchWords.push(crate);
685+
searchIndex.push({
686+
crate: crate,
687+
ty: 1, // == ExternCrate
688+
name: crate,
689+
path: "",
690+
desc: rawSearchIndex[crate].doc,
691+
type: null,
692+
});
693+
681694
// an array of [(Number) item type,
682695
// (String) name,
683696
// (String) full path or empty string for previous path,

src/librustdoc/html/static/styles/main.css

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ pre {
8282
}
8383

8484
.content a.primitive { color: #39a7bf; }
85-
.content span.mod, .content a.mod, block a.current.mod { color: #4d76ae; }
85+
.content span.externcrate, span.mod, .content a.mod, block a.current.mod { color: #4d76ae; }
8686
.content span.fn, .content a.fn, .block a.current.fn,
8787
.content span.method, .content a.method, .block a.current.method,
8888
.content span.tymethod, .content a.tymethod, .block a.current.tymethod,

0 commit comments

Comments
 (0)