@@ -56,9 +56,9 @@ struct TextWriter<'a, I> {
56
56
/// Stack of line styles.
57
57
line_styles : Vec < Style > ,
58
58
59
- #[ cfg( feature = "highlight-code" ) ]
60
59
/// Used to highlight code blocks, set when a codeblock is encountered
61
- current_code_highlighter : Option < HighlightLines < ' a > > ,
60
+ #[ cfg( feature = "highlight-code" ) ]
61
+ code_highlighter : Option < HighlightLines < ' a > > ,
62
62
63
63
/// Current list index as a stack of indices.
64
64
list_indices : Vec < Option < u64 > > ,
@@ -67,10 +67,9 @@ struct TextWriter<'a, I> {
67
67
}
68
68
69
69
#[ cfg( feature = "highlight-code" ) ]
70
- static SYNTAX_SET : LazyLock < SyntaxSet > =
71
- std:: sync:: LazyLock :: new ( SyntaxSet :: load_defaults_newlines) ;
70
+ static SYNTAX_SET : LazyLock < SyntaxSet > = LazyLock :: new ( SyntaxSet :: load_defaults_newlines) ;
72
71
#[ cfg( feature = "highlight-code" ) ]
73
- static THEME_SET : LazyLock < ThemeSet > = std :: sync :: LazyLock :: new ( ThemeSet :: load_defaults) ;
72
+ static THEME_SET : LazyLock < ThemeSet > = LazyLock :: new ( ThemeSet :: load_defaults) ;
74
73
75
74
impl < ' a , I > TextWriter < ' a , I >
76
75
where
86
85
list_indices : vec ! [ ] ,
87
86
needs_newline : false ,
88
87
#[ cfg( feature = "highlight-code" ) ]
89
- current_code_highlighter : None ,
88
+ code_highlighter : None ,
90
89
}
91
90
}
92
91
@@ -219,9 +218,9 @@ where
219
218
220
219
fn text ( & mut self , text : CowStr < ' a > ) {
221
220
#[ cfg( feature = "highlight-code" ) ]
222
- if let Some ( h ) = & mut self . current_code_highlighter {
221
+ if let Some ( highlighter ) = & mut self . code_highlighter {
223
222
let text: Text = LinesWithEndings :: from ( & text)
224
- . filter_map ( |line| h . highlight_line ( line, & SYNTAX_SET ) . ok ( ) )
223
+ . filter_map ( |line| highlighter . highlight_line ( line, & SYNTAX_SET ) . ok ( ) )
225
224
. filter_map ( |part| as_24_bit_terminal_escaped ( & part, false ) . into_text ( ) . ok ( ) )
226
225
. flatten ( )
227
226
. collect ( ) ;
@@ -300,27 +299,47 @@ where
300
299
CodeBlockKind :: Fenced ( ref lang) => lang. as_ref ( ) ,
301
300
CodeBlockKind :: Indented => "" ,
302
301
} ;
303
- if !cfg ! ( feature = "highlight-code" ) {
304
- self . line_styles . push ( styles:: CODE ) ;
305
- self . push_line ( Line :: default ( ) ) ;
306
- self . needs_newline = true ;
307
- }
308
302
309
- let span = Span :: from ( format ! ( "```{}" , lang ) ) ;
310
- self . push_line ( span . into ( ) ) ;
303
+ # [ cfg ( not ( feature = "highlight-code" ) ) ]
304
+ self . line_styles . push ( styles :: CODE ) ;
311
305
312
306
#[ cfg( feature = "highlight-code" ) ]
313
- self . set_current_code_highlighter ( lang) ;
307
+ self . set_code_highlighter ( lang) ;
308
+
309
+ let span = Span :: from ( format ! ( "```{lang}" ) ) ;
310
+ self . push_line ( span. into ( ) ) ;
311
+ self . needs_newline = true ;
314
312
}
315
313
316
314
fn end_codeblock ( & mut self ) {
317
315
let span = Span :: from ( "```" ) ;
318
316
self . push_line ( span. into ( ) ) ;
319
- self . line_styles . pop ( ) ;
320
317
self . needs_newline = true ;
321
318
319
+ #[ cfg( not( feature = "highlight-code" ) ) ]
320
+ self . line_styles . pop ( ) ;
321
+
322
322
#[ cfg( feature = "highlight-code" ) ]
323
- self . clear_current_code_highlighter ( ) ;
323
+ self . clear_code_highlighter ( ) ;
324
+ }
325
+
326
+ #[ cfg( feature = "highlight-code" ) ]
327
+ #[ instrument( level = "trace" , skip( self ) ) ]
328
+ fn set_code_highlighter ( & mut self , lang : & str ) {
329
+ if let Some ( syntax) = SYNTAX_SET . find_syntax_by_token ( lang) {
330
+ debug ! ( "Starting code block with syntax: {:?}" , lang) ;
331
+ let theme = & THEME_SET . themes [ "base16-ocean.dark" ] ;
332
+ let highlighter = HighlightLines :: new ( syntax, theme) ;
333
+ self . code_highlighter = Some ( highlighter) ;
334
+ } else {
335
+ warn ! ( "Could not find syntax for code block: {:?}" , lang) ;
336
+ }
337
+ }
338
+
339
+ #[ cfg( feature = "highlight-code" ) ]
340
+ #[ instrument( level = "trace" , skip( self ) ) ]
341
+ fn clear_code_highlighter ( & mut self ) {
342
+ self . code_highlighter = None ;
324
343
}
325
344
326
345
#[ instrument( level = "trace" , skip( self ) ) ]
@@ -362,25 +381,6 @@ where
362
381
self . push_line ( Line :: from ( vec ! [ span] ) ) ;
363
382
}
364
383
}
365
-
366
- #[ cfg( feature = "highlight-code" ) ]
367
- #[ instrument( level = "trace" , skip( self ) ) ]
368
- fn set_current_code_highlighter ( & mut self , lang : & str ) {
369
- let syntax = SYNTAX_SET . find_syntax_by_token ( lang) ;
370
- if let Some ( syntax) = syntax {
371
- debug ! ( "Starting code block with syntax: {:?}" , lang) ;
372
- let mut h = HighlightLines :: new ( syntax, & THEME_SET . themes [ "base16-ocean.dark" ] ) ;
373
- self . current_code_highlighter = Some ( h) ;
374
- } else {
375
- warn ! ( "Could not find syntax for code block: {:?}" , lang) ;
376
- }
377
- }
378
-
379
- #[ cfg( feature = "highlight-code" ) ]
380
- #[ instrument( level = "trace" , skip( self ) ) ]
381
- fn clear_current_code_highlighter ( & mut self ) {
382
- self . current_code_highlighter = None ;
383
- }
384
384
}
385
385
386
386
mod styles {
0 commit comments