diff --git a/src/utils/html.rs b/src/utils/html.rs index 518e08b22..a7fe2b41a 100644 --- a/src/utils/html.rs +++ b/src/utils/html.rs @@ -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 `
` and `` tags from an HTML document. -pub fn extract_head_and_body(html: &str) -> Result<(String, String)> { +/// Extracts the contents of the `` and `` tags from an HTML document, as well as the +/// classes on the `` 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)> { @@ -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() + } +} diff --git a/src/web/rustdoc.rs b/src/web/rustdoc.rs index 5873b1f46..033e0c80d 100644 --- a/src/web/rustdoc.rs +++ b/src/web/rustdoc.rs @@ -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, @@ -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(), @@ -52,6 +54,7 @@ impl ToJson for RustdocPage { let mut m: BTreeMap