@@ -3,29 +3,42 @@ use comrak::{
3
3
adapters:: SyntaxHighlighterAdapter , ComrakExtensionOptions , ComrakOptions , ComrakPlugins ,
4
4
ComrakRenderPlugins ,
5
5
} ;
6
- use std:: { collections:: HashMap , fmt :: Write } ;
6
+ use std:: collections:: HashMap ;
7
7
8
8
#[ derive( Debug ) ]
9
9
struct CodeAdapter < F > ( F ) ;
10
10
11
11
impl < 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 < ( ) > {
13
18
// comrak does not treat `,` as an info-string delimiter, so we do that here
14
19
// TODO: https://github.com/kivikakk/comrak/issues/246
15
20
let lang = lang. and_then ( |lang| lang. split ( ',' ) . next ( ) ) ;
16
- ( self . 0 ) ( lang, code)
21
+ write ! ( output , "{}" , ( self . 0 ) ( lang, code) )
17
22
}
18
23
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)
21
30
}
22
31
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 < ( ) > {
24
37
// similarly to above, since comrak does not treat `,` as an info-string delimiter it will
25
38
// try to apply `class="language-rust,ignore"` for the info-string `rust,ignore`, so we
26
39
// have to detect that case and fixup the class here
27
40
// TODO: https://github.com/kivikakk/comrak/issues/246
28
- let mut attributes = attributes. clone ( ) ;
41
+ let mut attributes = attributes;
29
42
if let Some ( classes) = attributes. get_mut ( "class" ) {
30
43
* classes = classes
31
44
. split ( ' ' )
@@ -35,17 +48,20 @@ impl<F: Fn(Option<&str>, &str) -> String> SyntaxHighlighterAdapter for CodeAdapt
35
48
// TODO: https://github.com/rust-lang/rust/issues/79524 or itertools
36
49
classes. pop ( ) ;
37
50
}
38
- build_opening_tag ( "code" , & attributes)
51
+ write_opening_tag ( output , "code" , & attributes)
39
52
}
40
53
}
41
54
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}" ) ?;
44
61
for ( attr, val) in attributes {
45
- write ! ( tag_parts , " {attr}=\" {val}\" " ) . unwrap ( ) ;
62
+ write ! ( output , " {attr}=\" {val}\" " ) ? ;
46
63
}
47
- tag_parts. push ( '>' ) ;
48
- tag_parts
64
+ write ! ( output, ">" )
49
65
}
50
66
51
67
fn render_with_highlighter (
0 commit comments