takunomi-blog/postUtils.go

195 lines
4.7 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
)
type Post struct {
2017-11-20 13:37:08 +00:00
Date time.Time
NormalDate string
2022-06-09 18:26:01 +00:00
Year int
2017-11-20 13:37:08 +00:00
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-09 18:26:01 +00:00
const normalDateLayout = "02 Jan"
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)
2022-06-09 18:26:01 +00:00
fmt.Println(dateParsed.Year())
2017-11-16 22:22:34 +00:00
var p = Post{
2017-11-20 13:37:08 +00:00
dateParsed,
dateFormatted,
2022-06-09 18:26:01 +00:00
dateParsed.Year(),
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)])),
}
2022-06-09 18:26:01 +00:00
fmt.Println(len(years))
if len(years) == 0 { //List is empty
fmt.Println("first")
years = append(years, []Post{p})
} else { // or go through each year
for i := 0; i < len(years); i++ {
if p.Year < years[i][0].Year{//This year is before the current year
// Insert a new year before this year
fmt.Println("earlier")
if i == 0 {
var earliestYear = [][]Post{[]Post{p}}
years = append(earliestYear, years...)
fmt.Println("earliest")
} else {
years = append(years[:i+1],years[i:]...)
years[i] = []Post{p}
fmt.Println("early inbetween")
}
break
} else if p.Year == years[i][0].Year { //found the year
fmt.Println("same year")
//n := append(years[i], p) //add the post to the year (fix sorting of that later)
n := insertPostAccordingToDate(p, years[i])
years[i] = n
break
} else if p.Year > years[i][0].Year { //Is this year greater?
fmt.Println("older")
if i == len(years) - 1 { // Is this the final element?
fmt.Println("oldest")
y := append(years, []Post{p}) // then append a new year
years = y
break
} else if years[i+1][0].Year > p.Year{ // Is the next year greater than the post's year?
fmt.Println("Inbetween")
// Insert a new year in between
years = append(years[:i+2], years[i+1:]...)
//years = append(years[:i+1], years[i:]...)
years[i+1] = []Post{p}
break
}
}
}
}
fmt.Println("")
/*
var finalYearExist := years[len(years)] != nil
var postIsNewest := years[len(years)][0].Date.Year() < p.Date.Year()
if finalYearsExist && postIsNewest { //is the year of this post greater than the year of the posts at the final slice?
var newYear := []Post{p} // Then add a nice slice with this post as an element
years = append(years, newYear)
} else {
var yearExists := false
for i := 0; i < len(years); i++ { //go through each year
if p.Date.Year() == years[i][0].Date.Year() { //found the year
yearExists = true
years = append(years[i], p) //add the post to the year
break
}
}
if yearExists == false {
}
}
aYear := years[0]
yearPosts := years[p.Date.Year()]
yearPosts = append(yearPosts, p)
//yearPosts = insertPostAccordingToDate(p, yearPosts)
years[p.Date.Year()] = yearPosts
2021-11-30 19:23:35 +00:00
posts = insertPostAccordingToDate(p, posts)
2022-06-09 18:26:01 +00:00
*/
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
}
2022-06-09 18:26:01 +00:00
func reverseYears() [][]Post {
length := len(years)
s := make([][]Post, length)
for i := 0; i < length; i++ {
s[i] = years[length-(i+1)] }
return s
}
func reversePosts(slice []Post) []Post {
length := len(slice)
2017-11-21 09:53:32 +00:00
s := make([]Post, length)
for i := 0; i < length; i++ {
2022-06-09 18:26:01 +00:00
s[i] = slice[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 {
2022-06-09 18:26:01 +00:00
for _, year := range years {
for _, post := range year {
if post.URLTitle == title {
return post
}
2017-11-16 22:22:34 +00:00
}
}
2022-06-09 18:26:01 +00:00
////for _, post := range posts {
// if post.URLTitle == title {
// return post
// }
//}
2017-11-16 22:22:34 +00:00
return Post{}
}
2022-06-09 18:26:01 +00:00
func insertPostAccordingToDate(post Post, postSlice []Post) []Post {
for i, p := range postSlice {
2017-11-16 22:22:34 +00:00
if p.Date.After(post.Date) {
2022-06-09 18:26:01 +00:00
s := make([]Post, len(postSlice)+1, cap(postSlice)+1)
copy(s[:], postSlice[:]) //make a copy of the slice
copy(s[i+1:], postSlice[i:]) //move the upper part of the slice ahead, creating a hole
2017-11-16 22:22:34 +00:00
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
}
}
2022-06-09 18:26:01 +00:00
postSlice = append(postSlice, post)
return postSlice
2017-11-16 22:22:34 +00:00
}