From 5c389cabe8629483f00261dfb455dd842dc05940 Mon Sep 17 00:00:00 2001 From: QuietMisdreavus Date: Thu, 6 Dec 2018 09:48:21 -0600 Subject: [PATCH] use rustdoc's body classes on the rustdoc-container --- src/utils/html.rs | 21 ++++++++++++++++++--- src/web/rustdoc.rs | 13 ++++++++++++- templates/rustdoc.hbs | 2 +- 3 files changed, 31 insertions(+), 5 deletions(-) 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 = 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()); @@ -160,10 +163,18 @@ pub fn rustdoc_html_server_handler(req: &mut Request) -> IronResult { 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); diff --git a/templates/rustdoc.hbs b/templates/rustdoc.hbs index 17737e148..600825d46 100644 --- a/templates/rustdoc.hbs +++ b/templates/rustdoc.hbs @@ -10,7 +10,7 @@ {{> navigation_rustdoc}} -
+
{{{content.rustdoc_body}}}