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
2318use html:: escape:: Escape ;
2419
@@ -33,7 +28,7 @@ use syntax::parse;
3328use 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 {
135127impl < 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
156147impl < ' 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
356347impl 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
393377fn write_footer ( out : & mut dyn Write ) -> io:: Result < ( ) > {
0 commit comments