-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.go
161 lines (139 loc) · 3.45 KB
/
feed.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
package internal
import (
"encoding/json"
"encoding/xml"
"fmt"
"io/ioutil"
"os"
"path"
"time"
)
type xmlLink struct {
Href string `xml:"href,attr"`
Rel string `xml:"rel,attr"`
Type string `xml:"type,attr"`
}
type xmlEntry struct {
Title string `xml:"title"`
Link xmlLink `xml:"link"`
Updated string `xml:"updated"`
ID string `xml:"id"`
Content struct {
Type string `xml:"type,attr"`
Text string `xml:",cdata"`
} `xml:"content"`
}
type feedXML struct {
XMLName xml.Name `xml:"feed"`
Title string `xml:"title"`
Subtitle string `xml:"subtitle"`
Links []xmlLink `xml:"link"`
Updated string `xml:"updated"`
ID string `xml:"id"`
Author struct {
Name string `xml:"name"`
Email string `xml:"email"`
} `xml:"author"`
Entries []xmlEntry `xml:"entry"`
}
// Write an RSS feed.xml based on the pages in the config's Articles field
// The site's domain and author fields must be configured for this to work
func (bh *BlogHead) writeFeed() error {
feed := feedXML{
Title: bh.config.Title,
Subtitle: bh.config.SubTitle,
Links: []xmlLink{
{
Href: "https://" + path.Join(bh.config.Domain, "feed.xml"),
Rel: "self",
Type: "application/atom+xml",
},
{
Href: "https://" + bh.config.Domain,
Rel: "alternate",
Type: "text/html",
},
},
Updated: time.Now().Format(time.RFC3339),
ID: bh.config.Domain,
Author: struct {
Name string `xml:"name"`
Email string `xml:"email"`
}{
Name: bh.config.Author,
Email: bh.config.Email,
},
Entries: []xmlEntry{},
}
for _, page := range bh.config.Articles {
text, title, updated, err := bh.getArticleData(page)
if err != nil {
return err
}
feed.Entries = append(feed.Entries, xmlEntry{
Title: title,
Link: xmlLink{
Href: "https://" + path.Join(bh.config.Domain, trimPath(bh.Root, page)),
},
Updated: updated,
ID: path.Join(bh.config.Domain, trimPath(bh.Root, page)),
Content: struct {
Type string `xml:"type,attr"`
Text string `xml:",cdata"`
}{"html", text},
})
}
f, err := createFile(path.Join(bh.Output, "feed.xml"))
if err != nil {
return err
}
defer f.Close()
encoder := xml.NewEncoder(f)
if err := encoder.Encode(feed); err != nil {
return err
}
return nil
}
func (bh *BlogHead) getArticleData(page string) (text, title, updated string, err error) {
// Get article metadata
b, err := ioutil.ReadFile(page[:len(page)-5] + "_meta.json")
if err != nil {
return text, title, updated, err
}
// Make a temporary file to compile templates with
tmpF, err := os.Create(path.Join(os.TempDir(), "_tmp.html"))
if err != nil {
return "", "", "", err
}
defer tmpF.Close()
if _, err := tmpF.WriteString(
fmt.Sprintf("{{template \"%v\" .}}", path.Join(".data", path.Base(page), "content.html"))); err != nil {
return "", "", "", err
}
// Copy article metadata to temporary file
tmplData, err := os.Create(path.Join(os.TempDir(), "_tmp_meta.json"))
if err != nil {
return "", "", "", err
}
defer tmplData.Close()
if _, err = tmplData.Write(b); err != nil {
return "", "", "", err
}
meta := make(map[string]string)
if err := json.Unmarshal(b, &meta); err != nil {
return "", "", "", err
}
var textBytes []byte
textBytes, err = bh.compile(tmpF.Name())
if err != nil {
return "", "", "", err
}
text = string(textBytes)
if m, ok := meta["title"]; ok {
title = m
}
if m, ok := meta["updated"]; ok {
updated = m
}
return text, title, updated, nil
}