2019-03-27 14:18:52 +00:00
|
|
|
package blog
|
|
|
|
|
|
|
|
import (
|
|
|
|
"html/template"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"sort"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2019-05-25 19:14:09 +00:00
|
|
|
"christine.website/internal/front"
|
2019-03-27 14:18:52 +00:00
|
|
|
"github.com/russross/blackfriday"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Post is a single blogpost.
|
|
|
|
type Post struct {
|
2019-03-27 14:31:57 +00:00
|
|
|
Title string `json:"title"`
|
|
|
|
Link string `json:"link"`
|
|
|
|
Summary string `json:"summary,omitifempty"`
|
|
|
|
Body string `json:"-"`
|
|
|
|
BodyHTML template.HTML `json:"body"`
|
2019-09-12 22:49:03 +00:00
|
|
|
Series string `json:"series"`
|
|
|
|
Tags []string `json:"tags"`
|
2019-05-22 01:47:09 +00:00
|
|
|
SlidesLink string `json:"slides_link"`
|
2019-11-01 21:48:19 +00:00
|
|
|
ImageURL string `json:"image_url"`
|
|
|
|
ThumbURL string `json:"thumb_url"`
|
2019-03-27 14:31:57 +00:00
|
|
|
Date time.Time
|
|
|
|
DateString string `json:"date"`
|
2019-03-27 14:18:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Posts implements sort.Interface for a slice of Post objects.
|
|
|
|
type Posts []Post
|
|
|
|
|
2019-09-12 22:49:03 +00:00
|
|
|
func (p Posts) Series() []string {
|
|
|
|
names := map[string]struct{}{}
|
|
|
|
|
|
|
|
for _, ps := range p {
|
|
|
|
if ps.Series != "" {
|
|
|
|
names[ps.Series] = struct{}{}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var result []string
|
|
|
|
|
|
|
|
for name := range names {
|
|
|
|
result = append(result, name)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
2019-03-27 14:18:52 +00:00
|
|
|
func (p Posts) Len() int { return len(p) }
|
|
|
|
func (p Posts) Less(i, j int) bool {
|
|
|
|
iDate := p[i].Date
|
|
|
|
jDate := p[j].Date
|
|
|
|
|
|
|
|
return iDate.Unix() < jDate.Unix()
|
|
|
|
}
|
|
|
|
func (p Posts) Swap(i, j int) { p[i], p[j] = p[j], p[i] }
|
|
|
|
|
|
|
|
// LoadPosts loads posts for a given directory.
|
2019-05-22 01:47:09 +00:00
|
|
|
func LoadPosts(path string, prepend string) (Posts, error) {
|
2019-03-27 14:18:52 +00:00
|
|
|
type postFM struct {
|
2019-05-22 01:47:09 +00:00
|
|
|
Title string
|
|
|
|
Date string
|
2019-09-12 22:49:03 +00:00
|
|
|
Series string
|
|
|
|
Tags []string
|
2019-05-22 01:47:09 +00:00
|
|
|
SlidesLink string `yaml:"slides_link"`
|
2019-11-01 21:48:19 +00:00
|
|
|
Image string
|
|
|
|
Thumb string
|
2019-03-27 14:18:52 +00:00
|
|
|
}
|
|
|
|
var result Posts
|
|
|
|
|
|
|
|
err := filepath.Walk(path, func(path string, info os.FileInfo, err error) error {
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if info.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
fin, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fin.Close()
|
|
|
|
|
|
|
|
content, err := ioutil.ReadAll(fin)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var fm postFM
|
|
|
|
remaining, err := front.Unmarshal(content, &fm)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
output := blackfriday.Run(remaining)
|
|
|
|
|
|
|
|
const timeFormat = `2006-01-02`
|
|
|
|
date, err := time.Parse(timeFormat, fm.Date)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-05-22 01:47:09 +00:00
|
|
|
fname := filepath.Base(path)
|
|
|
|
fname = strings.TrimSuffix(fname, filepath.Ext(fname))
|
|
|
|
|
2019-03-27 14:18:52 +00:00
|
|
|
p := Post{
|
2019-03-27 14:31:57 +00:00
|
|
|
Title: fm.Title,
|
|
|
|
Date: date,
|
|
|
|
DateString: fm.Date,
|
2019-05-22 01:47:09 +00:00
|
|
|
Link: filepath.Join(prepend, fname),
|
2019-03-27 14:31:57 +00:00
|
|
|
Body: string(remaining),
|
|
|
|
BodyHTML: template.HTML(output),
|
2019-05-22 01:47:09 +00:00
|
|
|
SlidesLink: fm.SlidesLink,
|
2019-09-12 22:49:03 +00:00
|
|
|
Series: fm.Series,
|
|
|
|
Tags: fm.Tags,
|
2019-11-01 21:48:19 +00:00
|
|
|
ImageURL: fm.Image,
|
|
|
|
ThumbURL: fm.Thumb,
|
2019-03-27 14:18:52 +00:00
|
|
|
}
|
|
|
|
result = append(result, p)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sort.Reverse(result))
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|