Skip to content

Commit d066faa

Browse files
authored
Merge pull request #273 from QuietMisdreavus/that-bodys-got-class
use rustdoc's body classes on the rustdoc-container
2 parents a57c49e + 5c389ca commit d066faa

File tree

3 files changed

+31
-5
lines changed

3 files changed

+31
-5
lines changed

src/utils/html.rs

+18-3
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,16 @@ use html5ever::rcdom::{RcDom, NodeData, Handle};
66
use html5ever::driver::{parse_document, ParseOpts};
77
use html5ever::tendril::TendrilSink;
88

9-
/// Extracts the contents of the `<head>` and `<body>` tags from an HTML document.
10-
pub fn extract_head_and_body(html: &str) -> Result<(String, String)> {
9+
/// Extracts the contents of the `<head>` and `<body>` tags from an HTML document, as well as the
10+
/// classes on the `<body>` tag, if any.
11+
pub fn extract_head_and_body(html: &str) -> Result<(String, String, String)> {
1112
let parser = parse_document(RcDom::default(), ParseOpts::default());
1213
let dom = parser.one(html);
1314

1415
let (head, body) = extract_from_rcdom(&dom)?;
16+
let class = extract_class(&body);
1517

16-
Ok((stringify(head), stringify(body)))
18+
Ok((stringify(head), stringify(body), class))
1719
}
1820

1921
fn extract_from_rcdom(dom: &RcDom) -> Result<(Handle, Handle)> {
@@ -57,3 +59,16 @@ fn stringify(node: Handle) -> String {
5759

5860
String::from_utf8(vec).expect("html5ever returned non-utf8 data")
5961
}
62+
63+
fn extract_class(node: &Handle) -> String {
64+
match node.data {
65+
NodeData::Element { ref attrs, .. } => {
66+
let attrs = attrs.borrow();
67+
68+
attrs.iter()
69+
.find(|a| &a.name.local == "class")
70+
.map_or(String::new(), |a| a.value.to_string())
71+
}
72+
_ => String::new()
73+
}
74+
}

src/web/rustdoc.rs

+12-1
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ use utils;
2424
struct RustdocPage {
2525
pub head: String,
2626
pub body: String,
27+
pub body_class: String,
2728
pub name: String,
2829
pub full: String,
2930
pub version: String,
@@ -37,6 +38,7 @@ impl Default for RustdocPage {
3738
RustdocPage {
3839
head: String::new(),
3940
body: String::new(),
41+
body_class: String::new(),
4042
name: String::new(),
4143
full: String::new(),
4244
version: String::new(),
@@ -52,6 +54,7 @@ impl ToJson for RustdocPage {
5254
let mut m: BTreeMap<String, Json> = BTreeMap::new();
5355
m.insert("rustdoc_head".to_string(), self.head.to_json());
5456
m.insert("rustdoc_body".to_string(), self.body.to_json());
57+
m.insert("rustdoc_body_class".to_string(), self.body_class.to_json());
5558
m.insert("rustdoc_full".to_string(), self.full.to_json());
5659
m.insert("rustdoc_status".to_string(), true.to_json());
5760
m.insert("name".to_string(), self.name.to_json());
@@ -160,10 +163,18 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {
160163

161164
let file_content = ctry!(String::from_utf8(file.content));
162165

163-
let (head, body) = ctry!(utils::extract_head_and_body(&file_content));
166+
let (head, body, mut body_class) = ctry!(utils::extract_head_and_body(&file_content));
164167
content.head = head;
165168
content.body = body;
166169

170+
if body_class.is_empty() {
171+
body_class = "rustdoc container-rustdoc".to_string();
172+
} else {
173+
// rustdoc adds its own "rustdoc" class to the body
174+
body_class.push_str(" container-rustdoc");
175+
}
176+
content.body_class = body_class;
177+
167178
content.full = file_content;
168179
let crate_details = cexpect!(CrateDetails::new(&conn, &name, &version));
169180
let latest_version = latest_version(&crate_details.versions, &version);

templates/rustdoc.hbs

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
</head>
1111
<body>
1212
{{> navigation_rustdoc}}
13-
<div class="rustdoc container-rustdoc">
13+
<div class="{{content.rustdoc_body_class}}">
1414
{{{content.rustdoc_body}}}
1515
</div>
1616
</body>

0 commit comments

Comments
 (0)