Skip to content

Commit

Permalink
Rollup merge of rust-lang#52628 - Mark-Simulacrum:rustdoc-cleanup-1, …
Browse files Browse the repository at this point in the history
…r=QuietMisdreavus

Cleanup some rustdoc code

Commits are mostly individual though some do depend on others.
  • Loading branch information
Mark-Simulacrum authored Aug 1, 2018
2 parents c1e2ea1 + 0f680b3 commit e292913
Show file tree
Hide file tree
Showing 9 changed files with 318 additions and 262 deletions.
14 changes: 10 additions & 4 deletions src/librustdoc/externalfiles.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use std::fs;
use std::path::Path;
use std::str;
use errors;
use html::markdown::Markdown;
use syntax::feature_gate::UnstableFeatures;
use html::markdown::{IdMap, ErrorCodes, Markdown};
use std::cell::RefCell;

#[derive(Clone)]
pub struct ExternalHtml {
Expand All @@ -29,24 +31,28 @@ pub struct ExternalHtml {

impl ExternalHtml {
pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler)
md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler,
id_map: &mut IdMap)
-> Option<ExternalHtml> {
let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
load_external_files(in_header, diag)
.and_then(|ih|
load_external_files(before_content, diag)
.map(|bc| (ih, bc))
)
.and_then(|(ih, bc)|
load_external_files(md_before_content, diag)
.map(|m_bc| (ih, format!("{}{}", bc, Markdown(&m_bc, &[]))))
.map(|m_bc| (ih,
format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map), codes))))
)
.and_then(|(ih, bc)|
load_external_files(after_content, diag)
.map(|ac| (ih, bc, ac))
)
.and_then(|(ih, bc, ac)|
load_external_files(md_after_content, diag)
.map(|m_ac| (ih, bc, format!("{}{}", ac, Markdown(&m_ac, &[]))))
.map(|m_ac| (ih, bc,
format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map), codes))))
)
.map(|(ih, bc, ac)|
ExternalHtml {
Expand Down
60 changes: 22 additions & 38 deletions src/librustdoc/html/highlight.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,7 @@
//! This module uses libsyntax's lexer to provide token-based highlighting for
//! the HTML documentation generated by rustdoc.
//!
//! If you just want to syntax highlighting for a Rust program, then you can use
//! the `render_inner_with_highlighting` or `render_with_highlighting`
//! functions. For more advanced use cases (if you want to supply your own css
//! classes or control how the HTML is generated, or even generate something
//! other then HTML), then you should implement the `Writer` trait and use a
//! `Classifier`.
//! Use the `render_with_highlighting` to highlight some rust code.

use html::escape::Escape;

Expand All @@ -33,7 +28,7 @@ use syntax::parse;
use syntax_pos::{Span, FileName};

/// 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>,
extension: Option<&str>,
tooltip: Option<(&str, &str)>) -> String {
debug!("highlighting: ================\n{}\n==============", src);
Expand All @@ -46,7 +41,7 @@ pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>
class='tooltiptext'>{}</span></div></div>",
class, tooltip).unwrap();
}
write_header(class, id, &mut out).unwrap();
write_header(class, &mut out).unwrap();

let mut classifier = Classifier::new(lexer::StringReader::new(&sess, fm, None), sess.codemap());
if let Err(_) = classifier.write_source(&mut out) {
Expand All @@ -63,7 +58,7 @@ pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>
/// Processes a program (nested in the internal `lexer`), classifying strings of
/// text by highlighting category (`Class`). Calls out to a `Writer` to write
/// each span of text in sequence.
pub struct Classifier<'a> {
struct Classifier<'a> {
lexer: lexer::StringReader<'a>,
codemap: &'a CodeMap,

Expand All @@ -75,7 +70,7 @@ pub struct Classifier<'a> {

/// How a span of text is classified. Mostly corresponds to token kinds.
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Class {
enum Class {
None,
Comment,
DocComment,
Expand Down Expand Up @@ -103,19 +98,17 @@ pub enum Class {
/// The classifier will call into the `Writer` implementation as it finds spans
/// of text to highlight. Exactly how that text should be highlighted is up to
/// the implementation.
pub trait Writer {
trait Writer {
/// Called when we start processing a span of text that should be highlighted.
/// The `Class` argument specifies how it should be highlighted.
fn enter_span(&mut self, _: Class) -> io::Result<()>;

/// Called at the end of a span of highlighted text.
fn exit_span(&mut self) -> io::Result<()>;

/// Called for a span of text, usually, but not always, a single token. If
/// the string of text (`T`) does correspond to a token, then the token will
/// also be passed. If the text should be highlighted differently from the
/// surrounding text, then the `Class` argument will be a value other than
/// `None`.
/// Called for a span of text. If the text should be highlighted differently from the
/// surrounding text, then the `Class` argument will be a value other than `None`.
///
/// The following sequences of callbacks are equivalent:
/// ```plain
/// enter_span(Foo), string("text", None), exit_span()
Expand All @@ -125,8 +118,7 @@ pub trait Writer {
/// more flexible.
fn string<T: Display>(&mut self,
text: T,
klass: Class,
tok: Option<&TokenAndSpan>)
klass: Class)
-> io::Result<()>;
}

Expand All @@ -135,8 +127,7 @@ pub trait Writer {
impl<U: Write> Writer for U {
fn string<T: Display>(&mut self,
text: T,
klass: Class,
_tas: Option<&TokenAndSpan>)
klass: Class)
-> io::Result<()> {
match klass {
Class::None => write!(self, "{}", text),
Expand All @@ -154,7 +145,7 @@ impl<U: Write> Writer for U {
}

impl<'a> Classifier<'a> {
pub fn new(lexer: lexer::StringReader<'a>, codemap: &'a CodeMap) -> Classifier<'a> {
fn new(lexer: lexer::StringReader<'a>, codemap: &'a CodeMap) -> Classifier<'a> {
Classifier {
lexer,
codemap,
Expand Down Expand Up @@ -186,7 +177,7 @@ impl<'a> Classifier<'a> {
/// is 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
/// source.
pub fn write_source<W: Writer>(&mut self,
fn write_source<W: Writer>(&mut self,
out: &mut W)
-> io::Result<()> {
loop {
Expand All @@ -208,7 +199,7 @@ impl<'a> Classifier<'a> {
-> io::Result<()> {
let klass = match tas.tok {
token::Shebang(s) => {
out.string(Escape(&s.as_str()), Class::None, Some(&tas))?;
out.string(Escape(&s.as_str()), Class::None)?;
return Ok(());
},

Expand Down Expand Up @@ -272,8 +263,8 @@ impl<'a> Classifier<'a> {
self.in_attribute = true;
out.enter_span(Class::Attribute)?;
}
out.string("#", Class::None, None)?;
out.string("!", Class::None, None)?;
out.string("#", Class::None)?;
out.string("!", Class::None)?;
return Ok(());
}

Expand All @@ -282,13 +273,13 @@ impl<'a> Classifier<'a> {
self.in_attribute = true;
out.enter_span(Class::Attribute)?;
}
out.string("#", Class::None, None)?;
out.string("#", Class::None)?;
return Ok(());
}
token::CloseDelim(token::Bracket) => {
if self.in_attribute {
self.in_attribute = false;
out.string("]", Class::None, None)?;
out.string("]", Class::None)?;
out.exit_span()?;
return Ok(());
} else {
Expand Down Expand Up @@ -344,7 +335,7 @@ impl<'a> Classifier<'a> {

// Anything that didn't return above is the simple case where we the
// class just spans a single token, so we can use the `string` method.
out.string(Escape(&self.snip(tas.sp)), klass, Some(&tas))
out.string(Escape(&self.snip(tas.sp)), klass)
}

// Helper function to get a snippet from the codemap.
Expand All @@ -355,7 +346,7 @@ impl<'a> Classifier<'a> {

impl Class {
/// Returns the css class expected by rustdoc for each `Class`.
pub fn rustdoc_class(self) -> &'static str {
fn rustdoc_class(self) -> &'static str {
match self {
Class::None => "",
Class::Comment => "comment",
Expand All @@ -379,15 +370,8 @@ impl Class {
}
}

fn write_header(class: Option<&str>,
id: Option<&str>,
out: &mut dyn Write)
-> io::Result<()> {
write!(out, "<pre ")?;
if let Some(id) = id {
write!(out, "id='{}' ", id)?;
}
write!(out, "class=\"rust {}\">\n", class.unwrap_or(""))
fn write_header(class: Option<&str>, out: &mut dyn Write) -> io::Result<()> {
write!(out, "<pre class=\"rust {}\">\n", class.unwrap_or(""))
}

fn write_footer(out: &mut dyn Write) -> io::Result<()> {
Expand Down
Loading

0 comments on commit e292913

Please sign in to comment.