Skip to content

Commit 612ddbc

Browse files
committed
Add version field for ReleasePost
1 parent 7cc5e2d commit 612ddbc

File tree

1 file changed

+45
-16
lines changed

1 file changed

+45
-16
lines changed

src/main.rs

Lines changed: 45 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,20 @@
1-
mod blogs;
2-
mod posts;
1+
use std::convert::AsRef;
2+
use std::error::Error;
3+
use std::fs::{self, File};
4+
use std::io::Write;
5+
use std::path::{Path, PathBuf};
36

4-
use crate::blogs::Blog;
5-
use crate::posts::Post;
67
use chrono::Timelike;
78
use handlebars::{handlebars_helper, Handlebars};
89
use sass_rs::{compile_file, Options};
910
use serde_derive::Serialize;
1011
use serde_json::json;
11-
use std::convert::AsRef;
12-
use std::error::Error;
13-
use std::fs::{self, File};
14-
use std::io::Write;
15-
use std::path::{Path, PathBuf};
12+
13+
use crate::blogs::Blog;
14+
use crate::posts::Post;
15+
16+
mod blogs;
17+
mod posts;
1618

1719
struct Generator<'a> {
1820
handlebars: Handlebars<'a>,
@@ -30,7 +32,33 @@ struct Releases {
3032
struct ReleasePost {
3133
title: String,
3234
url: String,
35+
version: String,
36+
}
37+
38+
impl ReleasePost {
39+
fn new(title: String, url: String) -> ReleasePost {
40+
let version = ReleasePost::parse_version_from_url(&url).unwrap();
41+
ReleasePost {
42+
url,
43+
title,
44+
version,
45+
}
46+
}
47+
48+
fn parse_version_from_url(url: &str) -> Option<String> {
49+
// Normalize special cases, such as /2020/05/07/Rust.1.43.1.html
50+
let url = url.to_lowercase().replacen("rust.", "rust-", 1);
51+
let mut split = url.split('-');
52+
split.nth(1).map(|v| {
53+
let mut v = v.replace(".html", "");
54+
if v.matches('.').count() == 1 {
55+
v.push_str(".0");
56+
}
57+
v
58+
})
59+
}
3360
}
61+
3462
handlebars_helper!(hb_month_name_helper: |month_num: u64| match month_num {
3563
1 => "Jan.",
3664
2 => "Feb.",
@@ -175,13 +203,14 @@ impl<'a> Generator<'a> {
175203
let is_released: Vec<&Post> = posts.iter().filter(|post| post.release).collect();
176204
let releases: Vec<ReleasePost> = is_released
177205
.iter()
178-
.map(|post| ReleasePost {
179-
title: post.title.clone(),
180-
url: blog
181-
.prefix()
182-
.join(post.url.clone())
183-
.to_string_lossy()
184-
.to_string(),
206+
.map(|post| {
207+
ReleasePost::new(
208+
post.title.clone(),
209+
blog.prefix()
210+
.join(post.url.clone())
211+
.to_string_lossy()
212+
.to_string(),
213+
)
185214
})
186215
.collect();
187216
let data = Releases {

0 commit comments

Comments
 (0)