@@ -3,29 +3,42 @@ use comrak::{
33 adapters:: SyntaxHighlighterAdapter , ComrakExtensionOptions , ComrakOptions , ComrakPlugins ,
44 ComrakRenderPlugins ,
55} ;
6- use std:: { collections:: HashMap , fmt :: Write } ;
6+ use std:: collections:: HashMap ;
77
88#[ derive( Debug ) ]
99struct CodeAdapter < F > ( F ) ;
1010
1111impl < F : Fn ( Option < & str > , & str ) -> String > SyntaxHighlighterAdapter for CodeAdapter < F > {
12- fn highlight ( & self , lang : Option < & str > , code : & str ) -> String {
12+ fn write_highlighted (
13+ & self ,
14+ output : & mut dyn std:: io:: Write ,
15+ lang : Option < & str > ,
16+ code : & str ,
17+ ) -> std:: io:: Result < ( ) > {
1318 // comrak does not treat `,` as an info-string delimiter, so we do that here
1419 // TODO: https://github.com/kivikakk/comrak/issues/246
1520 let lang = lang. and_then ( |lang| lang. split ( ',' ) . next ( ) ) ;
16- ( self . 0 ) ( lang, code)
21+ write ! ( output , "{}" , ( self . 0 ) ( lang, code) )
1722 }
1823
19- fn build_pre_tag ( & self , attributes : & HashMap < String , String > ) -> String {
20- build_opening_tag ( "pre" , attributes)
24+ fn write_pre_tag (
25+ & self ,
26+ output : & mut dyn std:: io:: Write ,
27+ attributes : HashMap < String , String > ,
28+ ) -> std:: io:: Result < ( ) > {
29+ write_opening_tag ( output, "pre" , & attributes)
2130 }
2231
23- fn build_code_tag ( & self , attributes : & HashMap < String , String > ) -> String {
32+ fn write_code_tag (
33+ & self ,
34+ output : & mut dyn std:: io:: Write ,
35+ attributes : HashMap < String , String > ,
36+ ) -> std:: io:: Result < ( ) > {
2437 // similarly to above, since comrak does not treat `,` as an info-string delimiter it will
2538 // try to apply `class="language-rust,ignore"` for the info-string `rust,ignore`, so we
2639 // have to detect that case and fixup the class here
2740 // TODO: https://github.com/kivikakk/comrak/issues/246
28- let mut attributes = attributes. clone ( ) ;
41+ let mut attributes = attributes;
2942 if let Some ( classes) = attributes. get_mut ( "class" ) {
3043 * classes = classes
3144 . split ( ' ' )
@@ -35,17 +48,20 @@ impl<F: Fn(Option<&str>, &str) -> String> SyntaxHighlighterAdapter for CodeAdapt
3548 // TODO: https://github.com/rust-lang/rust/issues/79524 or itertools
3649 classes. pop ( ) ;
3750 }
38- build_opening_tag ( "code" , & attributes)
51+ write_opening_tag ( output , "code" , & attributes)
3952 }
4053}
4154
42- fn build_opening_tag ( tag : & str , attributes : & HashMap < String , String > ) -> String {
43- let mut tag_parts = format ! ( "<{tag}" ) ;
55+ fn write_opening_tag (
56+ output : & mut dyn std:: io:: Write ,
57+ tag : & str ,
58+ attributes : & HashMap < String , String > ,
59+ ) -> std:: io:: Result < ( ) > {
60+ write ! ( output, "<{tag}" ) ?;
4461 for ( attr, val) in attributes {
45- write ! ( tag_parts , " {attr}=\" {val}\" " ) . unwrap ( ) ;
62+ write ! ( output , " {attr}=\" {val}\" " ) ? ;
4663 }
47- tag_parts. push ( '>' ) ;
48- tag_parts
64+ write ! ( output, ">" )
4965}
5066
5167fn render_with_highlighter (
0 commit comments