8000 Merge pull request #365 from dustinknopoff/master · Amndeep7/blog.rust-lang.org@445918c · GitHub
[go: up one dir, main page]

Skip to content

Commit 445918c

Browse files
Merge pull request rust-lang#365 from dustinknopoff/master
Add Optional release tag
2 parents 5bc444e + e44bba4 commit 445918c

File tree

2 files changed

+75
-21
lines changed

2 files changed

+75
-21
lines changed

posts/2019-01-17-Rust-1.32.0.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
layout: post
33
title: "Announcing Rust 1.32.0"
44
author: The Rust Release Team
5+
release: true
56
---
67

78
The Rust team is happy to announce a new version of Rust, 1.32.0. Rust is a

src/main.rs

Lines changed: 74 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77

88
use comrak::ComrakOptions;
99

10-
use handlebars::{Handlebars, Helper, Context, RenderContext, Output, HelperResult, RenderError};
10+
use handlebars::{Context, Handlebars, Helper, HelperResult, Output, RenderContext, RenderError};
1111

1212
use serde_derive::{Deserialize, Serialize};
1313
use serde_json::json;
@@ -20,7 +20,7 @@ struct Blog {
2020
out_directory: PathBuf,
2121
}
2222

23-
#[derive(Debug, Serialize, Deserialize)]
23+
#[derive(Debug, Serialize, Deserialize, Clone)]
2424
struct Post {
2525
filename: String,
2626
title: String,
@@ -32,32 +32,58 @@ struct Post {
3232
contents: String,
3333
url: String,
3434
published: String,
35+
release: bool,
3536
}
3637

3738
#[derive(Debug, PartialEq, Serialize, Deserialize)]
3839
struct YamlHeader {
3940
title: String,
4041
author: String,
42+
#[serde(default)]
43+
release: bool,
4144
}
4245

43-
fn hb_month_helper<'a>(h: &Helper, _b: &Handlebars, _ctx: &Context, _rc: &mut RenderContext,
44-
out: &mut Output) -> HelperResult {
45-
let num: u32 = h.param(0).unwrap().value().as_str().unwrap().parse()
46+
#[derive(Debug, Serialize)]
47+
struct Releases {
48+
releases: Vec<ReleasePost>,
49+
feed_updated: String,
50+
}
51+
52+
#[derive(Debug, Serialize)]
53+
struct ReleasePost {
54+
title: String,
55+
url: String,
56+
}
57+
58+
fn hb_month_helper<'a>(
59+
h: &Helper,
60+
_b: &Handlebars,
61+
_ctx: &Context,
62+
_rc: &mut RenderContext,
63+
out: &mut Output,
64+
) -> HelperResult {
65+
let num: u32 = h
66+
.param(0)
67+
.unwrap()
68+
.value()
69+
.as_str()
70+
.unwrap()
71+
.parse()
4672
.or_else(|_| Err(RenderError::new("The value is not a number")))?;
4773
let name = match num {
48-
1 => "Jan.",
49-
2 => "Feb.",
50-
3 => "Mar.",
51-
4 => "Apr.",
52-
5 => "May",
53-
6 => "June",
54-
7 => "July",
55-
8 => "Aug.",
56-
9 => "Sept.",
74+
1 => "Jan.",
75+
2 => "Feb.",
76+
3 => "Mar.",
77+
4 => "Apr.",
78+
5 => "May",
79+
6 => "June",
80+
7 => "July",
81+
8 => "Aug.",
82+
9 => "Sept.",
5783
10 => "Oct.",
5884
11 => "Nov.",
5985
12 => "Dec.",
60-
_ => "Error!",
86+
_ => "Error!",
6187
};
6288
out.write(name)?;
6389
Ok(())
@@ -109,9 +135,11 @@ impl Blog {
109135
// so we need to find the end. we need the fours to adjust for those first bytes
110136
let end_of_yaml = contents[4..].find("---").unwrap() + 4;
111137
let yaml = &contents[..end_of_yaml];
112-
113-
let YamlHeader { author, title } = serde_yaml::from_str(yaml)?;
114-
138+
let YamlHeader {
139+
author,
140+
title,
141+
release,
142+
} = serde_yaml::from_str(yaml)?;
115143
// next, the contents. we add + to get rid of the final "---\n\n"
116144
let options = ComrakOptions {
117145
ext_header_ids: Some(String::new()),
@@ -126,7 +154,7 @@ impl Blog {
126154

127155
// this is fine
128156
let url = format!("{}/{}/{}/{}", year, month, day, url.to_str().unwrap());
129-
157+
130158
// build the published time. this is only approximate, which is fine.
131159
// we do some unwraps because these need to be valid
132160
let published = time::Tm {
@@ -157,6 +185,7 @@ impl Blog {
157185
contents,
158186
url,
159187
published,
188+
release,
160189
};
161190

162191
posts.push(post);
@@ -168,7 +197,7 @@ impl Blog {
168197

169198
posts.reverse();
170199

171-
for i in 1 .. posts.len() {
200+
for i in 1..posts.len() {
172201
posts[i].show_year = posts[i - 1].year != posts[i].year;
173202
}
174203

@@ -192,6 +221,8 @@ impl Blog {
192221

193222
self.copy_static_files()?;
194223

224+
self.generate_releases_feed()?;
225+
195226
Ok(())
196227
}
197228

@@ -258,12 +289,34 @@ impl Blog {
258289

259290
fn render_feed(&self) -> Result<(), Box<Error>> {
260291
let posts: Vec<_> = self.posts.iter().by_ref().take(10).collect();
261-
let data = json!({ "posts": posts, "feed_updated": time::now_utc().rfc3339().to_string() });
292+
let data =
293+
json!({ "posts": posts, "feed_updated": time::now_utc().rfc3339().to_string() });
262294

263295
self.render_template("feed.xml", "feed", data)?;
264296
Ok(())
265297
}
266298

299+
fn generate_releases_feed(&self) -> Result<(), Box<Error>> {
300+
let posts = self.posts.clone();
301+
let is_released: Vec<&Post> = posts.iter().filter(|post| post.release).collect();
302+
let releases: Vec<ReleasePost> = is_released
303+
.iter()
304+
.map(|post| ReleasePost {
305+
title: post.title.clone(),
306+
url: post.url.clone(),
307+
})
308+
.collect();
309+
let data = Releases {
310+
releases: releases,
311+
feed_updated: time::now_utc().rfc3339().to_string(),
312+
};
313+
fs::write(
314+
self.out_directory.join("releases.json"),
315+
serde_json::to_string(&data)?,
316+
)?;
317+
Ok(())
318+
}
319+
267320
fn copy_static_files(&self) -> Result<(), Box<Error>> {
268321
use fs_extra::dir::{self, CopyOptions};
269322

0 commit comments

Comments
 (0)
0