-
-
Notifications
You must be signed in to change notification settings - Fork 5
/
blog_entry.go
175 lines (146 loc) · 3.77 KB
/
blog_entry.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
package ephemeris
import (
"fmt"
"regexp"
"sort"
"strings"
"time"
"github.com/shurcooL/github_flavored_markdown"
"github.com/skx/headerfile"
)
// BlogEntry holds a single blog-post.
//
// A post has a series of attributes associated with it, as you would
// expect, such as a title a set of tags, and an associated set of comments.
//
type BlogEntry struct {
// Title holds the blog-title.
Title string
// Path holds the path to the source-file, on-disk.
Path string
// Tags contains a list of tags for the given post.
Tags []string
// Content contains the post-body.
Content string
// The link to the post.
Link string
// Date is when the post was created.
Date time.Time
// CommentData contains any comments left upon this entry.
CommentData []BlogComment
}
// Year returns the year of a blog-post, as a string.
//
// Having a string return value is useful for template interpolation.
func (b BlogEntry) Year() string {
return (fmt.Sprintf("%d", b.Date.Year()))
}
// MonthName returns the value of a post's month, as a string, for example
// "January", "March", etc.
func (b BlogEntry) MonthName() string {
return (b.Date.Month().String())
}
// MonthNumber returns the value of a post's month, as a two-digit string.
// For example "01", "11", or "12".
//
// Having a string return value is useful for template interpolation.
func (b BlogEntry) MonthNumber() string {
return (fmt.Sprintf("%02d", int(b.Date.Month())))
}
// NewBlogEntry creates a new blog object from the contents of the given
// file.
//
// If the file is formatted in Markdown it will be expanded to HTML as
// part of the creation-process.
func NewBlogEntry(path string, site *Ephemeris) (BlogEntry, error) {
// The structure we'll return
var result BlogEntry
// Create a helper to read the entry.
reader := headerfile.New(path)
// Read the headers from the post
headers, err := reader.Headers()
if err != nil {
return result, err
}
// Read the body from the post
//
// errors can't happen here, because if they were present
// they would have happened in the header-read.
body, _ := reader.Body()
// Sanity-check the headers
for key, val := range headers {
// Now process known-good keys
switch key {
case "date":
t, err := time.Parse("02/01/2006 15:04", val)
if err != nil {
return result, err
}
result.Date = t
case "title", "subject":
result.Title = val
case "format":
if val == "markdown" {
body = string(github_flavored_markdown.Markdown([]byte(body)))
} else {
return result, fmt.Errorf("unknown entry-format %s", val)
}
case "tags":
tags := strings.Split(val, ",")
for _, t := range tags {
t = strings.TrimSpace(t)
t = strings.ToLower(t)
if len(t) >= 1 {
result.Tags = append(result.Tags, t)
}
}
sort.Strings(result.Tags)
default:
return result, fmt.Errorf("unknown header-key %s in file %s", key, path)
}
}
//
// Otherwise update the object some more.
//
result.Path = path
result.Content = body
//
// Make a Regex to say we only want letters and numbers
//
reg := regexp.MustCompile("[^a-zA-Z0-9]")
//
// Normalise the output
//
link := reg.ReplaceAllString(result.Title, "_") + ".html"
//
// Make our link absolute.
//
result.Link = site.Prefix + link
//
// Add any comments to the appropriate entry
//
for _, comment := range site.CommentFiles {
//
// We rely upon the fact that the naming
// scheme of the comments matches the
// title(s) of the post(s)
//
if strings.Contains(comment, strings.ToLower(link)) {
//
// Read the blog-comment
//
x, err := NewBlogComment(comment)
if err != nil {
return result, err
}
//
// Append it to our list.
//
result.CommentData = append(result.CommentData, x)
}
}
//
// And return
//
return result, nil
}