55
66use std:: fmt;
77
8+ use pulldown_cmark_escape:: FmtWriter ;
89use unicode_segmentation:: UnicodeSegmentation ;
910
1011/// Wrapper struct which will emit the HTML-escaped version of the contained
@@ -13,31 +14,7 @@ pub(crate) struct Escape<'a>(pub &'a str);
1314
1415impl fmt:: Display for Escape < ' _ > {
1516 fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
16- // Because the internet is always right, turns out there's not that many
17- // characters to escape: http://stackoverflow.com/questions/7381974
18- let Escape ( s) = * self ;
19- let pile_o_bits = s;
20- let mut last = 0 ;
21- for ( i, ch) in s. char_indices ( ) {
22- let s = match ch {
23- '>' => ">" ,
24- '<' => "<" ,
25- '&' => "&" ,
26- '\'' => "'" ,
27- '"' => """ ,
28- _ => continue ,
29- } ;
30- fmt. write_str ( & pile_o_bits[ last..i] ) ?;
31- fmt. write_str ( s) ?;
32- // NOTE: we only expect single byte characters here - which is fine as long as we
33- // only match single byte characters
34- last = i + 1 ;
35- }
36-
37- if last < s. len ( ) {
38- fmt. write_str ( & pile_o_bits[ last..] ) ?;
39- }
40- Ok ( ( ) )
17+ pulldown_cmark_escape:: escape_html ( FmtWriter ( fmt) , self . 0 )
4118 }
4219}
4320
@@ -51,29 +28,7 @@ pub(crate) struct EscapeBodyText<'a>(pub &'a str);
5128
5229impl fmt:: Display for EscapeBodyText < ' _ > {
5330 fn fmt ( & self , fmt : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
54- // Because the internet is always right, turns out there's not that many
55- // characters to escape: http://stackoverflow.com/questions/7381974
56- let EscapeBodyText ( s) = * self ;
57- let pile_o_bits = s;
58- let mut last = 0 ;
59- for ( i, ch) in s. char_indices ( ) {
60- let s = match ch {
61- '>' => ">" ,
62- '<' => "<" ,
63- '&' => "&" ,
64- _ => continue ,
65- } ;
66- fmt. write_str ( & pile_o_bits[ last..i] ) ?;
67- fmt. write_str ( s) ?;
68- // NOTE: we only expect single byte characters here - which is fine as long as we
69- // only match single byte characters
70- last = i + 1 ;
71- }
72-
73- if last < s. len ( ) {
74- fmt. write_str ( & pile_o_bits[ last..] ) ?;
75- }
76- Ok ( ( ) )
31+ pulldown_cmark_escape:: escape_html_body_text ( FmtWriter ( fmt) , self . 0 )
7732 }
7833}
7934
0 commit comments