13
13
//! This module uses libsyntax's lexer to provide token-based highlighting for
14
14
//! the HTML documentation generated by rustdoc.
15
15
//!
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.
22
17
23
18
use html:: escape:: Escape ;
24
19
@@ -33,7 +28,7 @@ use syntax::parse;
33
28
use syntax_pos:: { Span , FileName } ;
34
29
35
30
/// 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 > ,
37
32
extension : Option < & str > ,
38
33
tooltip : Option < ( & str , & str ) > ) -> String {
39
34
debug ! ( "highlighting: ================\n {}\n ==============" , src) ;
@@ -46,7 +41,7 @@ pub fn render_with_highlighting(src: &str, class: Option<&str>, id: Option<&str>
46
41
class='tooltiptext'>{}</span></div></div>",
47
42
class, tooltip) . unwrap ( ) ;
48
43
}
49
- write_header ( class, id , & mut out) . unwrap ( ) ;
44
+ write_header ( class, & mut out) . unwrap ( ) ;
50
45
51
46
let mut classifier = Classifier :: new ( lexer:: StringReader :: new ( & sess, fm, None ) , sess. codemap ( ) ) ;
52
47
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>
63
58
/// Processes a program (nested in the internal `lexer`), classifying strings of
64
59
/// text by highlighting category (`Class`). Calls out to a `Writer` to write
65
60
/// each span of text in sequence.
66
- pub struct Classifier < ' a > {
61
+ struct Classifier < ' a > {
67
62
lexer : lexer:: StringReader < ' a > ,
68
63
codemap : & ' a CodeMap ,
69
64
@@ -75,7 +70,7 @@ pub struct Classifier<'a> {
75
70
76
71
/// How a span of text is classified. Mostly corresponds to token kinds.
77
72
#[ derive( Clone , Copy , Debug , Eq , PartialEq ) ]
78
- pub enum Class {
73
+ enum Class {
79
74
None ,
80
75
Comment ,
81
76
DocComment ,
@@ -103,19 +98,17 @@ pub enum Class {
103
98
/// The classifier will call into the `Writer` implementation as it finds spans
104
99
/// of text to highlight. Exactly how that text should be highlighted is up to
105
100
/// the implementation.
106
- pub trait Writer {
101
+ trait Writer {
107
102
/// Called when we start processing a span of text that should be highlighted.
108
103
/// The `Class` argument specifies how it should be highlighted.
109
104
fn enter_span ( & mut self , _: Class ) -> io:: Result < ( ) > ;
110
105
111
106
/// Called at the end of a span of highlighted text.
112
107
fn exit_span ( & mut self ) -> io:: Result < ( ) > ;
113
108
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
+ ///
119
112
/// The following sequences of callbacks are equivalent:
120
113
/// ```plain
121
114
/// enter_span(Foo), string("text", None), exit_span()
@@ -125,8 +118,7 @@ pub trait Writer {
125
118
/// more flexible.
126
119
fn string < T : Display > ( & mut self ,
127
120
text : T ,
128
- klass : Class ,
129
- tok : Option < & TokenAndSpan > )
121
+ klass : Class )
130
122
-> io:: Result < ( ) > ;
131
123
}
132
124
@@ -135,8 +127,7 @@ pub trait Writer {
135
127
impl < U : Write > Writer for U {
136
128
fn string < T : Display > ( & mut self ,
137
129
text : T ,
138
- klass : Class ,
139
- _tas : Option < & TokenAndSpan > )
130
+ klass : Class )
140
131
-> io:: Result < ( ) > {
141
132
match klass {
142
133
Class :: None => write ! ( self , "{}" , text) ,
@@ -154,7 +145,7 @@ impl<U: Write> Writer for U {
154
145
}
155
146
156
147
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 > {
158
149
Classifier {
159
150
lexer,
160
151
codemap,
@@ -186,7 +177,7 @@ impl<'a> Classifier<'a> {
186
177
/// is used. All source code emission is done as slices from the source map,
187
178
/// not from the tokens themselves, in order to stay true to the original
188
179
/// source.
189
- pub fn write_source < W : Writer > ( & mut self ,
180
+ fn write_source < W : Writer > ( & mut self ,
190
181
out : & mut W )
191
182
-> io:: Result < ( ) > {
192
183
loop {
@@ -208,7 +199,7 @@ impl<'a> Classifier<'a> {
208
199
-> io:: Result < ( ) > {
209
200
let klass = match tas. tok {
210
201
token:: Shebang ( s) => {
211
- out. string ( Escape ( & s. as_str ( ) ) , Class :: None , Some ( & tas ) ) ?;
202
+ out. string ( Escape ( & s. as_str ( ) ) , Class :: None ) ?;
212
203
return Ok ( ( ) ) ;
213
204
} ,
214
205
@@ -272,8 +263,8 @@ impl<'a> Classifier<'a> {
272
263
self . in_attribute = true ;
273
264
out. enter_span ( Class :: Attribute ) ?;
274
265
}
275
- out. string ( "#" , Class :: None , None ) ?;
276
- out. string ( "!" , Class :: None , None ) ?;
266
+ out. string ( "#" , Class :: None ) ?;
267
+ out. string ( "!" , Class :: None ) ?;
277
268
return Ok ( ( ) ) ;
278
269
}
279
270
@@ -282,13 +273,13 @@ impl<'a> Classifier<'a> {
282
273
self . in_attribute = true ;
283
274
out. enter_span ( Class :: Attribute ) ?;
284
275
}
285
- out. string ( "#" , Class :: None , None ) ?;
276
+ out. string ( "#" , Class :: None ) ?;
286
277
return Ok ( ( ) ) ;
287
278
}
288
279
token:: CloseDelim ( token:: Bracket ) => {
289
280
if self . in_attribute {
290
281
self . in_attribute = false ;
291
- out. string ( "]" , Class :: None , None ) ?;
282
+ out. string ( "]" , Class :: None ) ?;
292
283
out. exit_span ( ) ?;
293
284
return Ok ( ( ) ) ;
294
285
} else {
@@ -344,7 +335,7 @@ impl<'a> Classifier<'a> {
344
335
345
336
// Anything that didn't return above is the simple case where we the
346
337
// 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)
348
339
}
349
340
350
341
// Helper function to get a snippet from the codemap.
@@ -355,7 +346,7 @@ impl<'a> Classifier<'a> {
355
346
356
347
impl Class {
357
348
/// 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 {
359
350
match self {
360
351
Class :: None => "" ,
361
352
Class :: Comment => "comment" ,
@@ -379,15 +370,8 @@ impl Class {
379
370
}
380
371
}
381
372
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( "" ) )
391
375
}
392
376
393
377
fn write_footer ( out : & mut dyn Write ) -> io:: Result < ( ) > {
0 commit comments