-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathposts.go
92 lines (77 loc) · 2.55 KB
/
posts.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
package main
import (
"fmt"
"os"
"strings"
)
type Blog struct {
Title string
Date string
Text string
}
func makeTitles(posts []Blog) (titles []string) {
for _, post := range posts {
titles = append(titles, "<div class='blog'><br><a href='https://j45.dev/blog/post_"+strings.ToLower(strings.ReplaceAll(post.Title, " ", "_"))+".html' style='text-decoration: none; color: #000;'><mybutton>"+post.Title+"</mybutton></a></div>")
}
return titles
}
func makePost(post Blog) (content string) {
return "<bigtext>" + post.Title + "</bigtext> - " + post.Date + "<a href='https://j45.dev/blog/'><exit style='color: #000;'>X</exit></a><br><p>" + post.Text + "</p><br><myButton onclick='function copy() { navigator.clipboard.writeText(`https://j45.dev/blog/post_" + strings.ToLower(strings.ReplaceAll(post.Title, " ", "_")) + ".html`); alert(`Copied blog post link to clipboard`); }; copy();'>Share this blog post!</myButton><br><a href='https://j45.dev/blog/'><img src='https://j45.dev/images/return.png' style='cursor: pointer;'></a>"
}
func main() {
files, err := os.ReadDir("./")
if err != nil {
panic(err)
}
for _, file := range files {
if strings.HasPrefix(file.Name(), "post_") && strings.HasSuffix(file.Name(), ".html") {
os.Remove(file.Name())
}
}
dat, err := os.ReadFile("blogs.csv")
if err != nil {
panic(err)
}
lines := strings.Split(string(dat), "\n")
var posts []Blog
lines = lines[1:] // first line is the layout so remove it
for i := len(lines) - 1; i >= 0; i-- {
line := lines[i]
fields := strings.Split(line, "|")
if len(fields) < 3 { // invalid line
continue
}
post := Blog{
Title: fields[0],
Date: fields[1],
Text: fields[2],
}
posts = append(posts, post)
template_post, err := os.ReadFile("template_post.html")
if err != nil {
panic(err)
}
var wanted string = "{{ post }}"
before, after, found := strings.Cut(string(template_post), wanted)
if found == false {
fmt.Println(wanted + " not found in provided template")
}
err = os.WriteFile("post_"+strings.ToLower(strings.ReplaceAll(post.Title, " ", "_"))+".html", []byte(before+makePost(post)+after), 0644)
if err != nil {
panic(err)
}
}
template_index, err := os.ReadFile("template_index.html")
if err != nil {
panic(err)
}
var wanted string = "{{ posts }}"
before, after, found := strings.Cut(string(template_index), wanted)
if found == false {
fmt.Println(wanted + " not found in provided template")
}
err = os.WriteFile("index.html", []byte(before+strings.Join(makeTitles(posts), "\n")+after), 0644)
if err != nil {
panic(err)
}
}