-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblog.go
181 lines (158 loc) · 3.92 KB
/
blog.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
package main
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// Version 2, December 2004
//
// Copyright (C) 2024 Thiago Kenji Okada <thiagokokada@gmail.com>
//
// Everyone is permitted to copy and distribute verbatim or modified
// copies of this license document, and changing it is allowed as long
// as the name is changed.
//
// DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
// TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
//
// 0. You just DO WHAT THE FUCK YOU WANT TO.
import (
"bytes"
"flag"
"fmt"
"io/fs"
"log"
"net/url"
"os"
"path/filepath"
"sort"
"strings"
"time"
"github.com/gorilla/feeds"
"github.com/yuin/goldmark"
)
const rssBaseLink = "https://github.com/thiagokokada/blog/blob/main"
const readmeTemplate = `# Blog
Mirror of my blog in https://kokada.capivaras.dev/.
## Posts
[](https://raw.githubusercontent.com/thiagokokada/blog/main/rss.xml)
%s
`
type post struct {
title string
file string
contents []byte
date time.Time
}
func must1[T any](v T, err error) T {
must(err)
return v
}
func must(err error) {
if err != nil {
panic(err)
}
}
func extractTitleAndContents(raw []byte) (title string, contents []byte, err error) {
for i, c := range raw {
// We are assuming that each file has one title as a H1 header
if c == '\n' {
original := string(raw[:i])
clean := strings.Replace(string(raw[:i]), "# ", "", 1)
if clean == original {
return "", nil, fmt.Errorf("could not find title")
}
title = clean
contents = raw[i:]
break
}
}
return title, contents, nil
}
func grabPosts() []post {
var posts []post
must(filepath.WalkDir(".", func(path string, d fs.DirEntry, err error) error {
if err != nil {
return err
}
// Find markdown files, but ignore hidden files
if filepath.Ext(d.Name()) == ".md" && d.Name()[0] != '.' {
dir := filepath.Dir(path)
// Ignore root directory
if dir == "." {
return nil
}
// Parse directory name as a date
date, err := time.Parse(time.DateOnly, dir)
if err != nil {
log.Printf("WARN: ignoring non-date directory: %s\n", path)
return nil
}
// Load the contents of the Markdown and try to parse
// the title
raw := must1(os.ReadFile(path))
title, contents, err := extractTitleAndContents(raw)
if err != nil || title == "" || contents == nil {
return fmt.Errorf(
"something is wrong with file: %s, title: %s, contents: %s, error: %w",
path,
title,
contents,
err,
)
}
posts = append(posts, post{
title: title,
file: path,
contents: contents,
date: date,
})
}
return nil
}))
sort.Slice(posts, func(i, j int) bool {
// Reverse sorting based on filename
return posts[i].file > posts[j].file
})
return posts
}
func genRss(posts []post) string {
feed := &feeds.Feed{
Title: "kokada's blog",
Description: "# dd if=/dev/urandom of=/dev/brain0",
}
var items []*feeds.Item
for _, post := range posts {
link := must1(url.JoinPath(rssBaseLink, post.file))
var buf bytes.Buffer
must(goldmark.Convert(post.contents, &buf))
items = append(items, &feeds.Item{
Title: post.title,
Link: &feeds.Link{Href: link},
Created: post.date,
Id: link,
Description: buf.String(),
})
}
feed.Items = items
return must1(feed.ToRss())
}
func genReadme(posts []post) string {
var titles []string
for _, post := range posts {
title := fmt.Sprintf(
"- [%s](%s) - %s",
post.title,
post.file,
post.date.Format(time.DateOnly),
)
titles = append(titles, title)
}
return fmt.Sprintf(readmeTemplate, strings.Join(titles, "\n"))
}
func main() {
rss := flag.Bool("rss", false, "Generate RSS (XML) instead of README.md")
flag.Parse()
posts := grabPosts()
if *rss {
fmt.Print(genRss(posts))
} else {
fmt.Print(genReadme(posts))
}
}