@@ -82,8 +82,8 @@ crate fn render_with_highlighting<'a>(
8282 }
8383
8484 write_header ( out, class, extra_content) ;
85- write_code ( out, src, edition, context_info) ;
86- write_footer ( out, playground_button) ;
85+ let expand = write_code ( out, src, edition, context_info) ;
86+ write_footer ( out, playground_button, expand ) ;
8787}
8888
8989fn write_header ( out : & mut Buffer , class : Option < & str > , extra_content : Option < Buffer > ) {
@@ -115,46 +115,55 @@ fn write_code(
115115 src : impl Iterator < Item = Line < ' a > > ,
116116 edition : Edition ,
117117 context_info : Option < ContextInfo < ' _ , ' _ , ' _ > > ,
118- ) {
118+ ) -> bool {
119119 let mut iter = src. peekable ( ) ;
120+ let mut expand = false ;
120121
121122 // For each `Line`, we replace DOS backlines with '\n'. This replace allows to fix how the code
122123 // source with DOS backline characters is displayed.
123124 while let Some ( line) = iter. next ( ) {
124- match line {
125+ let ( before , text , after ) = match line {
125126 Line :: Hidden ( text) => {
126- write ! (
127- out,
128- "<span class=\" hidden\" >{}{}</span>" ,
129- Escape ( & text. replace( "\r \n " , "\n " ) ) ,
130- if iter. peek( ) . is_some( ) && !text. ends_with( '\n' ) { "\n " } else { "" } ,
131- ) ;
132- }
133- Line :: Shown ( text) => {
134- Classifier :: new ( & text. replace ( "\r \n " , "\n " ) , edition, context_info. as_ref ( ) . map ( |c| c. file_span ) . unwrap_or ( DUMMY_SP ) ) . highlight ( & mut |highlight| {
135- match highlight {
136- Highlight :: Token { text, class } => string ( out, Escape ( text) , class) ,
137- Highlight :: EnterSpan { class } => enter_span ( out, class) ,
138- Highlight :: ExitSpan => exit_span ( out) ,
139- } ;
140- } ) ;
141- if iter. peek ( ) . is_some ( ) && !text. ends_with ( '\n' ) {
142- write ! ( out, "\n " ) ;
143- }
127+ expand = true ;
128+ ( "<span class=\" hidden\" >" , text. replace ( "\r \n " , "\n " ) , "</span>" )
144129 }
130+ Line :: Shown ( text) => ( "" , text. replace ( "\r \n " , "\n " ) , "" ) ,
131+ } ;
132+ if !before. is_empty ( ) {
133+ out. push_str ( before) ;
134+ }
135+ Classifier :: new (
136+ & text. replace ( "\r \n " , "\n " ) ,
137+ edition,
138+ context_info. as_ref ( ) . map ( |c| c. file_span ) . unwrap_or ( DUMMY_SP ) ,
139+ )
140+ . highlight ( & mut |highlight| {
141+ match highlight {
142+ Highlight :: Token { text, class } => string ( out, Escape ( text) , class, context_info) ,
143+ Highlight :: EnterSpan { class } => enter_span ( out, class) ,
144+ Highlight :: ExitSpan => exit_span ( out) ,
145+ } ;
146+ } ) ;
147+ if iter. peek ( ) . is_some ( ) && !text. ends_with ( '\n' ) {
148+ out. push_str ( "\n " ) ;
149+ }
150+ if !after. is_empty ( ) {
151+ out. push_str ( after) ;
145152 }
146153 }
154+ expand
147155}
148156
149- fn write_footer ( out : & mut Buffer , playground_button : Option < & str > ) {
157+ fn write_footer ( out : & mut Buffer , playground_button : Option < & str > , expand : bool ) {
150158 writeln ! (
151159 out,
152160 "</code></pre>\
153161 <div class=\" code-buttons\" >\
154- {}<button class=\" copy-code\" onclick=\" copyCode(this)\" ></button>\
162+ {}{} <button class=\" copy-code\" onclick=\" copyCode(this)\" ></button>\
155163 </div>\
156164 </div>",
157- playground_button. unwrap_or_default( )
165+ playground_button. unwrap_or_default( ) ,
166+ if expand { "<button class=\" expand\" onclick=\" expandCode(this)\" ></button>" } else { "" } ,
158167 ) ;
159168}
160169
0 commit comments