package main import ( "fmt" "golang.org/x/net/html" "html/template" "io/ioutil" "os" "strings" "time" ) type Post struct { Date time.Time NormalDate string Year int Title string URLTitle string Content template.HTML } const htmlDateLayout = "2006-01-02" const normalDateLayout = "02 Jan" var modTime time.Time func newPost(path string, f os.FileInfo, err error) error { content, err := ioutil.ReadFile(path) if err != nil { fmt.Println(err) return nil } title := strings.Replace(path, "posts/", "", -1) if !strings.Contains(title, ".html") { return nil } title = strings.Replace(title, ".html", "", -1) s := string(content) doc, _ := html.Parse(strings.NewReader(s)) dateParsed := getDate(doc) dateFormatted := dateParsed.Format(normalDateLayout) fmt.Println(dateParsed.Year()) var p = Post{ dateParsed, dateFormatted, dateParsed.Year(), title, strings.Replace(title, " ", "-", -1), template.HTML(string(content[:len(content)])), } 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 posts = insertPostAccordingToDate(p, posts) */ return nil } func getDate(n *html.Node) time.Time { var zero, d, t time.Time if n.Type == html.ElementNode && n.Data == "time" { for _, a := range n.Attr { if a.Key == "datetime" { t, _ = time.Parse(htmlDateLayout, a.Val) return t } } } for c := n.FirstChild; c != nil; c = c.NextSibling { d = getDate(c) if d != zero { return d } } return t } 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) s := make([]Post, length) for i := 0; i < length; i++ { s[i] = slice[length-(i+1)] } return s } func getPostByURLTitle(title string) Post { for _, year := range years { for _, post := range year { if post.URLTitle == title { return post } } } ////for _, post := range posts { // if post.URLTitle == title { // return post // } //} return Post{} } func insertPostAccordingToDate(post Post, postSlice []Post) []Post { for i, p := range postSlice { if p.Date.After(post.Date) { 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 s[i] = post //insert new element into hole return s } } postSlice = append(postSlice, post) return postSlice }