1
Fork 0

rustdoc: redirects from sane, namespaced URLs to Rustdoc's ridiculous ones

cc #35020 which does this properly
This commit is contained in:
Nick Cameron 2016-08-03 13:56:18 +12:00
parent a41454bf65
commit 77ca7dd107
2 changed files with 48 additions and 17 deletions

View file

@ -168,3 +168,9 @@ impl NameSpace {
} }
} }
} }
impl fmt::Display for NameSpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_static_str().fmt(f)
}
}

View file

@ -41,7 +41,7 @@ use std::collections::{BTreeMap, HashMap, HashSet};
use std::default::Default; use std::default::Default;
use std::error; use std::error;
use std::fmt::{self, Display, Formatter}; use std::fmt::{self, Display, Formatter};
use std::fs::{self, File}; use std::fs::{self, File, OpenOptions};
use std::io::prelude::*; use std::io::prelude::*;
use std::io::{self, BufWriter, BufReader}; use std::io::{self, BufWriter, BufReader};
use std::iter::repeat; use std::iter::repeat;
@ -1409,11 +1409,23 @@ impl Context {
self.render_item(&mut buf, &item, true).unwrap(); self.render_item(&mut buf, &item, true).unwrap();
// buf will be empty if the item is stripped and there is no redirect for it // buf will be empty if the item is stripped and there is no redirect for it
if !buf.is_empty() { if !buf.is_empty() {
let joint_dst = self.dst.join(&item_path(item_type(&item), let name = item.name.as_ref().unwrap();
item.name.as_ref().unwrap())); let item_type = item_type(&item);
let file_name = &item_path(item_type, name);
let joint_dst = self.dst.join(file_name);
try_err!(fs::create_dir_all(&self.dst), &self.dst); try_err!(fs::create_dir_all(&self.dst), &self.dst);
let mut dst = try_err!(File::create(&joint_dst), &joint_dst); let mut dst = try_err!(File::create(&joint_dst), &joint_dst);
try_err!(dst.write_all(&buf), &joint_dst); try_err!(dst.write_all(&buf), &joint_dst);
// Redirect from a sane URL using the namespace to Rustdoc's
// URL for the page.
let redir_name = format!("{}.{}.html", name, item_type.name_space());
let redir_dst = self.dst.join(redir_name);
if let Ok(mut redirect_out) = OpenOptions::new().create_new(true)
.write(true)
.open(&redir_dst) {
try_err!(layout::redirect(&mut redirect_out, file_name), &redir_dst);
}
} }
} }
Ok(()) Ok(())
@ -2270,9 +2282,12 @@ fn item_struct(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
if fields.peek().is_some() { if fields.peek().is_some() {
write!(w, "<h2 class='fields'>Fields</h2>")?; write!(w, "<h2 class='fields'>Fields</h2>")?;
for (field, ty) in fields { for (field, ty) in fields {
write!(w, "<span id='{item_type}.{name}' class='{item_type}'><code>{name}: {ty}</code> write!(w, "<span id='{item_type}.{name}' class='{item_type}'>
</span><span class='stab {stab}'></span>", <a id='{name}.{name_space}'>
<code>{name}: {ty}</code>
</a></span><span class='stab {stab}'></span>",
item_type = ItemType::StructField, item_type = ItemType::StructField,
name_space = ItemType::StructField.name_space(),
stab = field.stability_class(), stab = field.stability_class(),
name = field.name.as_ref().unwrap(), name = field.name.as_ref().unwrap(),
ty = ty)?; ty = ty)?;
@ -2341,8 +2356,10 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
if !e.variants.is_empty() { if !e.variants.is_empty() {
write!(w, "<h2 class='variants'>Variants</h2>\n")?; write!(w, "<h2 class='variants'>Variants</h2>\n")?;
for variant in &e.variants { for variant in &e.variants {
write!(w, "<span id='{item_type}.{name}' class='variant'><code>{name}", write!(w, "<span id='{item_type}.{name}' class='variant'>\
<a id='{name}.{name_space}'><code>{name}",
item_type = ItemType::Variant, item_type = ItemType::Variant,
name_space = ItemType::Variant.name_space(),
name = variant.name.as_ref().unwrap())?; name = variant.name.as_ref().unwrap())?;
if let clean::VariantItem(ref var) = variant.inner { if let clean::VariantItem(ref var) = variant.inner {
if let clean::TupleVariant(ref tys) = var.kind { if let clean::TupleVariant(ref tys) = var.kind {
@ -2356,7 +2373,7 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
write!(w, ")")?; write!(w, ")")?;
} }
} }
write!(w, "</code></span>")?; write!(w, "</code></a></span>")?;
document(w, cx, variant)?; document(w, cx, variant)?;
use clean::{Variant, StructVariant}; use clean::{Variant, StructVariant};
@ -2368,9 +2385,12 @@ fn item_enum(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
if let StructFieldItem(ref ty) = field.inner { if let StructFieldItem(ref ty) = field.inner {
write!(w, "<tr><td \ write!(w, "<tr><td \
id='variant.{v}.field.{f}'>\ id='variant.{v}.field.{f}'>\
<code>{f}:&nbsp;{t}</code></td><td>", <a id='{v}.{vns}.{f}.{fns}'>\
<code>{f}:&nbsp;{t}</code></a></td><td>",
v = variant.name.as_ref().unwrap(), v = variant.name.as_ref().unwrap(),
f = field.name.as_ref().unwrap(), f = field.name.as_ref().unwrap(),
vns = ItemType::Variant.name_space(),
fns = ItemType::StructField.name_space(),
t = *ty)?; t = *ty)?;
document(w, cx, field)?; document(w, cx, field)?;
write!(w, "</td></tr>")?; write!(w, "</td></tr>")?;
@ -2605,36 +2625,41 @@ fn render_impl(w: &mut fmt::Formatter, cx: &Context, i: &Impl, link: AssocItemLi
if !is_static || render_static { if !is_static || render_static {
let id = derive_id(format!("{}.{}", item_type, name)); let id = derive_id(format!("{}.{}", item_type, name));
write!(w, "<h4 id='{}' class='{}'>", id, item_type)?; write!(w, "<h4 id='{}' class='{}'>", id, item_type)?;
write!(w, "<a id='{}.{}'>", name, item_type.name_space())?;
write!(w, "<code>")?; write!(w, "<code>")?;
render_assoc_item(w, item, link.anchor(&id))?; render_assoc_item(w, item, link.anchor(&id))?;
write!(w, "</code>")?; write!(w, "</code>")?;
render_stability_since_raw(w, item.stable_since(), outer_version)?; render_stability_since_raw(w, item.stable_since(), outer_version)?;
write!(w, "</h4>\n")?; write!(w, "</a></h4>\n")?;
} }
} }
clean::TypedefItem(ref tydef, _) => { clean::TypedefItem(ref tydef, _) => {
let id = derive_id(format!("{}.{}", ItemType::AssociatedType, name)); let id = derive_id(format!("{}.{}", ItemType::AssociatedType, name));
write!(w, "<h4 id='{}' class='{}'><code>", id, item_type)?; write!(w, "<h4 id='{}' class='{}'>", id, item_type)?;
write!(w, "<a id='{}.{}'><code>", name, item_type.name_space())?;
assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?; assoc_type(w, item, &Vec::new(), Some(&tydef.type_), link.anchor(&id))?;
write!(w, "</code></h4>\n")?; write!(w, "</code></a></h4>\n")?;
} }
clean::AssociatedConstItem(ref ty, ref default) => { clean::AssociatedConstItem(ref ty, ref default) => {
let id = derive_id(format!("{}.{}", item_type, name)); let id = derive_id(format!("{}.{}", item_type, name));
write!(w, "<h4 id='{}' class='{}'><code>", id, item_type)?; write!(w, "<h4 id='{}' class='{}'>", id, item_type)?;
write!(w, "<a id='{}.{}'><code>", name, item_type.name_space())?;
assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?; assoc_const(w, item, ty, default.as_ref(), link.anchor(&id))?;
write!(w, "</code></h4>\n")?; write!(w, "</code></a></h4>\n")?;
} }
clean::ConstantItem(ref c) => { clean::ConstantItem(ref c) => {
let id = derive_id(format!("{}.{}", item_type, name)); let id = derive_id(format!("{}.{}", item_type, name));
write!(w, "<h4 id='{}' class='{}'><code>", id, item_type)?; write!(w, "<h4 id='{}' class='{}'>", id, item_type)?;
write!(w, "<a id='{}.{}'><code>", name, item_type.name_space())?;
assoc_const(w, item, &c.type_, Some(&c.expr), link.anchor(&id))?; assoc_const(w, item, &c.type_, Some(&c.expr), link.anchor(&id))?;
write!(w, "</code></h4>\n")?; write!(w, "</code></a></h4>\n")?;
} }
clean::AssociatedTypeItem(ref bounds, ref default) => { clean::AssociatedTypeItem(ref bounds, ref default) => {
let id = derive_id(format!("{}.{}", item_type, name)); let id = derive_id(format!("{}.{}", item_type, name));
write!(w, "<h4 id='{}' class='{}'><code>", id, item_type)?; write!(w, "<h4 id='{}' class='{}'>", id, item_type)?;
write!(w, "<a id='{}.{}'><code>", name, item_type.name_space())?;
assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?; assoc_type(w, item, bounds, default.as_ref(), link.anchor(&id))?;
write!(w, "</code></h4>\n")?; write!(w, "</code></a></h4>\n")?;
} }
clean::StrippedItem(..) => return Ok(()), clean::StrippedItem(..) => return Ok(()),
_ => panic!("can't make docs for trait item with name {:?}", item.name) _ => panic!("can't make docs for trait item with name {:?}", item.name)