]> git.lizzy.rs Git - rust.git/blob - src/librustdoc/html/render/write_shared.rs
Fix link generation in the sidebar for impls
[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 =
370                 if subs.is_empty() { String::new() } else { format!(",\"dirs\":[{}]", subs) };
371             let files = files.join(",");
372             let files =
373                 if files.is_empty() { String::new() } else { format!(",\"files\":[{}]", files) };
374             format!(
375                 "{{\"name\":\"{name}\"{dirs}{files}}}",
376                 name = self.elem.to_str().expect("invalid osstring conversion"),
377                 dirs = dirs,
378                 files = files
379             )
380         }
381     }
382
383     if cx.include_sources {
384         let mut hierarchy = Hierarchy::new(OsString::new());
385         for source in cx
386             .shared
387             .local_sources
388             .iter()
389             .filter_map(|p| p.0.strip_prefix(&cx.shared.src_root).ok())
390         {
391             let mut h = &mut hierarchy;
392             let mut elems = source
393                 .components()
394                 .filter_map(|s| match s {
395                     Component::Normal(s) => Some(s.to_owned()),
396                     _ => None,
397                 })
398                 .peekable();
399             loop {
400                 let cur_elem = elems.next().expect("empty file path");
401                 if elems.peek().is_none() {
402                     h.elems.insert(cur_elem);
403                     break;
404                 } else {
405                     let e = cur_elem.clone();
406                     h = h.children.entry(cur_elem.clone()).or_insert_with(|| Hierarchy::new(e));
407                 }
408             }
409         }
410
411         let dst = cx.dst.join(&format!("source-files{}.js", cx.shared.resource_suffix));
412         let make_sources = || {
413             let (mut all_sources, _krates) =
414                 try_err!(collect(&dst, krate.name(cx.tcx()).as_str(), "sourcesIndex"), &dst);
415             all_sources.push(format!(
416                 "sourcesIndex[\"{}\"] = {};",
417                 &krate.name(cx.tcx()),
418                 hierarchy.to_json_string()
419             ));
420             all_sources.sort();
421             Ok(format!(
422                 "var sourcesIndex = {{}};\n{}\ncreateSourceSidebar();\n",
423                 all_sources.join("\n")
424             )
425             .into_bytes())
426         };
427         write_crate("source-files.js", &make_sources)?;
428     }
429
430     // Update the search index and crate list.
431     let dst = cx.dst.join(&format!("search-index{}.js", cx.shared.resource_suffix));
432     let (mut all_indexes, mut krates) =
433         try_err!(collect_json(&dst, krate.name(cx.tcx()).as_str()), &dst);
434     all_indexes.push(search_index);
435     krates.push(krate.name(cx.tcx()).to_string());
436     krates.sort();
437
438     // Sort the indexes by crate so the file will be generated identically even
439     // with rustdoc running in parallel.
440     all_indexes.sort();
441     write_crate("search-index.js", &|| {
442         let mut v = String::from("var searchIndex = JSON.parse('{\\\n");
443         v.push_str(&all_indexes.join(",\\\n"));
444         v.push_str(
445             r#"\
446 }');
447 if (typeof window !== 'undefined' && window.initSearch) {window.initSearch(searchIndex)};
448 if (typeof exports !== 'undefined') {exports.searchIndex = searchIndex};
449 "#,
450         );
451         Ok(v.into_bytes())
452     })?;
453
454     write_crate("crates.js", &|| {
455         let krates = krates.iter().map(|k| format!("\"{}\"", k)).join(",");
456         Ok(format!("window.ALL_CRATES = [{}];", krates).into_bytes())
457     })?;
458
459     if options.enable_index_page {
460         if let Some(index_page) = options.index_page.clone() {
461             let mut md_opts = options.clone();
462             md_opts.output = cx.dst.clone();
463             md_opts.external_html = (*cx.shared).layout.external_html.clone();
464
465             crate::markdown::render(&index_page, md_opts, cx.shared.edition())
466                 .map_err(|e| Error::new(e, &index_page))?;
467         } else {
468             let shared = Rc::clone(&cx.shared);
469             let dst = cx.dst.join("index.html");
470             let page = layout::Page {
471                 title: "Index of crates",
472                 css_class: "mod",
473                 root_path: "./",
474                 static_root_path: shared.static_root_path.as_deref(),
475                 description: "List of crates",
476                 keywords: BASIC_KEYWORDS,
477                 resource_suffix: &shared.resource_suffix,
478             };
479
480             let content = format!(
481                 "<h1 class=\"fqn\">\
482                      <span class=\"in-band\">List of all crates</span>\
483                 </h1><ul class=\"crate mod\">{}</ul>",
484                 krates
485                     .iter()
486                     .map(|s| {
487                         format!(
488                             "<li><a class=\"crate mod\" href=\"{}index.html\">{}</a></li>",
489                             ensure_trailing_slash(s),
490                             s
491                         )
492                     })
493                     .collect::<String>()
494             );
495             let v = layout::render(&shared.layout, &page, "", content, &shared.style_files);
496             shared.fs.write(dst, v)?;
497         }
498     }
499
500     // Update the list of all implementors for traits
501     let dst = cx.dst.join("implementors");
502     let cache = cx.cache();
503     for (&did, imps) in &cache.implementors {
504         // Private modules can leak through to this phase of rustdoc, which
505         // could contain implementations for otherwise private types. In some
506         // rare cases we could find an implementation for an item which wasn't
507         // indexed, so we just skip this step in that case.
508         //
509         // FIXME: this is a vague explanation for why this can't be a `get`, in
510         //        theory it should be...
511         let (remote_path, remote_item_type) = match cache.exact_paths.get(&did) {
512             Some(p) => match cache.paths.get(&did).or_else(|| cache.external_paths.get(&did)) {
513                 Some((_, t)) => (p, t),
514                 None => continue,
515             },
516             None => match cache.external_paths.get(&did) {
517                 Some((p, t)) => (p, t),
518                 None => continue,
519             },
520         };
521
522         #[derive(Serialize)]
523         struct Implementor {
524             text: String,
525             synthetic: bool,
526             types: Vec<String>,
527         }
528
529         let implementors = imps
530             .iter()
531             .filter_map(|imp| {
532                 // If the trait and implementation are in the same crate, then
533                 // there's no need to emit information about it (there's inlining
534                 // going on). If they're in different crates then the crate defining
535                 // the trait will be interested in our implementation.
536                 //
537                 // If the implementation is from another crate then that crate
538                 // should add it.
539                 if imp.impl_item.item_id.krate() == did.krate || !imp.impl_item.item_id.is_local() {
540                     None
541                 } else {
542                     Some(Implementor {
543                         text: imp.inner_impl().print(false, cx).to_string(),
544                         synthetic: imp.inner_impl().kind.is_auto(),
545                         types: collect_paths_for_type(imp.inner_impl().for_.clone(), cache),
546                     })
547                 }
548             })
549             .collect::<Vec<_>>();
550
551         // Only create a js file if we have impls to add to it. If the trait is
552         // documented locally though we always create the file to avoid dead
553         // links.
554         if implementors.is_empty() && !cache.paths.contains_key(&did) {
555             continue;
556         }
557
558         let implementors = format!(
559             r#"implementors["{}"] = {};"#,
560             krate.name(cx.tcx()),
561             serde_json::to_string(&implementors).unwrap()
562         );
563
564         let mut mydst = dst.clone();
565         for part in &remote_path[..remote_path.len() - 1] {
566             mydst.push(part.to_string());
567         }
568         cx.shared.ensure_dir(&mydst)?;
569         mydst.push(&format!("{}.{}.js", remote_item_type, remote_path[remote_path.len() - 1]));
570
571         let (mut all_implementors, _) =
572             try_err!(collect(&mydst, krate.name(cx.tcx()).as_str(), "implementors"), &mydst);
573         all_implementors.push(implementors);
574         // Sort the implementors by crate so the file will be generated
575         // identically even with rustdoc running in parallel.
576         all_implementors.sort();
577
578         let mut v = String::from("(function() {var implementors = {};\n");
579         for implementor in &all_implementors {
580             writeln!(v, "{}", *implementor).unwrap();
581         }
582         v.push_str(
583             "if (window.register_implementors) {\
584                  window.register_implementors(implementors);\
585              } else {\
586                  window.pending_implementors = implementors;\
587              }",
588         );
589         v.push_str("})()");
590         cx.shared.fs.write(mydst, v)?;
591     }
592     Ok(())
593 }