/
post_util.go
214 lines (183 loc) · 5.24 KB
/
post_util.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
package models
import (
"github.com/slogsdon/b/util"
"gopkg.in/yaml.v1"
"html/template"
"io/ioutil"
"net/url"
"os"
"strings"
"time"
)
// ParsePostId parses a file path from an url parameter.
func ParsePostId(id string) string {
split := strings.Split(id, "_")
path := split[0]
if len(split) == 1 {
return path
}
if path == "" {
return strings.Join(split[1:], "_")
}
if strings.Contains(path, "-") {
path = strings.Join(strings.Split(path, "-"), "/")
}
return path + "/" + strings.Join(split[1:], "_")
}
// SavePost writes a new file or a file's new contents to storage.
func SavePost(root string, p interface{}) error {
var (
categories string
filename string
raw string
hm string
)
switch p.(type) {
case Post:
//json
post := Post(p.(Post))
filename = post.Filename
categories = strings.Join(post.HeadMatter.Categories, string(os.PathSeparator))
raw = post.Raw
hm = FormatPostHeadMatter(post.HeadMatter)
default:
var (
title string
date string
c []string
meta_description string
feature_image string
)
// x-www-form-urlencoded
form := url.Values(p.(url.Values))
if _, ok := form["filename"]; ok {
filename = form["filename"][0]
}
if _, ok := form["raw"]; ok {
raw = form["raw"][0]
}
if _, ok := form["title"]; ok {
title = form["title"][0]
}
if _, ok := form["date"]; ok {
date = form["date"][0]
}
if _, ok := form["categories"]; ok {
c = form["categories"]
}
if _, ok := form["meta_description"]; ok {
meta_description = form["meta_description"][0]
}
if _, ok := form["feature_image"]; ok {
feature_image = form["feature_image"][0]
}
hm = FormatPostHeadMatter(HeadMatter{
Title: title,
Date: date,
Categories: c,
MetaDescription: meta_description,
FeatureImage: feature_image,
})
categories = strings.Join(c, string(os.PathSeparator))
}
categories += string(os.PathSeparator)
raw = hm + raw
err := util.MakeDir(root + string(os.PathSeparator) + categories)
if err != nil {
return err
}
fullpath := root + string(os.PathSeparator) + categories + filename
return util.WriteFile(fullpath, raw)
}
// GetAllPosts returns all posts from the storage system by name.
func GetAllPosts(root string) []Post {
var posts []Post
for _, f := range util.ReadDir(root) {
posts = append(posts, preparePost(f))
}
return posts
}
// GetPost returns a single post from the storage system by name.
func GetPost(name, root string) Post {
var post Post
for _, f := range util.ReadDir(root) {
if f.Filename == name {
post = preparePost(f)
break
}
}
return post
}
// ParsePostContent parses the HeadMatter and HTML from a raw post.
func ParsePostContent(contents []byte, t string) template.HTML {
var c []byte
switch t {
case "md", "mdown", "markdown":
c = util.Markdown(contents)
}
return template.HTML(string(c))
}
// ParsePostSlugAndType parses a post's slug and type from
// its filename. The file extension is used for the post type.
// The slug is grabbed from the basename sans a prefixed date
// used for organization.
// It returns the post's slug and type.
func ParsePostSlugAndType(filename string) (string, string) {
filenameNoDate := strings.Join(strings.Split(filename, "-")[3:], "-")
split := strings.Split(filenameNoDate, ".")
slug := strings.ToLower(strings.Join(split[:len(split)-1], "."))
t := strings.ToLower(split[len(split)-1])
return slug, t
}
func preparePost(f util.FileReading) Post {
// Read file contents
contents, _ := ioutil.ReadFile(f.Filename)
// Grab slug and type from filename
slug, t := ParsePostSlugAndType(f.Info.Name())
// Parse our content/head matter from our file
// Return our prepared Post
head, contentsNoHead := ParsePostHeadMatter(contents)
formattedContents := ParsePostContent(contentsNoHead, t)
time, _ := time.Parse("2006-01-02 15:04:05", head.Date)
return Post{
Title: head.Title,
Slug: slug,
Content: formattedContents,
HeadMatter: head,
Filename: f.Info.Name(),
Directory: strings.Replace(f.Filename, string(os.PathSeparator)+f.Info.Name(), "", 1),
Type: t,
Raw: string(contentsNoHead),
// CreatedAt: f.Info.Sys().Ctim,
UpdatedAt: f.Info.ModTime(),
PublishedAt: time,
}
}
// Represents the possible data contained within the
// head matter section of a post, fenced with leading
// and following --- lines.
type HeadMatter struct {
Title string `json:"title",yaml:"title,omitempty"`
Date string `json:"date",yaml:"date,omitempty"`
Categories []string `json:"categories",yaml:"categories,omitempty"`
MetaDescription string `json:"meta_description",yaml:"meta_description,omitempty"`
FeatureImage string `json:"feature_image",yaml:"feature_image,omitempty"`
}
func FormatPostHeadMatter(hm HeadMatter) string {
var r string
b, _ := yaml.Marshal(hm)
if len(b) > 0 {
r = "---\n" + string(b) + "---\n\n"
}
return r
}
func ParsePostHeadMatter(contents []byte) (HeadMatter, []byte) {
m := HeadMatter{}
c := string(contents)
if strings.Count(c, "---") >= 2 {
split := strings.Split(c, "---")
_ = yaml.Unmarshal([]byte(split[1]), &m)
c = strings.Trim(strings.Join(split[2:], "---"), "\r\n")
}
return m, []byte(c)
}