75 lines
1.3 KiB
Go
75 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"errors"
|
|
"os"
|
|
"path/filepath"
|
|
"strings"
|
|
"time"
|
|
)
|
|
|
|
func getAllPosts(dir string) ([]string, error) {
|
|
entries, err := os.ReadDir(dir)
|
|
if err != nil {
|
|
return []string{}, err
|
|
}
|
|
|
|
posts := make([]string, 0)
|
|
for _, entry := range entries {
|
|
if filepath.Ext(entry.Name()) != ".md" {
|
|
continue
|
|
}
|
|
|
|
content, err := os.ReadFile(filepath.Join(dir, entry.Name()))
|
|
if err != nil {
|
|
return []string{}, err
|
|
}
|
|
|
|
posts = append(posts, string(content))
|
|
}
|
|
|
|
return posts, nil
|
|
}
|
|
|
|
type postInfo struct {
|
|
Title string
|
|
Date time.Time
|
|
}
|
|
|
|
// TODO: this could actually be a parser
|
|
// Pull out the old course notes on recursive descent.
|
|
func getPostInfo(post string) (postInfo, error) {
|
|
lines := strings.Split(post, "\n")
|
|
|
|
if len(lines) == 0 {
|
|
return postInfo{}, errors.New("Post has 0 lines")
|
|
}
|
|
|
|
if lines[0] != "+++" {
|
|
return postInfo{}, errors.New("Post does not contain metadata field +++ on first line")
|
|
}
|
|
|
|
info := postInfo{}
|
|
|
|
for _, line := range lines {
|
|
splitLine := strings.Split(line, " = ")
|
|
if len(splitLine) != 2 {
|
|
continue
|
|
}
|
|
|
|
switch splitLine[0] {
|
|
case "title":
|
|
info.Title = splitLine[1]
|
|
case "date":
|
|
t, err := time.Parse(time.RFC3339, splitLine[1])
|
|
if err != nil {
|
|
continue
|
|
}
|
|
|
|
info.Date = t
|
|
}
|
|
}
|
|
|
|
return info, nil
|
|
}
|