1
Fork 0

Add class for codeblocks

This commit is contained in:
Guillaume Gomez 2017-09-07 00:08:39 +02:00
parent a209539060
commit a095ee48d5
4 changed files with 79 additions and 9 deletions

View file

@ -22,6 +22,7 @@
use html::escape::Escape; use html::escape::Escape;
use std::collections::HashMap;
use std::fmt::Display; use std::fmt::Display;
use std::io; use std::io;
use std::io::prelude::*; use std::io::prelude::*;
@ -34,12 +35,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>,
extras: Option<HashMap<String, String>>) -> 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());
@ -389,12 +396,18 @@ impl Class {
fn write_header(class: Option<&str>, fn write_header(class: Option<&str>,
id: Option<&str>, id: Option<&str>,
out: &mut Write) out: &mut Write,
extras: Option<HashMap<String, String>>)
-> io::Result<()> { -> io::Result<()> {
write!(out, "<pre ")?; write!(out, "<pre ")?;
if let Some(id) = id { if let Some(id) = id {
write!(out, "id='{}' ", id)?; write!(out, "id='{}' ", id)?;
} }
if let Some(extras) = extras {
for (key, value) in &extras {
write!(out, "{}=\"{}\" ", key, value)?;
}
}
write!(out, "class=\"rust {}\">\n", class.unwrap_or("")) write!(out, "class=\"rust {}\">\n", class.unwrap_or(""))
} }

View file

@ -158,10 +158,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;
} }
@ -220,11 +225,28 @@ impl<'a, I: Iterator<Item = Event<'a>>> Iterator for CodeBlocks<'a, I> {
url, test_escaped, channel 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( 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),
title));
Some(Event::Html(s.into())) 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 = 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);
@ -614,11 +642,30 @@ pub fn render(w: &mut fmt::Formatter,
url, test_escaped, channel 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( 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),
title));
hoedown_buffer_put(ob, s.as_ptr(), s.len()); hoedown_buffer_put(ob, s.as_ptr(), s.len());
}) })
} }

View file

@ -1839,6 +1839,7 @@ fn render_assoc_const_value(item: &clean::Item) -> String {
None, None,
None, None,
None, None,
None,
) )
} }
_ => String::new(), _ => String::new(),
@ -3678,7 +3679,7 @@ 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(())
} }
} }
@ -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, 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

@ -203,3 +203,11 @@ a.test-arrow:hover{
:target > code { :target > code {
background: #FDFFD3; background: #FDFFD3;
} }
pre.compile_fail {
box-shadow: -6px 0 5px -3px #f00;
}
pre.ignore {
box-shadow: -6px 0 5px -3px #ff9200;
}