takunomi-blog/postCollection.go

67 lines
1.4 KiB
Go
Raw Normal View History

2017-11-07 11:16:35 +00:00
package main
import (
2017-11-09 08:56:33 +00:00
"fmt"
2017-11-07 11:16:35 +00:00
"html/template"
"io/ioutil"
"strings"
2017-11-09 08:56:33 +00:00
"time"
2017-11-07 11:16:35 +00:00
)
2017-11-09 08:56:33 +00:00
//Post : Is exported
2017-11-07 11:16:35 +00:00
type Post struct {
2017-11-09 08:56:33 +00:00
Date time.Time
Title string
2017-11-07 11:16:35 +00:00
URLTitle string
2017-11-09 08:56:33 +00:00
Content template.HTML
2017-11-07 11:16:35 +00:00
}
2017-11-09 08:56:33 +00:00
var lewd = "Lewd Interlude"
var reveries = "Takunomi Coffee Reveries, Vol. I"
var romantic = "Romantic Japanese Christmas Takunomi Saturday Recap"
2017-11-07 11:16:35 +00:00
2017-11-09 08:56:33 +00:00
func spaceToHyphen(s string) string {
2017-11-07 11:16:35 +00:00
return strings.Replace(s, " ", "-", -1)
}
2017-11-09 08:56:33 +00:00
func setDate(year int, month time.Month, day int) time.Time {
2017-11-07 11:16:35 +00:00
return time.Date(year, month, day, 0, 0, 0, 0, time.UTC)
}
2017-11-09 08:56:33 +00:00
func newPost(date time.Time, title string) Post {
2017-11-07 11:16:35 +00:00
return Post{date, title, spaceToHyphen(title), getPostContent(title)}
}
2017-11-09 08:56:33 +00:00
var postsCollection = []Post{
newPost(setDate(2017, time.September, 12), lewd),
2017-11-07 11:16:35 +00:00
newPost(setDate(2017, time.March, 4), reveries),
newPost(setDate(2017, time.April, 23), romantic),
}
2017-11-09 08:56:33 +00:00
func getPostByURLTitle(title string) Post {
2017-11-07 11:16:35 +00:00
for _, post := range postsCollection {
if post.URLTitle == title {
return post
}
}
return Post{}
}
2017-11-09 08:56:33 +00:00
func getPostContent(title string) template.HTML {
2017-11-07 11:16:35 +00:00
content, err := ioutil.ReadFile("posts/" + title)
if err != nil {
fmt.Println(content)
return template.HTML("")
}
return template.HTML(string(content[:len(content)]))
}
2017-11-09 08:56:33 +00:00
func getNewestPosts(numberOfPosts int) []Post {
2017-11-07 11:16:35 +00:00
var posts []Post
for i := 0; i < numberOfPosts; i++ {
post := postsCollection[i]
posts = append(posts, getPostByURLTitle(post.URLTitle))
}
return posts
}