takunomi-blog/postCollection.go

149 lines
2.8 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
var postsCollection []Post
2017-11-18 18:48:47 +00:00
func NewPostsCollection() {
verifyFolderModification()
initializeCollection()
go acquirePosts(100)
}
func initializeCollection() {
2017-11-17 14:12:04 +00:00
postsCollection = nil
filepath.Walk("./posts", newPost)
}
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-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)
return false
}
info, err := f.Stat()
if err != nil {
fmt.Println("info here: \n", info)
return false
}
2017-11-16 22:22:34 +00:00
_modTime := info.ModTime()
if modTime != _modTime {
modTime = _modTime
return true
}
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-17 14:12:04 +00:00
postsCollection = insertPostAccordingToDate(p, postsCollection)
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-09 08:56:33 +00:00
func getNewestPosts(numberOfPosts int) []Post {
2017-11-07 11:16:35 +00:00
var posts []Post
2017-11-18 18:48:47 +00:00
l := len(postsCollection)
for i := l - 1; i >= l-numberOfPosts; i-- {
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 {
for _, post := range postsCollection {
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-18 18:48:47 +00:00
postsCollection = append(postsCollection, post)
return postsCollection
2017-11-16 22:22:34 +00:00
}