From a095ee48d5d2287efa663ec196a017c69952b0b3 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 7 Sep 2017 00:08:39 +0200 Subject: [PATCH 1/3] Add class for codeblocks --- src/librustdoc/html/highlight.rs | 17 ++++++- src/librustdoc/html/markdown.rs | 59 +++++++++++++++++++--- src/librustdoc/html/render.rs | 4 +- src/librustdoc/html/static/styles/main.css | 8 +++ 4 files changed, 79 insertions(+), 9 deletions(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index e6b236deac4ee..cc1da35a99757 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -22,6 +22,7 @@ use html::escape::Escape; +use std::collections::HashMap; use std::fmt::Display; use std::io; use std::io::prelude::*; @@ -34,12 +35,18 @@ use syntax_pos::Span; /// Highlights `src`, returning the HTML output. pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>, - extension: Option<&str>) -> String { + extension: Option<&str>, + extras: Option>) -> String { debug!("highlighting: ================\n{}\n==============", src); let sess = parse::ParseSess::new(FilePathMapping::empty()); let fm = sess.codemap().new_filemap("".to_string(), src.to_string()); let mut out = Vec::new(); + if let Some((tooltip, class)) = tooltip { + write!(out, "
{}
", + class, tooltip).unwrap(); + } write_header(class, id, &mut out).unwrap(); let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm), sess.codemap()); @@ -389,12 +396,18 @@ impl Class { fn write_header(class: Option<&str>, id: Option<&str>, - out: &mut Write) + out: &mut Write, + extras: Option>) -> io::Result<()> { write!(out, "
\n", class.unwrap_or(""))
 }
 
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index 2d14c02bf8a59..d9fb35c4c269b 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -158,10 +158,15 @@ impl<'a, I: Iterator>> Iterator for CodeBlocks<'a, I> {
 
     fn next(&mut self) -> Option {
         let event = self.inner.next();
+        let compile_fail;
+        let ignore;
         if let Some(Event::Start(Tag::CodeBlock(lang))) = event {
-            if !LangString::parse(&lang).rust {
+            let parse_result = LangString::parse(&lang);
+            if !parse_result.rust {
                 return Some(Event::Start(Tag::CodeBlock(lang)));
             }
+            compile_fail = parse_result.compile_fail;
+            ignore = parse_result.ignore;
         } else {
             return event;
         }
@@ -220,11 +225,28 @@ impl<'a, I: Iterator>> Iterator for CodeBlocks<'a, I> {
                     url, test_escaped, channel
                 ))
             });
+            let title = if ignore {
+                let mut tmp = HashMap::new();
+                tmp.insert("title".to_owned(),
+                           "Be careful when using this code, it's not being tested!".to_owned());
+                Some(tmp)
+            } else if compile_fail {
+                let mut tmp = HashMap::new();
+                tmp.insert("title".to_owned(),
+                           "This code doesn't compile so be extra careful!".to_owned());
+                Some(tmp)
+            } else {
+                None
+            };
             s.push_str(&highlight::render_with_highlighting(
                         &text,
-                        Some("rust-example-rendered"),
+                        Some(&format!("rust-example-rendered{}",
+                                      if ignore { " ignore" }
+                                      else if compile_fail { " compile_fail" }
+                                      else { "" })),
                         None,
-                        playground_button.as_ref().map(String::as_str)));
+                        playground_button.as_ref().map(String::as_str),
+                        title));
             Some(Event::Html(s.into()))
         })
     }
@@ -554,12 +576,18 @@ pub fn render(w: &mut fmt::Formatter,
             let origtext = str::from_utf8(text).unwrap();
             let origtext = origtext.trim_left();
             debug!("docblock: ==============\n{:?}\n=======", text);
+            let mut compile_fail = false;
+            let mut ignore = false;
+
             let rendered = if lang.is_null() || origtext.is_empty() {
                 false
             } else {
                 let rlang = (*lang).as_bytes();
                 let rlang = str::from_utf8(rlang).unwrap();
-                if !LangString::parse(rlang).rust {
+                let parse_result = LangString::parse(rlang);
+                compile_fail = parse_result.compile_fail;
+                ignore = parse_result.ignore;
+                if !parse_result.rust {
                     (my_opaque.dfltblk)(ob, orig_text, lang,
                                         opaque as *const hoedown_renderer_data,
                                         line);
@@ -614,11 +642,30 @@ pub fn render(w: &mut fmt::Formatter,
                         url, test_escaped, channel
                     ))
                 });
+                let title = if ignore {
+                    let mut tmp = HashMap::new();
+                    tmp.insert("title".to_owned(),
+                               "Be careful when using this code, it's not being \
+                                tested!".to_owned());
+                    Some(tmp)
+                } else if compile_fail {
+                    let mut tmp = HashMap::new();
+                    tmp.insert("title".to_owned(),
+                               "This code doesn't compile so be extra \
+                                careful!".to_owned());
+                    Some(tmp)
+                } else {
+                    None
+                };
                 s.push_str(&highlight::render_with_highlighting(
                                &text,
-                               Some("rust-example-rendered"),
+                               Some(&format!("rust-example-rendered{}",
+                                             if ignore { " ignore" }
+                                             else if compile_fail { " compile_fail" }
+                                             else { "" })),
                                None,
-                               playground_button.as_ref().map(String::as_str)));
+                               playground_button.as_ref().map(String::as_str),
+                               title));
                 hoedown_buffer_put(ob, s.as_ptr(), s.len());
             })
         }
diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs
index 46bb119cf9c9d..f911ddb4d4f55 100644
--- a/src/librustdoc/html/render.rs
+++ b/src/librustdoc/html/render.rs
@@ -1839,6 +1839,7 @@ fn render_assoc_const_value(item: &clean::Item) -> String {
                 None,
                 None,
                 None,
+                None,
             )
         }
         _ => String::new(),
@@ -3678,7 +3679,7 @@ impl<'a> fmt::Display for Source<'a> {
             write!(fmt, "{0:1$}\n", i, cols)?;
         }
         write!(fmt, "
")?; - write!(fmt, "{}", highlight::render_with_highlighting(s, None, None, None))?; + write!(fmt, "{}", highlight::render_with_highlighting(s, None, None, None, None))?; Ok(()) } } @@ -3688,6 +3689,7 @@ fn item_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item, w.write_str(&highlight::render_with_highlighting(&t.source, Some("macro"), None, + None, None))?; document(w, cx, it) } diff --git a/src/librustdoc/html/static/styles/main.css b/src/librustdoc/html/static/styles/main.css index c5f4272b932fc..18b40b3d60c17 100644 --- a/src/librustdoc/html/static/styles/main.css +++ b/src/librustdoc/html/static/styles/main.css @@ -202,4 +202,12 @@ a.test-arrow:hover{ :target > code { background: #FDFFD3; +} + +pre.compile_fail { + box-shadow: -6px 0 5px -3px #f00; +} + +pre.ignore { + box-shadow: -6px 0 5px -3px #ff9200; } \ No newline at end of file From 9c12e5d4e8c1211783e2c9e6f6cc1fd0091b8724 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Thu, 7 Sep 2017 22:06:40 +0200 Subject: [PATCH 2/3] add test --- src/test/rustdoc/codeblock-title.rs | 31 +++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) create mode 100644 src/test/rustdoc/codeblock-title.rs diff --git a/src/test/rustdoc/codeblock-title.rs b/src/test/rustdoc/codeblock-title.rs new file mode 100644 index 0000000000000..accefd6b65f28 --- /dev/null +++ b/src/test/rustdoc/codeblock-title.rs @@ -0,0 +1,31 @@ +// Copyright 2015 The Rust Project Developers. See the COPYRIGHT +// file at the top-level directory of this distribution and at +// http://rust-lang.org/COPYRIGHT. +// +// Licensed under the Apache License, Version 2.0 or the MIT license +// , at your +// option. This file may not be copied, modified, or distributed +// except according to those terms. + +#![crate_name = "foo"] + +// ignore-tidy-linelength + +// @has foo/fn.bar.html '//*[@class="tooltip compile_fail"]/span' "This code doesn't compile so be extra careful!" +// @has foo/fn.bar.html '//*[@class="tooltip ignore"]/span' "Be careful when using this code, it's not being tested!" + +/// foo +/// +/// ```compile_fail +/// foo(); +/// ``` +/// +/// ```ignore (tidy) +/// goo(); +/// ``` +/// +/// ``` +/// let x = 0; +/// ``` +pub fn bar() -> usize { 2 } From 79f888da6810e9e38918f524b0882b8eaf40e5a1 Mon Sep 17 00:00:00 2001 From: Guillaume Gomez Date: Sat, 9 Sep 2017 15:33:57 +0200 Subject: [PATCH 3/3] Add arrow and improve display --- src/librustdoc/html/highlight.rs | 11 ++---- src/librustdoc/html/markdown.rs | 30 +++++----------- src/librustdoc/html/render.rs | 3 +- src/librustdoc/html/static/main.js | 18 ++++++++++ src/librustdoc/html/static/rustdoc.css | 42 +++++++++++++++++++++- src/librustdoc/html/static/styles/main.css | 30 ++++++++++++++-- 6 files changed, 98 insertions(+), 36 deletions(-) diff --git a/src/librustdoc/html/highlight.rs b/src/librustdoc/html/highlight.rs index cc1da35a99757..081f950e40db2 100644 --- a/src/librustdoc/html/highlight.rs +++ b/src/librustdoc/html/highlight.rs @@ -22,7 +22,6 @@ use html::escape::Escape; -use std::collections::HashMap; use std::fmt::Display; use std::io; use std::io::prelude::*; @@ -36,7 +35,7 @@ use syntax_pos::Span; /// Highlights `src`, returning the HTML output. pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>, extension: Option<&str>, - extras: Option>) -> String { + tooltip: Option<(&str, &str)>) -> String { debug!("highlighting: ================\n{}\n==============", src); let sess = parse::ParseSess::new(FilePathMapping::empty()); let fm = sess.codemap().new_filemap("".to_string(), src.to_string()); @@ -396,18 +395,12 @@ impl Class { fn write_header(class: Option<&str>, id: Option<&str>, - out: &mut Write, - extras: Option>) + out: &mut Write) -> io::Result<()> { write!(out, "
\n", class.unwrap_or(""))
 }
 
diff --git a/src/librustdoc/html/markdown.rs b/src/librustdoc/html/markdown.rs
index d9fb35c4c269b..3b03acf99081c 100644
--- a/src/librustdoc/html/markdown.rs
+++ b/src/librustdoc/html/markdown.rs
@@ -225,16 +225,10 @@ impl<'a, I: Iterator>> Iterator for CodeBlocks<'a, I> {
                     url, test_escaped, channel
                 ))
             });
-            let title = if ignore {
-                let mut tmp = HashMap::new();
-                tmp.insert("title".to_owned(),
-                           "Be careful when using this code, it's not being tested!".to_owned());
-                Some(tmp)
+            let tooltip = if ignore {
+                Some(("Be careful when using this code, it's not being tested!", "ignore"))
             } else if compile_fail {
-                let mut tmp = HashMap::new();
-                tmp.insert("title".to_owned(),
-                           "This code doesn't compile so be extra careful!".to_owned());
-                Some(tmp)
+                Some(("This code doesn't compile so be extra careful!", "compile_fail"))
             } else {
                 None
             };
@@ -246,7 +240,7 @@ impl<'a, I: Iterator>> Iterator for CodeBlocks<'a, I> {
                                       else { "" })),
                         None,
                         playground_button.as_ref().map(String::as_str),
-                        title));
+                        tooltip));
             Some(Event::Html(s.into()))
         })
     }
@@ -642,18 +636,10 @@ pub fn render(w: &mut fmt::Formatter,
                         url, test_escaped, channel
                     ))
                 });
-                let title = if ignore {
-                    let mut tmp = HashMap::new();
-                    tmp.insert("title".to_owned(),
-                               "Be careful when using this code, it's not being \
-                                tested!".to_owned());
-                    Some(tmp)
+                let tooltip = if ignore {
+                    Some(("Be careful when using this code, it's not being tested!", "ignore"))
                 } else if compile_fail {
-                    let mut tmp = HashMap::new();
-                    tmp.insert("title".to_owned(),
-                               "This code doesn't compile so be extra \
-                                careful!".to_owned());
-                    Some(tmp)
+                    Some(("This code doesn't compile so be extra careful!", "compile_fail"))
                 } else {
                     None
                 };
@@ -665,7 +651,7 @@ pub fn render(w: &mut fmt::Formatter,
                                              else { "" })),
                                None,
                                playground_button.as_ref().map(String::as_str),
-                               title));
+                               tooltip));
                 hoedown_buffer_put(ob, s.as_ptr(), s.len());
             })
         }
diff --git a/src/librustdoc/html/render.rs b/src/librustdoc/html/render.rs
index f911ddb4d4f55..993462a8d444a 100644
--- a/src/librustdoc/html/render.rs
+++ b/src/librustdoc/html/render.rs
@@ -3679,7 +3679,8 @@ impl<'a> fmt::Display for Source<'a> {
             write!(fmt, "{0:1$}\n", i, cols)?;
         }
         write!(fmt, "
")?; - write!(fmt, "{}", highlight::render_with_highlighting(s, None, None, None, None))?; + write!(fmt, "{}", + highlight::render_with_highlighting(s, None, None, None, None))?; Ok(()) } } diff --git a/src/librustdoc/html/static/main.js b/src/librustdoc/html/static/main.js index 8ec9cd8660a80..da4430d8a1539 100644 --- a/src/librustdoc/html/static/main.js +++ b/src/librustdoc/html/static/main.js @@ -1296,6 +1296,24 @@ collapseDocs(i_e.previousSibling.childNodes[0]); }); }); + + onEach(document.getElementsByClassName('rust-example-rendered'), function(e) { + if (hasClass(e, 'compile_fail')) { + e.addEventListener("mouseover", function(event) { + e.previousElementSibling.childNodes[0].style.color = '#f00'; + }); + e.addEventListener("mouseout", function(event) { + e.previousElementSibling.childNodes[0].style.color = ''; + }); + } else if (hasClass(e, 'ignore')) { + e.addEventListener("mouseover", function(event) { + e.previousElementSibling.childNodes[0].style.color = '#ff9200'; + }); + e.addEventListener("mouseout", function(event) { + e.previousElementSibling.childNodes[0].style.color = ''; + }); + } + }); }()); // Sets the focus on the search bar at the top of the page diff --git a/src/librustdoc/html/static/rustdoc.css b/src/librustdoc/html/static/rustdoc.css index ca55d0e5d2a8e..c15051376bf27 100644 --- a/src/librustdoc/html/static/rustdoc.css +++ b/src/librustdoc/html/static/rustdoc.css @@ -612,7 +612,6 @@ pre.rust .question-mark { font-weight: bold; } -pre.rust { position: relative; } a.test-arrow { display: inline-block; position: absolute; @@ -813,3 +812,44 @@ span.since { display: none; } } + +.information { + position: absolute; + left: -1px; + margin-top: 7px; +} + +.tooltip { + position: relative; + display: inline-block; + cursor: pointer; +} + +.tooltip .tooltiptext { + width: 120px; + display: none; + background-color: black; + color: #fff; + text-align: center; + padding: 5px 3px; + border-radius: 6px; + margin-left: 5px; + top: -5px; + left: 105%; + z-index: 1; +} + +.tooltip:hover .tooltiptext { + display: inline; +} + +.tooltip .tooltiptext::after { + content: " "; + position: absolute; + top: 50%; + left: 11px; + margin-top: -5px; + border-width: 5px; + border-style: solid; + border-color: transparent black transparent transparent; +} diff --git a/src/librustdoc/html/static/styles/main.css b/src/librustdoc/html/static/styles/main.css index 18b40b3d60c17..42d0ec704f45f 100644 --- a/src/librustdoc/html/static/styles/main.css +++ b/src/librustdoc/html/static/styles/main.css @@ -205,9 +205,33 @@ a.test-arrow:hover{ } pre.compile_fail { - box-shadow: -6px 0 5px -3px #f00; + border-left: 2px solid rgba(255,0,0,.4); +} + +pre.compile_fail:hover, .information:hover + pre.compile_fail { + border-left: 2px solid #f00; } pre.ignore { - box-shadow: -6px 0 5px -3px #ff9200; -} \ No newline at end of file + border-left: 2px solid rgba(255,142,0,.4); +} + +pre.ignore:hover, .information:hover + pre.ignore { + border-left: 2px solid #ff9200; +} + +.tooltip.compile_fail { + color: rgba(255,0,0,.3); +} + +.information > .compile_fail:hover { + color: #f00; +} + +.tooltip.ignore { + color: rgba(255,142,0,.3); +} + +.information > .ignore:hover { + color: rgba(255,142,0,1); +}