Merge aliases and search-index
This commit is contained in:
parent
9697c467ac
commit
f581cf7544
7 changed files with 118 additions and 108 deletions
|
@ -114,7 +114,6 @@ pub fn render<T: Print, S: Print>(
|
||||||
window.rootPath = \"{root_path}\";\
|
window.rootPath = \"{root_path}\";\
|
||||||
window.currentCrate = \"{krate}\";\
|
window.currentCrate = \"{krate}\";\
|
||||||
</script>\
|
</script>\
|
||||||
<script src=\"{root_path}aliases{suffix}.js\"></script>\
|
|
||||||
<script src=\"{static_root_path}main{suffix}.js\"></script>\
|
<script src=\"{static_root_path}main{suffix}.js\"></script>\
|
||||||
{static_extra_scripts}\
|
{static_extra_scripts}\
|
||||||
{extra_scripts}\
|
{extra_scripts}\
|
||||||
|
|
|
@ -825,42 +825,6 @@ themePicker.onblur = handleThemeButtonsBlur;
|
||||||
Ok((ret, krates))
|
Ok((ret, krates))
|
||||||
}
|
}
|
||||||
|
|
||||||
fn show_item(item: &IndexItem, krate: &str) -> String {
|
|
||||||
format!(
|
|
||||||
"{{'crate':'{}','ty':{},'name':'{}','desc':'{}','p':'{}'{}}}",
|
|
||||||
krate,
|
|
||||||
item.ty as usize,
|
|
||||||
item.name,
|
|
||||||
item.desc.replace("'", "\\'"),
|
|
||||||
item.path,
|
|
||||||
if let Some(p) = item.parent_idx { format!(",'parent':{}", p) } else { String::new() }
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
let dst = cx.dst.join(&format!("aliases{}.js", cx.shared.resource_suffix));
|
|
||||||
{
|
|
||||||
let (mut all_aliases, _) = try_err!(collect(&dst, &krate.name, "ALIASES"), &dst);
|
|
||||||
let mut output = String::with_capacity(100);
|
|
||||||
for (alias, items) in cx.cache.get_aliases() {
|
|
||||||
if items.is_empty() {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
output.push_str(&format!(
|
|
||||||
"\"{}\":[{}],",
|
|
||||||
alias,
|
|
||||||
items.iter().map(|v| show_item(v, &krate.name)).collect::<Vec<_>>().join(",")
|
|
||||||
));
|
|
||||||
}
|
|
||||||
all_aliases.push(format!("ALIASES[\"{}\"] = {{{}}};", krate.name, output));
|
|
||||||
all_aliases.sort();
|
|
||||||
let mut v = Buffer::html();
|
|
||||||
writeln!(&mut v, "var ALIASES = {{}};");
|
|
||||||
for aliases in &all_aliases {
|
|
||||||
writeln!(&mut v, "{}", aliases);
|
|
||||||
}
|
|
||||||
cx.shared.fs.write(&dst, v.into_inner().into_bytes())?;
|
|
||||||
}
|
|
||||||
|
|
||||||
use std::ffi::OsString;
|
use std::ffi::OsString;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
|
|
@ -503,27 +503,6 @@ impl DocFolder for Cache {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Cache {
|
|
||||||
pub fn get_aliases<'a>(&'a self) -> FxHashMap<String, Vec<&'a IndexItem>> {
|
|
||||||
self.aliases
|
|
||||||
.iter()
|
|
||||||
.map(|(k, values)| {
|
|
||||||
(
|
|
||||||
k.clone(),
|
|
||||||
values
|
|
||||||
.iter()
|
|
||||||
.filter(|v| {
|
|
||||||
let x = &self.search_index[**v];
|
|
||||||
x.parent_idx.is_some() == x.parent.is_some()
|
|
||||||
})
|
|
||||||
.map(|v| &self.search_index[*v])
|
|
||||||
.collect::<Vec<_>>(),
|
|
||||||
)
|
|
||||||
})
|
|
||||||
.collect()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Attempts to find where an external crate is located, given that we're
|
/// Attempts to find where an external crate is located, given that we're
|
||||||
/// rendering in to the specified source destination.
|
/// rendering in to the specified source destination.
|
||||||
fn extern_location(
|
fn extern_location(
|
||||||
|
@ -640,6 +619,23 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
|
||||||
.map(|module| shorten(plain_summary_line(module.doc_value())))
|
.map(|module| shorten(plain_summary_line(module.doc_value())))
|
||||||
.unwrap_or(String::new());
|
.unwrap_or(String::new());
|
||||||
|
|
||||||
|
let crate_aliases = aliases
|
||||||
|
.iter()
|
||||||
|
.map(|(k, values)| {
|
||||||
|
(
|
||||||
|
k.clone(),
|
||||||
|
values
|
||||||
|
.iter()
|
||||||
|
.filter_map(|v| {
|
||||||
|
let x = &crate_items[*v];
|
||||||
|
if x.parent_idx.is_some() == x.parent.is_some() { Some(*v) } else { None }
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>(),
|
||||||
|
)
|
||||||
|
})
|
||||||
|
.filter(|(_, values)| !values.is_empty())
|
||||||
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct CrateData<'a> {
|
struct CrateData<'a> {
|
||||||
doc: String,
|
doc: String,
|
||||||
|
@ -647,6 +643,11 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
|
||||||
items: Vec<&'a IndexItem>,
|
items: Vec<&'a IndexItem>,
|
||||||
#[serde(rename = "p")]
|
#[serde(rename = "p")]
|
||||||
paths: Vec<(ItemType, String)>,
|
paths: Vec<(ItemType, String)>,
|
||||||
|
// The String is alias name and the vec is the list of the elements with this alias.
|
||||||
|
//
|
||||||
|
// To be noted: the `usize` elements are indexes to `items`.
|
||||||
|
#[serde(rename = "a")]
|
||||||
|
aliases: Option<Vec<(String, Vec<usize>)>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
// Collect the index into a string
|
// Collect the index into a string
|
||||||
|
@ -657,6 +658,7 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> String {
|
||||||
doc: crate_doc,
|
doc: crate_doc,
|
||||||
items: crate_items,
|
items: crate_items,
|
||||||
paths: crate_paths,
|
paths: crate_paths,
|
||||||
|
aliases: if crate_aliases.is_empty() { None } else { Some(crate_aliases) },
|
||||||
})
|
})
|
||||||
.expect("failed serde conversion")
|
.expect("failed serde conversion")
|
||||||
// All these `replace` calls are because we have to go through JS string for JSON content.
|
// All these `replace` calls are because we have to go through JS string for JSON content.
|
||||||
|
|
|
@ -531,6 +531,7 @@ function getSearchElement() {
|
||||||
var OUTPUT_DATA = 1;
|
var OUTPUT_DATA = 1;
|
||||||
var NO_TYPE_FILTER = -1;
|
var NO_TYPE_FILTER = -1;
|
||||||
var currentResults, index, searchIndex;
|
var currentResults, index, searchIndex;
|
||||||
|
var ALIASES = {};
|
||||||
var params = getQueryStringParams();
|
var params = getQueryStringParams();
|
||||||
|
|
||||||
// Populate search bar with query string search term when provided,
|
// Populate search bar with query string search term when provided,
|
||||||
|
@ -963,46 +964,60 @@ function getSearchElement() {
|
||||||
return itemTypes[ty.ty] + ty.path + ty.name;
|
return itemTypes[ty.ty] + ty.path + ty.name;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function createAliasFromItem(item) {
|
||||||
|
return {
|
||||||
|
crate: item.crate,
|
||||||
|
name: item.name,
|
||||||
|
path: item.path,
|
||||||
|
desc: item.desc,
|
||||||
|
ty: item.ty,
|
||||||
|
parent: item.parent,
|
||||||
|
type: item.parent,
|
||||||
|
is_alias: true,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function handleAliases(ret, query, filterCrates) {
|
function handleAliases(ret, query, filterCrates) {
|
||||||
if (ALIASES) {
|
var aliases = [];
|
||||||
var aliases = [];
|
var i;
|
||||||
if (filterCrates !== undefined &&
|
if (filterCrates !== undefined &&
|
||||||
ALIASES[filterCrates] &&
|
ALIASES[filterCrates] &&
|
||||||
ALIASES[filterCrates][query.search]) {
|
ALIASES[filterCrates][query.search]) {
|
||||||
aliases = ALIASES[filterCrates][query.search];
|
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
|
||||||
} else {
|
aliases.push(
|
||||||
Object.keys(ALIASES).forEach(function(crate) {
|
createAliasFromItem(searchIndex[ALIASES[filterCrates][query.search]]));
|
||||||
if (ALIASES[crate][query.search]) {
|
|
||||||
for (var i = 0; i < ALIASES[crate][query.search].length; ++i) {
|
|
||||||
aliases.push(ALIASES[crate][query.search][i]);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
aliases.sort(function(aaa, bbb) {
|
} else {
|
||||||
if (aaa.path < bbb.path) {
|
Object.keys(ALIASES).forEach(function(crate) {
|
||||||
return 1;
|
if (ALIASES[crate][query.search]) {
|
||||||
} else if (aaa.path === bbb.path) {
|
for (i = 0; i < ALIASES[crate][query.search].length; ++i) {
|
||||||
return 0;
|
aliases.push(
|
||||||
|
createAliasFromItem(
|
||||||
|
searchIndex[ALIASES[crate][query.search][i]]));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return -1;
|
|
||||||
});
|
});
|
||||||
for (var i = 0; i < aliases.length; ++i) {
|
}
|
||||||
var alias = aliases[i];
|
aliases.sort(function(aaa, bbb) {
|
||||||
alias.is_alias = true;
|
if (aaa.path < bbb.path) {
|
||||||
if (typeof alias.parent === "number") {
|
return 1;
|
||||||
alias.parent = rawSearchIndex[alias.crate].p[alias.parent];
|
} else if (aaa.path === bbb.path) {
|
||||||
}
|
return 0;
|
||||||
alias.alias = query.raw;
|
}
|
||||||
alias.path = alias.p || alias.crate;
|
return -1;
|
||||||
var res = buildHrefAndPath(aliases[i]);
|
});
|
||||||
alias.displayPath = pathSplitter(res[0]);
|
for (i = 0; i < aliases.length; ++i) {
|
||||||
alias.fullPath = alias.displayPath + alias.name;
|
var alias = aliases[i];
|
||||||
alias.href = res[1];
|
|
||||||
ret.others.unshift(alias);
|
alias.alias = query.raw;
|
||||||
if (ret.others.length > MAX_RESULTS) {
|
var res = buildHrefAndPath(alias);
|
||||||
ret.others.pop();
|
alias.displayPath = pathSplitter(res[0]);
|
||||||
}
|
alias.fullPath = alias.displayPath + alias.name;
|
||||||
|
alias.href = res[1];
|
||||||
|
|
||||||
|
ret.others.unshift(alias);
|
||||||
|
if (ret.others.length > MAX_RESULTS) {
|
||||||
|
ret.others.pop();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1683,10 +1698,13 @@ function getSearchElement() {
|
||||||
searchIndex = [];
|
searchIndex = [];
|
||||||
var searchWords = [];
|
var searchWords = [];
|
||||||
var i;
|
var i;
|
||||||
|
var currentIndex = 0;
|
||||||
|
|
||||||
for (var crate in rawSearchIndex) {
|
for (var crate in rawSearchIndex) {
|
||||||
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
|
if (!rawSearchIndex.hasOwnProperty(crate)) { continue; }
|
||||||
|
|
||||||
|
var crateSize = 0;
|
||||||
|
|
||||||
searchWords.push(crate);
|
searchWords.push(crate);
|
||||||
searchIndex.push({
|
searchIndex.push({
|
||||||
crate: crate,
|
crate: crate,
|
||||||
|
@ -1696,6 +1714,7 @@ function getSearchElement() {
|
||||||
desc: rawSearchIndex[crate].doc,
|
desc: rawSearchIndex[crate].doc,
|
||||||
type: null,
|
type: null,
|
||||||
});
|
});
|
||||||
|
currentIndex += 1;
|
||||||
|
|
||||||
// an array of [(Number) item type,
|
// an array of [(Number) item type,
|
||||||
// (String) name,
|
// (String) name,
|
||||||
|
@ -1707,6 +1726,9 @@ function getSearchElement() {
|
||||||
// an array of [(Number) item type,
|
// an array of [(Number) item type,
|
||||||
// (String) name]
|
// (String) name]
|
||||||
var paths = rawSearchIndex[crate].p;
|
var paths = rawSearchIndex[crate].p;
|
||||||
|
// a array of [(String) alias name
|
||||||
|
// [Number] index to items]
|
||||||
|
var aliases = rawSearchIndex[crate].a;
|
||||||
|
|
||||||
// convert `rawPaths` entries into object form
|
// convert `rawPaths` entries into object form
|
||||||
var len = paths.length;
|
var len = paths.length;
|
||||||
|
@ -1725,9 +1747,18 @@ function getSearchElement() {
|
||||||
var lastPath = "";
|
var lastPath = "";
|
||||||
for (i = 0; i < len; ++i) {
|
for (i = 0; i < len; ++i) {
|
||||||
var rawRow = items[i];
|
var rawRow = items[i];
|
||||||
var row = {crate: crate, ty: rawRow[0], name: rawRow[1],
|
if (!rawRow[2]) {
|
||||||
path: rawRow[2] || lastPath, desc: rawRow[3],
|
rawRow[2] = lastPath;
|
||||||
parent: paths[rawRow[4]], type: rawRow[5]};
|
}
|
||||||
|
var row = {
|
||||||
|
crate: crate,
|
||||||
|
ty: rawRow[0],
|
||||||
|
name: rawRow[1],
|
||||||
|
path: rawRow[2],
|
||||||
|
desc: rawRow[3],
|
||||||
|
parent: paths[rawRow[4]],
|
||||||
|
type: rawRow[5],
|
||||||
|
};
|
||||||
searchIndex.push(row);
|
searchIndex.push(row);
|
||||||
if (typeof row.name === "string") {
|
if (typeof row.name === "string") {
|
||||||
var word = row.name.toLowerCase();
|
var word = row.name.toLowerCase();
|
||||||
|
@ -1736,7 +1767,24 @@ function getSearchElement() {
|
||||||
searchWords.push("");
|
searchWords.push("");
|
||||||
}
|
}
|
||||||
lastPath = row.path;
|
lastPath = row.path;
|
||||||
|
crateSize += 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (aliases) {
|
||||||
|
ALIASES[crate] = {};
|
||||||
|
var j, local_aliases;
|
||||||
|
for (i = 0; i < aliases.length; ++i) {
|
||||||
|
var alias_name = aliases[i][0];
|
||||||
|
if (!ALIASES[crate].hasOwnProperty(alias_name)) {
|
||||||
|
ALIASES[crate][alias_name] = [];
|
||||||
|
}
|
||||||
|
local_aliases = aliases[i][1];
|
||||||
|
for (j = 0; j < local_aliases.length; ++j) {
|
||||||
|
ALIASES[crate][alias_name].push(local_aliases[j] + currentIndex);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
currentIndex += crateSize;
|
||||||
}
|
}
|
||||||
return searchWords;
|
return searchWords;
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,9 @@ const QUERY = '+';
|
||||||
|
|
||||||
const EXPECTED = {
|
const EXPECTED = {
|
||||||
'others': [
|
'others': [
|
||||||
{ 'path': 'core', 'name': 'AddAssign' },
|
{ 'path': 'core::ops', 'name': 'AddAssign' },
|
||||||
{ 'path': 'core', 'name': 'Add' },
|
{ 'path': 'core::ops', 'name': 'Add' },
|
||||||
{ 'path': 'std', 'name': 'AddAssign' },
|
{ 'path': 'std::ops', 'name': 'AddAssign' },
|
||||||
{ 'path': 'std::ops', 'name': 'Add' },
|
{ 'path': 'std::ops', 'name': 'Add' },
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
|
@ -5,7 +5,7 @@ const QUERY = '[';
|
||||||
const EXPECTED = {
|
const EXPECTED = {
|
||||||
'others': [
|
'others': [
|
||||||
{ 'path': 'std', 'name': 'slice' },
|
{ 'path': 'std', 'name': 'slice' },
|
||||||
{ 'path': 'std', 'name': 'IndexMut' },
|
{ 'path': 'std::ops', 'name': 'IndexMut' },
|
||||||
{ 'path': 'std', 'name': 'Index' },
|
{ 'path': 'std::ops', 'name': 'Index' },
|
||||||
],
|
],
|
||||||
};
|
};
|
||||||
|
|
|
@ -218,7 +218,7 @@ function lookForEntry(entry, data) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
|
function loadMainJsAndIndex(mainJs, searchIndex, crate) {
|
||||||
if (searchIndex[searchIndex.length - 1].length === 0) {
|
if (searchIndex[searchIndex.length - 1].length === 0) {
|
||||||
searchIndex.pop();
|
searchIndex.pop();
|
||||||
}
|
}
|
||||||
|
@ -238,17 +238,15 @@ function loadMainJsAndIndex(mainJs, aliases, searchIndex, crate) {
|
||||||
var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
|
var functionsToLoad = ["buildHrefAndPath", "pathSplitter", "levenshtein", "validateResult",
|
||||||
"handleAliases", "getQuery", "buildIndex", "execQuery", "execSearch"];
|
"handleAliases", "getQuery", "buildIndex", "execQuery", "execSearch"];
|
||||||
|
|
||||||
|
ALIASES = {};
|
||||||
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
|
finalJS += 'window = { "currentCrate": "' + crate + '" };\n';
|
||||||
finalJS += 'var rootPath = "../";\n';
|
finalJS += 'var rootPath = "../";\n';
|
||||||
finalJS += aliases;
|
|
||||||
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
|
finalJS += loadThings(arraysToLoad, 'array', extractArrayVariable, mainJs);
|
||||||
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
|
finalJS += loadThings(variablesToLoad, 'variable', extractVariable, mainJs);
|
||||||
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
|
finalJS += loadThings(functionsToLoad, 'function', extractFunction, mainJs);
|
||||||
|
|
||||||
var loaded = loadContent(finalJS);
|
var loaded = loadContent(finalJS);
|
||||||
var index = loaded.buildIndex(searchIndex.rawSearchIndex);
|
var index = loaded.buildIndex(searchIndex.rawSearchIndex);
|
||||||
// We make it "global" so that the "loaded.execSearch" function will find it.
|
|
||||||
rawSearchIndex = searchIndex.rawSearchIndex;
|
|
||||||
|
|
||||||
return [loaded, index];
|
return [loaded, index];
|
||||||
}
|
}
|
||||||
|
@ -340,11 +338,10 @@ function runChecks(testFile, loaded, index) {
|
||||||
|
|
||||||
function load_files(doc_folder, resource_suffix, crate) {
|
function load_files(doc_folder, resource_suffix, crate) {
|
||||||
var mainJs = readFile(path.join(doc_folder, "main" + resource_suffix + ".js"));
|
var mainJs = readFile(path.join(doc_folder, "main" + resource_suffix + ".js"));
|
||||||
var aliases = readFile(path.join(doc_folder, "aliases" + resource_suffix + ".js"));
|
|
||||||
var searchIndex = readFile(
|
var searchIndex = readFile(
|
||||||
path.join(doc_folder, "search-index" + resource_suffix + ".js")).split("\n");
|
path.join(doc_folder, "search-index" + resource_suffix + ".js")).split("\n");
|
||||||
|
|
||||||
return loadMainJsAndIndex(mainJs, aliases, searchIndex, crate);
|
return loadMainJsAndIndex(mainJs, searchIndex, crate);
|
||||||
}
|
}
|
||||||
|
|
||||||
function showHelp() {
|
function showHelp() {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue