2021-09-03 04:31:27 +00:00
|
|
|
package post
|
|
|
|
|
|
|
|
import (
|
2022-07-07 05:05:18 +00:00
|
|
|
"html/template"
|
2021-09-03 04:31:27 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2022-07-07 05:05:18 +00:00
|
|
|
"sync"
|
2021-09-03 04:31:27 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.jolheiser.com/blog/markdown"
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2022-07-07 05:05:18 +00:00
|
|
|
"go.jolheiser.com/emdbed"
|
2021-09-03 04:31:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewBlog(basePath string) (*Blog, error) {
|
2022-07-07 05:05:18 +00:00
|
|
|
posts, err := scan(basePath)
|
2021-09-03 04:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return &Blog{
|
|
|
|
Path: basePath,
|
2022-07-07 05:05:18 +00:00
|
|
|
posts: posts,
|
2021-09-03 04:31:27 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2022-07-07 05:05:18 +00:00
|
|
|
func scan(basePath string) (map[string]*Post, error) {
|
2021-09-03 04:31:27 +00:00
|
|
|
posts := make(map[string]*Post)
|
|
|
|
ents, err := os.ReadDir(basePath)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
for _, ent := range ents {
|
2021-12-26 04:56:32 +00:00
|
|
|
var apn string
|
|
|
|
if ent.IsDir() {
|
|
|
|
apn = filepath.Join(basePath, ent.Name(), "index.md")
|
|
|
|
if _, err := os.Stat(apn); err != nil {
|
|
|
|
log.Error().Err(err).Msgf("could not parse %s", ent.Name())
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if !strings.HasSuffix(ent.Name(), ".md") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
apn = filepath.Join(basePath, ent.Name())
|
2021-09-03 04:31:27 +00:00
|
|
|
}
|
2021-12-26 04:56:32 +00:00
|
|
|
|
2021-09-03 04:31:27 +00:00
|
|
|
fi, err := os.Open(apn)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not open file")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
post := &Post{Path: apn, Slug: strings.TrimSuffix(ent.Name(), ".md")}
|
|
|
|
if err := markdown.Meta(fi, &post); err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not extract meta")
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
posts[post.Slug] = post
|
2022-07-07 05:05:18 +00:00
|
|
|
|
2021-09-03 04:31:27 +00:00
|
|
|
if err := fi.Close(); err != nil {
|
|
|
|
log.Error().Err(err).Msg("could not close file")
|
|
|
|
continue
|
|
|
|
}
|
2022-07-07 05:05:18 +00:00
|
|
|
|
|
|
|
if err := post.Load(); err != nil {
|
|
|
|
log.Err(err).Msg("could not load post")
|
|
|
|
continue
|
|
|
|
}
|
2021-09-03 04:31:27 +00:00
|
|
|
}
|
|
|
|
return posts, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type Blog struct {
|
|
|
|
Path string
|
2022-07-07 05:05:18 +00:00
|
|
|
posts map[string]*Post
|
|
|
|
mx sync.RWMutex
|
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Blog) Scan() error {
|
|
|
|
posts, err := scan(b.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
b.mx.Lock()
|
|
|
|
defer b.mx.Unlock()
|
|
|
|
b.posts = posts
|
|
|
|
return nil
|
2021-09-03 04:31:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (b *Blog) SortedPosts() []*Post {
|
2022-07-07 05:05:18 +00:00
|
|
|
posts := make([]*Post, 0, len(b.posts))
|
|
|
|
for _, post := range b.posts {
|
2021-09-03 04:31:27 +00:00
|
|
|
posts = append(posts, post)
|
|
|
|
}
|
|
|
|
sort.Slice(posts, func(i, j int) bool {
|
2021-09-04 00:55:29 +00:00
|
|
|
return posts[i].Date.After(posts[j].Date)
|
2021-09-03 04:31:27 +00:00
|
|
|
})
|
|
|
|
return posts
|
|
|
|
}
|
|
|
|
|
2022-07-07 05:05:18 +00:00
|
|
|
func (b *Blog) Post(name string) (*Post, bool) {
|
|
|
|
b.mx.RLock()
|
|
|
|
defer b.mx.RUnlock()
|
|
|
|
if post, ok := b.posts[name]; ok {
|
|
|
|
return post, true
|
|
|
|
}
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
|
2021-09-03 04:31:27 +00:00
|
|
|
type Post struct {
|
2022-07-07 05:05:18 +00:00
|
|
|
Path string `toml:"-"`
|
|
|
|
Slug string `toml:"-"`
|
|
|
|
Content template.HTML `toml:"-"`
|
|
|
|
Title string `toml:"title"`
|
|
|
|
Author string `toml:"author"`
|
|
|
|
Date time.Time `toml:"date"`
|
|
|
|
Tags []string `toml:"tags"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (p *Post) Load() error {
|
|
|
|
fi, err := os.Open(p.Path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
|
|
|
|
mdContent, err := markdown.Content(fi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
emdbedContent, err := emdbed.Convert(filepath.Dir(p.Path), strings.NewReader(mdContent))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
md, err := markdown.Convert(strings.NewReader(emdbedContent))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
p.Content = template.HTML(md)
|
|
|
|
return nil
|
2021-09-03 04:31:27 +00:00
|
|
|
}
|