Skip to content

Commit a095ee4

Browse files
Add class for codeblocks
1 parent a209539 commit a095ee4

File tree

4 files changed

+79
-9
lines changed

4 files changed

+79
-9
lines changed

src/librustdoc/html/highlight.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
2323
use html::escape::Escape;
2424

25+
use std::collections::HashMap;
2526
use std::fmt::Display;
2627
use std::io;
2728
use std::io::prelude::*;
@@ -34,12 +35,18 @@ use syntax_pos::Span;
3435

3536
/// Highlights `src`, returning the HTML output.
3637
pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>,
37-
extension: Option<&str>) -> String {
38+
extension: Option<&str>,
39+
extras: Option<HashMap<String, String>>) -> String {
3840
debug!("highlighting: ================\n{}\n==============", src);
3941
let sess = parse::ParseSess::new(FilePathMapping::empty());
4042
let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());
4143

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

4552
let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm), sess.codemap());
@@ -389,12 +396,18 @@ impl Class {
389396

390397
fn write_header(class: Option<&str>,
391398
id: Option<&str>,
392-
out: &mut Write)
399+
out: &mut Write,
400+
extras: Option<HashMap<String, String>>)
393401
-> io::Result<()> {
394402
write!(out, "<pre ")?;
395403
if let Some(id) = id {
396404
write!(out, "id='{}' ", id)?;
397405
}
406+
if let Some(extras) = extras {
407+
for (key, value) in &extras {
408+
write!(out, "{}=\"{}\" ", key, value)?;
409+
}
410+
}
398411
write!(out, "class=\"rust {}\">\n", class.unwrap_or(""))
399412
}
400413

src/librustdoc/html/markdown.rs

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

159159
fn next(&mut self) -> Option<Self::Item> {
160160
let event = self.inner.next();
161+
let compile_fail;
162+
let ignore;
161163
if let Some(Event::Start(Tag::CodeBlock(lang))) = event {
162-
if !LangString::parse(&lang).rust {
164+
let parse_result = LangString::parse(&lang);
165+
if !parse_result.rust {
163166
return Some(Event::Start(Tag::CodeBlock(lang)));
164167
}
168+
compile_fail = parse_result.compile_fail;
169+
ignore = parse_result.ignore;
165170
} else {
166171
return event;
167172
}
@@ -220,11 +225,28 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
220225
url, test_escaped, channel
221226
))
222227
});
228+
let title = if ignore {
229+
let mut tmp = HashMap::new();
230+
tmp.insert("title".to_owned(),
231+
"Be careful when using this code, it's not being tested!".to_owned());
232+
Some(tmp)
233+
} else if compile_fail {
234+
let mut tmp = HashMap::new();
235+
tmp.insert("title".to_owned(),
236+
"This code doesn't compile so be extra careful!".to_owned());
237+
Some(tmp)
238+
} else {
239+
None
240+
};
223241
s.push_str(&highlight::render_with_highlighting(
224242
&text,
225-
Some("rust-example-rendered"),
243+
Some(&format!("rust-example-rendered{}",
244+
if ignore { " ignore" }
245+
else if compile_fail { " compile_fail" }
246+
else { "" })),
226247
None,
227-
playground_button.as_ref().map(String::as_str)));
248+
playground_button.as_ref().map(String::as_str),
249+
title));
228250
Some(Event::Html(s.into()))
229251
})
230252
}
@@ -554,12 +576,18 @@ pub fn render(w: &mut fmt::Formatter,
554576
let origtext = str::from_utf8(text).unwrap();
555577
let origtext = origtext.trim_left();
556578
debug!("docblock: ==============\n{:?}\n=======", text);
579+
let mut compile_fail = false;
580+
let mut ignore = false;
581+
557582
let rendered = if lang.is_null() || origtext.is_empty() {
558583
false
559584
} else {
560585
let rlang = (*lang).as_bytes();
561586
let rlang = str::from_utf8(rlang).unwrap();
562-
if !LangString::parse(rlang).rust {
587+
let parse_result = LangString::parse(rlang);
588+
compile_fail = parse_result.compile_fail;
589+
ignore = parse_result.ignore;
590+
if !parse_result.rust {
563591
(my_opaque.dfltblk)(ob, orig_text, lang,
564592
opaque as *const hoedown_renderer_data,
565593
line);
@@ -614,11 +642,30 @@ pub fn render(w: &mut fmt::Formatter,
614642
url, test_escaped, channel
615643
))
616644
});
645+
let title = if ignore {
646+
let mut tmp = HashMap::new();
647+
tmp.insert("title".to_owned(),
648+
"Be careful when using this code, it's not being \
649+
tested!".to_owned());
650+
Some(tmp)
651+
} else if compile_fail {
652+
let mut tmp = HashMap::new();
653+
tmp.insert("title".to_owned(),
654+
"This code doesn't compile so be extra \
655+
careful!".to_owned());
656+
Some(tmp)
657+
} else {
658+
None
659+
};
617660
s.push_str(&highlight::render_with_highlighting(
618661
&text,
619-
Some("rust-example-rendered"),
662+
Some(&format!("rust-example-rendered{}",
663+
if ignore { " ignore" }
664+
else if compile_fail { " compile_fail" }
665+
else { "" })),
620666
None,
621-
playground_button.as_ref().map(String::as_str)));
667+
playground_button.as_ref().map(String::as_str),
668+
title));
622669
hoedown_buffer_put(ob, s.as_ptr(), s.len());
623670
})
624671
}

src/librustdoc/html/render.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -1839,6 +1839,7 @@ fn render_assoc_const_value(item: &clean::Item) -> String {
18391839
None,
18401840
None,
18411841
None,
1842+
None,
18421843
)
18431844
}
18441845
_ => String::new(),
@@ -3678,7 +3679,7 @@ impl<'a> fmt::Display for Source<'a> {
36783679
write!(fmt, "<span id=\"{0}\">{0:1$}</span>\n", i, cols)?;
36793680
}
36803681
write!(fmt, "</pre>")?;
3681-
write!(fmt, "{}", highlight::render_with_highlighting(s, None, None, None))?;
3682+
write!(fmt, "{}", highlight::render_with_highlighting(s, None, None, None, None))?;
36823683
Ok(())
36833684
}
36843685
}
@@ -3688,6 +3689,7 @@ fn item_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
36883689
w.write_str(&highlight::render_with_highlighting(&t.source,
36893690
Some("macro"),
36903691
None,
3692+
None,
36913693
None))?;
36923694
document(w, cx, it)
36933695
}

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

+8
Original file line numberDiff line numberDiff line change
@@ -202,4 +202,12 @@ a.test-arrow:hover{
202202

203203
:target > code {
204204
background: #FDFFD3;
205+
}
206+
207+
pre.compile_fail {
208+
box-shadow: -6px 0 5px -3px #f00;
209+
}
210+
211+
pre.ignore {
212+
box-shadow: -6px 0 5px -3px #ff9200;
205213
}

0 commit comments

Comments
 (0)