rustdoc: factor out function for getting inner html of highlighted source
This commit is contained in:
parent
c0b8c43820
commit
a4e2933c6a
3 changed files with 49 additions and 26 deletions
|
@ -17,22 +17,36 @@ use html::escape::Escape;
|
||||||
|
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::io::prelude::*;
|
use std::io::prelude::*;
|
||||||
use syntax::parse::lexer;
|
use syntax::parse::lexer::{self, Reader};
|
||||||
use syntax::parse::token;
|
use syntax::parse::token;
|
||||||
use syntax::parse;
|
use syntax::parse;
|
||||||
|
|
||||||
/// Highlights some source code, returning the HTML output.
|
/// Highlights `src`, returning the HTML output.
|
||||||
pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
|
pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>) -> String {
|
||||||
debug!("highlighting: ================\n{}\n==============", src);
|
debug!("highlighting: ================\n{}\n==============", src);
|
||||||
let sess = parse::ParseSess::new();
|
let sess = parse::ParseSess::new();
|
||||||
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();
|
||||||
doit(&sess,
|
write_header(class, id, &mut out).unwrap();
|
||||||
lexer::StringReader::new(&sess.span_diagnostic, fm),
|
write_source(&sess,
|
||||||
class,
|
lexer::StringReader::new(&sess.span_diagnostic, fm),
|
||||||
id,
|
&mut out).unwrap();
|
||||||
&mut out).unwrap();
|
write_footer(&mut out).unwrap();
|
||||||
|
String::from_utf8_lossy(&out[..]).into_owned()
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Highlights `src`, returning the HTML output. Returns only the inner html to
|
||||||
|
/// be inserted into an element. C.f., `render_with_highlighting` which includes
|
||||||
|
/// an enclosing `<pre>` block.
|
||||||
|
pub fn render_inner_with_highlighting(src: &str) -> String {
|
||||||
|
let sess = parse::ParseSess::new();
|
||||||
|
let fm = sess.codemap().new_filemap("<stdin>".to_string(), src.to_string());
|
||||||
|
|
||||||
|
let mut out = Vec::new();
|
||||||
|
write_source(&sess,
|
||||||
|
lexer::StringReader::new(&sess.span_diagnostic, fm),
|
||||||
|
&mut out).unwrap();
|
||||||
String::from_utf8_lossy(&out[..]).into_owned()
|
String::from_utf8_lossy(&out[..]).into_owned()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -43,17 +57,10 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
|
||||||
/// it's used. All source code emission is done as slices from the source map,
|
/// it's used. All source code emission is done as slices from the source map,
|
||||||
/// not from the tokens themselves, in order to stay true to the original
|
/// not from the tokens themselves, in order to stay true to the original
|
||||||
/// source.
|
/// source.
|
||||||
fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
|
fn write_source(sess: &parse::ParseSess,
|
||||||
class: Option<&str>, id: Option<&str>,
|
mut lexer: lexer::StringReader,
|
||||||
out: &mut Write) -> io::Result<()> {
|
out: &mut Write)
|
||||||
use syntax::parse::lexer::Reader;
|
-> io::Result<()> {
|
||||||
|
|
||||||
write!(out, "<pre ")?;
|
|
||||||
match id {
|
|
||||||
Some(id) => write!(out, "id='{}' ", id)?,
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
write!(out, "class='rust {}'>\n", class.unwrap_or(""))?;
|
|
||||||
let mut is_attribute = false;
|
let mut is_attribute = false;
|
||||||
let mut is_macro = false;
|
let mut is_macro = false;
|
||||||
let mut is_macro_nonterminal = false;
|
let mut is_macro_nonterminal = false;
|
||||||
|
@ -184,5 +191,21 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_header(class: Option<&str>,
|
||||||
|
id: Option<&str>,
|
||||||
|
out: &mut Write)
|
||||||
|
-> io::Result<()> {
|
||||||
|
write!(out, "<pre ")?;
|
||||||
|
match id {
|
||||||
|
Some(id) => write!(out, "id='{}' ", id)?,
|
||||||
|
None => {}
|
||||||
|
}
|
||||||
|
write!(out, "class='rust {}'>\n", class.unwrap_or(""))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn write_footer(out: &mut Write) -> io::Result<()> {
|
||||||
write!(out, "</pre>\n")
|
write!(out, "</pre>\n")
|
||||||
}
|
}
|
||||||
|
|
|
@ -262,9 +262,9 @@ pub fn render(w: &mut fmt::Formatter, s: &str, print_toc: bool) -> fmt::Result {
|
||||||
&Default::default());
|
&Default::default());
|
||||||
s.push_str(&format!("<span class='rusttest'>{}</span>", Escape(&test)));
|
s.push_str(&format!("<span class='rusttest'>{}</span>", Escape(&test)));
|
||||||
});
|
});
|
||||||
s.push_str(&highlight::highlight(&text,
|
s.push_str(&highlight::render_with_highlighting(&text,
|
||||||
Some("rust-example-rendered"),
|
Some("rust-example-rendered"),
|
||||||
None));
|
None));
|
||||||
let output = CString::new(s).unwrap();
|
let output = CString::new(s).unwrap();
|
||||||
hoedown_buffer_puts(ob, output.as_ptr());
|
hoedown_buffer_puts(ob, output.as_ptr());
|
||||||
})
|
})
|
||||||
|
|
|
@ -2661,16 +2661,16 @@ 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::highlight(s, None, None))?;
|
write!(fmt, "{}", highlight::render_with_highlighting(s, None, None))?;
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn item_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
fn item_macro(w: &mut fmt::Formatter, cx: &Context, it: &clean::Item,
|
||||||
t: &clean::Macro) -> fmt::Result {
|
t: &clean::Macro) -> fmt::Result {
|
||||||
w.write_str(&highlight::highlight(&t.source,
|
w.write_str(&highlight::render_with_highlighting(&t.source,
|
||||||
Some("macro"),
|
Some("macro"),
|
||||||
None))?;
|
None))?;
|
||||||
render_stability_since_raw(w, it.stable_since(), None)?;
|
render_stability_since_raw(w, it.stable_since(), None)?;
|
||||||
document(w, cx, it)
|
document(w, cx, it)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue