@@ -82,8 +82,8 @@ crate fn render_with_highlighting<'a>(
82
82
}
83
83
84
84
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 ) ;
87
87
}
88
88
89
89
fn write_header ( out : & mut Buffer , class : Option < & str > , extra_content : Option < Buffer > ) {
@@ -115,46 +115,55 @@ fn write_code(
115
115
src : impl Iterator < Item = Line < ' a > > ,
116
116
edition : Edition ,
117
117
context_info : Option < ContextInfo < ' _ , ' _ , ' _ > > ,
118
- ) {
118
+ ) -> bool {
119
119
let mut iter = src. peekable ( ) ;
120
+ let mut expand = false ;
120
121
121
122
// For each `Line`, we replace DOS backlines with '\n'. This replace allows to fix how the code
122
123
// source with DOS backline characters is displayed.
123
124
while let Some ( line) = iter. next ( ) {
124
- match line {
125
+ let ( before , text , after ) = match line {
125
126
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>" )
144
129
}
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) ;
145
152
}
146
153
}
154
+ expand
147
155
}
148
156
149
- fn write_footer ( out : & mut Buffer , playground_button : Option < & str > ) {
157
+ fn write_footer ( out : & mut Buffer , playground_button : Option < & str > , expand : bool ) {
150
158
writeln ! (
151
159
out,
152
160
"</code></pre>\
153
161
<div class=\" code-buttons\" >\
154
- {}<button class=\" copy-code\" onclick=\" copyCode(this)\" ></button>\
162
+ {}{} <button class=\" copy-code\" onclick=\" copyCode(this)\" ></button>\
155
163
</div>\
156
164
</div>",
157
- playground_button. unwrap_or_default( )
165
+ playground_button. unwrap_or_default( ) ,
166
+ if expand { "<button class=\" expand\" onclick=\" expandCode(this)\" ></button>" } else { "" } ,
158
167
) ;
159
168
}
160
169
0 commit comments