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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
|
/*
Package post provides the data and behavior for the fundamental atomic
unit of a site: a post. Posts are represented as text files, then converted to HTML and other formats
*/
package post
import (
"adammathes.com/snkt/config"
"adammathes.com/snkt/render"
"adammathes.com/snkt/text"
"adammathes.com/snkt/vlog"
"github.com/russross/blackfriday"
"io/ioutil"
"log"
"os"
"path"
"strconv"
"strings"
"time"
)
var Template = "post"
type Post struct {
// Representations of the entire post text
Raw []byte
Unparsed string
// Metadata
Meta map[string]string
SourceFile string
Title string `json:"title"`
Permalink string `json:"permalink"`
Time time.Time
Year int
Month time.Month
Day int
InFuture bool
// Content text -- raw, unprocessed, unfiltered markdown
Text string
// Content text -- processed into HTML via markdown and other filters
Content string
// Content with sources and references resolved to absolute URLs
AbsoluteContent string
// Post following chronologically (later)
Next *Post
// Post preceding chronologically (earlier)
Prev *Post
// Precomputed dates as strings
Date string
RssDate string
FileInfo os.FileInfo
Site sitemeta
}
type sitemeta interface {
GetURL() string
GetTitle() string
}
type Posts []*Post
func (posts Posts) Len() int {
return len(posts)
}
func (posts Posts) Less(i, j int) bool {
return posts[i].Time.Before(posts[j].Time)
}
func (s Posts) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}
func NewPost(s sitemeta) *Post {
var p Post
p.Site = s
return &p
}
/*
Read reads a post from file fi, and parses it into the Post struct, performing any work needed to fully populate the struct
*/
func (p *Post) Read(fi os.FileInfo) {
p.Meta = make(map[string]string)
p.FileInfo = fi
p.SourceFile = p.FileInfo.Name()
var err error
p.Raw, err = ioutil.ReadFile(path.Join(config.Config.TxtDir, fi.Name()))
if err != nil {
log.Println(err)
}
p.Unparsed = string(p.Raw)
p.parse()
}
/*
Parse parses the metadata prefix from the top of the post file's raw bytes, and puts the rest in the text segment. Meta is a name:value mapping
Title, date and other metadata are derived
*/
func (p *Post) parse() {
//
// Text + Meta[string][string]
//
p.splitTextMeta()
//
// Title
//
p.Title = p.Meta["title"]
// Use filename as backup fi we have no explicit title
if p.Title == "" {
p.Title = p.SourceFile
}
//
// Dates
//
// we only deal with yyyy-mm-dd [some legacy dates from my archives have times tacked on]
// TODO: recover from empty dates/titles
// TODO: probably should actually use times when present and clean up my archives
var date_str = ""
ds := strings.Fields(p.Meta["date"])
if len(ds) > 0 {
date_str = ds[0]
}
if date_str == "" {
p.Time = p.FileInfo.ModTime()
vlog.Printf("no date field in post %s, using file modification time\n", p.SourceFile)
} else {
var err error
p.Time, err = time.ParseInLocation("2006-1-2", date_str, time.Local)
if err != nil {
// fallback is to use file modtime
// should use create time but that doesn't seem to be in stdlib
// TODO: figure out how to use file birth time
vlog.Printf("no valid date parsed for post %s, using file modification time\n", p.SourceFile)
p.Time = p.FileInfo.ModTime()
}
}
p.Year, p.Month, p.Day = p.Time.Date()
/* golang date format refresher
1 2 3 4 5 7 6
Mon Jan 2 15:04:05 MST 2006 */
p.Date = p.Time.Format("January 2, 2006")
p.RssDate = p.Time.Format(time.RFC822)
p.InFuture = time.Now().Before(p.Time)
p.Permalink = p.GenPermalink()
//
// Content
//
p.Content = string(p.Filter([]byte(p.Text)))
p.AbsoluteContent = render.ResolveURLs(p.Content, p.Site.GetURL())
}
/*
splitText splits up p.Unparsed into p.Text and p.Meta[attr][value]
*/
func (p *Post) splitTextMeta() {
SEPARATOR := ":"
lines := strings.Split(p.Unparsed, "\n")
for _, line := range lines {
if !strings.Contains(line, SEPARATOR) {
break
}
splitdex := strings.Index(line, SEPARATOR)
attr := strings.ToLower(strings.TrimSpace(line[0:splitdex]))
value := strings.TrimSpace(line[splitdex+1:])
p.Meta[attr] = value
}
p.Text = strings.Join(lines[len(p.Meta):], "\n")
}
func (p *Post) ParseFmt(s string) string {
// TODO: document and add strftime like formats
s = strings.Replace(s, "%Y", strconv.Itoa(p.Year), -1)
s = strings.Replace(s, "%M", strconv.Itoa(int(p.Month)), -1)
s = strings.Replace(s, "%D", strconv.Itoa(p.Day), -1)
s = strings.Replace(s, "%F", p.CleanFilename(), -1)
s = strings.Replace(s, "%T", p.CleanTitle(), -1)
s = strings.Replace(s, "$Y", strconv.Itoa(p.Year), -1)
s = strings.Replace(s, "$M", strconv.Itoa(int(p.Month)), -1)
s = strings.Replace(s, "$D", strconv.Itoa(p.Day), -1)
s = strings.Replace(s, "$F", p.CleanFilename(), -1)
s = strings.Replace(s, "$T", p.CleanTitle(), -1)
s = strings.Replace(s, ".File", p.CleanFilename(), -1)
s = strings.Replace(s, ".Title", p.CleanTitle(), -1)
s = strings.Replace(s, ".Year", strconv.Itoa(p.Year), -1)
s = strings.Replace(s, ".Month", strconv.Itoa(int(p.Month)), -1)
s = strings.Replace(s, ".Day", strconv.Itoa(p.Day), -1)
return s
}
func (p *Post) CleanFilename() string {
return text.SanitizeFilename(text.RemoveExt(p.SourceFile))
}
func (p *Post) CleanTitle() string {
return text.SanitizeFilename(p.Title)
}
/*
GenPermalink generates the permalink for the post given the PermalinkFmt format specified in the configuration file.
*/
func (p *Post) GenPermalink() string {
pl := config.Config.PermalinkFmt
return p.ParseFmt(pl)
}
/*
Target returns a string representing the file system location to write the output file representing the post.
*/
func (p Post) Target() string {
pf := config.Config.PostFileFmt
return path.Join(config.Config.HtmlDir, p.ParseFmt(pf))
}
/*
Render returns the post rendered as HTML via the post template with Post and Site as context.
*/
func (p Post) Render() []byte {
data := struct {
Post interface{}
Site interface{}
}{&p, &p.Site}
return render.Render(Template, data)
}
/*
Filter runs the text through filters defined by render.Filter and marddown, returning text suitable for HTML output.
*/
func (p *Post) Filter(txt []byte) []byte {
txt = render.Filter(txt)
txt = blackfriday.MarkdownCommon(txt)
return txt
}
/*
Limit returns a slice of Posts up to the int limit provided. If the limit is larger than the slice, it just returns the whole slice.
*/
func (posts Posts) Limit(limit int) Posts {
if len(posts) < limit {
return posts
} else {
return posts[0:limit]
}
}
|