forked from rsdoiel/mkpage
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mkrss.go
219 lines (213 loc) · 5.93 KB
/
mkrss.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
215
216
217
218
219
package mkpage
import (
"fmt"
"io/ioutil"
"net/url"
"os"
"path"
"regexp"
"strings"
"time"
// Caltech Library packages
"github.com/caltechlibrary/rss2"
)
// Generate a Feed from walking the BlogMeta structure
func BlogMetaToRSS(blog *BlogMeta, feed *rss2.RSS2) error {
if len(blog.Name) > 0 {
feed.Title = blog.Name
}
if len(blog.BaseURL) > 0 {
feed.Link = blog.BaseURL
}
if len(blog.Quip) > 0 {
feed.Description = "> " + blog.Quip + "\n\n"
}
if len(blog.Description) > 0 {
feed.Description += blog.Description
}
if len(blog.Updated) > 0 {
dt, err := time.Parse("2006-01-02", blog.Updated)
if err != nil {
return err
}
feed.PubDate = dt.Format(time.RFC1123)
feed.LastBuildDate = dt.Format(time.RFC1123)
}
if len(blog.Language) > 0 {
feed.Language = blog.Language
}
if len(blog.Copyright) > 0 {
feed.Copyright = blog.Copyright
}
//FIXME: Need to iterate over years, months, days and build our
// blog items.
for _, years := range blog.Years {
yr := years.Year
for _, months := range years.Months {
mn := months.Month
for _, days := range months.Days {
dy := days.Day
for _, post := range days.Posts {
pubDate, err := time.Parse("2006-01-02", fmt.Sprintf("%s-%s-%s", yr, mn, dy))
if err != nil {
return err
}
// NOTE: We only want to process Markdown documents.
// We look for Markdown related file extensions.
includeDescription := false
for _, ext := range []string{".md", ".markdown", ".txt", ".asciidoc"} {
if strings.HasSuffix(post.Document, ext) {
includeDescription = true
}
}
item := new(rss2.Item)
item.Title = post.Title
item.Link = strings.Join([]string{blog.BaseURL, post.Document}, "/")
item.GUID = item.Link
item.PubDate = pubDate.Format(time.RFC1123)
if len(post.Description) == 0 && len(post.Document) > 0 {
// Read the article, extract a description
buf, err := ioutil.ReadFile(post.Document)
if err != nil {
return err
}
fMatter := map[string]interface{}{}
fType, fSrc, tSrc := SplitFrontMatter(buf)
if len(fSrc) > 0 {
if err := UnmarshalFrontMatter(fType, fSrc, &fMatter); err != nil {
fMatter = map[string]interface{}{}
}
}
if val, ok := fMatter["description"]; ok {
post.Description = val.(string)
} else if includeDescription {
post.Description = OpeningParagraphs(fmt.Sprintf("%s", tSrc), 5, "\n\n")
if len(post.Description) < len(tSrc) {
post.Description += " ..."
}
post.Description = PandocBlock(post.Description, "markdown", "xml")
}
}
if len(post.Abstract) > 0 {
item.Description = post.Abstract
}
if len(post.Description) > 0 {
item.Description = post.Description
}
if item.Title != "" || item.Description != "" {
feed.ItemList = append(feed.ItemList, *item)
}
}
}
}
}
return nil
}
// Generate a Feed by walking the file system.
func WalkRSS(feed *rss2.RSS2, htdocs string, excludeList string, titleExp string, bylineExp string, dateExp string) error {
// Required
channelLink := feed.Link
validBlogPath := regexp.MustCompile("/[0-9][0-9][0-9][0-9]/[0-9][0-9]/[0-9][0-9]/")
err := Walk(htdocs, func(p string, info os.FileInfo) bool {
fname := path.Base(p)
if validBlogPath.MatchString(p) == true &&
strings.HasSuffix(fname, ".md") == true {
// NOTE: We have a possible published markdown article.
// Make sure we have a HTML version before adding it
// to the feed.
if _, err := os.Stat(path.Join(p, path.Base(fname)+".html")); os.IsNotExist(err) {
return false
}
return true
}
return false
}, func(p string, info os.FileInfo) error {
// Read the article
buf, err := ioutil.ReadFile(p)
if err != nil {
return err
}
fMatter := map[string]interface{}{}
fType, fSrc, tSrc := SplitFrontMatter(buf)
if len(fSrc) > 0 {
if err := UnmarshalFrontMatter(fType, fSrc, &fMatter); err != nil {
fMatter = map[string]interface{}{}
}
}
// Calc URL path
pname := strings.TrimPrefix(p, htdocs)
if strings.HasPrefix(pname, "/") {
pname = strings.TrimPrefix(pname, "/")
}
dname := path.Dir(pname)
bname := strings.TrimSuffix(path.Base(pname), ".md") + ".html"
articleURL := fmt.Sprintf("%s/%s", channelLink, path.Join(dname, bname))
u, err := url.Parse(articleURL)
if err != nil {
return err
}
// Collect metadata
//NOTE: Use front matter if available otherwise
var (
title, byline, author, description, pubDate string
)
src := fmt.Sprintf("%s", buf)
if val, ok := fMatter["title"]; ok {
title = val.(string)
} else {
title = strings.TrimPrefix(Grep(titleExp, src), "# ")
}
if val, ok := fMatter["byline"]; ok {
byline = val.(string)
} else {
byline = Grep(bylineExp, src)
}
if val, ok := fMatter["pubDate"]; ok {
pubDate = val.(string)
} else {
pubDate = Grep(dateExp, byline)
}
if val, ok := fMatter["description"]; ok {
description = val.(string)
} else {
description = OpeningParagraphs(fmt.Sprintf("%s", tSrc), 5, "\n\n")
if len(description) < len(tSrc) {
description += " ..."
}
description = PandocBlock(description, "markdown", "xml")
}
if val, ok := fMatter["creator"]; ok {
author = val.(string)
} else if val, ok = fMatter["author"]; ok {
author = val.(string)
} else {
author = byline
if len(byline) > 2 {
author = strings.TrimSpace(strings.TrimSuffix(byline[2:], pubDate))
}
}
// Reformat pubDate to conform to RSS2 date formats
var (
dt time.Time
)
if pubDate == "" {
dt = time.Now()
} else {
dt, err = time.Parse(`2006-01-02`, pubDate)
if err != nil {
return err
}
}
pubDate = dt.Format(time.RFC1123)
item := new(rss2.Item)
item.GUID = articleURL
item.Title = title
item.Author = author
item.PubDate = pubDate
item.Link = u.String()
item.Description = description
feed.ItemList = append(feed.ItemList, *item)
return nil
})
return err
}