1 use std
::cell
::RefCell
;
2 use std
::collections
::BTreeMap
;
4 use std
::path
::PathBuf
;
6 use std
::sync
::mpsc
::channel
;
8 use rustc_hir
::def_id
::{LOCAL_CRATE}
;
9 use rustc_middle
::ty
::TyCtxt
;
10 use rustc_session
::Session
;
11 use rustc_span
::edition
::Edition
;
12 use rustc_span
::source_map
::FileName
;
13 use rustc_span
::symbol
::sym
;
15 use super::cache
::{build_index, ExternalLocation}
;
16 use super::print_item
::{full_path, item_path, print_item}
;
17 use super::write_shared
::write_shared
;
19 print_sidebar
, settings
, AllTypes
, NameDoc
, SharedContext
, StylePath
, BASIC_KEYWORDS
,
20 CURRENT_DEPTH
, INITIAL_IDS
,
23 use crate::clean
::{self, AttributesExt}
;
24 use crate::config
::RenderOptions
;
25 use crate::docfs
::{DocFS, PathError}
;
26 use crate::error
::Error
;
27 use crate::formats
::cache
::Cache
;
28 use crate::formats
::item_type
::ItemType
;
29 use crate::formats
::FormatRenderer
;
30 use crate::html
::escape
::Escape
;
31 use crate::html
::format
::Buffer
;
32 use crate::html
::markdown
::{self, plain_text_summary, ErrorCodes, IdMap}
;
33 use crate::html
::{layout, sources}
;
35 /// Major driving force in all rustdoc rendering. This contains information
36 /// about where in the tree-like hierarchy rendering is occurring and controls
37 /// how the current page is being rendered.
39 /// It is intended that this context is a lightweight object which can be fairly
40 /// easily cloned because it is cloned per work-job (about once per item in the
42 crate struct Context
<'tcx
> {
43 /// Current hierarchy of components leading down to what's currently being
45 pub(super) current
: Vec
<String
>,
46 /// The current destination folder of where HTML artifacts should be placed.
47 /// This changes as the context descends into the module hierarchy.
48 pub(super) dst
: PathBuf
,
49 /// A flag, which when `true`, will render pages which redirect to the
50 /// real location of an item. This is used to allow external links to
51 /// publicly reused items to redirect to the right location.
52 pub(super) render_redirect_pages
: bool
,
53 /// The map used to ensure all generated 'id=' attributes are unique.
54 pub(super) id_map
: RefCell
<IdMap
>,
55 /// Shared mutable state.
57 /// Issue for improving the situation: [#82381][]
59 /// [#82381]: https://github.com/rust-lang/rust/issues/82381
60 pub(super) shared
: Rc
<SharedContext
<'tcx
>>,
61 /// The [`Cache`] used during rendering.
63 /// Ideally the cache would be in [`SharedContext`], but it's mutated
64 /// between when the `SharedContext` is created and when `Context`
65 /// is created, so more refactoring would be needed.
67 /// It's immutable once in `Context`, so it's not as bad that it's not in
69 // FIXME: move `cache` to `SharedContext`
70 pub(super) cache
: Rc
<Cache
>,
73 // `Context` is cloned a lot, so we don't want the size to grow unexpectedly.
74 #[cfg(target_arch = "x86_64")]
75 rustc_data_structures
::static_assert_size
!(Context
<'_
>, 112);
77 impl<'tcx
> Context
<'tcx
> {
78 pub(super) fn path(&self, filename
: &str) -> PathBuf
{
79 // We use splitn vs Path::extension here because we might get a filename
80 // like `style.min.css` and we want to process that into
81 // `style-suffix.min.css`. Path::extension would just return `css`
82 // which would result in `style.min-suffix.css` which isn't what we
84 let (base
, ext
) = filename
.split_once('
.'
).unwrap();
85 let filename
= format
!("{}{}.{}", base
, self.shared
.resource_suffix
, ext
);
86 self.dst
.join(&filename
)
89 pub(super) fn tcx(&self) -> TyCtxt
<'tcx
> {
93 fn sess(&self) -> &'tcx Session
{
97 pub(super) fn derive_id(&self, id
: String
) -> String
{
98 let mut map
= self.id_map
.borrow_mut();
102 /// String representation of how to get back to the root path of the 'doc/'
103 /// folder in terms of a relative URL.
104 pub(super) fn root_path(&self) -> String
{
105 "../".repeat(self.current
.len())
108 fn render_item(&self, it
: &clean
::Item
, pushname
: bool
) -> String
{
109 // A little unfortunate that this is done like this, but it sure
110 // does make formatting *a lot* nicer.
111 CURRENT_DEPTH
.with(|slot
| {
112 slot
.set(self.current
.len());
115 let mut title
= if it
.is_primitive() || it
.is_keyword() {
116 // No need to include the namespace for primitive types and keywords
119 self.current
.join("::")
122 if !title
.is_empty() {
123 title
.push_str("::");
125 title
.push_str(&it
.name
.unwrap().as_str());
127 title
.push_str(" - Rust");
128 let tyname
= it
.type_();
129 let desc
= it
.doc_value().as_ref().map(|doc
| plain_text_summary(&doc
));
130 let desc
= if let Some(desc
) = desc
{
132 } else if it
.is_crate() {
133 format
!("API documentation for the Rust `{}` crate.", self.shared
.layout
.krate
)
136 "API documentation for the Rust `{}` {} in crate `{}`.",
137 it
.name
.as_ref().unwrap(),
139 self.shared
.layout
.krate
142 let keywords
= make_item_keywords(it
);
143 let page
= layout
::Page
{
144 css_class
: tyname
.as_str(),
145 root_path
: &self.root_path(),
146 static_root_path
: self.shared
.static_root_path
.as_deref(),
150 resource_suffix
: &self.shared
.resource_suffix
,
152 static_extra_scripts
: &[],
155 if !self.render_redirect_pages
{
159 |buf
: &mut _
| print_sidebar(self, it
, buf
),
160 |buf
: &mut _
| print_item(self, it
, buf
),
161 &self.shared
.style_files
,
164 if let Some(&(ref names
, ty
)) = self.cache
.paths
.get(&it
.def_id
) {
165 let mut path
= String
::new();
166 for name
in &names
[..names
.len() - 1] {
170 path
.push_str(&item_path(ty
, names
.last().unwrap()));
171 match self.shared
.redirections
{
172 Some(ref redirections
) => {
173 let mut current_path
= String
::new();
174 for name
in &self.current
{
175 current_path
.push_str(name
);
176 current_path
.push('
/'
);
178 current_path
.push_str(&item_path(ty
, names
.last().unwrap()));
179 redirections
.borrow_mut().insert(current_path
, path
);
181 None
=> return layout
::redirect(&format
!("{}{}", self.root_path(), path
)),
188 /// Construct a map of items shown in the sidebar to a plain-text summary of their docs.
189 fn build_sidebar_items(&self, m
: &clean
::Module
) -> BTreeMap
<String
, Vec
<NameDoc
>> {
190 // BTreeMap instead of HashMap to get a sorted output
191 let mut map
: BTreeMap
<_
, Vec
<_
>> = BTreeMap
::new();
192 for item
in &m
.items
{
193 if item
.is_stripped() {
197 let short
= item
.type_();
198 let myname
= match item
.name
{
200 Some(ref s
) => s
.to_string(),
202 let short
= short
.to_string();
203 map
.entry(short
).or_default().push((
205 Some(item
.doc_value().map_or_else(String
::new
, |s
| plain_text_summary(&s
))),
209 if self.shared
.sort_modules_alphabetically
{
210 for items
in map
.values_mut() {
217 /// Generates a url appropriate for an `href` attribute back to the source of
220 /// The url generated, when clicked, will redirect the browser back to the
221 /// original source code.
223 /// If `None` is returned, then a source link couldn't be generated. This
224 /// may happen, for example, with externally inlined items where the source
225 /// of their crate documentation isn't known.
226 pub(super) fn src_href(&self, item
: &clean
::Item
) -> Option
<String
> {
227 if item
.source
.is_dummy() {
230 let mut root
= self.root_path();
231 let mut path
= String
::new();
232 let cnum
= item
.source
.cnum(self.sess());
234 // We can safely ignore synthetic `SourceFile`s.
235 let file
= match item
.source
.filename(self.sess()) {
236 FileName
::Real(ref path
) => path
.local_path().to_path_buf(),
242 let (krate
, path
) = if cnum
== LOCAL_CRATE
{
243 if let Some(path
) = self.shared
.local_sources
.get(file
) {
244 (self.shared
.layout
.krate
.as_str(), path
)
249 let (krate
, src_root
) = match *self.cache
.extern_locations
.get(&cnum
)?
{
250 (name
, ref src
, ExternalLocation
::Local
) => (name
, src
),
251 (name
, ref src
, ExternalLocation
::Remote(ref s
)) => {
252 root
= s
.to_string();
255 (_
, _
, ExternalLocation
::Unknown
) => return None
,
258 sources
::clean_path(&src_root
, file
, false, |component
| {
259 path
.push_str(&component
.to_string_lossy());
262 let mut fname
= file
.file_name().expect("source has no filename").to_os_string();
264 path
.push_str(&fname
.to_string_lossy());
265 symbol
= krate
.as_str();
269 let loline
= item
.source
.lo(self.sess()).line
;
270 let hiline
= item
.source
.hi(self.sess()).line
;
272 if loline
== hiline { loline.to_string() }
else { format!("{}
-{}
", loline, hiline) };
274 "{root}src
/{krate}
/{path}
#{lines}",
275 root = Escape(&root),
283 /// Generates the documentation for `crate` into the directory `dst`
284 impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
285 fn descr() -> &'static str {
290 mut krate: clean::Crate,
291 options: RenderOptions,
295 ) -> Result<(Self, clean::Crate), Error> {
296 // need to save a copy of the options for rendering the index page
297 let md_opts = options.clone();
303 sort_modules_alphabetically,
309 generate_search_filter,
311 generate_redirect_map,
315 let src_root = match krate.src {
316 FileName::Real(ref p) => match p.local_path().parent() {
317 Some(p) => p.to_path_buf(),
318 None => PathBuf::new(),
322 // If user passed in `--playground-url` arg, we fill in crate name here
323 let mut playground = None;
324 if let Some(url) = playground_url {
326 Some(markdown::Playground { crate_name: Some(krate.name.to_string()), url });
328 let mut layout = layout::Layout {
330 favicon: String::new(),
333 krate: krate.name.to_string(),
334 css_file_extension: extension_css,
335 generate_search_filter,
337 let mut issue_tracker_base_url = None;
338 let mut include_sources = true;
340 // Crawl the crate attributes looking for attributes which control how we're
341 // going to emit HTML
342 if let Some(attrs) = krate.module.as_ref().map(|m| &m.attrs) {
343 for attr in attrs.lists(sym::doc) {
344 match (attr.name_or_empty(), attr.value_str()) {
345 (sym::html_favicon_url, Some(s)) => {
346 layout.favicon = s.to_string();
348 (sym::html_logo_url, Some(s)) => {
349 layout.logo = s.to_string();
351 (sym::html_playground_url, Some(s)) => {
352 playground = Some(markdown::Playground {
353 crate_name: Some(krate.name.to_string()),
357 (sym::issue_tracker_base_url, Some(s)) => {
358 issue_tracker_base_url = Some(s.to_string());
360 (sym::html_no_source, None) if attr.is_word() => {
361 include_sources = false;
367 let (sender, receiver) = channel();
368 let mut scx = SharedContext {
370 collapsed: krate.collapsed,
373 local_sources: Default::default(),
374 issue_tracker_base_url,
376 created_dirs: Default::default(),
377 sort_modules_alphabetically,
381 fs: DocFS::new(sender),
383 codes: ErrorCodes::from(unstable_features.is_nightly_build()),
385 all: RefCell::new(AllTypes::new()),
387 redirections: if generate_redirect_map { Some(Default::default()) } else { None },
390 // Add the default themes to the `Vec` of stylepaths
392 // Note that these must be added before `sources::render` is called
393 // so that the resulting source pages are styled
395 // `light.css` is not disabled because it is the stylesheet that stays loaded
396 // by the browser as the theme stylesheet. The theme system (hackily) works by
397 // changing the href to this stylesheet. All other themes are disabled to
398 // prevent rule conflicts
399 scx.style_files.push(StylePath { path: PathBuf::from("light.css"), disabled: false });
400 scx.style_files.push(StylePath { path: PathBuf::from("dark.css"), disabled: true });
401 scx.style_files.push(StylePath { path: PathBuf::from("ayu.css"), disabled: true });
404 scx.ensure_dir(&dst)?;
405 krate = sources::render(&dst, &mut scx, krate)?;
407 // Build our search index
408 let index = build_index(&krate, &mut cache, tcx);
410 let mut cx = Context {
413 render_redirect_pages: false,
414 id_map: RefCell::new(id_map),
415 shared: Rc::new(scx),
416 cache: Rc::new(cache),
419 CURRENT_DEPTH.with(|s| s.set(0));
421 // Write shared runs within a flock; disable thread dispatching of IO temporarily.
422 Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(true);
423 write_shared(&cx, &krate, index, &md_opts)?;
424 Rc::get_mut(&mut cx.shared).unwrap().fs.set_sync_only(false);
428 fn make_child_renderer(&self) -> Self {
429 let mut id_map = IdMap::new();
430 id_map.populate(&INITIAL_IDS);
433 current: self.current.clone(),
434 dst: self.dst.clone(),
435 render_redirect_pages: self.render_redirect_pages,
436 id_map: RefCell::new(id_map),
437 shared: Rc::clone(&self.shared),
438 cache: Rc::clone(&self.cache),
444 krate: &clean::Crate,
445 diag: &rustc_errors::Handler,
446 ) -> Result<(), Error> {
447 let final_file = self.dst.join(&*krate.name.as_str()).join("all.html");
448 let settings_file = self.dst.join("settings.html");
449 let crate_name = krate.name;
451 let mut root_path = self.dst.to_str().expect("invalid path").to_owned();
452 if !root_path.ends_with('/') {
455 let mut page = layout::Page {
456 title: "List of all items in this crate",
459 static_root_path: self.shared.static_root_path.as_deref(),
460 description: "List of all items in this crate",
461 keywords: BASIC_KEYWORDS,
462 resource_suffix: &self.shared.resource_suffix,
464 static_extra_scripts: &[],
466 let sidebar = if let Some(ref version) = self.cache.crate_version {
468 "<p class=\"location\">Crate {}</p>\
469 <div class=\"block version\">\
472 <a id=\"all-types\" href=\"index.html\"><p>Back to index</p></a>",
479 let all = self.shared.all.replace(AllTypes::new());
480 let v = layout::render(
484 |buf: &mut Buffer| all.print(buf),
485 &self.shared.style_files,
487 self.shared.fs.write(&final_file, v.as_bytes())?;
489 // Generating settings page.
490 page.title = "Rustdoc settings";
491 page.description = "Settings of Rustdoc";
492 page.root_path = "./";
494 let mut style_files = self.shared.style_files.clone();
495 let sidebar = "<p class=\"location\">Settings</p><div class=\"sidebar-elems\"></div>";
496 style_files.push(StylePath { path: PathBuf::from("settings.css"), disabled: false });
497 let v = layout::render(
502 self.shared.static_root_path.as_deref().unwrap_or("./"),
503 &self.shared.resource_suffix,
504 &self.shared.style_files,
508 self.shared.fs.write(&settings_file, v.as_bytes())?;
509 if let Some(ref redirections) = self.shared.redirections {
510 if !redirections.borrow().is_empty() {
511 let redirect_map_path =
512 self.dst.join(&*krate.name.as_str()).join("redirect-map.json");
513 let paths = serde_json::to_string(&*redirections.borrow()).unwrap();
514 self.shared.ensure_dir(&self.dst.join(&*krate.name.as_str()))?;
515 self.shared.fs.write(&redirect_map_path, paths.as_bytes())?;
519 // Flush pending errors.
520 Rc::get_mut(&mut self.shared).unwrap().fs.close();
521 let nb_errors = self.shared.errors.iter().map(|err| diag.struct_err(&err).emit()).count();
523 Err(Error::new(io::Error::new(io::ErrorKind::Other, "I/O error"), ""))
529 fn mod_item_in(&mut self, item: &clean::Item, item_name: &str) -> Result<(), Error> {
530 // Stripped modules survive the rustdoc passes (i.e., `strip-private`)
531 // if they contain impls for public types. These modules can also
532 // contain items such as publicly re-exported structures.
534 // External crates will provide links to these structures, so
535 // these modules are recursed into, but not rendered normally
536 // (a flag on the context).
537 if !self.render_redirect_pages {
538 self.render_redirect_pages = item.is_stripped();
540 let scx = &self.shared;
541 self.dst.push(item_name);
542 self.current.push(item_name.to_owned());
544 info!("Recursing into {}", self.dst.display());
546 let buf = self.render_item(item, false);
547 // buf will be empty if the module is stripped and there is no redirect for it
549 self.shared.ensure_dir(&self.dst)?;
550 let joint_dst = self.dst.join("index.html");
551 scx.fs.write(&joint_dst, buf.as_bytes())?;
554 // Render sidebar-items.js used throughout this module.
555 if !self.render_redirect_pages {
556 let module = match *item.kind {
557 clean::StrippedItem(box clean::ModuleItem(ref m)) | clean::ModuleItem(ref m) => m,
560 let items = self.build_sidebar_items(module);
561 let js_dst = self.dst.join("sidebar-items.js");
562 let v = format!("initSidebarItems({});", serde_json::to_string(&items).unwrap());
563 scx.fs.write(&js_dst, &v)?;
568 fn mod_item_out(&mut self, _item_name: &str) -> Result<(), Error> {
569 info!("Recursed; leaving {}", self.dst.display());
571 // Go back to where we were at
577 fn item(&mut self, item: clean::Item) -> Result<(), Error> {
578 // Stripped modules survive the rustdoc passes (i.e., `strip-private`)
579 // if they contain impls for public types. These modules can also
580 // contain items such as publicly re-exported structures.
582 // External crates will provide links to these structures, so
583 // these modules are recursed into, but not rendered normally
584 // (a flag on the context).
585 if !self.render_redirect_pages {
586 self.render_redirect_pages = item.is_stripped();
589 let buf = self.render_item(&item, true);
590 // buf will be empty if the item is stripped and there is no redirect for it
592 let name = item.name.as_ref().unwrap();
593 let item_type = item.type_();
594 let file_name = &item_path(item_type, &name.as_str());
595 self.shared.ensure_dir(&self.dst)?;
596 let joint_dst = self.dst.join(file_name);
597 self.shared.fs.write(&joint_dst, buf.as_bytes())?;
599 if !self.render_redirect_pages {
600 self.shared.all.borrow_mut().append(full_path(self, &item), &item_type);
602 // If the item is a macro, redirect from the old macro URL (with !)
603 // to the new one (without).
604 if item_type == ItemType::Macro {
605 let redir_name = format!("{}.{}!.html", item_type, name);
606 if let Some(ref redirections) = self.shared.redirections {
607 let crate_name = &self.shared.layout.krate;
608 redirections.borrow_mut().insert(
609 format!("{}/{}", crate_name, redir_name),
610 format!("{}/{}", crate_name, file_name),
613 let v = layout::redirect(file_name);
614 let redir_dst = self.dst.join(redir_name);
615 self.shared.fs.write(&redir_dst, v.as_bytes())?;
622 fn cache(&self) -> &Cache {
627 fn make_item_keywords(it: &clean::Item) -> String {
628 format!("{}, {}", BASIC_KEYWORDS, it.name.as_ref().unwrap())