Skip to content

Commit

Permalink
rustdoc: simplify URLs
Browse files Browse the repository at this point in the history
Changes rustdoc URLs to name.namespace.html, e.g., Foo.t.html. These are easier for clients to guess since they only need to know the namespace, not the kind of item.

Old URLs are preserved as redirects to the new ones.

I also add redirects for modules, e.g.,
foo/bar/baz.t.html to foo/bar/baz/index.html, so modules are not an exception to the URL rule.

And removes the ! from macro URLs.

Closes rust-lang#34271
  • Loading branch information
nrc committed Jul 31, 2016
1 parent 54c0dcf commit ef71c08
Show file tree
Hide file tree
Showing 87 changed files with 620 additions and 459 deletions.
8 changes: 5 additions & 3 deletions src/librustdoc/clean/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2727,7 +2727,7 @@ pub struct Macro {

impl Clean<Item> for doctree::Macro {
fn clean(&self, cx: &DocContext) -> Item {
let name = format!("{}!", self.name.clean(cx));
let name = self.name.clean(cx);
Item {
name: Some(name.clone()),
attrs: self.attrs.clean(cx),
Expand All @@ -2738,8 +2738,10 @@ impl Clean<Item> for doctree::Macro {
def_id: cx.map.local_def_id(self.id),
inner: MacroItem(Macro {
source: format!("macro_rules! {} {{\n{}}}",
name.trim_right_matches('!'), self.matchers.iter().map(|span|
format!(" {} => {{ ... }};\n", span.to_src(cx))).collect::<String>()),
name,
self.matchers.iter().map(|span| {
format!(" {} => {{ ... }};\n", span.to_src(cx))
}).collect::<String>()),
imported_from: self.imported_from.clean(cx),
}),
}
Expand Down
10 changes: 5 additions & 5 deletions src/librustdoc/html/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,9 +326,9 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
url.push_str("/index.html");
}
_ => {
url.push_str(shortty.to_static_str());
url.push_str(".");
url.push_str(fqp.last().unwrap());
url.push_str(".");
url.push_str(shortty.name_space().to_static_str());
url.push_str(".html");
}
}
Expand Down Expand Up @@ -382,7 +382,7 @@ fn primitive_link(f: &mut fmt::Formatter,
Some(&LOCAL_CRATE) => {
let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len());
let len = if len == 0 {0} else {len - 1};
write!(f, "<a class='primitive' href='{}primitive.{}.html'>",
write!(f, "<a class='primitive' href='{}{}.t.html'>",
repeat("../").take(len).collect::<String>(),
prim.to_url_str())?;
needs_termination = true;
Expand All @@ -397,7 +397,7 @@ fn primitive_link(f: &mut fmt::Formatter,
(_, render::Unknown) => None,
};
if let Some((cname, root)) = loc {
write!(f, "<a class='primitive' href='{}{}/primitive.{}.html'>",
write!(f, "<a class='primitive' href='{}{}/{}.t.html'>",
root,
cname,
prim.to_url_str())?;
Expand Down Expand Up @@ -439,7 +439,7 @@ impl<'a> fmt::Display for HRef<'a> {
match href(self.did) {
Some((url, shortty, fqp)) => {
write!(f, "<a class='{}' href='{}' title='{}'>{}</a>",
shortty, url, fqp.join("::"), self.text)
shortty.css_class(), url, fqp.join("::"), self.text)
}
_ => write!(f, "{}", self.text),
}
Expand Down
51 changes: 49 additions & 2 deletions src/librustdoc/html/item_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,13 @@ pub enum ItemType {
AssociatedConst = 18,
}

#[derive(Copy, Eq, PartialEq, Clone)]
pub enum NameSpace {
Type,
Value,
Macro,
}

impl ItemType {
pub fn from_item(item: &clean::Item) -> ItemType {
let inner = match item.inner {
Expand Down Expand Up @@ -90,7 +97,7 @@ impl ItemType {
}
}

pub fn to_static_str(&self) -> &'static str {
pub fn css_class(&self) -> &'static str {
match *self {
ItemType::Module => "mod",
ItemType::ExternCrate => "externcrate",
Expand All @@ -113,9 +120,49 @@ impl ItemType {
ItemType::AssociatedConst => "associatedconstant",
}
}

pub fn name_space(&self) -> NameSpace {
match *self {
ItemType::Struct |
ItemType::Enum |
ItemType::Module |
ItemType::Typedef |
ItemType::Trait |
ItemType::Primitive |
ItemType::AssociatedType => NameSpace::Type,

ItemType::ExternCrate |
ItemType::Import |
ItemType::Function |
ItemType::Static |
ItemType::Impl |
ItemType::TyMethod |
ItemType::Method |
ItemType::StructField |
ItemType::Variant |
ItemType::Constant |
ItemType::AssociatedConst => NameSpace::Value,

ItemType::Macro => NameSpace::Macro,
}
}
}

pub const NAMESPACE_TYPE: &'static str = "t";
pub const NAMESPACE_VALUE: &'static str = "v";
pub const NAMESPACE_MACRO: &'static str = "m";

impl NameSpace {
pub fn to_static_str(&self) -> &'static str {
match *self {
NameSpace::Type => NAMESPACE_TYPE,
NameSpace::Value => NAMESPACE_VALUE,
NameSpace::Macro => NAMESPACE_MACRO,
}
}
}

impl fmt::Display for ItemType {
impl fmt::Display for NameSpace {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
self.to_static_str().fmt(f)
}
Expand Down
6 changes: 3 additions & 3 deletions src/librustdoc/html/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub struct Layout {

pub struct Page<'a> {
pub title: &'a str,
pub ty: &'a str,
pub css_class: &'a str,
pub root_path: &'a str,
pub description: &'a str,
pub keywords: &'a str,
Expand Down Expand Up @@ -80,7 +80,7 @@ r##"<!DOCTYPE html>
</form>
</nav>
<section id='main' class="content {ty}">{content}</section>
<section id='main' class="content {css_class}">{content}</section>
<section id='search' class="content hidden"></section>
<section class="footer"></section>
Expand Down Expand Up @@ -152,7 +152,7 @@ r##"<!DOCTYPE html>
},
content = *t,
root_path = page.root_path,
ty = page.ty,
css_class = page.css_class,
logo = if layout.logo.is_empty() {
"".to_string()
} else {
Expand Down
Loading

0 comments on commit ef71c08

Please sign in to comment.