takunomi-blog/postUtils.go

108 lines
2.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"
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"
2022-06-02 18:41:16 +00:00
const normalDateLayout = "02 Jan - 06"
2017-11-16 22:22:34 +00:00
var modTime time.Time
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)
2017-12-02 19:37:45 +00:00
if !strings.Contains(title, ".html") {
2017-11-17 14:12:04 +00:00
return nil
}
2017-12-02 19:37:45 +00:00
title = strings.Replace(title, ".html", "", -1)
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)])),
}
2021-11-30 19:23:35 +00:00
posts = insertPostAccordingToDate(p, posts)
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 {
2021-11-30 19:23:35 +00:00
length := len(posts)
2017-11-21 09:53:32 +00:00
s := make([]Post, length)
for i := 0; i < length; i++ {
2021-11-30 19:23:35 +00:00
s[i] = posts[length-(i+1)]
2017-11-21 09:53:32 +00:00
}
return s
}
2017-11-16 22:22:34 +00:00
func getPostByURLTitle(title string) Post {
2021-11-30 19:23:35 +00:00
for _, post := range posts {
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
}
}
2021-11-30 19:23:35 +00:00
posts = append(posts, post)
return posts
2017-11-16 22:22:34 +00:00
}