Rollup merge of #100083 - notriddle:notriddle/source-files-js, r=GuillaumeGomez
rustdoc: use a more compact encoding for source-files.js This reduces the compiler-doc file from 40K to 36K, a 10% reduction in size.
This commit is contained in:
commit
d3b573d4c4
@ -366,13 +366,15 @@ pub(super) fn write_shared(
|
|||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
files.sort_unstable();
|
files.sort_unstable();
|
||||||
let subs = subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(",");
|
let subs = subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(",");
|
||||||
let dirs =
|
let dirs = if subs.is_empty() && files.is_empty() {
|
||||||
if subs.is_empty() { String::new() } else { format!(",\"dirs\":[{}]", subs) };
|
String::new()
|
||||||
|
} else {
|
||||||
|
format!(",[{}]", subs)
|
||||||
|
};
|
||||||
let files = files.join(",");
|
let files = files.join(",");
|
||||||
let files =
|
let files = if files.is_empty() { String::new() } else { format!(",[{}]", files) };
|
||||||
if files.is_empty() { String::new() } else { format!(",\"files\":[{}]", files) };
|
|
||||||
format!(
|
format!(
|
||||||
"{{\"name\":\"{name}\"{dirs}{files}}}",
|
"[\"{name}\"{dirs}{files}]",
|
||||||
name = self.elem.to_str().expect("invalid osstring conversion"),
|
name = self.elem.to_str().expect("invalid osstring conversion"),
|
||||||
dirs = dirs,
|
dirs = dirs,
|
||||||
files = files
|
files = files
|
||||||
@ -411,18 +413,23 @@ pub(super) fn write_shared(
|
|||||||
let dst = cx.dst.join(&format!("source-files{}.js", cx.shared.resource_suffix));
|
let dst = cx.dst.join(&format!("source-files{}.js", cx.shared.resource_suffix));
|
||||||
let make_sources = || {
|
let make_sources = || {
|
||||||
let (mut all_sources, _krates) =
|
let (mut all_sources, _krates) =
|
||||||
try_err!(collect(&dst, krate.name(cx.tcx()).as_str(), "sourcesIndex"), &dst);
|
try_err!(collect_json(&dst, krate.name(cx.tcx()).as_str()), &dst);
|
||||||
all_sources.push(format!(
|
all_sources.push(format!(
|
||||||
"sourcesIndex[\"{}\"] = {};",
|
r#""{}":{}"#,
|
||||||
&krate.name(cx.tcx()),
|
&krate.name(cx.tcx()),
|
||||||
hierarchy.to_json_string()
|
hierarchy
|
||||||
|
.to_json_string()
|
||||||
|
// All these `replace` calls are because we have to go through JS string for JSON content.
|
||||||
|
.replace('\\', r"\\")
|
||||||
|
.replace('\'', r"\'")
|
||||||
|
// We need to escape double quotes for the JSON.
|
||||||
|
.replace("\\\"", "\\\\\"")
|
||||||
));
|
));
|
||||||
all_sources.sort();
|
all_sources.sort();
|
||||||
Ok(format!(
|
let mut v = String::from("var sourcesIndex = JSON.parse('{\\\n");
|
||||||
"var sourcesIndex = {{}};\n{}\ncreateSourceSidebar();\n",
|
v.push_str(&all_sources.join(",\\\n"));
|
||||||
all_sources.join("\n")
|
v.push_str("\\\n}');\ncreateSourceSidebar();\n");
|
||||||
)
|
Ok(v.into_bytes())
|
||||||
.into_bytes())
|
|
||||||
};
|
};
|
||||||
write_crate("source-files.js", &make_sources)?;
|
write_crate("source-files.js", &make_sources)?;
|
||||||
}
|
}
|
||||||
|
@ -12,6 +12,10 @@
|
|||||||
const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value;
|
const rootPath = document.getElementById("rustdoc-vars").attributes["data-root-path"].value;
|
||||||
let oldScrollPosition = 0;
|
let oldScrollPosition = 0;
|
||||||
|
|
||||||
|
const NAME_OFFSET = 0;
|
||||||
|
const DIRS_OFFSET = 1;
|
||||||
|
const FILES_OFFSET = 2;
|
||||||
|
|
||||||
function closeSidebarIfMobile() {
|
function closeSidebarIfMobile() {
|
||||||
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
|
if (window.innerWidth < window.RUSTDOC_MOBILE_BREAKPOINT) {
|
||||||
updateLocalStorage("source-sidebar-show", "false");
|
updateLocalStorage("source-sidebar-show", "false");
|
||||||
@ -24,15 +28,15 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
|
|||||||
|
|
||||||
dirEntry.className = "dir-entry";
|
dirEntry.className = "dir-entry";
|
||||||
|
|
||||||
fullPath += elem["name"] + "/";
|
fullPath += elem[NAME_OFFSET] + "/";
|
||||||
|
|
||||||
summary.innerText = elem["name"];
|
summary.innerText = elem[NAME_OFFSET];
|
||||||
dirEntry.appendChild(summary);
|
dirEntry.appendChild(summary);
|
||||||
|
|
||||||
const folders = document.createElement("div");
|
const folders = document.createElement("div");
|
||||||
folders.className = "folders";
|
folders.className = "folders";
|
||||||
if (elem.dirs) {
|
if (elem[DIRS_OFFSET]) {
|
||||||
for (const dir of elem.dirs) {
|
for (const dir of elem[DIRS_OFFSET]) {
|
||||||
if (createDirEntry(dir, folders, fullPath, false)) {
|
if (createDirEntry(dir, folders, fullPath, false)) {
|
||||||
dirEntry.open = true;
|
dirEntry.open = true;
|
||||||
hasFoundFile = true;
|
hasFoundFile = true;
|
||||||
@ -43,8 +47,8 @@ function createDirEntry(elem, parent, fullPath, hasFoundFile) {
|
|||||||
|
|
||||||
const files = document.createElement("div");
|
const files = document.createElement("div");
|
||||||
files.className = "files";
|
files.className = "files";
|
||||||
if (elem.files) {
|
if (elem[FILES_OFFSET]) {
|
||||||
for (const file_text of elem.files) {
|
for (const file_text of elem[FILES_OFFSET]) {
|
||||||
const file = document.createElement("a");
|
const file = document.createElement("a");
|
||||||
file.innerText = file_text;
|
file.innerText = file_text;
|
||||||
file.href = rootPath + "src/" + fullPath + file_text + ".html";
|
file.href = rootPath + "src/" + fullPath + file_text + ".html";
|
||||||
@ -125,7 +129,7 @@ function createSourceSidebar() {
|
|||||||
title.innerText = "Files";
|
title.innerText = "Files";
|
||||||
sidebar.appendChild(title);
|
sidebar.appendChild(title);
|
||||||
Object.keys(sourcesIndex).forEach(key => {
|
Object.keys(sourcesIndex).forEach(key => {
|
||||||
sourcesIndex[key].name = key;
|
sourcesIndex[key][NAME_OFFSET] = key;
|
||||||
hasFoundFile = createDirEntry(sourcesIndex[key], sidebar, "",
|
hasFoundFile = createDirEntry(sourcesIndex[key], sidebar, "",
|
||||||
hasFoundFile);
|
hasFoundFile);
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user