@@ -8,6 +8,27 @@ use std::path::Path;
88use std:: sync:: Arc ;
99use tera:: Tera ;
1010
11+ /// Renderer for the tera backend.
12+ ///
13+ /// This will read all the files in the `RenderContext` and render them using the `Tera` template.
14+ ///
15+ /// # Example
16+ ///
17+ /// ```
18+ /// let mut stdin = io::stdin();
19+ /// let ctx = RenderContext::from_json(&mut stdin).unwrap();
20+ /// let config: TeraRendererConfig = ctx
21+ /// .config
22+ /// .get_deserialized_opt("output.tera-backend")
23+ /// .expect("Failed to get tera-backend config")
24+ /// .unwrap();
25+ ///
26+ /// let tera_template = config
27+ /// .create_template(&ctx.root)
28+ /// .expect("Failed to create components");
29+ /// let mut renderer = Renderer::new(ctx, tera_template).expect("Failed to create renderer");
30+ /// renderer.render_book().expect("Failed to render book");
31+ /// ```
1132pub ( crate ) struct Renderer {
1233 ctx : Arc < RenderContext > ,
1334 serialized_ctx : serde_json:: Value ,
@@ -16,6 +37,12 @@ pub(crate) struct Renderer {
1637}
1738
1839impl Renderer {
40+ /// 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.
1946 pub ( crate ) fn new ( ctx : RenderContext , tera_template : Tera ) -> Result < Renderer > {
2047 let mut renderer = Renderer {
2148 serialized_ctx : serde_json:: to_value ( & ctx) ?,
@@ -29,6 +56,16 @@ impl Renderer {
2956 Ok ( renderer)
3057 }
3158
59+ /// Render the book.
60+ pub ( crate ) fn render_book ( & mut self ) -> Result < ( ) > {
61+ let dest_dir = self . ctx . destination . parent ( ) . unwrap ( ) . to_owned ( ) ;
62+ if !dest_dir. is_dir ( ) {
63+ return Err ( anyhow ! ( "{dest_dir:?} is not a directory" ) ) ;
64+ }
65+ self . render_book_directory ( & dest_dir)
66+ }
67+
68+ /// Create the `get_context` function for the `Tera` template, a helper that allows retrieving values from `ctx`.
3269 fn create_get_context_function ( & self ) -> impl tera:: Function {
3370 let ctx_rc = Arc :: clone ( & self . ctx ) ;
3471 move |args : & HashMap < String , serde_json:: value:: Value > | -> tera:: Result < tera:: Value > {
@@ -48,14 +85,7 @@ impl Renderer {
4885 }
4986 }
5087
51- pub ( crate ) fn render_book ( & mut self ) -> Result < ( ) > {
52- let dest_dir = self . ctx . destination . parent ( ) . unwrap ( ) . to_owned ( ) ;
53- if !dest_dir. is_dir ( ) {
54- return Err ( anyhow ! ( "{dest_dir:?} is not a directory" ) ) ;
55- }
56- self . render_book_directory ( & dest_dir)
57- }
58-
88+ /// Render the book directory located at `path` recursively.
5989 fn render_book_directory ( & mut self , path : & Path ) -> Result < ( ) > {
6090 for entry in path. read_dir ( ) ? {
6191 let entry = entry?;
@@ -69,17 +99,23 @@ impl Renderer {
6999 Ok ( ( ) )
70100 }
71101
102+ /// Reads the file at `path` and renders it.
72103 fn process_file ( & mut self , path : & Path ) -> Result < ( ) > {
73104 if path. extension ( ) . unwrap_or_default ( ) != "html" {
74105 return Ok ( ( ) ) ;
75106 }
76107 let file_content = std:: fs:: read_to_string ( path) ?;
77- let output = self . render ( & file_content, path) ?;
108+ let output = self . render_file_content ( & file_content, path) ?;
78109 let mut output_file = fs:: File :: create ( path) ?;
79110 output_file. write_all ( output. as_bytes ( ) ) ?;
80111 Ok ( ( ) )
81112 }
82113
114+ /// Creates the rendering context to be passed to the templates.
115+ ///
116+ /// # Arguments
117+ ///
118+ /// `path`: The path to the file that will be added as extra context to the renderer.
83119 fn create_context ( & mut self , path : & Path ) -> tera:: Context {
84120 let mut context = tera:: Context :: new ( ) ;
85121 context. insert ( "path" , path) ;
@@ -92,7 +128,17 @@ impl Renderer {
92128 context
93129 }
94130
95- fn render ( & mut self , file_content : & str , path : & Path ) -> Result < String > {
131+ /// 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.
141+ fn render_file_content ( & mut self , file_content : & str , path : & Path ) -> Result < String > {
96142 let tera_context = self . create_context ( path) ;
97143
98144 let rendered_file = self
0 commit comments