takunomi-blog/postCollection.go

164 lines
3.1 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-16 22:22:34 +00:00
"golang.org/x/net/html"
2017-11-07 11:16:35 +00:00
"html/template"
"io/ioutil"
2017-11-16 22:22:34 +00:00
"os"
"path/filepath"
2017-11-07 11:16:35 +00:00
"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-20 13:37:08 +00:00
Date time.Time
NormalDate string
Title string
URLTitle string
Content template.HTML
2017-11-07 11:16:35 +00:00
}
2017-11-20 13:37:08 +00:00
const htmlDateLayout = "2006-01-02"
const normalDateLayout = "02 Jan 2006"
2017-11-07 11:16:35 +00:00
2017-11-16 22:22:34 +00:00
var modTime time.Time
2017-11-21 09:53:32 +00:00
var PostsCollection []Post
2017-11-16 22:22:34 +00:00
2017-11-18 18:48:47 +00:00
func NewPostsCollection() {
verifyFolderModification()
initializeCollection()
go acquirePosts(100)
}
func initializeCollection() {
2017-11-21 09:53:32 +00:00
PostsCollection = nil
2017-11-17 14:12:04 +00:00
filepath.Walk("./posts", newPost)
2017-11-21 09:53:32 +00:00
PostsCollection = reversePosts()
2017-11-17 14:12:04 +00:00
}
2017-11-18 18:48:47 +00:00
func acquirePosts(duration time.Duration) {
2017-11-16 22:22:34 +00:00
for {
2017-11-18 18:48:47 +00:00
if verifyFolderModification() == true {
initializeCollection()
2017-11-21 09:53:32 +00:00
PostsCollection = reversePosts()
2017-11-16 22:22:34 +00:00
}
time.Sleep(duration * time.Second)
}
2017-11-07 11:16:35 +00:00
}
2017-11-18 18:48:47 +00:00
func verifyFolderModification() bool {
2017-11-17 14:12:04 +00:00
f, err := os.Open("./posts")
if err != nil {
fmt.Println("file-error here: \n", f)
f.Close()
2017-11-17 14:12:04 +00:00
return false
}
info, err := f.Stat()
if err != nil {
fmt.Println("info here: \n", info)
f.Close()
2017-11-17 14:12:04 +00:00
return false
}
2017-11-16 22:22:34 +00:00
_modTime := info.ModTime()
if modTime != _modTime {
modTime = _modTime
f.Close()
2017-11-16 22:22:34 +00:00
return true
}
f.Close()
2017-11-16 22:22:34 +00:00
return false
2017-11-07 11:16:35 +00:00
}
2017-11-16 22:22:34 +00:00
func newPost(path string, f os.FileInfo, err error) error {
2017-11-17 14:12:04 +00:00
content, err := ioutil.ReadFile(path)
2017-11-16 22:22:34 +00:00
if err != nil {
2017-11-17 14:12:04 +00:00
fmt.Println(err)
2017-11-18 18:48:47 +00:00
return nil
2017-11-16 22:22:34 +00:00
}
2017-11-17 14:12:04 +00:00
title := strings.Replace(path, "posts/", "", -1)
if title == ".DS_Store" {
return nil
}
2017-11-16 22:22:34 +00:00
s := string(content)
doc, _ := html.Parse(strings.NewReader(s))
2017-11-20 13:37:08 +00:00
dateParsed := getDate(doc)
dateFormatted := dateParsed.Format(normalDateLayout)
2017-11-16 22:22:34 +00:00
var p = Post{
2017-11-20 13:37:08 +00:00
dateParsed,
dateFormatted,
2017-11-16 22:22:34 +00:00
title,
2017-11-17 14:12:04 +00:00
strings.Replace(title, " ", "-", -1),
2017-11-16 22:22:34 +00:00
template.HTML(string(content[:len(content)])),
}
2017-11-21 09:53:32 +00:00
PostsCollection = insertPostAccordingToDate(p, PostsCollection)
2017-11-17 14:12:04 +00:00
return nil
2017-11-07 11:16:35 +00:00
}
2017-11-16 22:22:34 +00:00
func getDate(n *html.Node) time.Time {
2017-11-17 14:12:04 +00:00
2017-11-18 18:48:47 +00:00
var zero, d, t time.Time
2017-11-17 14:12:04 +00:00
2017-11-16 22:22:34 +00:00
if n.Type == html.ElementNode && n.Data == "time" {
for _, a := range n.Attr {
if a.Key == "datetime" {
2017-11-20 13:37:08 +00:00
t, _ = time.Parse(htmlDateLayout, a.Val)
2017-11-18 18:48:47 +00:00
return t
2017-11-16 22:22:34 +00:00
}
2017-11-07 11:16:35 +00:00
}
}
2017-11-17 14:12:04 +00:00
2017-11-16 22:22:34 +00:00
for c := n.FirstChild; c != nil; c = c.NextSibling {
2017-11-18 18:48:47 +00:00
d = getDate(c)
if d != zero {
return d
}
2017-11-16 22:22:34 +00:00
}
2017-11-07 11:16:35 +00:00
2017-11-17 14:12:04 +00:00
return t
2017-11-07 11:16:35 +00:00
}
2017-11-21 09:53:32 +00:00
func reversePosts() []Post {
length := len(PostsCollection)
s := make([]Post, length)
for i := 0; i < length; i++ {
s[i] = PostsCollection[length-(i+1)]
}
return s
}
2017-11-09 08:56:33 +00:00
func getNewestPosts(numberOfPosts int) []Post {
2017-11-07 11:16:35 +00:00
var posts []Post
2017-11-21 09:53:32 +00:00
l := len(PostsCollection)
2017-11-18 18:48:47 +00:00
for i := l - 1; i >= l-numberOfPosts; i-- {
2017-11-21 09:53:32 +00:00
posts = append(posts, PostsCollection[i])
2017-11-07 11:16:35 +00:00
}
return posts
}
2017-11-16 22:22:34 +00:00
func getPostByURLTitle(title string) Post {
2017-11-21 09:53:32 +00:00
for _, post := range PostsCollection {
2017-11-16 22:22:34 +00:00
if post.URLTitle == title {
return post
}
}
return Post{}
}
2017-11-17 14:12:04 +00:00
func insertPostAccordingToDate(post Post, arr []Post) []Post {
2017-11-16 22:22:34 +00:00
for i, p := range arr {
if p.Date.After(post.Date) {
s := make([]Post, len(arr)+1, cap(arr)+1)
copy(s[:], arr[:]) //make a copy of the slice
copy(s[i+1:], arr[i:]) //move the upper part of the slice ahead, creating a hole
s[i] = post //insert new element into hole
2017-11-18 18:48:47 +00:00
return s
2017-11-16 22:22:34 +00:00
}
}
2017-11-21 09:53:32 +00:00
PostsCollection = append(PostsCollection, post)
return PostsCollection
2017-11-16 22:22:34 +00:00
}