@@ -17,22 +17,36 @@ use html::escape::Escape;
1717
1818use std:: io;
1919use std:: io:: prelude:: * ;
20- use syntax:: parse:: lexer;
20+ use syntax:: parse:: lexer:: { self , Reader } ;
2121use syntax:: parse:: token;
2222use syntax:: parse;
2323
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 {
2626 debug ! ( "highlighting: ================\n {}\n ==============" , src) ;
2727 let sess = parse:: ParseSess :: new ( ) ;
2828 let fm = sess. codemap ( ) . new_filemap ( "<stdin>" . to_string ( ) , src. to_string ( ) ) ;
2929
3030 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 ( ) ;
3650 String :: from_utf8_lossy ( & out[ ..] ) . into_owned ( )
3751}
3852
@@ -43,17 +57,10 @@ pub fn highlight(src: &str, class: Option<&str>, id: Option<&str>) -> String {
4357/// it's used. All source code emission is done as slices from the source map,
4458/// not from the tokens themselves, in order to stay true to the original
4559/// 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 < ( ) > {
5764 let mut is_attribute = false ;
5865 let mut is_macro = false ;
5966 let mut is_macro_nonterminal = false ;
@@ -184,5 +191,21 @@ fn doit(sess: &parse::ParseSess, mut lexer: lexer::StringReader,
184191 }
185192 }
186193
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 < ( ) > {
187210 write ! ( out, "</pre>\n " )
188211}
0 commit comments