2021-09-03 04:31:27 +00:00
|
|
|
package post
|
|
|
|
|
|
|
|
import (
|
2022-07-07 17:27:57 +00:00
|
|
|
"html/template"
|
2021-09-03 04:31:27 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
2022-07-07 17:27:57 +00:00
|
|
|
"sync"
|
2021-09-03 04:31:27 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"go.jolheiser.com/blog/markdown"
|
|
|
|
|
2022-07-07 17:27:57 +00:00
|
|
|
"github.com/fsnotify/fsnotify"
|
2021-09-03 04:31:27 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
2022-07-07 17:27:57 +00:00
|
|
|
"go.jolheiser.com/emdbed"
|
2021-09-03 04:31:27 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func NewBlog(basePath string) (*Blog, error) {
|
2022-07-07 17:27:57 +00:00
|
|
|
posts, err := scan(basePath)
|
2021-09-03 04:31:27 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2022-07-07 17:27:57 +00:00
|
|
|
|
|
|
|
watcher, err := fsnotify.NewWatcher()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if err := watcher.Add(basePath); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
b := &Blog{
|
2021-09-03 04:31:27 +00:00
|
|
|
Path: basePath,
|
2022-07-07 17:27:57 +00:00
|
|
|
posts: posts,
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
var debounce time.Time
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case event := <-watcher.Events:
|
|
|
|
log.Debug().Str("op", event.Op.String()).Msgf("fsnotify %q", event.Name)
|
|
|
|
debounce = time.Now()
|
|
|
|
case err := <-watcher.Errors:
|
|
|
|
log.Err(err).Msg("")
|
|
|
|
default:
|
|
|
|
if !debounce.IsZero() && time.Since(debounce) > (time.Second*5) {
|
|
|
|
log.Info().Msg("reloading blog")
|
|
|
|
if err := b.Scan(); err != nil {
|
|
|
|
log.Err(err).Msg("")
|
|
|
|
}
|
|
|
|
debounce = time.Time{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
return b, nil
|
2021-09-03 04:31:27 +00:00
|
|
|
}
|
|
|
|
|
2022-07-07 17:27:57 +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 17:27:57 +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 17:27:57 +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 17:27:57 +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 17:27:57 +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 17:27:57 +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 17:27:57 +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
|
|
|
}
|