]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/render/write_shared.rs
6fb41ff327916a104e3e2afa6192cc227ec40366
[rust.git] / src / librustdoc / html / render / write_shared.rs
1 use std::ffi::OsStr;
2 use std::fmt::Write;
3 use std::fs::{self, File};
4 use std::io::prelude::*;
5 use std::io::{self, BufReader};
6 use std::path::{Component, Path, PathBuf};
7 use std::rc::Rc;
8 use std::sync::LazyLock as Lazy;
9
10 use itertools::Itertools;
11 use rustc_data_structures::flock;
12 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
13 use serde::Serialize;
14
15 use super::{collect_paths_for_type, ensure_trailing_slash, Context, BASIC_KEYWORDS};
16 use crate::clean::Crate;
17 use crate::config::{EmitType, RenderOptions};
18 use crate::docfs::PathError;
19 use crate::error::Error;
20 use crate::html::{layout, static_files};
21 use crate::{try_err, try_none};
22
23 static FILES_UNVERSIONED: Lazy<FxHashMap<&str, &[u8]>> = Lazy::new(|| {
24     map! {
25         "FiraSans-Regular.woff2" => static_files::fira_sans::REGULAR,
26         "FiraSans-Medium.woff2" => static_files::fira_sans::MEDIUM,
27         "FiraSans-LICENSE.txt" => static_files::fira_sans::LICENSE,
28         "SourceSerif4-Regular.ttf.woff2" => static_files::source_serif_4::REGULAR,
29         "SourceSerif4-Bold.ttf.woff2" => static_files::source_serif_4::BOLD,
30         "SourceSerif4-It.ttf.woff2" => static_files::source_serif_4::ITALIC,
31         "SourceSerif4-LICENSE.md" => static_files::source_serif_4::LICENSE,
32         "SourceCodePro-Regular.ttf.woff2" => static_files::source_code_pro::REGULAR,
33         "SourceCodePro-Semibold.ttf.woff2" => static_files::source_code_pro::SEMIBOLD,
34         "SourceCodePro-It.ttf.woff2" => static_files::source_code_pro::ITALIC,
35         "SourceCodePro-LICENSE.txt" => static_files::source_code_pro::LICENSE,
36         "NanumBarunGothic.ttf.woff2" => static_files::nanum_barun_gothic::REGULAR,
37         "NanumBarunGothic-LICENSE.txt" => static_files::nanum_barun_gothic::LICENSE,
38         "LICENSE-MIT.txt" => static_files::LICENSE_MIT,
39         "LICENSE-APACHE.txt" => static_files::LICENSE_APACHE,
40         "COPYRIGHT.txt" => static_files::COPYRIGHT,
41     }
42 });
43
44 enum SharedResource<'a> {
45     /// This file will never change, no matter what toolchain is used to build it.
46     ///
47     /// It does not have a resource suffix.
48     Unversioned { name: &'static str },
49     /// This file may change depending on the toolchain.
50     ///
51     /// It has a resource suffix.
52     ToolchainSpecific { basename: &'static str },
53     /// This file may change for any crate within a build, or based on the CLI arguments.
54     ///
55     /// This differs from normal invocation-specific files because it has a resource suffix.
56     InvocationSpecific { basename: &'a str },
57 }
58
59 impl SharedResource<'_> {
60     fn extension(&self) -> Option<&OsStr> {
61         use SharedResource::*;
62         match self {
63             Unversioned { name }
64             | ToolchainSpecific { basename: name }
65             | InvocationSpecific { basename: name } => Path::new(name).extension(),
66         }
67     }
68
69     fn path(&self, cx: &Context<'_>) -> PathBuf {
70         match self {
71             SharedResource::Unversioned { name } => cx.dst.join(name),
72             SharedResource::ToolchainSpecific { basename } => cx.suffix_path(basename),
73             SharedResource::InvocationSpecific { basename } => cx.suffix_path(basename),
74         }
75     }
76
77     fn should_emit(&self, emit: &[EmitType]) -> bool {
78         if emit.is_empty() {
79             return true;
80         }
81         let kind = match self {
82             SharedResource::Unversioned { .. } => EmitType::Unversioned,
83             SharedResource::ToolchainSpecific { .. } => EmitType::Toolchain,
84             SharedResource::InvocationSpecific { .. } => EmitType::InvocationSpecific,
85         };
86         emit.contains(&kind)
87     }
88 }
89
90 impl Context<'_> {
91     fn suffix_path(&self, filename: &str) -> PathBuf {
92         // We use splitn vs Path::extension here because we might get a filename
93         // like `style.min.css` and we want to process that into
94         // `style-suffix.min.css`.  Path::extension would just return `css`
95         // which would result in `style.min-suffix.css` which isn't what we
96         // want.
97         let (base, ext) = filename.split_once('.').unwrap();
98         let filename = format!("{}{}.{}", base, self.shared.resource_suffix, ext);
99         self.dst.join(&filename)
100     }
101
102     fn write_shared(
103         &self,
104         resource: SharedResource<'_>,
105         contents: impl 'static + Send + AsRef<[u8]>,
106         emit: &[EmitType],
107     ) -> Result<(), Error> {
108         if resource.should_emit(emit) {
109             self.shared.fs.write(resource.path(self), contents)
110         } else {
111             Ok(())
112         }
113     }
114
115     fn write_minify(
116         &self,
117         resource: SharedResource<'_>,
118         contents: impl 'static + Send + AsRef<str> + AsRef<[u8]>,
119         minify: bool,
120         emit: &[EmitType],
121     ) -> Result<(), Error> {
122         if minify {
123             let contents = contents.as_ref();
124             let contents = if resource.extension() == Some(OsStr::new("css")) {
125                 minifier::css::minify(contents)
126                     .map_err(|e| {
127                         Error::new(format!("failed to minify CSS file: {}", e), resource.path(self))
128                     })?
129                     .to_string()
130             } else {
131                 minifier::js::minify(contents).to_string()
132             };
133             self.write_shared(resource, contents, emit)
134         } else {
135             self.write_shared(resource, contents, emit)
136         }
137     }
138 }
139
140 pub(super) fn write_shared(
141     cx: &mut Context<'_>,
142     krate: &Crate,
143     search_index: String,
144     options: &RenderOptions,
145 ) -> Result<(), Error> {
146     // Write out the shared files. Note that these are shared among all rustdoc
147     // docs placed in the output directory, so this needs to be a synchronized
148     // operation with respect to all other rustdocs running around.
149     let lock_file = cx.dst.join(".lock");
150     let _lock = try_err!(flock::Lock::new(&lock_file, true, true, true), &lock_file);
151
152     // Minified resources are usually toolchain resources. If they're not, they should use `cx.write_minify` directly.
153     fn write_minify(
154         basename: &'static str,
155         contents: impl 'static + Send + AsRef<str> + AsRef<[u8]>,
156         cx: &Context<'_>,
157         options: &RenderOptions,
158     ) -> Result<(), Error> {
159         cx.write_minify(
160             SharedResource::ToolchainSpecific { basename },
161             contents,
162             options.enable_minification,
163             &options.emit,
164         )
165     }
166
167     // Toolchain resources should never be dynamic.
168     let write_toolchain = |p: &'static _, c: &'static _| {
169         cx.write_shared(SharedResource::ToolchainSpecific { basename: p }, c, &options.emit)
170     };
171
172     // Crate resources should always be dynamic.
173     let write_crate = |p: &_, make_content: &dyn Fn() -> Result<Vec<u8>, Error>| {
174         let content = make_content()?;
175         cx.write_shared(SharedResource::InvocationSpecific { basename: p }, content, &options.emit)
176     };
177
178     // Given "foo.svg", return e.g. "url(\"foo1.58.0.svg\")"
179     fn ver_url(cx: &Context<'_>, basename: &'static str) -> String {
180         format!(
181             "url(\"{}\")",
182             SharedResource::ToolchainSpecific { basename }
183                 .path(cx)
184                 .file_name()
185                 .unwrap()
186                 .to_str()
187                 .unwrap()
188         )
189     }
190
191     // We use the AUTOREPLACE mechanism to inject into our static JS and CSS certain
192     // values that are only known at doc build time. Since this mechanism is somewhat
193     // surprising when reading the code, please limit it to rustdoc.css.
194     write_minify(
195         "rustdoc.css",
196         static_files::RUSTDOC_CSS
197             .replace(
198                 "/* AUTOREPLACE: */url(\"toggle-minus.svg\")",
199                 &ver_url(cx, "toggle-minus.svg"),
200             )
201             .replace("/* AUTOREPLACE: */url(\"toggle-plus.svg\")", &ver_url(cx, "toggle-plus.svg"))
202             .replace("/* AUTOREPLACE: */url(\"down-arrow.svg\")", &ver_url(cx, "down-arrow.svg")),
203         cx,
204         options,
205     )?;
206
207     // Add all the static files. These may already exist, but we just
208     // overwrite them anyway to make sure that they're fresh and up-to-date.
209     write_minify("settings.css", static_files::SETTINGS_CSS, cx, options)?;
210     write_minify("noscript.css", static_files::NOSCRIPT_CSS, cx, options)?;
211
212     // To avoid "light.css" to be overwritten, we'll first run over the received themes and only
213     // then we'll run over the "official" styles.
214     let mut themes: FxHashSet<String> = FxHashSet::default();
215
216     for entry in &cx.shared.style_files {
217         let theme = entry.basename()?;
218         let extension =
219             try_none!(try_none!(entry.path.extension(), &entry.path).to_str(), &entry.path);
220
221         // Handle the official themes
222         match theme.as_str() {
223             "light" => write_minify("light.css", static_files::themes::LIGHT, cx, options)?,
224             "dark" => write_minify("dark.css", static_files::themes::DARK, cx, options)?,
225             "ayu" => write_minify("ayu.css", static_files::themes::AYU, cx, options)?,
226             _ => {
227                 // Handle added third-party themes
228                 let filename = format!("{}.{}", theme, extension);
229                 write_crate(&filename, &|| Ok(try_err!(fs::read(&entry.path), &entry.path)))?;
230             }
231         };
232
233         themes.insert(theme.to_owned());
234     }
235
236     if (*cx.shared).layout.logo.is_empty() {
237         write_toolchain("rust-logo.svg", static_files::RUST_LOGO_SVG)?;
238     }
239     if (*cx.shared).layout.favicon.is_empty() {
240         write_toolchain("favicon.svg", static_files::RUST_FAVICON_SVG)?;
241         write_toolchain("favicon-16x16.png", static_files::RUST_FAVICON_PNG_16)?;
242         write_toolchain("favicon-32x32.png", static_files::RUST_FAVICON_PNG_32)?;
243     }
244     write_toolchain("wheel.svg", static_files::WHEEL_SVG)?;
245     write_toolchain("clipboard.svg", static_files::CLIPBOARD_SVG)?;
246     write_toolchain("down-arrow.svg", static_files::DOWN_ARROW_SVG)?;
247     write_toolchain("toggle-minus.svg", static_files::TOGGLE_MINUS_PNG)?;
248     write_toolchain("toggle-plus.svg", static_files::TOGGLE_PLUS_PNG)?;
249
250     let mut themes: Vec<&String> = themes.iter().collect();
251     themes.sort();
252
253     write_minify("main.js", static_files::MAIN_JS, cx, options)?;
254     write_minify("search.js", static_files::SEARCH_JS, cx, options)?;
255     write_minify("settings.js", static_files::SETTINGS_JS, cx, options)?;
256
257     if cx.include_sources {
258         write_minify("source-script.js", static_files::sidebar::SOURCE_SCRIPT, cx, options)?;
259     }
260
261     write_minify("storage.js", static_files::STORAGE_JS, cx, options)?;
262
263     if cx.shared.layout.scrape_examples_extension {
264         cx.write_minify(
265             SharedResource::InvocationSpecific { basename: "scrape-examples.js" },
266             static_files::SCRAPE_EXAMPLES_JS,
267             options.enable_minification,
268             &options.emit,
269         )?;
270     }
271
272     if let Some(ref css) = cx.shared.layout.css_file_extension {
273         let buffer = try_err!(fs::read_to_string(css), css);
274         // This varies based on the invocation, so it can't go through the write_minify wrapper.
275         cx.write_minify(
276             SharedResource::InvocationSpecific { basename: "theme.css" },
277             buffer,
278             options.enable_minification,
279             &options.emit,
280         )?;
281     }
282     write_minify("normalize.css", static_files::NORMALIZE_CSS, cx, options)?;
283     for (name, contents) in &*FILES_UNVERSIONED {
284         cx.write_shared(SharedResource::Unversioned { name }, contents, &options.emit)?;
285     }
286
287     fn collect(path: &Path, krate: &str, key: &str) -> io::Result<(Vec<String>, Vec<String>)> {
288         let mut ret = Vec::new();
289         let mut krates = Vec::new();
290
291         if path.exists() {
292             let prefix = format!(r#"{}["{}"]"#, key, krate);
293             for line in BufReader::new(File::open(path)?).lines() {
294                 let line = line?;
295                 if !line.starts_with(key) {
296                     continue;
297                 }
298                 if line.starts_with(&prefix) {
299                     continue;
300                 }
301                 ret.push(line.to_string());
302                 krates.push(
303                     line[key.len() + 2..]
304                         .split('"')
305                         .next()
306                         .map(|s| s.to_owned())
307                         .unwrap_or_else(String::new),
308                 );
309             }
310         }
311         Ok((ret, krates))
312     }
313
314     fn collect_json(path: &Path, krate: &str) -> io::Result<(Vec<String>, Vec<String>)> {
315         let mut ret = Vec::new();
316         let mut krates = Vec::new();
317
318         if path.exists() {
319             let prefix = format!("\"{}\"", krate);
320             for line in BufReader::new(File::open(path)?).lines() {
321                 let line = line?;
322                 if !line.starts_with('"') {
323                     continue;
324                 }
325                 if line.starts_with(&prefix) {
326                     continue;
327                 }
328                 if line.ends_with(",\\") {
329                     ret.push(line[..line.len() - 2].to_string());
330                 } else {
331                     // Ends with "\\" (it's the case for the last added crate line)
332                     ret.push(line[..line.len() - 1].to_string());
333                 }
334                 krates.push(
335                     line.split('"')
336                         .find(|s| !s.is_empty())
337                         .map(|s| s.to_owned())
338                         .unwrap_or_else(String::new),
339                 );
340             }
341         }
342         Ok((ret, krates))
343     }
344
345     use std::ffi::OsString;
346
347     #[derive(Debug)]
348     struct Hierarchy {
349         elem: OsString,
350         children: FxHashMap<OsString, Hierarchy>,
351         elems: FxHashSet<OsString>,
352     }
353
354     impl Hierarchy {
355         fn new(elem: OsString) -> Hierarchy {
356             Hierarchy { elem, children: FxHashMap::default(), elems: FxHashSet::default() }
357         }
358
359         fn to_json_string(&self) -> String {
360             let mut subs: Vec<&Hierarchy> = self.children.values().collect();
361             subs.sort_unstable_by(|a, b| a.elem.cmp(&b.elem));
362             let mut files = self
363                 .elems
364                 .iter()
365                 .map(|s| format!("\"{}\"", s.to_str().expect("invalid osstring conversion")))
366                 .collect::<Vec<_>>();
367             files.sort_unstable();
368             let subs = subs.iter().map(|s| s.to_json_string()).collect::<Vec<_>>().join(",");
369             let dirs = if subs.is_empty() && files.is_empty() {
370                 String::new()
371             } else {
372                 format!(",[{}]", subs)
373             };
374             let files = files.join(",");
375             let files = if files.is_empty() { String::new() } else { format!(",[{}]", files) };
376             format!(
377                 "[\"{name}\"{dirs}{files}]",
378                 name = self.elem.to_str().expect("invalid osstring conversion"),
379                 dirs = dirs,
380                 files = files
381             )
382         }
383     }
384
385     if cx.include_sources {
386         let mut hierarchy = Hierarchy::new(OsString::new());
387         for source in cx
388             .shared
389             .local_sources
390             .iter()
391             .filter_map(|p| p.0.strip_prefix(&cx.shared.src_root).ok())
392         {
393             let mut h = &mut hierarchy;
394             let mut elems = source
395                 .components()
396                 .filter_map(|s| match s {
397                     Component::Normal(s) => Some(s.to_owned()),
398                     _ => None,
399                 })
400                 .peekable();
401             loop {
402                 let cur_elem = elems.next().expect("empty file path");
403                 if elems.peek().is_none() {
404                     h.elems.insert(cur_elem);
405                     break;
406                 } else {
407                     let e = cur_elem.clone();
408                     h = h.children.entry(cur_elem.clone()).or_insert_with(|| Hierarchy::new(e));
409                 }
410             }
411         }
412
413         let dst = cx.dst.join(&format!("source-files{}.js", cx.shared.resource_suffix));
414         let make_sources = || {
415             let (mut all_sources, _krates) =
416                 try_err!(collect_json(&dst, krate.name(cx.tcx()).as_str()), &dst);
417             all_sources.push(format!(
418                 r#""{}":{}"#,
419                 &krate.name(cx.tcx()),
420                 hierarchy
421                     .to_json_string()
422                     // All these `replace` calls are because we have to go through JS string for JSON content.
423                     .replace('\\', r"\\")
424                     .replace('\'', r"\'")
425                     // We need to escape double quotes for the JSON.
426                     .replace("\\\"", "\\\\\"")
427             ));
428             all_sources.sort();
429             let mut v = String::from("var sourcesIndex = JSON.parse('{\\\n");
430             v.push_str(&all_sources.join(",\\\n"));
431             v.push_str("\\\n}');\ncreateSourceSidebar();\n");
432             Ok(v.into_bytes())
433         };
434         write_crate("source-files.js", &make_sources)?;
435     }
436
437     // Update the search index and crate list.
438     let dst = cx.dst.join(&format!("search-index{}.js", cx.shared.resource_suffix));
439     let (mut all_indexes, mut krates) =
440         try_err!(collect_json(&dst, krate.name(cx.tcx()).as_str()), &dst);
441     all_indexes.push(search_index);
442     krates.push(krate.name(cx.tcx()).to_string());
443     krates.sort();
444
445     // Sort the indexes by crate so the file will be generated identically even
446     // with rustdoc running in parallel.
447     all_indexes.sort();
448     write_crate("search-index.js", &|| {
449         let mut v = String::from("var searchIndex = JSON.parse('{\\\n");
450         v.push_str(&all_indexes.join(",\\\n"));
451         v.push_str(
452             r#"\
453 }');
454 if (typeof window !== 'undefined' && window.initSearch) {window.initSearch(searchIndex)};
455 if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
456 "#,
457         );
458         Ok(v.into_bytes())
459     })?;
460
461     write_crate("crates.js", &|| {
462         let krates = krates.iter().map(|k| format!("\"{}\"", k)).join(",");
463         Ok(format!("window.ALL_CRATES = [{}];", krates).into_bytes())
464     })?;
465
466     if options.enable_index_page {
467         if let Some(index_page) = options.index_page.clone() {
468             let mut md_opts = options.clone();
469             md_opts.output = cx.dst.clone();
470             md_opts.external_html = (*cx.shared).layout.external_html.clone();
471
472             crate::markdown::render(&index_page, md_opts, cx.shared.edition())
473                 .map_err(|e| Error::new(e, &index_page))?;
474         } else {
475             let shared = Rc::clone(&cx.shared);
476             let dst = cx.dst.join("index.html");
477             let page = layout::Page {
478                 title: "Index of crates",
479                 css_class: "mod",
480                 root_path: "./",
481                 static_root_path: shared.static_root_path.as_deref(),
482                 description: "List of crates",
483                 keywords: BASIC_KEYWORDS,
484                 resource_suffix: &shared.resource_suffix,
485             };
486
487             let content = format!(
488                 "<h1 class=\"fqn\">\
489                      <span class=\"in-band\">List of all crates</span>\
490                 </h1><ul class=\"crate mod\">{}</ul>",
491                 krates
492                     .iter()
493                     .map(|s| {
494                         format!(
495                             "<li><a class=\"crate mod\" href=\"{}index.html\">{}</a></li>",
496                             ensure_trailing_slash(s),
497                             s
498                         )
499                     })
500                     .collect::<String>()
501             );
502             let v = layout::render(&shared.layout, &page, "", content, &shared.style_files);
503             shared.fs.write(dst, v)?;
504         }
505     }
506
507     // Update the list of all implementors for traits
508     let dst = cx.dst.join("implementors");
509     let cache = cx.cache();
510     for (&did, imps) in &cache.implementors {
511         // Private modules can leak through to this phase of rustdoc, which
512         // could contain implementations for otherwise private types. In some
513         // rare cases we could find an implementation for an item which wasn't
514         // indexed, so we just skip this step in that case.
515         //
516         // FIXME: this is a vague explanation for why this can't be a `get`, in
517         //        theory it should be...
518         let (remote_path, remote_item_type) = match cache.exact_paths.get(&did) {
519             Some(p) => match cache.paths.get(&did).or_else(|| cache.external_paths.get(&did)) {
520                 Some((_, t)) => (p, t),
521                 None => continue,
522             },
523             None => match cache.external_paths.get(&did) {
524                 Some((p, t)) => (p, t),
525                 None => continue,
526             },
527         };
528
529         #[derive(Serialize)]
530         struct Implementor {
531             text: String,
532             synthetic: bool,
533             types: Vec<String>,
534         }
535
536         let implementors = imps
537             .iter()
538             .filter_map(|imp| {
539                 // If the trait and implementation are in the same crate, then
540                 // there's no need to emit information about it (there's inlining
541                 // going on). If they're in different crates then the crate defining
542                 // the trait will be interested in our implementation.
543                 //
544                 // If the implementation is from another crate then that crate
545                 // should add it.
546                 if imp.impl_item.item_id.krate() == did.krate || !imp.impl_item.item_id.is_local() {
547                     None
548                 } else {
549                     Some(Implementor {
550                         text: imp.inner_impl().print(false, cx).to_string(),
551                         synthetic: imp.inner_impl().kind.is_auto(),
552                         types: collect_paths_for_type(imp.inner_impl().for_.clone(), cache),
553                     })
554                 }
555             })
556             .collect::<Vec<_>>();
557
558         // Only create a js file if we have impls to add to it. If the trait is
559         // documented locally though we always create the file to avoid dead
560         // links.
561         if implementors.is_empty() && !cache.paths.contains_key(&did) {
562             continue;
563         }
564
565         let implementors = format!(
566             r#"implementors["{}"] = {};"#,
567             krate.name(cx.tcx()),
568             serde_json::to_string(&implementors).unwrap()
569         );
570
571         let mut mydst = dst.clone();
572         for part in &remote_path[..remote_path.len() - 1] {
573             mydst.push(part.to_string());
574         }
575         cx.shared.ensure_dir(&mydst)?;
576         mydst.push(&format!("{}.{}.js", remote_item_type, remote_path[remote_path.len() - 1]));
577
578         let (mut all_implementors, _) =
579             try_err!(collect(&mydst, krate.name(cx.tcx()).as_str(), "implementors"), &mydst);
580         all_implementors.push(implementors);
581         // Sort the implementors by crate so the file will be generated
582         // identically even with rustdoc running in parallel.
583         all_implementors.sort();
584
585         let mut v = String::from("(function() {var implementors = {};\n");
586         for implementor in &all_implementors {
587             writeln!(v, "{}", *implementor).unwrap();
588         }
589         v.push_str(
590             "if (window.register_implementors) {\
591                  window.register_implementors(implementors);\
592              } else {\
593                  window.pending_implementors = implementors;\
594              }",
595         );
596         v.push_str("})()");
597         cx.shared.fs.write(mydst, v)?;
598     }
599     Ok(())
600 }