2021-02-13 23:17:38 -08:00
|
|
|
use std::ffi::OsStr;
|
|
|
|
use std::fs::{self, File};
|
|
|
|
use std::io::prelude::*;
|
|
|
|
use std::io::{self, BufReader};
|
2021-03-05 10:54:37 -05:00
|
|
|
use std::path::{Component, Path, PathBuf};
|
2022-05-26 20:18:00 +02:00
|
|
|
use std::rc::Rc;
|
2022-06-16 19:39:39 +04:00
|
|
|
use std::sync::LazyLock as Lazy;
|
2021-02-13 23:17:38 -08:00
|
|
|
|
|
|
|
use itertools::Itertools;
|
|
|
|
use rustc_data_structures::flock;
|
|
|
|
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
|
2022-08-05 16:36:47 -07:00
|
|
|
use serde::ser::SerializeSeq;
|
|
|
|
use serde::{Serialize, Serializer};
|
2021-02-13 23:17:38 -08:00
|
|
|
|
|
|
|
use super::{collect_paths_for_type, ensure_trailing_slash, Context, BASIC_KEYWORDS};
|
|
|
|
use crate::clean::Crate;
|
2021-03-25 12:46:35 -04:00
|
|
|
use crate::config::{EmitType, RenderOptions};
|
2021-03-25 11:40:32 -04:00
|
|
|
use crate::docfs::PathError;
|
2021-02-13 23:17:38 -08:00
|
|
|
use crate::error::Error;
|
|
|
|
use crate::html::{layout, static_files};
|
2021-10-30 02:07:37 +00:00
|
|
|
use crate::{try_err, try_none};
|
2021-02-13 23:17:38 -08:00
|
|
|
|
2021-05-28 21:23:39 +02:00
|
|
|
static FILES_UNVERSIONED: Lazy<FxHashMap<&str, &[u8]>> = Lazy::new(|| {
|
2021-03-05 10:54:37 -05:00
|
|
|
map! {
|
2022-04-21 10:57:57 +02:00
|
|
|
"FiraSans-Regular.woff2" => static_files::fira_sans::REGULAR,
|
|
|
|
"FiraSans-Medium.woff2" => static_files::fira_sans::MEDIUM,
|
2021-03-05 10:54:37 -05:00
|
|
|
"FiraSans-LICENSE.txt" => static_files::fira_sans::LICENSE,
|
2022-04-21 10:57:57 +02:00
|
|
|
"SourceSerif4-Regular.ttf.woff2" => static_files::source_serif_4::REGULAR,
|
|
|
|
"SourceSerif4-Bold.ttf.woff2" => static_files::source_serif_4::BOLD,
|
|
|
|
"SourceSerif4-It.ttf.woff2" => static_files::source_serif_4::ITALIC,
|
2021-03-29 18:33:22 +02:00
|
|
|
"SourceSerif4-LICENSE.md" => static_files::source_serif_4::LICENSE,
|
2022-04-21 10:57:57 +02:00
|
|
|
"SourceCodePro-Regular.ttf.woff2" => static_files::source_code_pro::REGULAR,
|
|
|
|
"SourceCodePro-Semibold.ttf.woff2" => static_files::source_code_pro::SEMIBOLD,
|
|
|
|
"SourceCodePro-It.ttf.woff2" => static_files::source_code_pro::ITALIC,
|
2021-03-05 10:54:37 -05:00
|
|
|
"SourceCodePro-LICENSE.txt" => static_files::source_code_pro::LICENSE,
|
2022-04-21 10:57:57 +02:00
|
|
|
"NanumBarunGothic.ttf.woff2" => static_files::nanum_barun_gothic::REGULAR,
|
2021-10-26 10:58:13 +09:00
|
|
|
"NanumBarunGothic-LICENSE.txt" => static_files::nanum_barun_gothic::LICENSE,
|
2021-03-05 10:54:37 -05:00
|
|
|
"LICENSE-MIT.txt" => static_files::LICENSE_MIT,
|
|
|
|
"LICENSE-APACHE.txt" => static_files::LICENSE_APACHE,
|
|
|
|
"COPYRIGHT.txt" => static_files::COPYRIGHT,
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-03-25 11:40:32 -04:00
|
|
|
enum SharedResource<'a> {
|
|
|
|
/// This file will never change, no matter what toolchain is used to build it.
|
|
|
|
///
|
|
|
|
/// It does not have a resource suffix.
|
2021-03-31 11:13:51 -04:00
|
|
|
Unversioned { name: &'static str },
|
2021-03-25 11:40:32 -04:00
|
|
|
/// This file may change depending on the toolchain.
|
|
|
|
///
|
|
|
|
/// It has a resource suffix.
|
2021-03-31 11:13:51 -04:00
|
|
|
ToolchainSpecific { basename: &'static str },
|
2021-03-31 11:35:57 -04:00
|
|
|
/// This file may change for any crate within a build, or based on the CLI arguments.
|
2021-03-25 11:40:32 -04:00
|
|
|
///
|
2021-03-31 11:35:57 -04:00
|
|
|
/// This differs from normal invocation-specific files because it has a resource suffix.
|
|
|
|
InvocationSpecific { basename: &'a str },
|
2021-03-25 11:40:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SharedResource<'_> {
|
|
|
|
fn extension(&self) -> Option<&OsStr> {
|
|
|
|
use SharedResource::*;
|
|
|
|
match self {
|
|
|
|
Unversioned { name }
|
|
|
|
| ToolchainSpecific { basename: name }
|
2021-03-31 11:35:57 -04:00
|
|
|
| InvocationSpecific { basename: name } => Path::new(name).extension(),
|
2021-03-25 11:40:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn path(&self, cx: &Context<'_>) -> PathBuf {
|
|
|
|
match self {
|
|
|
|
SharedResource::Unversioned { name } => cx.dst.join(name),
|
|
|
|
SharedResource::ToolchainSpecific { basename } => cx.suffix_path(basename),
|
2021-03-31 11:35:57 -04:00
|
|
|
SharedResource::InvocationSpecific { basename } => cx.suffix_path(basename),
|
2021-03-25 11:40:32 -04:00
|
|
|
}
|
|
|
|
}
|
2021-03-25 12:46:35 -04:00
|
|
|
|
|
|
|
fn should_emit(&self, emit: &[EmitType]) -> bool {
|
|
|
|
if emit.is_empty() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
let kind = match self {
|
|
|
|
SharedResource::Unversioned { .. } => EmitType::Unversioned,
|
|
|
|
SharedResource::ToolchainSpecific { .. } => EmitType::Toolchain,
|
2021-03-31 11:35:57 -04:00
|
|
|
SharedResource::InvocationSpecific { .. } => EmitType::InvocationSpecific,
|
2021-03-25 12:46:35 -04:00
|
|
|
};
|
|
|
|
emit.contains(&kind)
|
|
|
|
}
|
2021-03-25 11:40:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Context<'_> {
|
|
|
|
fn suffix_path(&self, filename: &str) -> PathBuf {
|
|
|
|
// We use splitn vs Path::extension here because we might get a filename
|
|
|
|
// like `style.min.css` and we want to process that into
|
|
|
|
// `style-suffix.min.css`. Path::extension would just return `css`
|
|
|
|
// which would result in `style.min-suffix.css` which isn't what we
|
|
|
|
// want.
|
|
|
|
let (base, ext) = filename.split_once('.').unwrap();
|
|
|
|
let filename = format!("{}{}.{}", base, self.shared.resource_suffix, ext);
|
|
|
|
self.dst.join(&filename)
|
|
|
|
}
|
|
|
|
|
2021-08-22 02:25:25 +00:00
|
|
|
fn write_shared(
|
2021-03-25 12:46:35 -04:00
|
|
|
&self,
|
|
|
|
resource: SharedResource<'_>,
|
2021-08-22 02:25:25 +00:00
|
|
|
contents: impl 'static + Send + AsRef<[u8]>,
|
2021-03-25 12:46:35 -04:00
|
|
|
emit: &[EmitType],
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
if resource.should_emit(emit) {
|
|
|
|
self.shared.fs.write(resource.path(self), contents)
|
|
|
|
} else {
|
|
|
|
Ok(())
|
|
|
|
}
|
2021-03-25 11:40:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fn write_minify(
|
|
|
|
&self,
|
|
|
|
resource: SharedResource<'_>,
|
2021-08-22 02:25:25 +00:00
|
|
|
contents: impl 'static + Send + AsRef<str> + AsRef<[u8]>,
|
2021-03-25 11:40:32 -04:00
|
|
|
minify: bool,
|
2021-03-25 12:46:35 -04:00
|
|
|
emit: &[EmitType],
|
2021-03-25 11:40:32 -04:00
|
|
|
) -> Result<(), Error> {
|
2021-08-22 02:25:25 +00:00
|
|
|
if minify {
|
|
|
|
let contents = contents.as_ref();
|
2021-10-01 17:12:39 +02:00
|
|
|
let contents = if resource.extension() == Some(OsStr::new("css")) {
|
2022-06-05 23:37:59 +02:00
|
|
|
minifier::css::minify(contents)
|
|
|
|
.map_err(|e| {
|
|
|
|
Error::new(format!("failed to minify CSS file: {}", e), resource.path(self))
|
|
|
|
})?
|
|
|
|
.to_string()
|
2021-03-25 11:40:32 -04:00
|
|
|
} else {
|
2022-06-05 23:37:59 +02:00
|
|
|
minifier::js::minify(contents).to_string()
|
2021-03-25 11:40:32 -04:00
|
|
|
};
|
2021-08-22 02:25:25 +00:00
|
|
|
self.write_shared(resource, contents, emit)
|
2021-03-25 11:40:32 -04:00
|
|
|
} else {
|
2021-08-22 02:25:25 +00:00
|
|
|
self.write_shared(resource, contents, emit)
|
|
|
|
}
|
2021-03-25 11:40:32 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 23:17:38 -08:00
|
|
|
pub(super) fn write_shared(
|
2022-05-26 20:18:00 +02:00
|
|
|
cx: &mut Context<'_>,
|
2021-02-13 23:17:38 -08:00
|
|
|
krate: &Crate,
|
|
|
|
search_index: String,
|
|
|
|
options: &RenderOptions,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
// Write out the shared files. Note that these are shared among all rustdoc
|
|
|
|
// docs placed in the output directory, so this needs to be a synchronized
|
|
|
|
// operation with respect to all other rustdocs running around.
|
|
|
|
let lock_file = cx.dst.join(".lock");
|
|
|
|
let _lock = try_err!(flock::Lock::new(&lock_file, true, true, true), &lock_file);
|
|
|
|
|
2021-08-22 02:25:25 +00:00
|
|
|
// Minified resources are usually toolchain resources. If they're not, they should use `cx.write_minify` directly.
|
|
|
|
fn write_minify(
|
|
|
|
basename: &'static str,
|
|
|
|
contents: impl 'static + Send + AsRef<str> + AsRef<[u8]>,
|
|
|
|
cx: &Context<'_>,
|
|
|
|
options: &RenderOptions,
|
|
|
|
) -> Result<(), Error> {
|
2021-03-25 11:40:32 -04:00
|
|
|
cx.write_minify(
|
2021-08-22 02:25:25 +00:00
|
|
|
SharedResource::ToolchainSpecific { basename },
|
|
|
|
contents,
|
2021-03-25 11:40:32 -04:00
|
|
|
options.enable_minification,
|
2021-03-25 12:46:35 -04:00
|
|
|
&options.emit,
|
2021-03-25 11:40:32 -04:00
|
|
|
)
|
2021-08-22 02:25:25 +00:00
|
|
|
}
|
|
|
|
|
2021-03-31 11:13:51 -04:00
|
|
|
// Toolchain resources should never be dynamic.
|
|
|
|
let write_toolchain = |p: &'static _, c: &'static _| {
|
2021-03-25 12:46:35 -04:00
|
|
|
cx.write_shared(SharedResource::ToolchainSpecific { basename: p }, c, &options.emit)
|
|
|
|
};
|
2021-03-31 11:13:51 -04:00
|
|
|
|
|
|
|
// Crate resources should always be dynamic.
|
|
|
|
let write_crate = |p: &_, make_content: &dyn Fn() -> Result<Vec<u8>, Error>| {
|
|
|
|
let content = make_content()?;
|
2021-03-31 11:35:57 -04:00
|
|
|
cx.write_shared(SharedResource::InvocationSpecific { basename: p }, content, &options.emit)
|
2021-03-31 11:13:51 -04:00
|
|
|
};
|
2021-03-25 11:40:32 -04:00
|
|
|
|
2021-11-23 21:23:54 -08:00
|
|
|
// Given "foo.svg", return e.g. "url(\"foo1.58.0.svg\")"
|
|
|
|
fn ver_url(cx: &Context<'_>, basename: &'static str) -> String {
|
|
|
|
format!(
|
|
|
|
"url(\"{}\")",
|
|
|
|
SharedResource::ToolchainSpecific { basename }
|
2021-07-31 16:23:51 +02:00
|
|
|
.path(cx)
|
|
|
|
.file_name()
|
|
|
|
.unwrap()
|
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
2021-11-23 21:23:54 -08:00
|
|
|
)
|
2021-07-31 16:23:51 +02:00
|
|
|
}
|
|
|
|
|
2021-11-23 21:23:54 -08:00
|
|
|
// We use the AUTOREPLACE mechanism to inject into our static JS and CSS certain
|
|
|
|
// values that are only known at doc build time. Since this mechanism is somewhat
|
|
|
|
// surprising when reading the code, please limit it to rustdoc.css.
|
|
|
|
write_minify(
|
|
|
|
"rustdoc.css",
|
|
|
|
static_files::RUSTDOC_CSS
|
|
|
|
.replace(
|
|
|
|
"/* AUTOREPLACE: */url(\"toggle-minus.svg\")",
|
|
|
|
&ver_url(cx, "toggle-minus.svg"),
|
|
|
|
)
|
|
|
|
.replace("/* AUTOREPLACE: */url(\"toggle-plus.svg\")", &ver_url(cx, "toggle-plus.svg"))
|
|
|
|
.replace("/* AUTOREPLACE: */url(\"down-arrow.svg\")", &ver_url(cx, "down-arrow.svg")),
|
2021-07-31 16:23:51 +02:00
|
|
|
cx,
|
2021-11-23 21:23:54 -08:00
|
|
|
options,
|
|
|
|
)?;
|
2021-07-31 16:23:51 +02:00
|
|
|
|
2021-02-13 23:17:38 -08:00
|
|
|
// Add all the static files. These may already exist, but we just
|
|
|
|
// overwrite them anyway to make sure that they're fresh and up-to-date.
|
2021-08-22 02:25:25 +00:00
|
|
|
write_minify("settings.css", static_files::SETTINGS_CSS, cx, options)?;
|
|
|
|
write_minify("noscript.css", static_files::NOSCRIPT_CSS, cx, options)?;
|
2021-02-13 23:17:38 -08:00
|
|
|
|
|
|
|
// To avoid "light.css" to be overwritten, we'll first run over the received themes and only
|
|
|
|
// then we'll run over the "official" styles.
|
|
|
|
let mut themes: FxHashSet<String> = FxHashSet::default();
|
|
|
|
|
|
|
|
for entry in &cx.shared.style_files {
|
2021-11-22 23:23:58 -08:00
|
|
|
let theme = entry.basename()?;
|
2021-02-13 23:17:38 -08:00
|
|
|
let extension =
|
|
|
|
try_none!(try_none!(entry.path.extension(), &entry.path).to_str(), &entry.path);
|
|
|
|
|
|
|
|
// Handle the official themes
|
2021-11-22 23:23:58 -08:00
|
|
|
match theme.as_str() {
|
2021-08-22 02:25:25 +00:00
|
|
|
"light" => write_minify("light.css", static_files::themes::LIGHT, cx, options)?,
|
|
|
|
"dark" => write_minify("dark.css", static_files::themes::DARK, cx, options)?,
|
|
|
|
"ayu" => write_minify("ayu.css", static_files::themes::AYU, cx, options)?,
|
2021-02-13 23:17:38 -08:00
|
|
|
_ => {
|
|
|
|
// Handle added third-party themes
|
2021-03-31 11:13:51 -04:00
|
|
|
let filename = format!("{}.{}", theme, extension);
|
|
|
|
write_crate(&filename, &|| Ok(try_err!(fs::read(&entry.path), &entry.path)))?;
|
2021-02-13 23:17:38 -08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
themes.insert(theme.to_owned());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (*cx.shared).layout.logo.is_empty() {
|
2022-01-13 14:44:30 +01:00
|
|
|
write_toolchain("rust-logo.svg", static_files::RUST_LOGO_SVG)?;
|
2021-02-13 23:17:38 -08:00
|
|
|
}
|
|
|
|
if (*cx.shared).layout.favicon.is_empty() {
|
2021-03-25 11:40:32 -04:00
|
|
|
write_toolchain("favicon.svg", static_files::RUST_FAVICON_SVG)?;
|
|
|
|
write_toolchain("favicon-16x16.png", static_files::RUST_FAVICON_PNG_16)?;
|
|
|
|
write_toolchain("favicon-32x32.png", static_files::RUST_FAVICON_PNG_32)?;
|
2021-02-13 23:17:38 -08:00
|
|
|
}
|
2021-03-25 11:40:32 -04:00
|
|
|
write_toolchain("wheel.svg", static_files::WHEEL_SVG)?;
|
2021-05-09 20:41:24 +02:00
|
|
|
write_toolchain("clipboard.svg", static_files::CLIPBOARD_SVG)?;
|
2021-03-25 11:40:32 -04:00
|
|
|
write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;
|
2021-07-31 16:23:51 +02:00
|
|
|
write_toolchain("toggle-minus.svg", static_files::TOGGLE_MINUS_PNG)?;
|
|
|
|
write_toolchain("toggle-plus.svg", static_files::TOGGLE_PLUS_PNG)?;
|
2021-02-13 23:17:38 -08:00
|
|
|
|
|
|
|
let mut themes: Vec<&String> = themes.iter().collect();
|
|
|
|
themes.sort();
|
|
|
|
|
2021-11-23 19:22:29 -08:00
|
|
|
write_minify("main.js", static_files::MAIN_JS, cx, options)?;
|
2021-08-22 02:25:25 +00:00
|
|
|
write_minify("search.js", static_files::SEARCH_JS, cx, options)?;
|
|
|
|
write_minify("settings.js", static_files::SETTINGS_JS, cx, options)?;
|
2021-05-14 22:45:00 +02:00
|
|
|
|
2021-04-06 00:07:46 +02:00
|
|
|
if cx.include_sources {
|
2021-08-22 02:25:25 +00:00
|
|
|
write_minify("source-script.js", static_files::sidebar::SOURCE_SCRIPT, cx, options)?;
|
2021-02-13 23:17:38 -08:00
|
|
|
}
|
|
|
|
|
2021-11-23 19:22:29 -08:00
|
|
|
write_minify("storage.js", static_files::STORAGE_JS, cx, options)?;
|
2021-02-13 23:17:38 -08:00
|
|
|
|
2021-10-01 13:05:35 -07:00
|
|
|
if cx.shared.layout.scrape_examples_extension {
|
2021-10-01 13:57:57 -07:00
|
|
|
cx.write_minify(
|
2021-10-01 14:12:26 -07:00
|
|
|
SharedResource::InvocationSpecific { basename: "scrape-examples.js" },
|
2021-10-01 13:57:57 -07:00
|
|
|
static_files::SCRAPE_EXAMPLES_JS,
|
|
|
|
options.enable_minification,
|
|
|
|
&options.emit,
|
|
|
|
)?;
|
2021-10-01 13:05:35 -07:00
|
|
|
}
|
|
|
|
|
2021-02-13 23:17:38 -08:00
|
|
|
if let Some(ref css) = cx.shared.layout.css_file_extension {
|
|
|
|
let buffer = try_err!(fs::read_to_string(css), css);
|
2021-03-31 17:23:32 -04:00
|
|
|
// This varies based on the invocation, so it can't go through the write_minify wrapper.
|
|
|
|
cx.write_minify(
|
|
|
|
SharedResource::InvocationSpecific { basename: "theme.css" },
|
2021-08-22 02:25:25 +00:00
|
|
|
buffer,
|
2021-03-31 17:23:32 -04:00
|
|
|
options.enable_minification,
|
|
|
|
&options.emit,
|
|
|
|
)?;
|
2021-02-13 23:17:38 -08:00
|
|
|
}
|
2021-08-22 02:25:25 +00:00
|
|
|
write_minify("normalize.css", static_files::NORMALIZE_CSS, cx, options)?;
|
2021-03-25 11:40:32 -04:00
|
|
|
for (name, contents) in &*FILES_UNVERSIONED {
|
2021-03-25 12:46:35 -04:00
|
|
|
cx.write_shared(SharedResource::Unversioned { name }, contents, &options.emit)?;
|
2021-03-05 10:35:22 -05:00
|
|
|
}
|
2021-02-13 23:17:38 -08:00
|
|
|
|
2022-08-04 12:13:16 -07:00
|
|
|
/// Read a file and return all lines that match the `"{crate}":{data},` format,
|
|
|
|
/// and return a tuple `(Vec<DataString>, Vec<CrateNameString>)`.
|
|
|
|
///
|
|
|
|
/// This forms the payload of files that look like this:
|
|
|
|
///
|
|
|
|
/// ```javascript
|
|
|
|
/// var data = {
|
|
|
|
/// "{crate1}":{data},
|
|
|
|
/// "{crate2}":{data}
|
|
|
|
/// };
|
|
|
|
/// use_data(data);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The file needs to be formatted so that *only crate data lines start with `"`*.
|
|
|
|
fn collect(path: &Path, krate: &str) -> io::Result<(Vec<String>, Vec<String>)> {
|
2021-02-13 23:17:38 -08:00
|
|
|
let mut ret = Vec::new();
|
|
|
|
let mut krates = Vec::new();
|
|
|
|
|
|
|
|
if path.exists() {
|
2022-08-04 12:13:16 -07:00
|
|
|
let prefix = format!("\"{}\"", krate);
|
2021-02-13 23:17:38 -08:00
|
|
|
for line in BufReader::new(File::open(path)?).lines() {
|
|
|
|
let line = line?;
|
2022-08-04 12:13:16 -07:00
|
|
|
if !line.starts_with('"') {
|
2021-02-13 23:17:38 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if line.starts_with(&prefix) {
|
|
|
|
continue;
|
|
|
|
}
|
2022-09-03 22:57:22 +02:00
|
|
|
if line.ends_with(',') {
|
2022-08-04 12:13:16 -07:00
|
|
|
ret.push(line[..line.len() - 1].to_string());
|
|
|
|
} else {
|
|
|
|
// No comma (it's the case for the last added crate line)
|
|
|
|
ret.push(line.to_string());
|
|
|
|
}
|
2021-02-13 23:17:38 -08:00
|
|
|
krates.push(
|
2022-08-04 12:13:16 -07:00
|
|
|
line.split('"')
|
|
|
|
.find(|s| !s.is_empty())
|
2021-02-13 23:17:38 -08:00
|
|
|
.map(|s| s.to_owned())
|
|
|
|
.unwrap_or_else(String::new),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok((ret, krates))
|
|
|
|
}
|
|
|
|
|
2022-08-04 12:13:16 -07:00
|
|
|
/// Read a file and return all lines that match the <code>"{crate}":{data},\</code> format,
|
|
|
|
/// and return a tuple `(Vec<DataString>, Vec<CrateNameString>)`.
|
|
|
|
///
|
|
|
|
/// This forms the payload of files that look like this:
|
|
|
|
///
|
|
|
|
/// ```javascript
|
|
|
|
/// var data = JSON.parse('{\
|
|
|
|
/// "{crate1}":{data},\
|
|
|
|
/// "{crate2}":{data}\
|
|
|
|
/// }');
|
|
|
|
/// use_data(data);
|
|
|
|
/// ```
|
|
|
|
///
|
|
|
|
/// The file needs to be formatted so that *only crate data lines start with `"`*.
|
2021-02-13 23:17:38 -08:00
|
|
|
fn collect_json(path: &Path, krate: &str) -> io::Result<(Vec<String>, Vec<String>)> {
|
|
|
|
let mut ret = Vec::new();
|
|
|
|
let mut krates = Vec::new();
|
|
|
|
|
|
|
|
if path.exists() {
|
|
|
|
let prefix = format!("\"{}\"", krate);
|
|
|
|
for line in BufReader::new(File::open(path)?).lines() {
|
|
|
|
let line = line?;
|
|
|
|
if !line.starts_with('"') {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if line.starts_with(&prefix) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if line.ends_with(",\\") {
|
|
|
|
ret.push(line[..line.len() - 2].to_string());
|
|
|
|
} else {
|
|
|
|
// Ends with "\\" (it's the case for the last added crate line)
|
|
|
|
ret.push(line[..line.len() - 1].to_string());
|
|
|
|
}
|
|
|
|
krates.push(
|
|
|
|
line.split('"')
|
|
|
|
.find(|s| !s.is_empty())
|
|
|
|
.map(|s| s.to_owned())
|
|
|
|
.unwrap_or_else(String::new),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Ok((ret, krates))
|
|
|
|
}
|
|
|
|
|
|
|
|
use std::ffi::OsString;
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Hierarchy {
|
|
|
|
elem: OsString,
|
|
|
|
children: FxHashMap<OsString, Hierarchy>,
|
|
|
|
elems: FxHashSet<OsString>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Hierarchy {
|
|
|
|
fn new(elem: OsString) -> Hierarchy {
|
|
|
|
Hierarchy { elem, children: FxHashMap::default(), elems: FxHashSet::default() }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn to_json_string(&self) -> String {
|
|
|
|
let mut subs: Vec<&Hierarchy> = self.children.values().collect();
|
|
|
|
subs.sort_unstable_by(|a, b| a.elem.cmp(&b.elem));
|
|
|
|
let mut files = self
|
|
|
|
.elems
|
|
|
|
.iter()
|
|
|
|
.map(|s| format!("\"{}\"", s.to_str().expect("invalid osstring conversion")))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
files.sort_unstable();
|
|
|
|
let subs = subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(",");
|
2022-08-02 13:24:34 -07:00
|
|
|
let dirs = if subs.is_empty() && files.is_empty() {
|
|
|
|
String::new()
|
|
|
|
} else {
|
|
|
|
format!(",[{}]", subs)
|
|
|
|
};
|
2021-02-13 23:17:38 -08:00
|
|
|
let files = files.join(",");
|
2022-08-02 13:24:34 -07:00
|
|
|
let files = if files.is_empty() { String::new() } else { format!(",[{}]", files) };
|
2021-02-13 23:17:38 -08:00
|
|
|
format!(
|
2022-08-02 13:24:34 -07:00
|
|
|
"[\"{name}\"{dirs}{files}]",
|
2021-02-13 23:17:38 -08:00
|
|
|
name = self.elem.to_str().expect("invalid osstring conversion"),
|
|
|
|
dirs = dirs,
|
|
|
|
files = files
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-06 00:07:46 +02:00
|
|
|
if cx.include_sources {
|
2021-02-13 23:17:38 -08:00
|
|
|
let mut hierarchy = Hierarchy::new(OsString::new());
|
|
|
|
for source in cx
|
|
|
|
.shared
|
|
|
|
.local_sources
|
|
|
|
.iter()
|
|
|
|
.filter_map(|p| p.0.strip_prefix(&cx.shared.src_root).ok())
|
|
|
|
{
|
|
|
|
let mut h = &mut hierarchy;
|
|
|
|
let mut elems = source
|
|
|
|
.components()
|
|
|
|
.filter_map(|s| match s {
|
|
|
|
Component::Normal(s) => Some(s.to_owned()),
|
|
|
|
_ => None,
|
|
|
|
})
|
|
|
|
.peekable();
|
|
|
|
loop {
|
|
|
|
let cur_elem = elems.next().expect("empty file path");
|
|
|
|
if elems.peek().is_none() {
|
|
|
|
h.elems.insert(cur_elem);
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
let e = cur_elem.clone();
|
|
|
|
h = h.children.entry(cur_elem.clone()).or_insert_with(|| Hierarchy::new(e));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let dst = cx.dst.join(&format!("source-files{}.js", cx.shared.resource_suffix));
|
2021-03-31 11:13:51 -04:00
|
|
|
let make_sources = || {
|
|
|
|
let (mut all_sources, _krates) =
|
2022-08-02 13:24:34 -07:00
|
|
|
try_err!(collect_json(&dst, krate.name(cx.tcx()).as_str()), &dst);
|
2021-03-31 11:13:51 -04:00
|
|
|
all_sources.push(format!(
|
2022-08-02 13:24:34 -07:00
|
|
|
r#""{}":{}"#,
|
2021-10-28 20:55:02 -07:00
|
|
|
&krate.name(cx.tcx()),
|
2022-08-02 13:24:34 -07:00
|
|
|
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("\\\"", "\\\\\"")
|
2021-03-31 11:13:51 -04:00
|
|
|
));
|
|
|
|
all_sources.sort();
|
2022-08-02 13:24:34 -07:00
|
|
|
let mut v = String::from("var sourcesIndex = JSON.parse('{\\\n");
|
|
|
|
v.push_str(&all_sources.join(",\\\n"));
|
|
|
|
v.push_str("\\\n}');\ncreateSourceSidebar();\n");
|
|
|
|
Ok(v.into_bytes())
|
2021-03-31 11:13:51 -04:00
|
|
|
};
|
|
|
|
write_crate("source-files.js", &make_sources)?;
|
2021-02-13 23:17:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Update the search index and crate list.
|
|
|
|
let dst = cx.dst.join(&format!("search-index{}.js", cx.shared.resource_suffix));
|
2021-10-28 20:55:02 -07:00
|
|
|
let (mut all_indexes, mut krates) =
|
2021-12-15 14:39:23 +11:00
|
|
|
try_err!(collect_json(&dst, krate.name(cx.tcx()).as_str()), &dst);
|
2021-02-13 23:17:38 -08:00
|
|
|
all_indexes.push(search_index);
|
2021-10-28 20:55:02 -07:00
|
|
|
krates.push(krate.name(cx.tcx()).to_string());
|
2021-02-13 23:17:38 -08:00
|
|
|
krates.sort();
|
|
|
|
|
|
|
|
// Sort the indexes by crate so the file will be generated identically even
|
|
|
|
// with rustdoc running in parallel.
|
|
|
|
all_indexes.sort();
|
2021-03-31 11:13:51 -04:00
|
|
|
write_crate("search-index.js", &|| {
|
2021-02-13 23:17:38 -08:00
|
|
|
let mut v = String::from("var searchIndex = JSON.parse('{\\\n");
|
|
|
|
v.push_str(&all_indexes.join(",\\\n"));
|
2022-05-15 21:09:55 -07:00
|
|
|
v.push_str(
|
|
|
|
r#"\
|
|
|
|
}');
|
|
|
|
if (typeof window !== 'undefined' && window.initSearch) {window.initSearch(searchIndex)};
|
|
|
|
if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
|
|
|
|
"#,
|
|
|
|
);
|
2021-03-31 11:13:51 -04:00
|
|
|
Ok(v.into_bytes())
|
|
|
|
})?;
|
2021-02-13 23:17:38 -08:00
|
|
|
|
2021-03-31 11:13:51 -04:00
|
|
|
write_crate("crates.js", &|| {
|
|
|
|
let krates = krates.iter().map(|k| format!("\"{}\"", k)).join(",");
|
|
|
|
Ok(format!("window.ALL_CRATES = [{}];", krates).into_bytes())
|
|
|
|
})?;
|
2021-02-13 23:17:38 -08:00
|
|
|
|
|
|
|
if options.enable_index_page {
|
|
|
|
if let Some(index_page) = options.index_page.clone() {
|
|
|
|
let mut md_opts = options.clone();
|
|
|
|
md_opts.output = cx.dst.clone();
|
|
|
|
md_opts.external_html = (*cx.shared).layout.external_html.clone();
|
|
|
|
|
2021-04-22 19:38:20 -04:00
|
|
|
crate::markdown::render(&index_page, md_opts, cx.shared.edition())
|
2021-02-13 23:17:38 -08:00
|
|
|
.map_err(|e| Error::new(e, &index_page))?;
|
|
|
|
} else {
|
2022-05-26 20:18:00 +02:00
|
|
|
let shared = Rc::clone(&cx.shared);
|
2021-02-13 23:17:38 -08:00
|
|
|
let dst = cx.dst.join("index.html");
|
|
|
|
let page = layout::Page {
|
|
|
|
title: "Index of crates",
|
|
|
|
css_class: "mod",
|
|
|
|
root_path: "./",
|
2022-05-26 20:18:00 +02:00
|
|
|
static_root_path: shared.static_root_path.as_deref(),
|
2021-02-13 23:17:38 -08:00
|
|
|
description: "List of crates",
|
|
|
|
keywords: BASIC_KEYWORDS,
|
2022-05-26 20:18:00 +02:00
|
|
|
resource_suffix: &shared.resource_suffix,
|
2021-02-13 23:17:38 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
let content = format!(
|
|
|
|
"<h1 class=\"fqn\">\
|
|
|
|
<span class=\"in-band\">List of all crates</span>\
|
|
|
|
</h1><ul class=\"crate mod\">{}</ul>",
|
|
|
|
krates
|
|
|
|
.iter()
|
|
|
|
.map(|s| {
|
|
|
|
format!(
|
|
|
|
"<li><a class=\"crate mod\" href=\"{}index.html\">{}</a></li>",
|
|
|
|
ensure_trailing_slash(s),
|
|
|
|
s
|
|
|
|
)
|
|
|
|
})
|
|
|
|
.collect::<String>()
|
|
|
|
);
|
2022-05-26 20:18:00 +02:00
|
|
|
let v = layout::render(&shared.layout, &page, "", content, &shared.style_files);
|
|
|
|
shared.fs.write(dst, v)?;
|
2021-02-13 23:17:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the list of all implementors for traits
|
|
|
|
let dst = cx.dst.join("implementors");
|
2021-08-22 03:36:37 +00:00
|
|
|
let cache = cx.cache();
|
|
|
|
for (&did, imps) in &cache.implementors {
|
2021-02-13 23:17:38 -08:00
|
|
|
// Private modules can leak through to this phase of rustdoc, which
|
|
|
|
// could contain implementations for otherwise private types. In some
|
|
|
|
// rare cases we could find an implementation for an item which wasn't
|
|
|
|
// indexed, so we just skip this step in that case.
|
|
|
|
//
|
|
|
|
// FIXME: this is a vague explanation for why this can't be a `get`, in
|
|
|
|
// theory it should be...
|
2022-05-05 17:20:14 -07:00
|
|
|
let (remote_path, remote_item_type) = match cache.exact_paths.get(&did) {
|
|
|
|
Some(p) => match cache.paths.get(&did).or_else(|| cache.external_paths.get(&did)) {
|
|
|
|
Some((_, t)) => (p, t),
|
|
|
|
None => continue,
|
|
|
|
},
|
2021-08-22 03:36:37 +00:00
|
|
|
None => match cache.external_paths.get(&did) {
|
2022-05-05 17:20:14 -07:00
|
|
|
Some((p, t)) => (p, t),
|
2021-02-13 23:17:38 -08:00
|
|
|
None => continue,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
struct Implementor {
|
|
|
|
text: String,
|
|
|
|
synthetic: bool,
|
|
|
|
types: Vec<String>,
|
|
|
|
}
|
|
|
|
|
2022-08-05 16:36:47 -07:00
|
|
|
impl Serialize for Implementor {
|
|
|
|
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
|
|
|
where
|
|
|
|
S: Serializer,
|
|
|
|
{
|
|
|
|
let mut seq = serializer.serialize_seq(None)?;
|
|
|
|
seq.serialize_element(&self.text)?;
|
|
|
|
if self.synthetic {
|
|
|
|
seq.serialize_element(&1)?;
|
|
|
|
seq.serialize_element(&self.types)?;
|
2022-08-04 12:13:16 -07:00
|
|
|
}
|
2022-08-05 16:36:47 -07:00
|
|
|
seq.end()
|
2022-08-04 12:13:16 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-02-13 23:17:38 -08:00
|
|
|
let implementors = imps
|
|
|
|
.iter()
|
|
|
|
.filter_map(|imp| {
|
|
|
|
// If the trait and implementation are in the same crate, then
|
|
|
|
// there's no need to emit information about it (there's inlining
|
|
|
|
// going on). If they're in different crates then the crate defining
|
|
|
|
// the trait will be interested in our implementation.
|
|
|
|
//
|
|
|
|
// If the implementation is from another crate then that crate
|
|
|
|
// should add it.
|
2022-04-16 14:28:09 +02:00
|
|
|
if imp.impl_item.item_id.krate() == did.krate || !imp.impl_item.item_id.is_local() {
|
2021-02-13 23:17:38 -08:00
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(Implementor {
|
2021-03-17 11:41:01 -07:00
|
|
|
text: imp.inner_impl().print(false, cx).to_string(),
|
2021-11-07 18:26:37 -08:00
|
|
|
synthetic: imp.inner_impl().kind.is_auto(),
|
2021-08-22 03:36:37 +00:00
|
|
|
types: collect_paths_for_type(imp.inner_impl().for_.clone(), cache),
|
2021-02-13 23:17:38 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
// Only create a js file if we have impls to add to it. If the trait is
|
|
|
|
// documented locally though we always create the file to avoid dead
|
|
|
|
// links.
|
2021-08-22 03:36:37 +00:00
|
|
|
if implementors.is_empty() && !cache.paths.contains_key(&did) {
|
2021-02-13 23:17:38 -08:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
let implementors = format!(
|
2022-08-05 16:36:47 -07:00
|
|
|
r#""{}":{}"#,
|
2021-10-28 20:55:02 -07:00
|
|
|
krate.name(cx.tcx()),
|
2022-08-05 16:36:47 -07:00
|
|
|
serde_json::to_string(&implementors).expect("failed serde conversion"),
|
2021-02-13 23:17:38 -08:00
|
|
|
);
|
|
|
|
|
|
|
|
let mut mydst = dst.clone();
|
|
|
|
for part in &remote_path[..remote_path.len() - 1] {
|
2021-12-15 06:18:18 +11:00
|
|
|
mydst.push(part.to_string());
|
2021-02-13 23:17:38 -08:00
|
|
|
}
|
|
|
|
cx.shared.ensure_dir(&mydst)?;
|
|
|
|
mydst.push(&format!("{}.{}.js", remote_item_type, remote_path[remote_path.len() - 1]));
|
|
|
|
|
|
|
|
let (mut all_implementors, _) =
|
2022-08-04 12:13:16 -07:00
|
|
|
try_err!(collect(&mydst, krate.name(cx.tcx()).as_str()), &mydst);
|
2021-02-13 23:17:38 -08:00
|
|
|
all_implementors.push(implementors);
|
|
|
|
// Sort the implementors by crate so the file will be generated
|
|
|
|
// identically even with rustdoc running in parallel.
|
|
|
|
all_implementors.sort();
|
|
|
|
|
2022-08-04 12:13:16 -07:00
|
|
|
let mut v = String::from("(function() {var implementors = {\n");
|
|
|
|
v.push_str(&all_implementors.join(",\n"));
|
|
|
|
v.push_str("\n};");
|
2021-02-13 23:17:38 -08:00
|
|
|
v.push_str(
|
|
|
|
"if (window.register_implementors) {\
|
|
|
|
window.register_implementors(implementors);\
|
|
|
|
} else {\
|
|
|
|
window.pending_implementors = implementors;\
|
|
|
|
}",
|
|
|
|
);
|
|
|
|
v.push_str("})()");
|
2021-08-22 02:25:25 +00:00
|
|
|
cx.shared.fs.write(mydst, v)?;
|
2021-02-13 23:17:38 -08:00
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|