Skip to content

Commit 03df573

Browse files
authored
Rollup merge of rust-lang#52628 - Mark-Simulacrum:rustdoc-cleanup-1, r=QuietMisdreavus
Cleanup some rustdoc code Commits are mostly individual though some do depend on others.
2 parents b7ee110 + 0f680b3 commit 03df573

File tree

9 files changed

+318
-262
lines changed

9 files changed

+318
-262
lines changed

src/librustdoc/externalfiles.rs

+10-4
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@ use std::fs;
1212
use std::path::Path;
1313
use std::str;
1414
use errors;
15-
use html::markdown::Markdown;
15+
use syntax::feature_gate::UnstableFeatures;
16+
use html::markdown::{IdMap, ErrorCodes, Markdown};
17+
use std::cell::RefCell;
1618

1719
#[derive(Clone)]
1820
pub struct ExternalHtml {
@@ -29,24 +31,28 @@ pub struct ExternalHtml {
2931

3032
impl ExternalHtml {
3133
pub fn load(in_header: &[String], before_content: &[String], after_content: &[String],
32-
md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler)
34+
md_before_content: &[String], md_after_content: &[String], diag: &errors::Handler,
35+
id_map: &mut IdMap)
3336
-> Option<ExternalHtml> {
37+
let codes = ErrorCodes::from(UnstableFeatures::from_environment().is_nightly_build());
3438
load_external_files(in_header, diag)
3539
.and_then(|ih|
3640
load_external_files(before_content, diag)
3741
.map(|bc| (ih, bc))
3842
)
3943
.and_then(|(ih, bc)|
4044
load_external_files(md_before_content, diag)
41-
.map(|m_bc| (ih, format!("{}{}", bc, Markdown(&m_bc, &[]))))
45+
.map(|m_bc| (ih,
46+
format!("{}{}", bc, Markdown(&m_bc, &[], RefCell::new(id_map), codes))))
4247
)
4348
.and_then(|(ih, bc)|
4449
load_external_files(after_content, diag)
4550
.map(|ac| (ih, bc, ac))
4651
)
4752
.and_then(|(ih, bc, ac)|
4853
load_external_files(md_after_content, diag)
49-
.map(|m_ac| (ih, bc, format!("{}{}", ac, Markdown(&m_ac, &[]))))
54+
.map(|m_ac| (ih, bc,
55+
format!("{}{}", ac, Markdown(&m_ac, &[], RefCell::new(id_map), codes))))
5056
)
5157
.map(|(ih, bc, ac)|
5258
ExternalHtml {

src/librustdoc/html/highlight.rs

+22-38
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@
1313
//! This module uses libsyntax's lexer to provide token-based highlighting for
1414
//! the HTML documentation generated by rustdoc.
1515
//!
16-
//! If you just want to syntax highlighting for a Rust program, then you can use
17-
//! the `render_inner_with_highlighting` or `render_with_highlighting`
18-
//! functions. For more advanced use cases (if you want to supply your own css
19-
//! classes or control how the HTML is generated, or even generate something
20-
//! other then HTML), then you should implement the `Writer` trait and use a
21-
//! `Classifier`.
16+
//! Use the `render_with_highlighting` to highlight some rust code.
2217
2318
use html::escape::Escape;
2419

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

3530
/// Highlights `src`, returning the HTML output.
36-
pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>,
31+
pub fn render_with_highlighting(src: &str, class: Option<&str>,
3732
extension: Option<&str>,
3833
tooltip: Option<(&str, &str)>) -> String {
3934
debug!("highlighting: ================\n{}\n==============", src);
@@ -46,7 +41,7 @@ pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>
4641
class='tooltiptext'>{}</span></div></div>",
4742
class, tooltip).unwrap();
4843
}
49-
write_header(class, id, &mut out).unwrap();
44+
write_header(class, &mut out).unwrap();
5045

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

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

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

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

114-
/// Called for a span of text, usually, but not always, a single token. If
115-
/// the string of text (`T`) does correspond to a token, then the token will
116-
/// also be passed. If the text should be highlighted differently from the
117-
/// surrounding text, then the `Class` argument will be a value other than
118-
/// `None`.
109+
/// Called for a span of text. If the text should be highlighted differently from the
110+
/// surrounding text, then the `Class` argument will be a value other than `None`.
111+
///
119112
/// The following sequences of callbacks are equivalent:
120113
/// ```plain
121114
/// enter_span(Foo), string("text", None), exit_span()
@@ -125,8 +118,7 @@ pub trait Writer {
125118
/// more flexible.
126119
fn string<T: Display>(&mut self,
127120
text: T,
128-
klass: Class,
129-
tok: Option<&TokenAndSpan>)
121+
klass: Class)
130122
-> io::Result<()>;
131123
}
132124

@@ -135,8 +127,7 @@ pub trait Writer {
135127
impl<U: Write> Writer for U {
136128
fn string<T: Display>(&mut self,
137129
text: T,
138-
klass: Class,
139-
_tas: Option<&TokenAndSpan>)
130+
klass: Class)
140131
-> io::Result<()> {
141132
match klass {
142133
Class::None => write!(self, "{}", text),
@@ -154,7 +145,7 @@ impl<U: Write> Writer for U {
154145
}
155146

156147
impl<'a> Classifier<'a> {
157-
pub fn new(lexer: lexer::StringReader<'a>, codemap: &'a CodeMap) -> Classifier<'a> {
148+
fn new(lexer: lexer::StringReader<'a>, codemap: &'a CodeMap) -> Classifier<'a> {
158149
Classifier {
159150
lexer,
160151
codemap,
@@ -186,7 +177,7 @@ impl<'a> Classifier<'a> {
186177
/// is used. All source code emission is done as slices from the source map,
187178
/// not from the tokens themselves, in order to stay true to the original
188179
/// source.
189-
pub fn write_source<W: Writer>(&mut self,
180+
fn write_source<W: Writer>(&mut self,
190181
out: &mut W)
191182
-> io::Result<()> {
192183
loop {
@@ -208,7 +199,7 @@ impl<'a> Classifier<'a> {
208199
-> io::Result<()> {
209200
let klass = match tas.tok {
210201
token::Shebang(s) => {
211-
out.string(Escape(&s.as_str()), Class::None, Some(&tas))?;
202+
out.string(Escape(&s.as_str()), Class::None)?;
212203
return Ok(());
213204
},
214205

@@ -272,8 +263,8 @@ impl<'a> Classifier<'a> {
272263
self.in_attribute = true;
273264
out.enter_span(Class::Attribute)?;
274265
}
275-
out.string("#", Class::None, None)?;
276-
out.string("!", Class::None, None)?;
266+
out.string("#", Class::None)?;
267+
out.string("!", Class::None)?;
277268
return Ok(());
278269
}
279270

@@ -282,13 +273,13 @@ impl<'a> Classifier<'a> {
282273
self.in_attribute = true;
283274
out.enter_span(Class::Attribute)?;
284275
}
285-
out.string("#", Class::None, None)?;
276+
out.string("#", Class::None)?;
286277
return Ok(());
287278
}
288279
token::CloseDelim(token::Bracket) => {
289280
if self.in_attribute {
290281
self.in_attribute = false;
291-
out.string("]", Class::None, None)?;
282+
out.string("]", Class::None)?;
292283
out.exit_span()?;
293284
return Ok(());
294285
} else {
@@ -344,7 +335,7 @@ impl<'a> Classifier<'a> {
344335

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

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

356347
impl Class {
357348
/// Returns the css class expected by rustdoc for each `Class`.
358-
pub fn rustdoc_class(self) -> &'static str {
349+
fn rustdoc_class(self) -> &'static str {
359350
match self {
360351
Class::None => "",
361352
Class::Comment => "comment",
@@ -379,15 +370,8 @@ impl Class {
379370
}
380371
}
381372

382-
fn write_header(class: Option<&str>,
383-
id: Option<&str>,
384-
out: &mut dyn Write)
385-
-> io::Result<()> {
386-
write!(out, "<pre ")?;
387-
if let Some(id) = id {
388-
write!(out, "id='{}' ", id)?;
389-
}
390-
write!(out, "class=\"rust {}\">\n", class.unwrap_or(""))
373+
fn write_header(class: Option<&str>, out: &mut dyn Write) -> io::Result<()> {
374+
write!(out, "<pre class=\"rust {}\">\n", class.unwrap_or(""))
391375
}
392376

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

0 commit comments

Comments
 (0)