Skip to content

Commit 6ecec1b

Browse files
authored
Rollup merge of rust-lang#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
2 parents 45e4306 + 79f888d commit 6ecec1b

File tree

7 files changed

+173
-10
lines changed

7 files changed

+173
-10
lines changed

src/librustdoc/html/highlight.rs

+7-1
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,18 @@ use syntax_pos::Span;
3434

3535
/// Highlights `src`, returning the HTML output.
3636
pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>,
37-
extension: Option<&str>) -> String {
37+
extension: Option<&str>,
38+
tooltip: Option<(&str, &str)>) -> String {
3839
debug!("highlighting: ================\n{}\n==============", src);
3940
let sess = parse::ParseSess::new(FilePathMapping::empty());
4041
let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());
4142

4243
let mut out = Vec::new();
44+
if let Some((tooltip, class)) = tooltip {
45+
write!(out, "<div class='information'><div class='tooltip {}'>⚠<span \
46+
class='tooltiptext'>{}</span></div></div>",
47+
class, tooltip).unwrap();
48+
}
4349
write_header(class, id, &mut out).unwrap();
4450

4551
let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm), sess.codemap());

src/librustdoc/html/markdown.rs

+39-6
Original file line numberDiff line numberDiff line change
@@ -160,10 +160,15 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
160160

161161
fn next(&mut self) -> Option<Self::Item> {
162162
let event = self.inner.next();
163+
let compile_fail;
164+
let ignore;
163165
if let Some(Event::Start(Tag::CodeBlock(lang))) = event {
164-
if !LangString::parse(&lang).rust {
166+
let parse_result = LangString::parse(&lang);
167+
if !parse_result.rust {
165168
return Some(Event::Start(Tag::CodeBlock(lang)));
166169
}
170+
compile_fail = parse_result.compile_fail;
171+
ignore = parse_result.ignore;
167172
} else {
168173
return event;
169174
}
@@ -222,11 +227,22 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
222227
url, test_escaped, channel
223228
))
224229
});
230+
let tooltip = if ignore {
231+
Some(("Be careful when using this code, it's not being tested!", "ignore"))
232+
} else if compile_fail {
233+
Some(("This code doesn't compile so be extra careful!", "compile_fail"))
234+
} else {
235+
None
236+
};
225237
s.push_str(&highlight::render_with_highlighting(
226238
&text,
227-
Some("rust-example-rendered"),
239+
Some(&format!("rust-example-rendered{}",
240+
if ignore { " ignore" }
241+
else if compile_fail { " compile_fail" }
242+
else { "" })),
228243
None,
229-
playground_button.as_ref().map(String::as_str)));
244+
playground_button.as_ref().map(String::as_str),
245+
tooltip));
230246
Some(Event::Html(s.into()))
231247
})
232248
}
@@ -556,12 +572,18 @@ pub fn render(w: &mut fmt::Formatter,
556572
let origtext = str::from_utf8(text).unwrap();
557573
let origtext = origtext.trim_left();
558574
debug!("docblock: ==============\n{:?}\n=======", text);
575+
let mut compile_fail = false;
576+
let mut ignore = false;
577+
559578
let rendered = if lang.is_null() || origtext.is_empty() {
560579
false
561580
} else {
562581
let rlang = (*lang).as_bytes();
563582
let rlang = str::from_utf8(rlang).unwrap();
564-
if !LangString::parse(rlang).rust {
583+
let parse_result = LangString::parse(rlang);
584+
compile_fail = parse_result.compile_fail;
585+
ignore = parse_result.ignore;
586+
if !parse_result.rust {
565587
(my_opaque.dfltblk)(ob, orig_text, lang,
566588
opaque as *const hoedown_renderer_data,
567589
line);
@@ -616,11 +638,22 @@ pub fn render(w: &mut fmt::Formatter,
616638
url, test_escaped, channel
617639
))
618640
});
641+
let tooltip = if ignore {
642+
Some(("Be careful when using this code, it's not being tested!", "ignore"))
643+
} else if compile_fail {
644+
Some(("This code doesn't compile so be extra careful!", "compile_fail"))
645+
} else {
646+
None
647+
};
619648
s.push_str(&highlight::render_with_highlighting(
620649
&text,
621-
Some("rust-example-rendered"),
650+
Some(&format!("rust-example-rendered{}",
651+
if ignore { " ignore" }
652+
else if compile_fail { " compile_fail" }
653+
else { "" })),
622654
None,
623-
playground_button.as_ref().map(String::as_str)));
655+
playground_button.as_ref().map(String::as_str),
656+
tooltip));
624657
hoedown_buffer_put(ob, s.as_ptr(), s.len());
625658
})
626659
}

src/librustdoc/html/render.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -1819,6 +1819,7 @@ fn render_assoc_const_value(item: &clean::Item) -> String {
18191819
None,
18201820
None,
18211821
None,
1822+
None,
18221823
)
18231824
}
18241825
_ => String::new(),
@@ -3647,7 +3648,8 @@ impl<'a> fmt::Display for Source<'a> {
36473648
write!(fmt, "<span id=\"{0}\">{0:1$}</span>\n", i, cols)?;
36483649
}
36493650
write!(fmt, "</pre>")?;
3650-
write!(fmt, "{}", highlight::render_with_highlighting(s, None, None, None))?;
3651+
write!(fmt, "{}",
3652+
highlight::render_with_highlighting(s, None, None, None, None))?;
36513653
Ok(())
36523654
}
36533655
}
@@ -3657,6 +3659,7 @@ fn item_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
36573659
w.write_str(&highlight::render_with_highlighting(&t.source,
36583660
Some("macro"),
36593661
None,
3662+
None,
36603663
None))?;
36613664
document(w, cx, it)
36623665
}

src/librustdoc/html/static/main.js

+18
Original file line numberDiff line numberDiff line change
@@ -1296,6 +1296,24 @@
12961296
collapseDocs(i_e.previousSibling.childNodes[0]);
12971297
});
12981298
});
1299+
1300+
onEach(document.getElementsByClassName('rust-example-rendered'), function(e) {
1301+
if (hasClass(e, 'compile_fail')) {
1302+
e.addEventListener("mouseover", function(event) {
1303+
e.previousElementSibling.childNodes[0].style.color = '#f00';
1304+
});
1305+
e.addEventListener("mouseout", function(event) {
1306+
e.previousElementSibling.childNodes[0].style.color = '';
1307+
});
1308+
} else if (hasClass(e, 'ignore')) {
1309+
e.addEventListener("mouseover", function(event) {
1310+
e.previousElementSibling.childNodes[0].style.color = '#ff9200';
1311+
});
1312+
e.addEventListener("mouseout", function(event) {
1313+
e.previousElementSibling.childNodes[0].style.color = '';
1314+
});
1315+
}
1316+
});
12991317
}());
13001318

13011319
// Sets the focus on the search bar at the top of the page

src/librustdoc/html/static/rustdoc.css

+41-1
Original file line numberDiff line numberDiff line change
@@ -612,7 +612,6 @@ pre.rust .question-mark {
612612
font-weight: bold;
613613
}
614614

615-
pre.rust { position: relative; }
616615
a.test-arrow {
617616
display: inline-block;
618617
position: absolute;
@@ -813,3 +812,44 @@ span.since {
813812
display: none;
814813
}
815814
}
815+
816+
.information {
817+
position: absolute;
818+
left: -1px;
819+
margin-top: 7px;
820+
}
821+
822+
.tooltip {
823+
position: relative;
824+
display: inline-block;
825+
cursor: pointer;
826+
}
827+
828+
.tooltip .tooltiptext {
829+
width: 120px;
830+
display: none;
831+
background-color: black;
832+
color: #fff;
833+
text-align: center;
834+
padding: 5px 3px;
835+
border-radius: 6px;
836+
margin-left: 5px;
837+
top: -5px;
838+
left: 105%;
839+
z-index: 1;
840+
}
841+
842+
.tooltip:hover .tooltiptext {
843+
display: inline;
844+
}
845+
846+
.tooltip .tooltiptext::after {
847+
content: " ";
848+
position: absolute;
849+
top: 50%;
850+
left: 11px;
851+
margin-top: -5px;
852+
border-width: 5px;
853+
border-style: solid;
854+
border-color: transparent black transparent transparent;
855+
}

src/librustdoc/html/static/styles/main.css

+33-1
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,36 @@ a.test-arrow:hover{
202202

203203
:target > code {
204204
background: #FDFFD3;
205-
}
205+
}
206+
207+
pre.compile_fail {
208+
border-left: 2px solid rgba(255,0,0,.4);
209+
}
210+
211+
pre.compile_fail:hover, .information:hover + pre.compile_fail {
212+
border-left: 2px solid #f00;
213+
}
214+
215+
pre.ignore {
216+
border-left: 2px solid rgba(255,142,0,.4);
217+
}
218+
219+
pre.ignore:hover, .information:hover + pre.ignore {
220+
border-left: 2px solid #ff9200;
221+
}
222+
223+
.tooltip.compile_fail {
224+
color: rgba(255,0,0,.3);
225+
}
226+
227+
.information > .compile_fail:hover {
228+
color: #f00;
229+
}
230+
231+
.tooltip.ignore {
232+
color: rgba(255,142,0,.3);
233+
}
234+
235+
.information > .ignore:hover {
236+
color: rgba(255,142,0,1);
237+
}

src/test/rustdoc/codeblock-title.rs

+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2015 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![crate_name = "foo"]
12+
13+
// ignore-tidy-linelength
14+
15+
// @has foo/fn.bar.html '//*[@class="tooltip compile_fail"]/span' "This code doesn't compile so be extra careful!"
16+
// @has foo/fn.bar.html '//*[@class="tooltip ignore"]/span' "Be careful when using this code, it's not being tested!"
17+
18+
/// foo
19+
///
20+
/// ```compile_fail
21+
/// foo();
22+
/// ```
23+
///
24+
/// ```ignore (tidy)
25+
/// goo();
26+
/// ```
27+
///
28+
/// ```
29+
/// let x = 0;
30+
/// ```
31+
pub fn bar() -> usize { 2 }

0 commit comments

Comments
 (0)