package main import ( "os" "regexp" "sort" "strings" ) type Post struct { FileName string Title string Date string Tags []string Image string } type Posts struct { Contents []*Post } func (p Post) containsTag(filterTag string) bool { if filterTag == "" { return true } for _, tag := range p.Tags { if filterTag == tag { return true } } return false } func (app *application) loadPosts(location string, postCount int) (p *Posts, err error) { if postCount == 0 || postCount < -1 { return nil, os.ErrInvalid } var posts []*Post files, err := os.ReadDir(location) if err != nil { return nil, err } for _, file := range files { if !file.IsDir() && strings.HasSuffix(file.Name(), ".tmpl.html") { newPost, err := app.readFile(location + "/" + file.Name()) if err != nil { return nil, err } /* // filtering by tag if !newPost.containsTag(filterTag) { continue } */ posts = append(posts, newPost) } } sort.Slice(posts, func(i, j int) bool { return posts[i].Date > posts[j].Date }) if postCount == -1 { return &Posts{Contents: posts}, nil } else if postCount < len(posts) { return &Posts{Contents: posts[:postCount]}, nil } else { return &Posts{Contents: posts}, nil } } func (app *application) readFile(location string) (p *Post, err error) { fileContent, err := os.ReadFile(location) if err != nil { return nil, err } var tmp *Post = new(Post) fileName := strings.TrimSuffix(strings.Split(location, "/")[2], ".tmpl.html") // title title := strings.ReplaceAll(fileName, "_", " ") // date datePattern := regexp.MustCompile(`