Skip to content

Commit 223c043

Browse files
committed
auto merge of rust-lang#16254 : brson/rust/rustdocmeta, r=aturon
This teach rustdoc to add `<meta name="description">` and `<meta name="keywords">` tags to crate docs. Description is important for search engines because they display it as the page description. Keywords are less useful but still generally recommended. This also changes the "stability dashboard" link to just say "stability", because the current link takes up a lot of space. cc rust-lang#12466
2 parents 2a47fa7 + bcdc8fb commit 223c043

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/librustdoc/html/layout.rs

+6-1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ pub struct Page<'a> {
2626
pub title: &'a str,
2727
pub ty: &'a str,
2828
pub root_path: &'a str,
29+
pub description: &'a str,
30+
pub keywords: &'a str
2931
}
3032

3133
pub fn render<T: fmt::Show, S: fmt::Show>(
@@ -38,8 +40,9 @@ r##"<!DOCTYPE html>
3840
<head>
3941
<meta charset="utf-8">
4042
<meta name="viewport" content="width=device-width, initial-scale=1.0">
41-
<meta name="description" content="The {krate} library documentation.">
4243
<meta name="generator" content="rustdoc">
44+
<meta name="description" content="{description}">
45+
<meta name="keywords" content="{keywords}">
4346
4447
<title>{title}</title>
4548
@@ -135,6 +138,8 @@ r##"<!DOCTYPE html>
135138
layout.logo)
136139
},
137140
title = page.title,
141+
description = page.description,
142+
keywords = page.keywords,
138143
favicon = if layout.favicon.len() == 0 {
139144
"".to_string()
140145
} else {

src/librustdoc/html/render.rs

+32-2
Original file line numberDiff line numberDiff line change
@@ -742,10 +742,13 @@ impl<'a> SourceCollector<'a> {
742742
let mut w = BufferedWriter::new(try!(File::create(&cur)));
743743

744744
let title = format!("{} -- source", cur.filename_display());
745+
let desc = format!("Source to the Rust file `{}`.", filename);
745746
let page = layout::Page {
746747
title: title.as_slice(),
747748
ty: "source",
748749
root_path: root_path.as_slice(),
750+
description: desc.as_slice(),
751+
keywords: get_basic_keywords(),
749752
};
750753
try!(layout::render(&mut w as &mut Writer, &self.cx.layout,
751754
&page, &(""), &Source(contents)));
@@ -1072,10 +1075,14 @@ impl Context {
10721075
try!(stability.encode(&mut json::Encoder::new(&mut json_out)));
10731076

10741077
let title = stability.name.clone().append(" - Stability dashboard");
1078+
let desc = format!("API stability overview for the Rust `{}` crate.",
1079+
this.layout.krate);
10751080
let page = layout::Page {
10761081
ty: "mod",
10771082
root_path: this.root_path.as_slice(),
10781083
title: title.as_slice(),
1084+
description: desc.as_slice(),
1085+
keywords: get_basic_keywords(),
10791086
};
10801087
let html_dst = &this.dst.join("stability.html");
10811088
let mut html_out = BufferedWriter::new(try!(File::create(html_dst)));
@@ -1120,10 +1127,25 @@ impl Context {
11201127
title.push_str(it.name.get_ref().as_slice());
11211128
}
11221129
title.push_str(" - Rust");
1130+
let tyname = shortty(it).to_static_str();
1131+
let is_crate = match it.inner {
1132+
clean::ModuleItem(clean::Module { items: _, is_crate: true }) => true,
1133+
_ => false
1134+
};
1135+
let desc = if is_crate {
1136+
format!("API documentation for the Rust `{}` crate.",
1137+
cx.layout.krate)
1138+
} else {
1139+
format!("API documentation for the Rust `{}` {} in crate `{}`.",
1140+
it.name.get_ref(), tyname, cx.layout.krate)
1141+
};
1142+
let keywords = make_item_keywords(it);
11231143
let page = layout::Page {
1124-
ty: shortty(it).to_static_str(),
1144+
ty: tyname,
11251145
root_path: cx.root_path.as_slice(),
11261146
title: title.as_slice(),
1147+
description: desc.as_slice(),
1148+
keywords: keywords.as_slice(),
11271149
};
11281150

11291151
markdown::reset_headers();
@@ -1311,7 +1333,7 @@ impl<'a> fmt::Show for Item<'a> {
13111333
// Write stability dashboard link
13121334
match self.item.inner {
13131335
clean::ModuleItem(ref m) if m.is_crate => {
1314-
try!(write!(fmt, "<a href='stability.html'>[stability dashboard]</a> "));
1336+
try!(write!(fmt, "<a href='stability.html'>[stability]</a> "));
13151337
}
13161338
_ => {}
13171339
};
@@ -2152,3 +2174,11 @@ fn ignore_private_item(it: &clean::Item) -> bool {
21522174
_ => false,
21532175
}
21542176
}
2177+
2178+
fn get_basic_keywords() -> &'static str {
2179+
"rust, rustlang, rust-lang"
2180+
}
2181+
2182+
fn make_item_keywords(it: &clean::Item) -> String {
2183+
format!("{}, {}", get_basic_keywords(), it.name.get_ref())
2184+
}

0 commit comments

Comments
 (0)