|
| 1 | +//! This file will go through all doc comments and retrieve local resources to then store them |
| 2 | +//! in the rustdoc output directory. |
| 3 | +
|
| 4 | +use pulldown_cmark::{Event, Parser, Tag}; |
| 5 | + |
| 6 | +use rustc_span::def_id::LOCAL_CRATE; |
| 7 | +use rustc_span::FileName; |
| 8 | + |
| 9 | +use crate::clean::{Crate, Item}; |
| 10 | +use crate::core::DocContext; |
| 11 | +use crate::html::markdown::main_body_opts; |
| 12 | +use crate::html::render::root_path; |
| 13 | +use crate::html::LOCAL_RESOURCES_FOLDER_NAME; |
| 14 | +use crate::passes::Pass; |
| 15 | +use crate::visit::DocVisitor; |
| 16 | + |
| 17 | +use std::path::{Path, PathBuf}; |
| 18 | + |
| 19 | +pub(crate) const COLLECT_LOCAL_RESOURCES: Pass = Pass { |
| 20 | + name: "collect-local-resources", |
| 21 | + run: collect_local_resources, |
| 22 | + description: "resolves intra-doc links", |
| 23 | +}; |
| 24 | + |
| 25 | +fn span_file_path(cx: &DocContext<'_>, item: &Item) -> Option<PathBuf> { |
| 26 | + item.span(cx.tcx).and_then(|span| match span.filename(cx.sess()) { |
| 27 | + FileName::Real(ref path) => Some(path.local_path_if_available().into()), |
| 28 | + _ => None, |
| 29 | + }) |
| 30 | +} |
| 31 | + |
| 32 | +struct ResourcesCollector<'a, 'tcx> { |
| 33 | + cx: &'a mut DocContext<'tcx>, |
| 34 | + /// The depth is used to know how many "../" needs to be generated to get the original file |
| 35 | + /// path. |
| 36 | + depth: usize, |
| 37 | +} |
| 38 | + |
| 39 | +fn collect_local_resources(krate: Crate, cx: &mut DocContext<'_>) -> Crate { |
| 40 | + let mut collector = ResourcesCollector { cx, depth: 1 }; |
| 41 | + collector.visit_crate(&krate); |
| 42 | + krate |
| 43 | +} |
| 44 | + |
| 45 | +impl<'a, 'tcx> ResourcesCollector<'a, 'tcx> { |
| 46 | + pub fn handle_event( |
| 47 | + &mut self, |
| 48 | + event: Event<'_>, |
| 49 | + current_path: &mut Option<PathBuf>, |
| 50 | + item: &Item, |
| 51 | + ) { |
| 52 | + if let Event::Start(Tag::Image(_, ref ori_path, _)) = event && |
| 53 | + !ori_path.starts_with("http://") && |
| 54 | + !ori_path.starts_with("https://") |
| 55 | + { |
| 56 | + let ori_path = ori_path.to_string(); |
| 57 | + if self.cx.cache.local_resources.resources_correspondance |
| 58 | + .get(&self.depth) |
| 59 | + .and_then(|entry| entry.get(&ori_path)) |
| 60 | + .is_some() |
| 61 | + { |
| 62 | + // We already have this entry so nothing to be done! |
| 63 | + return; |
| 64 | + } |
| 65 | + if current_path.is_none() { |
| 66 | + *current_path = span_file_path(self.cx, item); |
| 67 | + } |
| 68 | + let Some(current_path) = current_path else { return }; |
| 69 | + |
| 70 | + let path = match current_path.parent() |
| 71 | + .unwrap_or_else(|| Path::new(".")) |
| 72 | + .join(&ori_path) |
| 73 | + .canonicalize() |
| 74 | + { |
| 75 | + Ok(p) => p, |
| 76 | + Err(_) => { |
| 77 | + self.cx.tcx.sess.struct_span_err( |
| 78 | + item.attr_span(self.cx.tcx), |
| 79 | + &format!("`{ori_path}`: No such file"), |
| 80 | + ).emit(); |
| 81 | + return; |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + if !path.is_file() { |
| 86 | + self.cx.tcx.sess.struct_span_err( |
| 87 | + item.attr_span(self.cx.tcx), |
| 88 | + &format!("`{ori_path}`: No such file (expanded into `{}`)", path.display()), |
| 89 | + ).emit(); |
| 90 | + return; |
| 91 | + } |
| 92 | + |
| 93 | + // We now enter the file into the `resources_to_copy` in case it's not already in |
| 94 | + // and then generate a path the file that we store into `resources_correspondance` |
| 95 | + // with the `add_entry_at_depth` method. |
| 96 | + let current_nb = self.cx.cache.local_resources.resources_to_copy.len(); |
| 97 | + let file_name = self.cx.cache.local_resources.resources_to_copy |
| 98 | + .entry(path.clone()) |
| 99 | + .or_insert_with(|| { |
| 100 | + let extension = path.extension(); |
| 101 | + let (extension, dot) = match extension.and_then(|e| e.to_str()) { |
| 102 | + Some(e) => (e, "."), |
| 103 | + None => ("", ""), |
| 104 | + }; |
| 105 | + format!( |
| 106 | + "{current_nb}{}{dot}{extension}", |
| 107 | + self.cx.render_options.resource_suffix, |
| 108 | + ) |
| 109 | + }); |
| 110 | + let file = format!( |
| 111 | + "{}{LOCAL_RESOURCES_FOLDER_NAME}/{}/{file_name}", |
| 112 | + root_path(self.depth), |
| 113 | + self.cx.tcx.crate_name(LOCAL_CRATE).as_str(), |
| 114 | + ); |
| 115 | + self.cx.cache.local_resources.add_entry_at_depth(self.depth, ori_path, file); |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +impl<'a, 'tcx> DocVisitor for ResourcesCollector<'a, 'tcx> { |
| 121 | + fn visit_item(&mut self, item: &Item) { |
| 122 | + if let Some(md) = item.collapsed_doc_value() { |
| 123 | + let mut current_path = None; |
| 124 | + for event in Parser::new_ext(&md, main_body_opts()).into_iter() { |
| 125 | + self.handle_event(event, &mut current_path, item); |
| 126 | + } |
| 127 | + } |
| 128 | + |
| 129 | + if item.is_mod() && !item.is_crate() { |
| 130 | + self.depth += 1; |
| 131 | + self.visit_item_recur(item); |
| 132 | + self.depth -= 1; |
| 133 | + } else { |
| 134 | + self.visit_item_recur(item) |
| 135 | + } |
| 136 | + } |
| 137 | +} |
0 commit comments