takunomi-blog/postCollection.go
2017-11-07 12:16:35 +01:00

66 lines
1.4 KiB
Go

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