Skip to content

Commit d0015f7

Browse files
authored
Merge pull request #323 from Aloso/year-date-on-homepage
Add publish date and author to posts
2 parents 98e3f3b + 8a59981 commit d0015f7

File tree

4 files changed

+58
-4
lines changed

4 files changed

+58
-4
lines changed

src/main.rs

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use std::{
77

88
use comrak::ComrakOptions;
99

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

1212
use serde_derive::{Deserialize, Serialize};
1313
use serde_json::json;
@@ -26,6 +26,7 @@ struct Post {
2626
title: String,
2727
author: String,
2828
year: String,
29+
show_year: bool,
2930
month: String,
3031
day: String,
3132
contents: String,
@@ -38,6 +39,29 @@ struct YamlHeader {
3839
author: String,
3940
}
4041

42+
fn hb_month_helper<'a>(h: &Helper, _b: &Handlebars, _ctx: &Context, _rc: &mut RenderContext,
43+
out: &mut Output) -> HelperResult {
44+
let num: u32 = h.param(0).unwrap().value().as_str().unwrap().parse()
45+
.or_else(|_| Err(RenderError::new("The value is not a number")))?;
46+
let name = match num {
47+
1 => "Jan.",
48+
2 => "Feb.",
49+
3 => "Mar.",
50+
4 => "Apr.",
51+
5 => "May",
52+
6 => "June",
53+
7 => "July",
54+
8 => "Aug.",
55+
9 => "Sept.",
56+
10 => "Oct.",
57+
11 => "Nov.",
58+
12 => "Dec.",
59+
_ => "Error!",
60+
};
61+
out.write(name)?;
62+
Ok(())
63+
}
64+
4165
impl Blog {
4266
fn new<T>(out_directory: T, posts_directory: T) -> Result<Blog, Box<Error>>
4367
where
@@ -49,6 +73,8 @@ impl Blog {
4973

5074
handlebars.register_templates_directory(".hbs", "templates")?;
5175

76+
handlebars.register_helper("month_name", Box::new(hb_month_helper));
77+
5278
let posts = Blog::load_posts(posts_directory.into())?;
5379

5480
Ok(Blog {
@@ -105,6 +131,7 @@ impl Blog {
105131
title,
106132
author,
107133
year,
134+
show_year: false,
108135
month,
109136
day,
110137
contents,
@@ -120,6 +147,10 @@ impl Blog {
120147

121148
posts.reverse();
122149

150+
for i in 1 .. posts.len() {
151+
posts[i].show_year = posts[i - 1].year != posts[i].year;
152+
}
153+
123154
Ok(posts)
124155
}
125156

src/styles/app.scss

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,18 @@ div.brand {
145145
}
146146
}
147147

148+
table.post-list a {
149+
text-decoration: none;
150+
}
151+
table.post-list a:hover {
152+
text-decoration: underline;
153+
}
154+
155+
.publish-date-author {
156+
color: $gray;
157+
margin: -60px 0 60px 0;
158+
}
159+
148160
footer {
149161
padding: 30px 0;
150162
background-color: black;

templates/index.hbs

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,20 @@
99

1010
<section id="posts" class="purple">
1111
<div class="w-100 mw-none ph3 mw8-m mw9-l center f3">
12-
<ul>
12+
13+
<table class="post-list collapse w-100 f2-l f2-m f3-s">
1314
{{#each posts}}
14-
<li><h3><a href="{{url}}">{{title}}</a></h3></li>
15+
{{#if show_year}}<tr>
16+
<td class="bn"></td>
17+
<td class="bn"><h3 class="f0-l f1-m f2-s mt4 mb0">{{year}}</h3></td>
18+
</tr>{{/if}}
19+
<tr>
20+
<td class="tr o-60 pr4 pr5-l bn">{{month_name month}}&nbsp;{{day}}</td>
21+
<td class="bn"><a href="{{url}}">{{title}}</a></td>
22+
</tr>
1523
{{/each}}
16-
</ul>
24+
</table>
25+
1726
</div>
1827
</section>
1928
{{/inline}}

templates/post.hbs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
<div class="highlight"></div>
77
</header>
88

9+
<div class="publish-date-author">{{month_name post.month}} {{post.day}}, {{post.year}} &middot; {{post.author}}</div>
10+
911
<div class="post">
1012
{{{ post.contents }}}
1113
</div>

0 commit comments

Comments
 (0)