@@ -7,7 +7,7 @@ use std::rc::Rc;
77use std:: { fmt, io} ;
88
99use super :: SharedLine ;
10- use crate :: html_generate :: HtmlGenerate ;
10+ use crate :: generate :: Generate ;
1111use crate :: lexer:: { Lexer , Token , TokenKind } ;
1212
1313use itertools:: Itertools ;
@@ -153,42 +153,43 @@ impl Ast {
153153 }
154154 }
155155
156- // Iterate through each block of the Ast and process the block into a 'html' string
157- pub ( crate ) fn render_html ( & self , html : & impl HtmlGenerate ) -> String {
156+ // TODO: optimize
157+ pub ( crate ) fn generate ( & self , gen : & impl Generate ) -> String {
158158 let mut buff: Vec < String > = vec ! [ ] ;
159- let body = self . render_html_body ( html ) ;
159+ let body = self . generate_body ( gen ) ;
160160
161161 buff. push ( String :: from ( "<!doctype html><html>" ) ) ;
162- buff. push ( html . head ( ) ) ;
163- buff. push ( html . body_begin ( ) ) ;
162+ buff. push ( gen . head ( ) ) ;
163+ buff. push ( gen . body_begin ( ) ) ;
164164 buff. push ( body) ;
165- buff. push ( html . body_end ( ) ) ;
165+ buff. push ( gen . body_end ( ) ) ;
166166 buff. push ( String :: from ( "</html>" ) ) ;
167167
168168 buff. join ( "\n " )
169169 }
170170
171- pub ( crate ) fn render_html_body ( & self , html : & impl HtmlGenerate ) -> String {
171+ // Iterate through each block of the Ast and process the block into a 'html' string
172+ pub ( crate ) fn generate_body ( & self , gen : & impl Generate ) -> String {
172173 self . blocks ( )
173174 . iter ( )
174175 . filter ( |b| b. kind ( ) != Kind :: Meta__ && b. kind ( ) != Kind :: ListNesting__ )
175176 . map ( |b| match b. kind ( ) {
176- Kind :: Title => html . body_title ( b. first ( ) ) ,
177- Kind :: PlainText => html . body_plain_text ( b. contains ( ) ) ,
178- Kind :: Dividing => html . body_dividing ( b. first ( ) ) ,
179- Kind :: CodeBlock => html . body_code ( b. contains ( ) ) ,
180- Kind :: UnorderedList => html . body_unordered_list ( b. contains ( ) ) ,
181- Kind :: Blank => html . body_blank ( b. contains ( ) ) ,
177+ Kind :: Title => gen . body_title ( b. first ( ) ) ,
178+ Kind :: PlainText => gen . body_plain_text ( b. contains ( ) ) ,
179+ Kind :: Dividing => gen . body_dividing ( b. first ( ) ) ,
180+ Kind :: CodeBlock => gen . body_code ( b. contains ( ) ) ,
181+ Kind :: UnorderedList => gen . body_unordered_list ( b. contains ( ) ) ,
182+ Kind :: Blank => gen . body_blank ( b. contains ( ) ) ,
182183 Kind :: Quote => {
183184 let s = b
184185 . quote_ast
185186 . as_ref ( )
186- . map ( |a| a. render_html_body ( html ) )
187+ . map ( |a| a. generate_body ( gen ) )
187188 . unwrap_or_else ( || "" . to_string ( ) ) ;
188- html . body_quote ( & s)
189+ gen . body_quote ( & s)
189190 }
190- Kind :: OrderedList => html . body_ordered_list ( b. contains ( ) ) ,
191- Kind :: CodeBlockMark => html . body_plain_text ( b. contains ( ) ) , // treat code block mark as plain text
191+ Kind :: OrderedList => gen . body_ordered_list ( b. contains ( ) ) ,
192+ Kind :: CodeBlockMark => gen . body_plain_text ( b. contains ( ) ) , // treat code block mark as plain text
192193 _ => unreachable ! ( ) ,
193194 } )
194195 . filter ( |s| !s. is_empty ( ) )
@@ -536,7 +537,7 @@ impl Line {
536537 l
537538 }
538539
539- pub ( crate ) fn enter_nested_blocks ( & self , html : & impl HtmlGenerate ) -> String {
540+ pub ( crate ) fn enter_nested_blocks ( & self , gen : & impl Generate ) -> String {
540541 self . nested_blocks
541542 . iter ( )
542543 . filter ( |b| {
@@ -549,21 +550,21 @@ impl Line {
549550 && b. kind ( ) != Kind :: CodeBlock
550551 } )
551552 . map ( |b| match b. kind ( ) {
552- Kind :: Title => html . body_title ( b. first ( ) ) ,
553- Kind :: PlainText => html . body_plain_text ( b. contains ( ) ) ,
554- Kind :: Dividing => html . body_dividing ( b. first ( ) ) ,
555- Kind :: CodeBlock => html . body_code ( b. contains ( ) ) ,
556- Kind :: UnorderedList => html . body_unordered_list ( b. contains ( ) ) ,
557- Kind :: Blank => html . body_blank ( b. contains ( ) ) ,
553+ Kind :: Title => gen . body_title ( b. first ( ) ) ,
554+ Kind :: PlainText => gen . body_plain_text ( b. contains ( ) ) ,
555+ Kind :: Dividing => gen . body_dividing ( b. first ( ) ) ,
556+ Kind :: CodeBlock => gen . body_code ( b. contains ( ) ) ,
557+ Kind :: UnorderedList => gen . body_unordered_list ( b. contains ( ) ) ,
558+ Kind :: Blank => gen . body_blank ( b. contains ( ) ) ,
558559 Kind :: Quote => {
559560 let s = b
560561 . quote_ast
561562 . as_ref ( )
562- . map ( |a| a. render_html_body ( html ) )
563+ . map ( |a| a. generate_body ( gen ) )
563564 . unwrap_or_else ( || "" . to_string ( ) ) ;
564- html . body_quote ( & s)
565+ gen . body_quote ( & s)
565566 }
566- Kind :: OrderedList => html . body_ordered_list ( b. contains ( ) ) ,
567+ Kind :: OrderedList => gen . body_ordered_list ( b. contains ( ) ) ,
567568 _ => "" . to_string ( ) ,
568569 } )
569570 . join ( "\n " )
0 commit comments