@@ -9,7 +9,6 @@ use rayon::prelude::*;
99use sass_rs:: { Options , compile_file} ;
1010use serde:: Serialize ;
1111use serde_json:: { Value , json} ;
12- use std:: collections:: HashMap ;
1312use std:: fs:: { self , File } ;
1413use std:: io:: { self , Write } ;
1514use std:: path:: { Path , PathBuf } ;
@@ -33,53 +32,12 @@ struct ReleasePost {
3332 url : String ,
3433}
3534
36- fn month_name ( month_num : & Value , _args : & HashMap < String , Value > ) -> tera:: Result < Value > {
37- let month_num = month_num
38- . as_u64 ( )
39- . expect ( "month_num should be an unsigned integer" ) ;
40- let name = match month_num {
41- 1 => "Jan." ,
42- 2 => "Feb." ,
43- 3 => "Mar." ,
44- 4 => "Apr." ,
45- 5 => "May" ,
46- 6 => "June" ,
47- 7 => "July" ,
48- 8 => "Aug." ,
49- 9 => "Sept." ,
50- 10 => "Oct." ,
51- 11 => "Nov." ,
52- 12 => "Dec." ,
53- _ => panic ! ( "invalid month! ({month_num})" ) ,
54- } ;
55- Ok ( name. into ( ) )
56- }
57-
58- // Tera and Handlebars escape HTML differently by default.
59- // Tera: &<>"'/
60- // Handlebars: &<>"'`=
61- // To make the transition testable, this function escapes just like Handlebars.
62- fn escape_hbs ( input : & Value , _args : & HashMap < String , Value > ) -> tera:: Result < Value > {
63- let input = input. as_str ( ) . expect ( "input should be a string" ) ;
64- Ok ( input
65- . replace ( "&" , "&" )
66- . replace ( "<" , "<" )
67- . replace ( ">" , ">" )
68- . replace ( "\" " , """ )
69- . replace ( "'" , "'" )
70- . replace ( "`" , "`" )
71- . replace ( "=" , "=" )
72- . into ( ) )
73- }
74-
7535impl Generator {
7636 fn new (
7737 out_directory : impl AsRef < Path > ,
7838 posts_directory : impl AsRef < Path > ,
7939 ) -> eyre:: Result < Self > {
8040 let mut tera = Tera :: new ( "templates/*" ) ?;
81- tera. register_filter ( "month_name" , month_name) ;
82- tera. register_filter ( "escape_hbs" , escape_hbs) ;
8341 tera. autoescape_on ( vec ! [ ] ) ; // disable auto-escape for .html templates
8442 Ok ( Generator {
8543 tera,
@@ -148,7 +106,7 @@ impl Generator {
148106 }
149107
150108 fn render_blog ( & self , blog : & Blog ) -> eyre:: Result < ( ) > {
151- std:: fs:: create_dir_all ( self . out_directory . join ( blog. prefix ( ) ) ) ?;
109+ std:: fs:: create_dir_all ( self . out_directory . join ( blog. path ( ) ) ) ?;
152110
153111 let path = self . render_index ( blog) ?;
154112
@@ -177,24 +135,24 @@ impl Generator {
177135 . map ( |other_blog| {
178136 json ! ( {
179137 "link_text" : other_blog. link_text( ) ,
180- "url" : other_blog. prefix ( ) . join( "index.html" ) ,
138+ "url" : other_blog. path ( ) . join( "index.html" ) ,
181139 } )
182140 } )
183141 . collect ( ) ;
184142
185143 let data = json ! ( {
186144 "title" : blog. index_title( ) ,
187- "blog " : blog,
145+ "section " : blog,
188146 "other_blogs" : other_blogs,
189147 } ) ;
190- let path = blog. prefix ( ) . join ( "index.html" ) ;
148+ let path = blog. path ( ) . join ( "index.html" ) ;
191149 self . render_template ( & path, "index.html" , data) ?;
192150 Ok ( path)
193151 }
194152
195153 fn render_post ( & self , blog : & Blog , post : & Post ) -> eyre:: Result < PathBuf > {
196154 let path = blog
197- . prefix ( )
155+ . path ( )
198156 . join ( format ! ( "{:04}" , & post. year) )
199157 . join ( format ! ( "{:02}" , & post. month) )
200158 . join ( format ! ( "{:02}" , & post. day) ) ;
@@ -206,8 +164,8 @@ impl Generator {
206164
207165 let data = json ! ( {
208166 "title" : format!( "{} | {}" , post. title, blog. title( ) ) ,
209- "blog " : blog,
210- "post " : post,
167+ "section " : blog,
168+ "page " : post,
211169 } ) ;
212170
213171 let path = path. join ( filename) ;
@@ -218,12 +176,12 @@ impl Generator {
218176 fn render_feed ( & self , blog : & Blog ) -> eyre:: Result < ( ) > {
219177 let posts: Vec < _ > = blog. posts ( ) . iter ( ) . take ( 10 ) . collect ( ) ;
220178 let data = json ! ( {
221- "blog " : blog,
222- "posts " : posts,
179+ "section " : blog,
180+ "pages " : posts,
223181 "feed_updated" : chrono:: Utc :: now( ) . with_nanosecond( 0 ) . unwrap( ) . to_rfc3339( ) ,
224182 } ) ;
225183
226- self . render_template ( blog. prefix ( ) . join ( "feed.xml" ) , "feed.xml" , data) ?;
184+ self . render_template ( blog. path ( ) . join ( "feed.xml" ) , "feed.xml" , data) ?;
227185 Ok ( ( ) )
228186 }
229187
@@ -235,8 +193,8 @@ impl Generator {
235193 . map ( |post| ReleasePost {
236194 title : post. title . clone ( ) ,
237195 url : blog
238- . prefix ( )
239- . join ( post. url . clone ( ) )
196+ . path ( )
197+ . join ( post. path . clone ( ) )
240198 . to_string_lossy ( )
241199 . to_string ( ) ,
242200 } )
@@ -246,7 +204,7 @@ impl Generator {
246204 feed_updated : chrono:: Utc :: now ( ) . with_nanosecond ( 0 ) . unwrap ( ) . to_rfc3339 ( ) ,
247205 } ;
248206 fs:: write (
249- self . out_directory . join ( blog. prefix ( ) ) . join ( "releases.json" ) ,
207+ self . out_directory . join ( blog. path ( ) ) . join ( "releases.json" ) ,
250208 serde_json:: to_string ( & data) ?,
251209 ) ?;
252210 Ok ( ( ) )
0 commit comments