Add --generate-redirect-map option to replace HTML redirection file with a unique JSON map
This commit is contained in:
@@ -265,6 +265,8 @@ crate struct RenderOptions {
|
|||||||
crate document_private: bool,
|
crate document_private: bool,
|
||||||
/// Document items that have `doc(hidden)`.
|
/// Document items that have `doc(hidden)`.
|
||||||
crate document_hidden: bool,
|
crate document_hidden: bool,
|
||||||
|
/// If `true`, generate a JSON file in the crate folder instead of HTML redirection files.
|
||||||
|
crate generate_redirect_map: bool,
|
||||||
crate unstable_features: rustc_feature::UnstableFeatures,
|
crate unstable_features: rustc_feature::UnstableFeatures,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -586,6 +588,7 @@ impl Options {
|
|||||||
let document_private = matches.opt_present("document-private-items");
|
let document_private = matches.opt_present("document-private-items");
|
||||||
let document_hidden = matches.opt_present("document-hidden-items");
|
let document_hidden = matches.opt_present("document-hidden-items");
|
||||||
let run_check = matches.opt_present("check");
|
let run_check = matches.opt_present("check");
|
||||||
|
let generate_redirect_map = matches.opt_present("generate-redirect-map");
|
||||||
|
|
||||||
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
|
let (lint_opts, describe_lints, lint_cap) = get_cmd_lint_options(matches, error_format);
|
||||||
|
|
||||||
@@ -643,6 +646,7 @@ impl Options {
|
|||||||
generate_search_filter,
|
generate_search_filter,
|
||||||
document_private,
|
document_private,
|
||||||
document_hidden,
|
document_hidden,
|
||||||
|
generate_redirect_map,
|
||||||
unstable_features: rustc_feature::UnstableFeatures::from_environment(
|
unstable_features: rustc_feature::UnstableFeatures::from_environment(
|
||||||
crate_name.as_deref(),
|
crate_name.as_deref(),
|
||||||
),
|
),
|
||||||
|
|||||||
@@ -111,6 +111,10 @@ crate struct Context<'tcx> {
|
|||||||
/// real location of an item. This is used to allow external links to
|
/// real location of an item. This is used to allow external links to
|
||||||
/// publicly reused items to redirect to the right location.
|
/// publicly reused items to redirect to the right location.
|
||||||
crate render_redirect_pages: bool,
|
crate render_redirect_pages: bool,
|
||||||
|
/// `None` by default, depends on the `generate-redirect-map` option flag. If this field is set
|
||||||
|
/// to `Some(...)`, it'll store redirections and then generate a JSON file at the top level of
|
||||||
|
/// the crate.
|
||||||
|
crate redirections: Option<Rc<RefCell<FxHashMap<String, String>>>>,
|
||||||
/// The map used to ensure all generated 'id=' attributes are unique.
|
/// The map used to ensure all generated 'id=' attributes are unique.
|
||||||
id_map: Rc<RefCell<IdMap>>,
|
id_map: Rc<RefCell<IdMap>>,
|
||||||
/// Tracks section IDs for `Deref` targets so they match in both the main
|
/// Tracks section IDs for `Deref` targets so they match in both the main
|
||||||
@@ -405,6 +409,7 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
|||||||
static_root_path,
|
static_root_path,
|
||||||
generate_search_filter,
|
generate_search_filter,
|
||||||
unstable_features,
|
unstable_features,
|
||||||
|
generate_redirect_map,
|
||||||
..
|
..
|
||||||
} = options;
|
} = options;
|
||||||
|
|
||||||
@@ -510,6 +515,11 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
|||||||
all: Rc::new(RefCell::new(AllTypes::new())),
|
all: Rc::new(RefCell::new(AllTypes::new())),
|
||||||
errors: Rc::new(receiver),
|
errors: Rc::new(receiver),
|
||||||
cache: Rc::new(cache),
|
cache: Rc::new(cache),
|
||||||
|
redirections: if generate_redirect_map {
|
||||||
|
Some(Rc::new(RefCell::new(FxHashMap::default())))
|
||||||
|
} else {
|
||||||
|
None
|
||||||
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
CURRENT_DEPTH.with(|s| s.set(0));
|
CURRENT_DEPTH.with(|s| s.set(0));
|
||||||
@@ -588,6 +598,15 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
|||||||
&style_files,
|
&style_files,
|
||||||
);
|
);
|
||||||
self.shared.fs.write(&settings_file, v.as_bytes())?;
|
self.shared.fs.write(&settings_file, v.as_bytes())?;
|
||||||
|
if let Some(redirections) = self.redirections.take() {
|
||||||
|
if !redirections.borrow().is_empty() {
|
||||||
|
let redirect_map_path =
|
||||||
|
self.dst.join(&*krate.name.as_str()).join("redirect-map.json");
|
||||||
|
let paths = serde_json::to_string(&*redirections.borrow()).unwrap();
|
||||||
|
self.shared.ensure_dir(&self.dst.join(&*krate.name.as_str()))?;
|
||||||
|
self.shared.fs.write(&redirect_map_path, paths.as_bytes())?;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Flush pending errors.
|
// Flush pending errors.
|
||||||
Arc::get_mut(&mut self.shared).unwrap().fs.close();
|
Arc::get_mut(&mut self.shared).unwrap().fs.close();
|
||||||
@@ -664,9 +683,9 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
|||||||
if !buf.is_empty() {
|
if !buf.is_empty() {
|
||||||
let name = item.name.as_ref().unwrap();
|
let name = item.name.as_ref().unwrap();
|
||||||
let item_type = item.type_();
|
let item_type = item.type_();
|
||||||
let file_name = &item_path(item_type, &name.as_str());
|
let file_name = item_path(item_type, &name.as_str());
|
||||||
self.shared.ensure_dir(&self.dst)?;
|
self.shared.ensure_dir(&self.dst)?;
|
||||||
let joint_dst = self.dst.join(file_name);
|
let joint_dst = self.dst.join(&file_name);
|
||||||
self.shared.fs.write(&joint_dst, buf.as_bytes())?;
|
self.shared.fs.write(&joint_dst, buf.as_bytes())?;
|
||||||
|
|
||||||
if !self.render_redirect_pages {
|
if !self.render_redirect_pages {
|
||||||
@@ -676,11 +695,19 @@ impl<'tcx> FormatRenderer<'tcx> for Context<'tcx> {
|
|||||||
// to the new one (without).
|
// to the new one (without).
|
||||||
if item_type == ItemType::Macro {
|
if item_type == ItemType::Macro {
|
||||||
let redir_name = format!("{}.{}!.html", item_type, name);
|
let redir_name = format!("{}.{}!.html", item_type, name);
|
||||||
|
if let Some(ref redirections) = self.redirections {
|
||||||
|
let crate_name = &self.shared.layout.krate;
|
||||||
|
redirections.borrow_mut().insert(
|
||||||
|
format!("{}/{}", crate_name, redir_name),
|
||||||
|
format!("{}/{}", crate_name, file_name),
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
let v = layout::redirect(&file_name);
|
||||||
let redir_dst = self.dst.join(redir_name);
|
let redir_dst = self.dst.join(redir_name);
|
||||||
let v = layout::redirect(file_name);
|
|
||||||
self.shared.fs.write(&redir_dst, v.as_bytes())?;
|
self.shared.fs.write(&redir_dst, v.as_bytes())?;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1588,19 +1615,29 @@ impl Context<'_> {
|
|||||||
&self.shared.style_files,
|
&self.shared.style_files,
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
let mut url = self.root_path();
|
|
||||||
if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id) {
|
if let Some(&(ref names, ty)) = self.cache.paths.get(&it.def_id) {
|
||||||
|
let mut path = String::new();
|
||||||
for name in &names[..names.len() - 1] {
|
for name in &names[..names.len() - 1] {
|
||||||
url.push_str(name);
|
path.push_str(name);
|
||||||
url.push('/');
|
path.push('/');
|
||||||
|
}
|
||||||
|
path.push_str(&item_path(ty, names.last().unwrap()));
|
||||||
|
match self.redirections {
|
||||||
|
Some(ref redirections) => {
|
||||||
|
let mut current_path = String::new();
|
||||||
|
for name in &self.current {
|
||||||
|
current_path.push_str(name);
|
||||||
|
current_path.push('/');
|
||||||
|
}
|
||||||
|
current_path.push_str(&item_path(ty, names.last().unwrap()));
|
||||||
|
redirections.borrow_mut().insert(current_path, path);
|
||||||
|
}
|
||||||
|
None => return layout::redirect(&format!("{}{}", self.root_path(), path)),
|
||||||
|
}
|
||||||
}
|
}
|
||||||
url.push_str(&item_path(ty, names.last().unwrap()));
|
|
||||||
layout::redirect(&url)
|
|
||||||
} else {
|
|
||||||
String::new()
|
String::new()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/// Construct a map of items shown in the sidebar to a plain-text summary of their docs.
|
/// Construct a map of items shown in the sidebar to a plain-text summary of their docs.
|
||||||
fn build_sidebar_items(&self, m: &clean::Module) -> BTreeMap<String, Vec<NameDoc>> {
|
fn build_sidebar_items(&self, m: &clean::Module) -> BTreeMap<String, Vec<NameDoc>> {
|
||||||
|
|||||||
@@ -423,6 +423,13 @@ fn opts() -> Vec<RustcOptGroup> {
|
|||||||
o.optopt("", "test-builder", "The rustc-like binary to use as the test builder", "PATH")
|
o.optopt("", "test-builder", "The rustc-like binary to use as the test builder", "PATH")
|
||||||
}),
|
}),
|
||||||
unstable("check", |o| o.optflag("", "check", "Run rustdoc checks")),
|
unstable("check", |o| o.optflag("", "check", "Run rustdoc checks")),
|
||||||
|
unstable("generate-redirect-map", |o| {
|
||||||
|
o.optflag(
|
||||||
|
"",
|
||||||
|
"generate-redirect-map",
|
||||||
|
"Generate JSON file at the top level instead of generating HTML redirection files",
|
||||||
|
)
|
||||||
|
}),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user