@@ -17,22 +17,36 @@ use html::escape::Escape;
17
17
18
18
use std:: io;
19
19
use std:: io:: prelude:: * ;
20
- use syntax:: parse:: lexer;
20
+ use syntax:: parse:: lexer:: { self , Reader } ;
21
21
use syntax:: parse:: token;
22
22
use syntax:: parse;
23
23
24
- /// Highlights some source code , returning the HTML output.
25
- pub fn highlight ( src : & str , class : Option < & str > , id : Option < & str > ) -> String {
24
+ /// Highlights `src` , returning the HTML output.
25
+ pub fn render_with_highlighting ( src : & str , class : Option < & str > , id : Option < & str > ) -> String {
26
26
debug ! ( "highlighting: ================\n {}\n ==============" , src) ;
27
27
let sess = parse:: ParseSess :: new ( ) ;
28
28
let fm = sess. codemap ( ) . new_filemap ( "<stdin>" . to_string ( ) , src. to_string ( ) ) ;
29
29
30
30
let mut out = Vec :: new ( ) ;
31
- doit ( & sess,
32
- lexer:: StringReader :: new ( & sess. span_diagnostic , fm) ,
33
- class,
34
- id,
35
- & mut out) . unwrap ( ) ;
31
+ write_header ( class, id, & mut out) . unwrap ( ) ;
32
+ write_source ( & sess,
33
+ lexer:: StringReader :: new ( & sess. span_diagnostic , fm) ,
34
+ & mut out) . unwrap ( ) ;
35
+ write_footer ( & mut out) . unwrap ( ) ;
36
+ String :: from_utf8_lossy ( & out[ ..] ) . into_owned ( )
37
+ }
38
+
39
+ /// Highlights `src`, returning the HTML output. Returns only the inner html to
40
+ /// be inserted into an element. C.f., `render_with_highlighting` which includes
41
+ /// an enclosing `<pre>` block.
42
+ pub fn render_inner_with_highlighting ( src : & str ) -> String {
43
+ let sess = parse:: ParseSess :: new ( ) ;
44
+ let fm = sess. codemap ( ) . new_filemap ( "<stdin>" . to_string ( ) , src. to_string ( ) ) ;
45
+
46
+ let mut out = Vec :: new ( ) ;
47
+ write_source ( & sess,
48
+ lexer:: StringReader :: new ( & sess. span_diagnostic , fm) ,
49
+ & mut out) . unwrap ( ) ;
36
50
String :: from_utf8_lossy ( & out[ ..] ) . into_owned ( )
37
51
}
38
52
@@ -43,17 +57,10 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
43
57
/// it's used. All source code emission is done as slices from the source map,
44
58
/// not from the tokens themselves, in order to stay true to the original
45
59
/// source.
46
- fn doit ( sess : & parse:: ParseSess , mut lexer : lexer:: StringReader ,
47
- class : Option < & str > , id : Option < & str > ,
48
- out : & mut Write ) -> io:: Result < ( ) > {
49
- use syntax:: parse:: lexer:: Reader ;
50
-
51
- write ! ( out, "<pre " ) ?;
52
- match id {
53
- Some ( id) => write ! ( out, "id='{}' " , id) ?,
54
- None => { }
55
- }
56
- write ! ( out, "class='rust {}'>\n " , class. unwrap_or( "" ) ) ?;
60
+ fn write_source ( sess : & parse:: ParseSess ,
61
+ mut lexer : lexer:: StringReader ,
62
+ out : & mut Write )
63
+ -> io:: Result < ( ) > {
57
64
let mut is_attribute = false ;
58
65
let mut is_macro = false ;
59
66
let mut is_macro_nonterminal = false ;
@@ -184,5 +191,21 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
184
191
}
185
192
}
186
193
194
+ Ok ( ( ) )
195
+ }
196
+
197
+ fn write_header ( class : Option < & str > ,
198
+ id : Option < & str > ,
199
+ out : & mut Write )
200
+ -> io:: Result < ( ) > {
201
+ write ! ( out, "<pre " ) ?;
202
+ match id {
203
+ Some ( id) => write ! ( out, "id='{}' " , id) ?,
204
+ None => { }
205
+ }
206
+ write ! ( out, "class='rust {}'>\n " , class. unwrap_or( "" ) )
207
+ }
208
+
209
+ fn write_footer ( out : & mut Write ) -> io:: Result < ( ) > {
187
210
write ! ( out, "</pre>\n " )
188
211
}
0 commit comments