//! Basic syntax highlighting functionality.
//!
-//! This module uses libsyntax's lexer to provide token-based highlighting for
+//! This module uses librustc_ast's lexer to provide token-based highlighting for
//! the HTML documentation generated by rustdoc.
//!
//! Use the `render_with_highlighting` to highlight some rust code.
use std::io;
use std::io::prelude::*;
+use rustc_ast::token::{self, Token};
use rustc_parse::lexer;
+use rustc_session::parse::ParseSess;
use rustc_span::source_map::SourceMap;
use rustc_span::symbol::{kw, sym};
use rustc_span::{FileName, Span};
-use syntax::sess::ParseSess;
-use syntax::token::{self, Token};
/// Highlights `src`, returning the HTML output.
pub fn render_with_highlighting(
src: &str,
class: Option<&str>,
- extension: Option<&str>,
+ playground_button: Option<&str>,
tooltip: Option<(&str, &str)>,
) -> String {
debug!("highlighting: ================\n{}\n==============", src);
}
let sess = ParseSess::with_silent_emitter();
- let fm = sess
+ let sf = sess
.source_map()
.new_source_file(FileName::Custom(String::from("rustdoc-highlighting")), src.to_owned());
let highlight_result = rustc_driver::catch_fatal_errors(|| {
- let lexer = lexer::StringReader::new(&sess, fm, None);
+ let lexer = lexer::StringReader::new(&sess, sf, None);
let mut classifier = Classifier::new(lexer, sess.source_map());
let mut highlighted_source = vec![];
Ok(highlighted_source) => {
write_header(class, &mut out).unwrap();
write!(out, "{}", highlighted_source).unwrap();
- if let Some(extension) = extension {
- write!(out, "{}", extension).unwrap();
- }
- write_footer(&mut out).unwrap();
+ write_footer(&mut out, playground_button).unwrap();
}
Err(()) => {
// If errors are encountered while trying to highlight, just emit
fn string<T: Display>(&mut self, text: T, klass: Class) -> io::Result<()>;
}
-// Implement `Writer` for anthing that can be written to, this just implements
+// Implement `Writer` for anything that can be written to, this just implements
// the default rustdoc behaviour.
impl<U: Write> Writer for U {
fn string<T: Display>(&mut self, text: T, klass: Class) -> io::Result<()> {
write!(out, "<div class=\"example-wrap\"><pre class=\"rust {}\">\n", class.unwrap_or(""))
}
-fn write_footer(out: &mut dyn Write) -> io::Result<()> {
- write!(out, "</pre></div>\n")
+fn write_footer(out: &mut dyn Write, playground_button: Option<&str>) -> io::Result<()> {
+ write!(out, "</pre>{}</div>\n", if let Some(button) = playground_button { button } else { "" })
}