@@ -9,7 +9,6 @@ use rayon::prelude::*;
9
9
use sass_rs:: { Options , compile_file} ;
10
10
use serde:: Serialize ;
11
11
use serde_json:: { Value , json} ;
12
- use std:: collections:: HashMap ;
13
12
use std:: fs:: { self , File } ;
14
13
use std:: io:: { self , Write } ;
15
14
use std:: path:: { Path , PathBuf } ;
@@ -33,53 +32,12 @@ struct ReleasePost {
33
32
url : String ,
34
33
}
35
34
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
-
75
35
impl Generator {
76
36
fn new (
77
37
out_directory : impl AsRef < Path > ,
78
38
posts_directory : impl AsRef < Path > ,
79
39
) -> eyre:: Result < Self > {
80
40
let mut tera = Tera :: new ( "templates/*" ) ?;
81
- tera. register_filter ( "month_name" , month_name) ;
82
- tera. register_filter ( "escape_hbs" , escape_hbs) ;
83
41
tera. autoescape_on ( vec ! [ ] ) ; // disable auto-escape for .html templates
84
42
Ok ( Generator {
85
43
tera,
@@ -148,7 +106,7 @@ impl Generator {
148
106
}
149
107
150
108
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 ( ) ) ) ?;
152
110
153
111
let path = self . render_index ( blog) ?;
154
112
@@ -177,24 +135,24 @@ impl Generator {
177
135
. map ( |other_blog| {
178
136
json ! ( {
179
137
"link_text" : other_blog. link_text( ) ,
180
- "url" : other_blog. prefix ( ) . join( "index.html" ) ,
138
+ "url" : other_blog. path ( ) . join( "index.html" ) ,
181
139
} )
182
140
} )
183
141
. collect ( ) ;
184
142
185
143
let data = json ! ( {
186
144
"title" : blog. index_title( ) ,
187
- "blog " : blog,
145
+ "section " : blog,
188
146
"other_blogs" : other_blogs,
189
147
} ) ;
190
- let path = blog. prefix ( ) . join ( "index.html" ) ;
148
+ let path = blog. path ( ) . join ( "index.html" ) ;
191
149
self . render_template ( & path, "index.html" , data) ?;
192
150
Ok ( path)
193
151
}
194
152
195
153
fn render_post ( & self , blog : & Blog , post : & Post ) -> eyre:: Result < PathBuf > {
196
154
let path = blog
197
- . prefix ( )
155
+ . path ( )
198
156
. join ( format ! ( "{:04}" , & post. year) )
199
157
. join ( format ! ( "{:02}" , & post. month) )
200
158
. join ( format ! ( "{:02}" , & post. day) ) ;
@@ -206,8 +164,8 @@ impl Generator {
206
164
207
165
let data = json ! ( {
208
166
"title" : format!( "{} | {}" , post. title, blog. title( ) ) ,
209
- "blog " : blog,
210
- "post " : post,
167
+ "section " : blog,
168
+ "page " : post,
211
169
} ) ;
212
170
213
171
let path = path. join ( filename) ;
@@ -218,12 +176,12 @@ impl Generator {
218
176
fn render_feed ( & self , blog : & Blog ) -> eyre:: Result < ( ) > {
219
177
let posts: Vec < _ > = blog. posts ( ) . iter ( ) . take ( 10 ) . collect ( ) ;
220
178
let data = json ! ( {
221
- "blog " : blog,
222
- "posts " : posts,
179
+ "section " : blog,
180
+ "pages " : posts,
223
181
"feed_updated" : chrono:: Utc :: now( ) . with_nanosecond( 0 ) . unwrap( ) . to_rfc3339( ) ,
224
182
} ) ;
225
183
226
- self . render_template ( blog. prefix ( ) . join ( "feed.xml" ) , "feed.xml" , data) ?;
184
+ self . render_template ( blog. path ( ) . join ( "feed.xml" ) , "feed.xml" , data) ?;
227
185
Ok ( ( ) )
228
186
}
229
187
@@ -235,8 +193,8 @@ impl Generator {
235
193
. map ( |post| ReleasePost {
236
194
title : post. title . clone ( ) ,
237
195
url : blog
238
- . prefix ( )
239
- . join ( post. url . clone ( ) )
196
+ . path ( )
197
+ . join ( post. path . clone ( ) )
240
198
. to_string_lossy ( )
241
199
. to_string ( ) ,
242
200
} )
@@ -246,7 +204,7 @@ impl Generator {
246
204
feed_updated : chrono:: Utc :: now ( ) . with_nanosecond ( 0 ) . unwrap ( ) . to_rfc3339 ( ) ,
247
205
} ;
248
206
fs:: write (
249
- self . out_directory . join ( blog. prefix ( ) ) . join ( "releases.json" ) ,
207
+ self . out_directory . join ( blog. path ( ) ) . join ( "releases.json" ) ,
250
208
serde_json:: to_string ( & data) ?,
251
209
) ?;
252
210
Ok ( ( ) )
0 commit comments