11//! Adapter for the Syntect syntax highlighter plugin.
22
3- use crate :: adapters:: SyntaxHighlighterAdapter ;
4- use crate :: html;
3+ use std:: borrow:: Cow ;
54use std:: collections:: { hash_map, HashMap } ;
65use std:: fmt:: { self , Write } ;
76use syntect:: easy:: HighlightLines ;
@@ -13,6 +12,9 @@ use syntect::parsing::{SyntaxReference, SyntaxSet};
1312use syntect:: util:: LinesWithEndings ;
1413use syntect:: Error ;
1514
15+ use crate :: adapters:: SyntaxHighlighterAdapter ;
16+ use crate :: html;
17+
1618#[ derive( Debug ) ]
1719/// Syntect syntax highlighter plugin.
1820pub struct SyntectAdapter {
@@ -103,10 +105,10 @@ impl SyntaxHighlighterAdapter for SyntectAdapter {
103105 }
104106 }
105107
106- fn write_pre_tag (
108+ fn write_pre_tag < ' s > (
107109 & self ,
108110 output : & mut dyn Write ,
109- attributes : HashMap < & ' static str , String > ,
111+ attributes : HashMap < & ' static str , Cow < ' s , str > > ,
110112 ) -> fmt:: Result {
111113 match & self . theme {
112114 Some ( theme) => {
@@ -121,37 +123,33 @@ impl SyntaxHighlighterAdapter for SyntectAdapter {
121123 let mut pre_attributes = SyntectPreAttributes :: new ( attributes, & style) ;
122124 html:: write_opening_tag ( output, "pre" , pre_attributes. iter_mut ( ) )
123125 }
124- None => html:: write_opening_tag (
125- output,
126- "pre" ,
127- vec ! [ ( "class" , "syntax-highlighting" ) ] ,
128- ) ,
126+ None => html:: write_opening_tag ( output, "pre" , vec ! [ ( "class" , "syntax-highlighting" ) ] ) ,
129127 }
130128 }
131129
132- fn write_code_tag (
130+ fn write_code_tag < ' s > (
133131 & self ,
134132 output : & mut dyn Write ,
135- attributes : HashMap < & ' static str , String > ,
133+ attributes : HashMap < & ' static str , Cow < ' s , str > > ,
136134 ) -> fmt:: Result {
137135 html:: write_opening_tag ( output, "code" , attributes)
138136 }
139137}
140138
141- struct SyntectPreAttributes {
139+ struct SyntectPreAttributes < ' s > {
142140 syntect_style : String ,
143- attributes : HashMap < & ' static str , String > ,
141+ attributes : HashMap < & ' static str , Cow < ' s , str > > ,
144142}
145143
146- impl SyntectPreAttributes {
147- fn new ( attributes : HashMap < & ' static str , String > , syntect_style : & str ) -> Self {
144+ impl < ' s > SyntectPreAttributes < ' s > {
145+ fn new ( attributes : HashMap < & ' static str , Cow < ' s , str > > , syntect_style : & str ) -> Self {
148146 Self {
149147 syntect_style : syntect_style. into ( ) ,
150148 attributes,
151149 }
152150 }
153151
154- fn iter_mut ( & mut self ) -> SyntectPreAttributesIter < ' _ > {
152+ fn iter_mut ( & mut self ) -> SyntectPreAttributesIter < ' _ , ' s > {
155153 SyntectPreAttributesIter {
156154 iter_mut : self . attributes . iter_mut ( ) ,
157155 syntect_style : & self . syntect_style ,
@@ -160,20 +158,20 @@ impl SyntectPreAttributes {
160158 }
161159}
162160
163- struct SyntectPreAttributesIter < ' a > {
164- iter_mut : hash_map:: IterMut < ' a , & ' static str , String > ,
161+ struct SyntectPreAttributesIter < ' a , ' s > {
162+ iter_mut : hash_map:: IterMut < ' a , & ' static str , Cow < ' s , str > > ,
165163 syntect_style : & ' a str ,
166164 style_written : bool ,
167165}
168166
169- impl < ' a > Iterator for SyntectPreAttributesIter < ' a > {
167+ impl < ' a , ' s > Iterator for SyntectPreAttributesIter < ' a , ' s > {
170168 type Item = ( & ' a str , & ' a str ) ;
171169
172170 fn next ( & mut self ) -> Option < Self :: Item > {
173171 match self . iter_mut . next ( ) {
174172 Some ( ( k, v) ) if * k == "style" && !self . style_written => {
175173 self . style_written = true ;
176- v. insert_str ( 0 , self . syntect_style ) ;
174+ v. to_mut ( ) . insert_str ( 0 , self . syntect_style ) ;
177175 Some ( ( k, v) )
178176 }
179177 Some ( ( k, v) ) => Some ( ( k, v) ) ,
0 commit comments