forked from lalluviamola/web-blog
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgen_html.go
352 lines (317 loc) · 8.43 KB
/
gen_html.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package main
import (
"fmt"
"html/template"
"io"
"io/ioutil"
"math/rand"
"net/url"
"path/filepath"
"sort"
"strings"
"time"
"github.com/chilts/sid"
"github.com/kjk/betterguid"
"github.com/oklog/ulid"
"github.com/rs/xid"
uuid "github.com/satori/go.uuid"
"github.com/segmentio/ksuid"
"github.com/sony/sonyflake"
atom "github.com/thomas11/atomgenerator"
)
func copyAndSortArticles(articles []*Article) []*Article {
n := len(articles)
res := make([]*Article, n)
copy(res, articles)
sort.Slice(res, func(i, j int) bool {
return res[j].PublishedOn.After(res[i].PublishedOn)
})
return res
}
func genAtomXML(store *Articles, excludeNotes bool) ([]byte, error) {
articles := store.getBlogNotHidden()
if excludeNotes {
articles = filterArticlesByTag(articles, "note", false)
}
articles = copyAndSortArticles(articles)
n := 25
if n > len(articles) {
n = len(articles)
}
latest := make([]*Article, n)
size := len(articles)
for i := 0; i < n; i++ {
latest[i] = articles[size-1-i]
}
pubTime := time.Now()
if len(articles) > 0 {
pubTime = articles[0].PublishedOn
}
feed := &atom.Feed{
Title: "Krzysztof Kowalczyk blog",
Link: "https://blog.kowalczyk.info/atom.xml",
PubDate: pubTime,
}
for _, a := range latest {
//id := fmt.Sprintf("tag:blog.kowalczyk.info,1999:%d", a.Id)
e := &atom.Entry{
Title: a.Title,
Link: "https://blog.kowalczyk.info" + a.URL(),
Content: a.BodyHTML,
PubDate: a.PublishedOn,
}
feed.AddEntry(e)
}
return feed.GenXml()
}
func wwwPath(fileName string) string {
fileName = strings.TrimLeft(fileName, "/")
path := filepath.Join(dirWwwGenerated, fileName)
must(createDirForFile(path))
return path
}
func wwwWriteFile(fileName string, d []byte) {
path := wwwPath(fileName)
//logf(ctx(), "%s\n", path)
ioutil.WriteFile(path, d, 0644)
}
// TODO: should be https://blog.kjk.workers.dev for dev deployment
func getHostURL() string {
return "https://blog.kowalczyk.info"
}
// https://www.linkedin.com/shareArticle?mini=true&;url=https://nodesource.com/blog/why-the-new-v8-is-so-damn-fast"
func makeLinkedinShareURL(article *Article) string {
uri := getHostURL() + article.URL()
uri = url.QueryEscape(uri)
return fmt.Sprintf(`https://www.linkedin.com/shareArticle?mini=true&url=%s`, uri)
}
// https://www.facebook.com/sharer/sharer.php?u=https://nodesource.com/blog/why-the-new-v8-is-so-damn-fast
func makeFacebookShareURL(article *Article) string {
uri := getHostURL() + article.URL()
uri = url.QueryEscape(uri)
return fmt.Sprintf(`https://www.facebook.com/sharer/sharer.php?u=%s`, uri)
}
// https://twitter.com/intent/tweet?text=%s&url=%s&via=kjk
func makeTwitterShareURL(article *Article) string {
title := url.QueryEscape(article.Title)
uri := getHostURL() + article.URL()
uri = url.QueryEscape(uri)
return fmt.Sprintf(`https://twitter.com/intent/tweet?text=%s&url=%s&via=kjk`, title, uri)
}
// TagInfo represents a single tag for articles
type TagInfo struct {
URL string
Name string
Count int
}
var (
allTags []*TagInfo
)
func buildTags(articles []*Article) []*TagInfo {
if allTags != nil {
return allTags
}
var res []*TagInfo
ti := &TagInfo{
URL: "/archives.html",
Name: "all",
Count: len(articles),
}
res = append(res, ti)
tagCounts := make(map[string]int)
for _, a := range articles {
for _, tag := range a.Tags {
tagCounts[tag]++
}
}
var tags []string
for tag := range tagCounts {
tags = append(tags, tag)
}
sort.Strings(tags)
for _, tag := range tags {
count := tagCounts[tag]
ti = &TagInfo{
URL: "/tag/" + tag,
Name: tag,
Count: count,
}
res = append(res, ti)
}
allTags = res
return res
}
func writeArticlesArchiveForTag(store *Articles, tag string, w io.Writer) error {
path := "/archives.html"
articles := store.getBlogNotHidden()
if tag != "" {
articles = filterArticlesByTag(articles, tag, true)
// must manually resolve conflict due to urlify
tagInPath := tag
if tag == "c#" {
tagInPath = "csharp"
} else if tag == "c++" {
tagInPath = "cplusplus"
}
tagInPath = urlify(tagInPath)
path = fmt.Sprintf("/article/archives-by-tag-%s.html", tagInPath)
from := "/tag/" + tag
addRewrite(from, path)
}
model := struct {
Article *Article
PostsCount int
Tag string
Years []Year
Tags []*TagInfo
}{
PostsCount: len(articles),
Years: buildYearsFromArticles(articles),
Tag: tag,
Tags: buildTags(articles),
}
return execTemplate(path, "archive.tmpl.html", model, w)
}
func genIndex(store *Articles, w io.Writer) error {
// /
articles := store.getBlogNotHidden()
if len(articles) > 5 {
articles = articles[:5]
}
articleCount := len(articles)
websiteIndexPage := store.idToArticle[notionWebsiteStartPage]
model := struct {
Article *Article
Articles []*Article
ArticleCount int
WebsiteHTML template.HTML
}{
Article: nil, // always nil
ArticleCount: articleCount,
Articles: articles,
WebsiteHTML: websiteIndexPage.HTMLBody,
}
return execTemplate("/index.html", "mainpage.tmpl.html", model, w)
}
func genChangelog(store *Articles, w io.Writer) error {
// /changelog.html
var articles []*Article
for _, a := range store.articles {
if !a.IsHidden() {
articles = append(articles, a)
}
}
sort.Slice(articles, func(i, j int) bool {
a1 := articles[i]
a2 := articles[j]
return a1.UpdatedOn.After(a2.UpdatedOn)
})
if len(articles) > 64 {
articles = articles[:64]
}
prevAge := -1
for _, a := range articles {
age := a.UpdatedAge()
if prevAge != age {
a.UpdatedAgeStr = fmt.Sprintf("%d d", a.UpdatedAge())
}
prevAge = age
}
model := struct {
Article *Article
Articles []*Article
}{
Article: nil, // always nil
Articles: articles,
}
return execTemplate("/changelog.html", "changelog.tmpl.html", model, w)
}
func gen404(store *Articles, w io.Writer) error {
// store is not used
model := struct {
}{}
return execTemplate("/404.html", "404.tmpl.html", model, w)
}
func genArticle(article *Article, w io.Writer) error {
canonicalURL := getHostURL() + article.URL()
model := struct {
Article *Article
CanonicalURL string
CoverImage string
PageTitle string
TagsDisplay string
HeaderImageURL string
NotionEditURL string
Description string
TwitterShareURL string
FacebookShareURL string
LinkedInShareURL string
}{
Article: article,
CanonicalURL: canonicalURL,
CoverImage: "https://blog.kowalczyk.info" + article.HeaderImageURL,
PageTitle: article.Title,
Description: article.Description,
TwitterShareURL: makeTwitterShareURL(article),
FacebookShareURL: makeFacebookShareURL(article),
LinkedInShareURL: makeLinkedinShareURL(article),
}
if article.page != nil {
id := normalizeID(article.page.ID)
model.NotionEditURL = "https://notion.so/" + id
}
path := fmt.Sprintf("/article/%s.html", article.ID)
logvf("%s => %s, %s, %s\n", article.ID, path, article.URL(), article.Title)
return execTemplate(path, "article.tmpl.html", model, w)
}
func genGoCookbook(store *Articles, w io.Writer) error {
// url: /book/go-cookbook.html
model := struct {
}{}
return execTemplate("/book/go-cookbook.html", "go-cookbook.tmpl.html", model, w)
}
/*
func genWindowsProgramming(store *Articles, w io.Writer) error {
// url: /book/windows-programming-in-go.html
model := struct {
}{}
return execTemplate("/book/go-cookbook.html", tmplGoC"go-cookbook.tmpl.html"ookBook, model, w)
}
*/
func genToolGenerateUniqueID(store *Articles, w io.Writer) error {
// /tools/generate-unique-id
idXid := xid.New()
idKsuid := ksuid.New()
t := time.Now().UTC()
entropy := rand.New(rand.NewSource(t.UnixNano()))
idUlid := ulid.MustNew(ulid.Timestamp(t), entropy)
betterGUID := betterguid.New()
uuid := uuid.NewV4()
flake := sonyflake.NewSonyflake(sonyflake.Settings{})
sfid, err := flake.NextID()
sfidstr := fmt.Sprintf("%x", sfid)
if err != nil {
sfidstr = err.Error()
}
model := struct {
Xid string
Ksuid string
Ulid string
BetterGUID string
Sonyflake string
Sid string
UUIDv4 string
}{
Xid: idXid.String(),
Ksuid: idKsuid.String(),
Ulid: idUlid.String(),
BetterGUID: betterGUID,
Sonyflake: sfidstr,
Sid: sid.Id(),
UUIDv4: uuid.String(),
}
// make sure /tools/generate-unique-id is served as html
path := "/tools/generate-unique-id.html"
addRewrite("/tools/generate-unique-id", path)
return execTemplate(path, "generate-unique-id.tmpl.html", model, w)
}