-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathssg.go
240 lines (219 loc) · 6.18 KB
/
ssg.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
/*
Copyright (c) 2021 Branden J Brown
This software is provided 'as-is', without any express or implied warranty.
In no event will the authors be held liable for any damages arising from the
use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it freely,
subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software in a
product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source distribution.
*/
package main
import (
"embed"
"encoding/json"
"flag"
"fmt"
"html/template"
"log"
"net/url"
"os"
"path/filepath"
"time"
"github.com/gorilla/feeds"
"golang.org/x/tools/present"
)
type manifest struct {
Title string `json:"title"`
Author string `json:"author"`
Email string `json:"email"`
Href string `json:"href"`
Description string `json:"description"`
Sections []section `json:"sections"`
}
type section struct {
Section string `json:"section"`
Articles []string `json:"articles"`
}
type metadata struct {
Section string
Articles []article
}
type article struct {
URL string
Title string
Summary string
Time time.Time
}
//go:embed templates/actions.html
//go:embed templates/head.html
//go:embed templates/index.html
//go:embed templates/article.html
//go:embed templates/footer.html
var static embed.FS
func main() {
var (
outdir string
mf string
)
flag.StringVar(&outdir, "out", "", "output directory")
flag.StringVar(&mf, "mf", "", "manifest json")
flag.Parse()
if outdir == "" {
log.Fatalln("need -out")
}
if mf == "" {
log.Fatalln("need -mf")
}
outdir, err := filepath.Abs(outdir)
if err != nil {
log.Fatalln("couldn't abs outdir:", err)
}
s, err := os.ReadFile(mf)
if err != nil {
log.Fatalln("error reading manifest:", err)
}
var manifest manifest
if err := json.Unmarshal(s, &manifest); err != nil {
log.Fatalln("error unmarshaling manifest:", err)
}
tmpl := present.Template()
if tmpl, err = tmpl.ParseFS(static, "templates/*.html"); err != nil {
log.Fatalln("error parsing templates:", err)
}
// render articles
if err := os.MkdirAll(filepath.Join(outdir, "articles"), 0644); err != nil {
log.Fatalln("couldn't make articles output dir:", err)
}
var meta []metadata
feed := feeds.Feed{
Title: manifest.Title,
Link: &feeds.Link{Href: manifest.Href},
Description: manifest.Description,
Author: &feeds.Author{Name: manifest.Author, Email: manifest.Email},
Created: time.Now(),
}
for _, sec := range manifest.Sections {
m := metadata{Section: sec.Section}
for _, art := range sec.Articles {
log.Println(sec.Section, art)
doc, u, when, err := doArticle(outdir, art, tmpl)
if err != nil {
log.Println(err)
continue
}
m.Articles = append(m.Articles, article{URL: u, Title: doc.Title, Summary: doc.Summary, Time: doc.Time})
l, err := url.JoinPath(manifest.Href, u)
if err != nil {
log.Println(err)
continue
}
fi := feeds.Item{
Title: doc.Title,
Link: &feeds.Link{Href: l},
Author: &feeds.Author{Name: manifest.Author, Email: manifest.Email},
Description: doc.Summary,
Id: l,
Created: when,
}
feed.Items = append(feed.Items, &fi)
}
meta = append(meta, m)
}
// render index
log.Println("index")
if err := renderIndex(filepath.Join(outdir, "index.html"), meta, tmpl); err != nil {
log.Println(err)
}
// render feed
atom, err := feed.ToAtom()
if err != nil {
log.Println("generating atom feed:", err, "(continuing)")
}
if err := os.WriteFile(filepath.Join(outdir, "weblog.atom"), []byte(atom), 0644); err != nil {
log.Println("writing atom feed:", err, "(continuing)")
}
log.Println("done")
}
func doArticle(out, in string, tmpl *template.Template) (doc *present.Doc, url string, when time.Time, err error) {
var cwd string
cwd, err = os.Getwd()
if err != nil {
return nil, "", time.Time{}, fmt.Errorf("couldn't get cwd: %w", err)
}
if err := os.Chdir(in); err != nil {
return nil, "", time.Time{}, fmt.Errorf("couldn't cd to %s: %w", in, err)
}
defer func() {
nerr := os.Chdir(cwd)
if nerr != nil {
if err == nil {
err = fmt.Errorf("couldn't cd back to %s: %w", cwd, nerr)
return
}
err = fmt.Errorf("%w; and couldn't cd back to %s: %v", err, cwd, nerr)
}
}()
if doc, err = parse(in); err != nil {
return nil, "", time.Time{}, err // error already wrapped
}
url = filepath.Join("articles", in+".html")
err = render(filepath.Join(out, url), doc, tmpl) // error already wrapped
return doc, filepath.ToSlash(url), doc.Time, err
}
func parse(in string) (*present.Doc, error) {
art, err := os.Open(in + ".article")
if err != nil {
return nil, fmt.Errorf("couldn't open %[1]s/%[1]s.article: %[2]w", in, err)
}
defer art.Close()
doc, err := present.Parse(art, in, 0)
if err != nil {
return nil, fmt.Errorf("couldn't parse %s: %w", in, err)
}
return doc, nil
}
func render(out string, doc *present.Doc, tmpl *template.Template) (err error) {
var f *os.File
f, err = os.Create(out)
if err != nil {
return fmt.Errorf("couldn't create %s: %w", out, err)
}
defer func() {
nerr := f.Close()
if nerr != nil {
if err == nil {
err = fmt.Errorf("couldn't close %s: %w", out, nerr)
return
}
err = fmt.Errorf("%w; and couldn't close %s: %v", err, out, nerr)
}
}()
err = doc.Render(f, tmpl)
return err
}
func renderIndex(out string, sections []metadata, tmpl *template.Template) (err error) {
var f *os.File
f, err = os.Create(out)
if err != nil {
return fmt.Errorf("couldn't create %s: %w", out, err)
}
defer func() {
nerr := f.Close()
if nerr != nil {
if err == nil {
err = fmt.Errorf("couldn't close %s: %w", out, nerr)
return
}
err = fmt.Errorf("%w; and couldn't close %s: %v", err, out, nerr)
}
}()
err = tmpl.ExecuteTemplate(f, "index", sections)
return err
}