Skip to content

Commit ef71c08

Browse files
committed
rustdoc: simplify URLs
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
1 parent 54c0dcf commit ef71c08

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

87 files changed

+620
-459
lines changed

src/librustdoc/clean/mod.rs

+5-3
Original file line numberDiff line numberDiff line change
@@ -2727,7 +2727,7 @@ pub struct Macro {
27272727

27282728
impl Clean<Item> for doctree::Macro {
27292729
fn clean(&self, cx: &DocContext) -> Item {
2730-
let name = format!("{}!", self.name.clean(cx));
2730+
let name = self.name.clean(cx);
27312731
Item {
27322732
name: Some(name.clone()),
27332733
attrs: self.attrs.clean(cx),
@@ -2738,8 +2738,10 @@ impl Clean<Item> for doctree::Macro {
27382738
def_id: cx.map.local_def_id(self.id),
27392739
inner: MacroItem(Macro {
27402740
source: format!("macro_rules! {} {{\n{}}}",
2741-
name.trim_right_matches('!'), self.matchers.iter().map(|span|
2742-
format!(" {} => {{ ... }};\n", span.to_src(cx))).collect::<String>()),
2741+
name,
2742+
self.matchers.iter().map(|span| {
2743+
format!(" {} => {{ ... }};\n", span.to_src(cx))
2744+
}).collect::<String>()),
27432745
imported_from: self.imported_from.clean(cx),
27442746
}),
27452747
}

src/librustdoc/html/format.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -326,9 +326,9 @@ pub fn href(did: DefId) -> Option<(String, ItemType, Vec<String>)> {
326326
url.push_str("/index.html");
327327
}
328328
_ => {
329-
url.push_str(shortty.to_static_str());
330-
url.push_str(".");
331329
url.push_str(fqp.last().unwrap());
330+
url.push_str(".");
331+
url.push_str(shortty.name_space().to_static_str());
332332
url.push_str(".html");
333333
}
334334
}
@@ -382,7 +382,7 @@ fn primitive_link(f: &mut fmt::Formatter,
382382
Some(&LOCAL_CRATE) => {
383383
let len = CURRENT_LOCATION_KEY.with(|s| s.borrow().len());
384384
let len = if len == 0 {0} else {len - 1};
385-
write!(f, "<a class='primitive' href='{}primitive.{}.html'>",
385+
write!(f, "<a class='primitive' href='{}{}.t.html'>",
386386
repeat("../").take(len).collect::<String>(),
387387
prim.to_url_str())?;
388388
needs_termination = true;
@@ -397,7 +397,7 @@ fn primitive_link(f: &mut fmt::Formatter,
397397
(_, render::Unknown) => None,
398398
};
399399
if let Some((cname, root)) = loc {
400-
write!(f, "<a class='primitive' href='{}{}/primitive.{}.html'>",
400+
write!(f, "<a class='primitive' href='{}{}/{}.t.html'>",
401401
root,
402402
cname,
403403
prim.to_url_str())?;
@@ -439,7 +439,7 @@ impl<'a> fmt::Display for HRef<'a> {
439439
match href(self.did) {
440440
Some((url, shortty, fqp)) => {
441441
write!(f, "<a class='{}' href='{}' title='{}'>{}</a>",
442-
shortty, url, fqp.join("::"), self.text)
442+
shortty.css_class(), url, fqp.join("::"), self.text)
443443
}
444444
_ => write!(f, "{}", self.text),
445445
}

src/librustdoc/html/item_type.rs

+49-2
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,13 @@ pub enum ItemType {
4242
AssociatedConst = 18,
4343
}
4444

45+
#[derive(Copy, Eq, PartialEq, Clone)]
46+
pub enum NameSpace {
47+
Type,
48+
Value,
49+
Macro,
50+
}
51+
4552
impl ItemType {
4653
pub fn from_item(item: &clean::Item) -> ItemType {
4754
let inner = match item.inner {
@@ -90,7 +97,7 @@ impl ItemType {
9097
}
9198
}
9299

93-
pub fn to_static_str(&self) -> &'static str {
100+
pub fn css_class(&self) -> &'static str {
94101
match *self {
95102
ItemType::Module => "mod",
96103
ItemType::ExternCrate => "externcrate",
@@ -113,9 +120,49 @@ impl ItemType {
113120
ItemType::AssociatedConst => "associatedconstant",
114121
}
115122
}
123+
124+
pub fn name_space(&self) -> NameSpace {
125+
match *self {
126+
ItemType::Struct |
127+
ItemType::Enum |
128+
ItemType::Module |
129+
ItemType::Typedef |
130+
ItemType::Trait |
131+
ItemType::Primitive |
132+
ItemType::AssociatedType => NameSpace::Type,
133+
134+
ItemType::ExternCrate |
135+
ItemType::Import |
136+
ItemType::Function |
137+
ItemType::Static |
138+
ItemType::Impl |
139+
ItemType::TyMethod |
140+
ItemType::Method |
141+
ItemType::StructField |
142+
ItemType::Variant |
143+
ItemType::Constant |
144+
ItemType::AssociatedConst => NameSpace::Value,
145+
146+
ItemType::Macro => NameSpace::Macro,
147+
}
148+
}
149+
}
150+
151+
pub const NAMESPACE_TYPE: &'static str = "t";
152+
pub const NAMESPACE_VALUE: &'static str = "v";
153+
pub const NAMESPACE_MACRO: &'static str = "m";
154+
155+
impl NameSpace {
156+
pub fn to_static_str(&self) -> &'static str {
157+
match *self {
158+
NameSpace::Type => NAMESPACE_TYPE,
159+
NameSpace::Value => NAMESPACE_VALUE,
160+
NameSpace::Macro => NAMESPACE_MACRO,
161+
}
162+
}
116163
}
117164

118-
impl fmt::Display for ItemType {
165+
impl fmt::Display for NameSpace {
119166
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
120167
self.to_static_str().fmt(f)
121168
}

src/librustdoc/html/layout.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ pub struct Layout {
2424

2525
pub struct Page<'a> {
2626
pub title: &'a str,
27-
pub ty: &'a str,
27+
pub css_class: &'a str,
2828
pub root_path: &'a str,
2929
pub description: &'a str,
3030
pub keywords: &'a str,
@@ -80,7 +80,7 @@ r##"<!DOCTYPE html>
8080
</form>
8181
</nav>
8282
83-
<section id='main' class="content {ty}">{content}</section>
83+
<section id='main' class="content {css_class}">{content}</section>
8484
<section id='search' class="content hidden"></section>
8585
8686
<section class="footer"></section>
@@ -152,7 +152,7 @@ r##"<!DOCTYPE html>
152152
},
153153
content = *t,
154154
root_path = page.root_path,
155-
ty = page.ty,
155+
css_class = page.css_class,
156156
logo = if layout.logo.is_empty() {
157157
"".to_string()
158158
} else {

0 commit comments

Comments
 (0)