@@ -31,23 +31,14 @@ use tera::Tera;
3131/// ```
3232pub ( crate ) struct Renderer {
3333 ctx : Arc < RenderContext > ,
34- serialized_ctx : serde_json:: Value ,
35- counter : u64 ,
3634 tera_template : Tera ,
3735}
3836
3937impl Renderer {
4038 /// Create a new `Renderer` from the `RenderContext` and `Tera` template.
41- ///
42- /// # Arguments
43- ///
44- /// `ctx`: The `RenderContext` to be used for rendering. This is usually obtained from `stdin`.
45- /// `tera_template`: A pre-configured `Tera` template.
4639 pub ( crate ) fn new ( ctx : RenderContext , tera_template : Tera ) -> Result < Renderer > {
4740 let mut renderer = Renderer {
48- serialized_ctx : serde_json:: to_value ( & ctx) ?,
4941 ctx : Arc :: new ( ctx) ,
50- counter : 0 ,
5142 tera_template,
5243 } ;
5344 renderer
@@ -56,11 +47,21 @@ impl Renderer {
5647 Ok ( renderer)
5748 }
5849
59- /// Render the book.
50+ /// Render the book. This goes through the output of the HTML renderer
51+ /// by considering all the output HTML files as input to the Tera template.
52+ /// It overwrites the preexisting files with their Tera-rendered version.
6053 pub ( crate ) fn render_book ( & mut self ) -> Result < ( ) > {
61- let dest_dir = self . ctx . destination . parent ( ) . unwrap ( ) . to_owned ( ) ;
54+ let dest_dir = self
55+ . ctx
56+ . destination
57+ . parent ( )
58+ . unwrap ( )
59+ . join ( "html" )
60+ . to_owned ( ) ;
6261 if !dest_dir. is_dir ( ) {
63- return Err ( anyhow ! ( "{dest_dir:?} is not a directory" ) ) ;
62+ return Err ( anyhow ! (
63+ "{dest_dir:?} is not a directory. Please make sure the HTML renderer is enabled."
64+ ) ) ;
6465 }
6566 self . render_book_directory ( & dest_dir)
6667 }
@@ -116,30 +117,19 @@ impl Renderer {
116117 /// # Arguments
117118 ///
118119 /// `path`: The path to the file that will be added as extra context to the renderer.
119- fn create_context ( & mut self , path : & Path ) -> tera:: Context {
120+ fn create_context ( & mut self , path : & Path ) -> Result < tera:: Context > {
120121 let mut context = tera:: Context :: new ( ) ;
121122 context. insert ( "path" , path) ;
122- context. insert ( "ctx" , & self . serialized_ctx ) ;
123+ context. insert ( "ctx" , & serde_json :: to_value ( & * self . ctx ) ? ) ;
123124 context. insert ( "book_dir" , & self . ctx . destination . parent ( ) . unwrap ( ) ) ;
124- context. insert ( "counter" , & self . counter ) ;
125125 context. insert ( "attributes" , & BTreeMap :: < String , String > :: new ( ) ) ;
126- self . counter += 1 ;
127126
128- context
127+ Ok ( context)
129128 }
130129
131130 /// Rendering logic for an individual file.
132- ///
133- /// # Arguments
134- ///
135- /// `file_content`: The content of the file to be rendered.
136- /// `path`: The path of the file to be rendered.
137- ///
138- /// # Returns
139- ///
140- /// The rendered file.
141131 fn render_file_content ( & mut self , file_content : & str , path : & Path ) -> Result < String > {
142- let tera_context = self . create_context ( path) ;
132+ let tera_context = self . create_context ( path) ? ;
143133
144134 let rendered_file = self
145135 . tera_template
0 commit comments