Rollup merge of #44397 - GuillaumeGomez:codeblock-color, r=QuietMisdreavus

Codeblock color

<img width="1440" alt="screen shot 2017-09-07 at 21 53 58" src="https://user-images.githubusercontent.com/3050060/30183045-4319108e-9419-11e7-98da-da54952cab37.png">

This screenshot has been generated from:

```rust
/// foo
///
/// ```compile_fail
/// foo();
/// ```
///
/// ```ignore
/// goo();
/// ```
///
/// ```
/// let x = 0;
/// ```
pub fn bar() -> usize { 2 }
```

r? @QuietMisdreavus
cc @rust-lang/docs
This commit is contained in:
Tim Neumann 2017-09-17 13:19:02 +02:00 committed by GitHub
commit 49bc845807
7 changed files with 173 additions and 10 deletions

View File

@ -34,12 +34,18 @@ use syntax_pos::Span;
/// Highlights `src`, returning the HTML output. /// Highlights `src`, returning the HTML output.
pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>, pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>,
extension: Option<&str>) -> String { extension: Option<&str>,
tooltip: Option<(&str, &str)>) -> String {
debug!("highlighting: ================\n{}\n==============", src); debug!("highlighting: ================\n{}\n==============", src);
let sess = parse::ParseSess::new(FilePathMapping::empty()); let sess = parse::ParseSess::new(FilePathMapping::empty());
let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string()); let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());
let mut out = Vec::new(); let mut out = Vec::new();
if let Some((tooltip, class)) = tooltip {
write!(out, "<div class='information'><div class='tooltip {}'>⚠<span \
class='tooltiptext'>{}</span></div></div>",
class, tooltip).unwrap();
}
write_header(class, id, &mut out).unwrap(); write_header(class, id, &mut out).unwrap();
let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm), sess.codemap()); let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm), sess.codemap());

View File

@ -160,10 +160,15 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
fn next(&mut self) -> Option<Self::Item> { fn next(&mut self) -> Option<Self::Item> {
let event = self.inner.next(); let event = self.inner.next();
let compile_fail;
let ignore;
if let Some(Event::Start(Tag::CodeBlock(lang))) = event { 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))); return Some(Event::Start(Tag::CodeBlock(lang)));
} }
compile_fail = parse_result.compile_fail;
ignore = parse_result.ignore;
} else { } else {
return event; return event;
} }
@ -222,11 +227,22 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
url, test_escaped, channel url, test_escaped, channel
)) ))
}); });
let tooltip = if ignore {
Some(("Be careful when using this code, it's not being tested!", "ignore"))
} else if compile_fail {
Some(("This code doesn't compile so be extra careful!", "compile_fail"))
} else {
None
};
s.push_str(&highlight::render_with_highlighting( s.push_str(&highlight::render_with_highlighting(
&text, &text,
Some("rust-example-rendered"), Some(&format!("rust-example-rendered{}",
if ignore { " ignore" }
else if compile_fail { " compile_fail" }
else { "" })),
None, None,
playground_button.as_ref().map(String::as_str))); playground_button.as_ref().map(String::as_str),
tooltip));
Some(Event::Html(s.into())) Some(Event::Html(s.into()))
}) })
} }
@ -556,12 +572,18 @@ pub fn render(w: &mut fmt::Formatter,
let origtext = str::from_utf8(text).unwrap(); let origtext = str::from_utf8(text).unwrap();
let origtext = origtext.trim_left(); let origtext = origtext.trim_left();
debug!("docblock: ==============\n{:?}\n=======", text); debug!("docblock: ==============\n{:?}\n=======", text);
let mut compile_fail = false;
let mut ignore = false;
let rendered = if lang.is_null() || origtext.is_empty() { let rendered = if lang.is_null() || origtext.is_empty() {
false false
} else { } else {
let rlang = (*lang).as_bytes(); let rlang = (*lang).as_bytes();
let rlang = str::from_utf8(rlang).unwrap(); 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, (my_opaque.dfltblk)(ob, orig_text, lang,
opaque as *const hoedown_renderer_data, opaque as *const hoedown_renderer_data,
line); line);
@ -616,11 +638,22 @@ pub fn render(w: &mut fmt::Formatter,
url, test_escaped, channel url, test_escaped, channel
)) ))
}); });
let tooltip = if ignore {
Some(("Be careful when using this code, it's not being tested!", "ignore"))
} else if compile_fail {
Some(("This code doesn't compile so be extra careful!", "compile_fail"))
} else {
None
};
s.push_str(&highlight::render_with_highlighting( s.push_str(&highlight::render_with_highlighting(
&text, &text,
Some("rust-example-rendered"), Some(&format!("rust-example-rendered{}",
if ignore { " ignore" }
else if compile_fail { " compile_fail" }
else { "" })),
None, None,
playground_button.as_ref().map(String::as_str))); playground_button.as_ref().map(String::as_str),
tooltip));
hoedown_buffer_put(ob, s.as_ptr(), s.len()); hoedown_buffer_put(ob, s.as_ptr(), s.len());
}) })
} }

View File

@ -1819,6 +1819,7 @@ fn render_assoc_const_value(item: &clean::Item) -> String {
None, None,
None, None,
None, None,
None,
) )
} }
_ => String::new(), _ => String::new(),
@ -3647,7 +3648,8 @@ impl<'a> fmt::Display for Source<'a> {
write!(fmt, "<span id=\"{0}\">{0:1$}</span>\n", i, cols)?; write!(fmt, "<span id=\"{0}\">{0:1$}</span>\n", i, cols)?;
} }
write!(fmt, "</pre>")?; write!(fmt, "</pre>")?;
write!(fmt, "{}", highlight::render_with_highlighting(s, None, None, None))?; write!(fmt, "{}",
highlight::render_with_highlighting(s, None, None, None, None))?;
Ok(()) Ok(())
} }
} }
@ -3657,6 +3659,7 @@ fn item_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
w.write_str(&highlight::render_with_highlighting(&t.source, w.write_str(&highlight::render_with_highlighting(&t.source,
Some("macro"), Some("macro"),
None, None,
None,
None))?; None))?;
document(w, cx, it) document(w, cx, it)
} }

View File

@ -1296,6 +1296,24 @@
collapseDocs(i_e.previousSibling.childNodes[0]); 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 // Sets the focus on the search bar at the top of the page

View File

@ -612,7 +612,6 @@ pre.rust .question-mark {
font-weight: bold; font-weight: bold;
} }
pre.rust { position: relative; }
a.test-arrow { a.test-arrow {
display: inline-block; display: inline-block;
position: absolute; position: absolute;
@ -813,3 +812,44 @@ span.since {
display: none; 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;
}

View File

@ -202,4 +202,36 @@ a.test-arrow:hover{
:target > code { :target > code {
background: #FDFFD3; background: #FDFFD3;
} }
pre.compile_fail {
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 {
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);
}

View File

@ -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 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, 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 }