From 9eb336a0206178f52146d41dd152166233df703b Mon Sep 17 00:00:00 2001 From: Kang Seonghoon Date: Thu, 10 Apr 2014 01:27:35 +0900 Subject: [PATCH] rustdoc: Get rid of `allPaths` global variable by merging it into `searchIndex`. --- src/librustdoc/html/render.rs | 9 +++--- src/librustdoc/html/static/main.js | 44 ++++++++++++++++++++---------- 2 files changed, 34 insertions(+), 19 deletions(-) diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs index aa54642262b83..d7739daad57e7 100644 --- a/src/librustdoc/html/render.rs +++ b/src/librustdoc/html/render.rs @@ -308,7 +308,7 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> { // Publish the search index let index = { let mut w = MemWriter::new(); - try!(write!(&mut w, "searchIndex['{}'] = [", krate.name)); + try!(write!(&mut w, r#"searchIndex['{}'] = \{"items":["#, krate.name)); for (i, item) in cache.search_index.iter().enumerate() { if i > 0 { try!(write!(&mut w, ",")); @@ -325,8 +325,7 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> { } try!(write!(&mut w, "]")); } - try!(write!(&mut w, "];")); - try!(write!(&mut w, "allPaths['{}'] = [", krate.name)); + try!(write!(&mut w, r#"],"paths":["#)); for (i, &nodeid) in pathid_to_nodeid.iter().enumerate() { let &(ref fqp, short) = cache.paths.find(&nodeid).unwrap(); if i > 0 { @@ -335,7 +334,7 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> { try!(write!(&mut w, r#"[{:u},"{}"]"#, short, *fqp.last().unwrap())); } - try!(write!(&mut w, "];")); + try!(write!(&mut w, r"]\};")); str::from_utf8(w.unwrap().as_slice()).unwrap().to_owned() }; @@ -371,7 +370,7 @@ pub fn run(mut krate: clean::Crate, dst: Path) -> io::IoResult<()> { } } let mut w = try!(File::create(&dst)); - try!(writeln!(&mut w, r"var searchIndex = \{\}; var allPaths = \{\};")); + try!(writeln!(&mut w, r"var searchIndex = \{\};")); for index in all_indexes.iter() { try!(writeln!(&mut w, "{}", *index)); } diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 692ab5b9ac286..9ef25c50206f6 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -9,7 +9,7 @@ // except according to those terms. /*jslint browser: true, es5: true */ -/*globals $: true, rootPath: true, allPaths: true */ +/*globals $: true, rootPath: true */ (function() { "use strict"; @@ -258,7 +258,7 @@ var result = results[i], name = result.item.name.toLowerCase(), path = result.item.path.toLowerCase(), - parent = allPaths[result.item.crate][result.item.parent]; + parent = result.item.parent; var valid = validateResult(name, path, split, parent); if (!valid) { @@ -294,7 +294,7 @@ if ((validate) && (name.toLowerCase().indexOf(keys[i]) > -1 || path.toLowerCase().indexOf(keys[i]) > -1 || - parent[1].toLowerCase().indexOf(keys[i]) > -1)) + parent.name.toLowerCase().indexOf(keys[i]) > -1)) { validate = true; } else { @@ -422,15 +422,13 @@ '/index.html" class="' + type + '">' + name + ''; } else if (item.parent !== undefined) { - var myparent = allPaths[item.crate][item.parent]; - var parentType = myparent[0]; - var parentName = myparent[1]; + var myparent = item.parent; var anchor = '#' + type + '.' + name; - output += item.path + '::' + parentName + + output += item.path + '::' + myparent.name + '::' + name + ''; @@ -538,18 +536,36 @@ var searchWords = []; for (var crate in rawSearchIndex) { if (!rawSearchIndex.hasOwnProperty(crate)) { continue } - var len = rawSearchIndex[crate].length; - var i = 0; + // an array of [(Number) item type, + // (String) name, + // (String) full path, + // (String) description, + // (optional Number) the parent path index to `paths`] + var items = rawSearchIndex[crate].items; + // an array of [(Number) item type, + // (String) name] + var paths = rawSearchIndex[crate].paths; + + // convert `paths` into an object form + var len = paths.length; + for (var i = 0; i < len; ++i) { + paths[i] = {ty: paths[i][0], name: paths[i][1]}; + } + + // convert `items` into an object form, and construct word indices. + // // before any analysis is performed lets gather the search terms to // search against apart from the rest of the data. This is a quick // operation that is cached for the life of the page state so that // all other search operations have access to this cached data for // faster analysis operations - for (i = 0; i < len; i += 1) { - var rawRow = rawSearchIndex[crate][i]; + var len = items.length; + for (var i = 0; i < len; i += 1) { + var rawRow = items[i]; var row = {crate: crate, ty: rawRow[0], name: rawRow[1], - path: rawRow[2], desc: rawRow[3], parent: rawRow[4]}; + path: rawRow[2], desc: rawRow[3], + parent: paths[rawRow[4]]}; searchIndex.push(row); if (typeof row.name === "string") { var word = row.name.toLowerCase();