-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerate_website.go
295 lines (255 loc) · 7.01 KB
/
generate_website.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package main
import (
"bufio"
"fmt"
"html/template"
"log"
"net/url"
"os"
"strings"
"time"
"github.com/mmyoungman/website/internal/bech32"
"github.com/mmyoungman/website/nostr"
)
type PostLink struct {
Text string
Link string
}
type PostFile struct {
FileName string
Title string
Date string
Content string
}
func main() {
// clear public folder
publicFiles, err := os.ReadDir("./public")
if err != nil {
log.Fatal(err)
}
for _, file := range publicFiles {
name := file.Name()
if name == "fonts" ||
name == "images" ||
name == "style.css" ||
name == "keybase.txt" ||
name == ".well-known" {
continue
}
os.Remove("./public/" + name)
}
// construct posts
files, err := os.ReadDir("./content")
if err != nil {
log.Fatal(err)
}
// TODO: sort files?
var posts []PostFile
for _, file := range files {
if isPost(file.Name()) {
post := createPostFile(file.Name())
posts = append(posts, post)
}
}
// construct sidebar post links
var postlinks []PostLink
numLinks := 10
for i := len(posts) - 1; i >= 0; i-- {
if i <= len(posts)-1-numLinks {
postlinks = append(postlinks,
PostLink{Text: "[More Posts]", Link: "archive.html"})
break
}
postlink := PostLink{Text: posts[i].Title, Link: posts[i].FileName}
postlinks = append(postlinks, postlink)
}
// create index.html
post_template := template.Must(template.ParseFiles("templates/post.template"))
var frontpageContent strings.Builder
numFrontpagePosts := 5
for i := len(posts) - 1; i >= 0 && i > len(posts)-1-numFrontpagePosts; i-- {
frontpageContent.WriteString(posts[i].Content)
frontpageContent.WriteString("<br><br><br>\n\n")
}
func() {
f, err := os.Create("./public/index.html")
if err != nil {
log.Fatal(err)
}
defer f.Close()
post_template.Execute(f, struct {
PostList []PostLink
Content template.HTML
}{postlinks, template.HTML(frontpageContent.String())})
}()
//create about.html
var aboutContent strings.Builder
func() {
f, err := os.Open("content/about.html")
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
for scanner.Scan() {
aboutContent.WriteString(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
}()
func() {
about_template := template.Must(template.ParseFiles("templates/about.template"))
f, err := os.Create("./public/about.html")
if err != nil {
log.Fatal(err)
}
defer f.Close()
about_template.Execute(f, struct {
Content template.HTML
}{template.HTML(aboutContent.String())})
}()
// create notes.html
// @MarkFix this will require pagination at some point
nostrEvents := func() []nostr.Event {
db := nostr.DBConnect()
defer db.Close()
// fetch latest nostr notes
nostr.FetchNewNostrMessages(db)
// fetch nostr notes from DB
return nostr.DBGetEvents(db)
}()
var notesContent strings.Builder
notesContent.WriteString("<h1>Notes</h1>\n")
eventLoop:
for i := len(nostrEvents) - 1; i >= 0; i-- {
if nostrEvents[i].Kind != nostr.KindTextNote { // && nostrEvents[i].Kind != nostr.KindRepost { // @MarkFix TODO support reposts
continue
}
content := nostrEvents[i].Content
for _, tag := range nostrEvents[i].Tags {
if tag[0] == "r" && len(tag) == 2 {
originalLink := tag[1]
link := tag[1]
_, err := url.Parse(link)
if err != nil {
log.Printf("note link could not be parsed - %s", link)
continue
}
if strings.Contains(link, "\"") {
log.Printf("note links shouldn't contain a '\"' - %s", link)
continue
}
if !strings.Contains(content, link) {
log.Printf("note contents should have contained link")
continue
}
if !strings.HasPrefix(link, "https://") && !strings.HasPrefix(link, "http://") {
log.Printf("note link needs 'https://' added - %s", link)
link = "https://" + link
}
if strings.HasSuffix(link, ".jpg") {
content = strings.Replace(content, originalLink, fmt.Sprintf("<br><img style=\"max-width: 40%%;\" src=\"%s\" />", link), 1)
// @MarkFix Something for YouTube videos?
} else {
content = strings.Replace(content, originalLink, fmt.Sprintf("<a href=\"%s\">%s</a>", link, originalLink), 1)
}
}
// @MarkFix TODO What to do with replies?
if tag[0] == "e" && len(tag) == 4 && tag[3] == "reply" {
log.Printf("skipping note reply")
continue eventLoop
}
if tag[0] == "e" && len(tag) == 4 && tag[3] == "mention" {
log.Printf("skipping note mention")
continue eventLoop
}
}
date := time.Unix(nostrEvents[i].CreatedAt, 0)
bech32NoteId := bech32.Encode("note", nostrEvents[i].Id)
notesContent.WriteString(fmt.Sprintf("<div id=\"%s\">\n", bech32NoteId))
notesContent.WriteString(fmt.Sprintf("<h2>%02d:%02d, %d %s %d</h2>\n", date.Hour(), date.Minute(), date.Day(), date.Month().String(), date.Year()))
notesContent.WriteString(fmt.Sprintf("<p>%s</p>\n", content))
notesContent.WriteString(fmt.Sprintf("<p><small><a href=\"nostr:%s\">Link</a></small></p>\n\n", bech32NoteId))
notesContent.WriteString("</div>")
}
func() {
f, err := os.Create("./public/notes.html")
if err != nil {
log.Fatal(err)
}
defer f.Close()
post_template.Execute(f, struct {
PostList []PostLink
Content template.HTML
}{postlinks, template.HTML(notesContent.String())})
}()
// create archive.html
var archiveContent strings.Builder
archiveContent.WriteString("<h1>Archive</h1>\n")
for i := len(posts) - 1; i >= 0; i-- {
archiveContent.WriteString("<h2>" + posts[i].Date + "</h2>\n")
archiveContent.WriteString("<a href='" + posts[i].FileName + "'>" + posts[i].Title + "</a>\n<br><br>\n")
}
func() {
f, err := os.Create("./public/archive.html")
if err != nil {
log.Fatal(err)
}
defer f.Close()
post_template.Execute(f, struct {
PostList []PostLink
Content template.HTML
}{postlinks, template.HTML(archiveContent.String())})
}()
// create post files
for _, post := range posts {
func() {
f, err := os.Create("./public/" + post.FileName)
if err != nil {
log.Fatal(err)
}
defer f.Close()
postContent := template.HTML(post.Content)
post_template.Execute(f, struct {
PostList []PostLink
Content template.HTML
}{postlinks, postContent})
}()
}
}
func createPostFile(fileName string) (result PostFile) {
result.FileName = fileName
f, err := os.Open("content/" + fileName)
if err != nil {
log.Fatal(err)
}
defer f.Close()
scanner := bufio.NewScanner(f)
if scanner.Scan() {
result.Title = scanner.Text()
}
if scanner.Scan() {
result.Date = scanner.Text()
}
var resultContent strings.Builder
resultContent.WriteString("<h1>" + result.Title + "</h1>")
resultContent.WriteString("<h2>" + result.Date + "</h2>")
for scanner.Scan() {
resultContent.WriteString(scanner.Text())
}
if err := scanner.Err(); err != nil {
log.Fatal(err)
}
result.Content = resultContent.String()
return result
}
func isPost(name string) (result bool) {
if name == "about.html" || strings.HasPrefix(name, "0000-") {
return false
}
if strings.HasSuffix(name, ".html") {
return true
}
return false
}