@@ -3,39 +3,58 @@ use pulldown_cmark_escape::StrWrite;
33use crate :: html:: config:: HtmlConfig ;
44use crate :: html:: state:: HtmlState ;
55use crate :: html:: writer:: HtmlWriter ;
6+ use crate :: html_writer;
67
7- /// Default HTML writer implementation that can work with any StrWrite-compatible writer
8- pub struct DefaultHtmlWriter < ' a , W : StrWrite > {
8+ /// Base type for HTML writers that handles common functionality
9+ pub struct HtmlWriterBase < W : StrWrite > {
910 writer : W ,
10- config : & ' a HtmlConfig ,
11+ config : HtmlConfig ,
1112 state : HtmlState ,
1213}
1314
14- impl < ' a , W : StrWrite > DefaultHtmlWriter < ' a , W > {
15- /// Create a new DefaultHtmlWriter with the given writer and configuration
16- pub fn new ( writer : W , config : & ' a HtmlConfig ) -> Self {
15+ impl < W : StrWrite > HtmlWriterBase < W > {
16+ /// Create a new HtmlWriterBase with the given writer and configuration
17+ pub fn new ( writer : W , config : HtmlConfig ) -> Self {
1718 Self {
1819 writer,
1920 config,
2021 state : HtmlState :: new ( ) ,
2122 }
2223 }
23- }
2424
25- impl < ' a , W : StrWrite > HtmlWriter < W > for DefaultHtmlWriter < ' a , W > {
26- fn get_writer ( & mut self ) -> & mut W {
25+ /// Get a mutable reference to the underlying writer
26+ pub fn get_writer ( & mut self ) -> & mut W {
2727 & mut self . writer
2828 }
2929
30- fn get_config ( & self ) -> & HtmlConfig {
31- self . config
30+ /// Get a reference to the configuration
31+ pub fn get_config ( & self ) -> & HtmlConfig {
32+ & self . config
3233 }
3334
34- fn get_state ( & mut self ) -> & mut HtmlState {
35+ /// Get a mutable reference to the state
36+ pub fn get_state ( & mut self ) -> & mut HtmlState {
3537 & mut self . state
3638 }
3739}
3840
41+ /// Default HTML writer implementation that can work with any StrWrite-compatible writer
42+ /// This should be the approximate amount of code any custom implementation needs to
43+ /// provide
44+ #[ html_writer]
45+ pub struct DefaultHtmlWriter < W : StrWrite > {
46+ base : HtmlWriterBase < W > ,
47+ }
48+
49+ impl < W : StrWrite > DefaultHtmlWriter < W > {
50+ /// Create a new DefaultHtmlWriter with the given writer and configuration
51+ pub fn new ( writer : W , config : HtmlConfig ) -> Self {
52+ Self {
53+ base : HtmlWriterBase :: new ( writer, config. clone ( ) ) ,
54+ }
55+ }
56+ }
57+
3958#[ cfg( test) ]
4059mod tests {
4160 use super :: * ;
@@ -46,7 +65,7 @@ mod tests {
4665 fn test_basic_writing ( ) {
4766 let mut output = String :: new ( ) ;
4867 let config = HtmlConfig :: default ( ) ;
49- let mut writer = DefaultHtmlWriter :: new ( FmtWriter ( & mut output) , & config) ;
68+ let mut writer = DefaultHtmlWriter :: new ( FmtWriter ( & mut output) , config) ;
5069
5170 writer. write_str ( "<p>" ) . unwrap ( ) ;
5271 let _ = escape_html ( & mut writer. get_writer ( ) , "Hello & World" ) ;
@@ -66,7 +85,7 @@ mod tests {
6685 . collect ( ) ,
6786 ) ;
6887
69- let mut writer = DefaultHtmlWriter :: new ( FmtWriter ( & mut output) , & config) ;
88+ let mut writer = DefaultHtmlWriter :: new ( FmtWriter ( & mut output) , config) ;
7089 writer. start_paragraph ( ) . unwrap ( ) ;
7190 writer. text ( "Test" ) . unwrap ( ) ;
7291 writer. end_paragraph ( ) . unwrap ( ) ;
@@ -99,7 +118,7 @@ mod tests {
99118 #[ test]
100119 fn test_custom_writer ( ) {
101120 let config = HtmlConfig :: default ( ) ;
102- let mut writer = DefaultHtmlWriter :: new ( TestWriter ( String :: new ( ) ) , & config) ;
121+ let mut writer = DefaultHtmlWriter :: new ( TestWriter ( String :: new ( ) ) , config) ;
103122
104123 writer. write_str ( "Test" ) . unwrap ( ) ;
105124 assert_eq ! ( writer. get_writer( ) . 0 , "Test" ) ;
@@ -110,7 +129,7 @@ mod tests {
110129 let mut output = String :: new ( ) ;
111130 let mut config = HtmlConfig :: default ( ) ;
112131 config. html . escape_html = true ;
113- let mut writer = DefaultHtmlWriter :: new ( FmtWriter ( & mut output) , & config) ;
132+ let mut writer = DefaultHtmlWriter :: new ( FmtWriter ( & mut output) , config) ;
114133
115134 assert ! ( !writer. get_state( ) . currently_in_code_block) ;
116135 writer. get_state ( ) . currently_in_code_block = true ;
0 commit comments