Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use rustdoc's body classes on the rustdoc-container #273

Merged
merged 1 commit into from
Dec 18, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 18 additions & 3 deletions src/utils/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,16 @@ use html5ever::rcdom::{RcDom, NodeData, Handle};
use html5ever::driver::{parse_document, ParseOpts};
use html5ever::tendril::TendrilSink;

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

let (head, body) = extract_from_rcdom(&dom)?;
let class = extract_class(&body);

Ok((stringify(head), stringify(body)))
Ok((stringify(head), stringify(body), class))
}

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

String::from_utf8(vec).expect("html5ever returned non-utf8 data")
}

fn extract_class(node: &Handle) -> String {
match node.data {
NodeData::Element { ref attrs, .. } => {
let attrs = attrs.borrow();

attrs.iter()
.find(|a| &a.name.local == "class")
.map_or(String::new(), |a| a.value.to_string())
}
_ => String::new()
}
}
13 changes: 12 additions & 1 deletion src/web/rustdoc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use utils;
struct RustdocPage {
pub head: String,
pub body: String,
pub body_class: String,
pub name: String,
pub full: String,
pub version: String,
Expand All @@ -37,6 +38,7 @@ impl Default for RustdocPage {
RustdocPage {
head: String::new(),
body: String::new(),
body_class: String::new(),
name: String::new(),
full: String::new(),
version: String::new(),
Expand All @@ -52,6 +54,7 @@ impl ToJson for RustdocPage {
let mut m: BTreeMap<String, Json> = BTreeMap::new();
m.insert("rustdoc_head".to_string(), self.head.to_json());
m.insert("rustdoc_body".to_string(), self.body.to_json());
m.insert("rustdoc_body_class".to_string(), self.body_class.to_json());
m.insert("rustdoc_full".to_string(), self.full.to_json());
m.insert("rustdoc_status".to_string(), true.to_json());
m.insert("name".to_string(), self.name.to_json());
Expand Down Expand Up @@ -160,10 +163,18 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult<Response> {

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

let (head, body) = ctry!(utils::extract_head_and_body(&file_content));
let (head, body, mut body_class) = ctry!(utils::extract_head_and_body(&file_content));
content.head = head;
content.body = body;

if body_class.is_empty() {
body_class = "rustdoc container-rustdoc".to_string();
} else {
// rustdoc adds its own "rustdoc" class to the body
body_class.push_str(" container-rustdoc");
}
content.body_class = body_class;

content.full = file_content;
let crate_details = cexpect!(CrateDetails::new(&conn, &name, &version));
let latest_version = latest_version(&crate_details.versions, &version);
Expand Down
2 changes: 1 addition & 1 deletion templates/rustdoc.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
</head>
<body>
{{> navigation_rustdoc}}
<div class="rustdoc container-rustdoc">
<div class="{{content.rustdoc_body_class}}">
{{{content.rustdoc_body}}}
</div>
</body>
Expand Down