-
Notifications
You must be signed in to change notification settings - Fork 43
/
frontmatter.go
172 lines (153 loc) · 3.87 KB
/
frontmatter.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package hugo
import (
"fmt"
"io"
"os"
"strings"
"time"
"github.com/gohugoio/hugo/parser/pageparser"
"github.com/pkg/errors"
)
const (
fmTitle = "title"
fmAuthor = "author"
fmCategories = "categories"
fmTags = "tags"
fmDate = "date" // priority high
fmLastmod = "lastmod" // priority middle
fmPublishDate = "publishDate" // priority low
)
type FrontMatter struct {
Title string
Author string
Category string
Tags []string
Date time.Time
}
// ParseFrontMatter parses the frontmatter of the specified Hugo content.
func ParseFrontMatter(w io.Writer, filename string, currentTime time.Time) (*FrontMatter, error) {
file, err := os.Open(filename)
if err != nil {
return nil, err
}
defer file.Close()
return parseFrontMatter(w, file, currentTime)
}
func parseFrontMatter(w io.Writer, r io.Reader, currentTime time.Time) (*FrontMatter, error) {
cfm, err := pageparser.ParseFrontMatterAndContent(r)
if err != nil {
return nil, err
}
fm := &FrontMatter{}
if fm.Title, err = getString(&cfm, fmTitle); err != nil {
return nil, err
}
if isArray := isArray(&cfm, fmAuthor); isArray {
if fm.Author, err = getFirstStringItem(&cfm, fmAuthor); err != nil {
return nil, err
}
} else {
if fm.Author, err = getString(&cfm, fmAuthor); err != nil {
return nil, err
}
}
if fm.Category, err = getFirstStringItem(&cfm, fmCategories); err != nil {
return nil, err
}
if fm.Tags, err = getAllStringItems(&cfm, fmTags); err != nil {
return nil, err
}
if fm.Date, err = getContentDate(&cfm, currentTime); err != nil {
var fe *FMNotExistError
if errors.As(err, &fe) {
fmt.Fprintf(w, "WARN: %s\n", err.Error())
return fm, nil
}
return nil, err
}
return fm, nil
}
func getContentDate(cfm *pageparser.ContentFrontMatter, currentTime time.Time) (time.Time, error) {
for _, key := range []string{fmDate, fmLastmod, fmPublishDate} {
t, err := getTime(cfm, key, currentTime)
if err != nil {
switch err.(type) {
case *FMNotExistError:
continue
}
}
return t, err
}
return currentTime, NewFMNotExistError(
strings.Join([]string{fmDate, fmLastmod, fmPublishDate}, ", "))
}
func getTime(cfm *pageparser.ContentFrontMatter, fmKey string, currentTIme time.Time) (time.Time, error) {
v, ok := cfm.FrontMatter[fmKey]
if !ok {
return currentTIme, NewFMNotExistError(fmKey)
}
switch t := v.(type) {
case string:
return time.Parse(time.RFC3339, t)
case time.Time:
return t, nil
default:
return currentTIme, NewFMInvalidTypeError(fmKey, "time.Time or string", t)
}
}
func getString(cfm *pageparser.ContentFrontMatter, fmKey string) (string, error) {
v, ok := cfm.FrontMatter[fmKey]
if !ok {
return "", NewFMNotExistError(fmKey)
}
switch s := v.(type) {
case string:
if s == "" {
return "", NewFMNotExistError(fmKey)
}
return s, nil
default:
return "", NewFMInvalidTypeError(fmKey, "string", s)
}
}
func getAllStringItems(cfm *pageparser.ContentFrontMatter, fmKey string) ([]string, error) {
v, ok := cfm.FrontMatter[fmKey]
if !ok {
return nil, NewFMNotExistError(fmKey)
}
switch arr := v.(type) {
case []interface{}:
var strarr []string
for _, item := range arr {
switch s := item.(type) {
case string:
if s != "" {
strarr = append(strarr, s)
}
default:
return nil, NewFMInvalidTypeError(fmKey, "string", s)
}
}
if len(strarr) < 1 {
return nil, NewFMNotExistError(fmKey)
}
return strarr, nil
default:
return nil, NewFMInvalidTypeError(fmKey, "[]interface{}", arr)
}
}
func getFirstStringItem(cfm *pageparser.ContentFrontMatter, fmKey string) (string, error) {
arr, err := getAllStringItems(cfm, fmKey)
if err != nil {
return "", err
}
return arr[0], nil
}
func isArray(cfm *pageparser.ContentFrontMatter, fmKey string) bool {
switch v := cfm.FrontMatter[fmKey]; v.(type) {
case []interface{}:
return true
default:
return false
}
}