cmd/server/genblog.go (view raw)
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 |
package main
import (
"fmt"
"os"
"sort"
"strings"
"text/template"
)
type (
blogData struct {
Date string
Title string
URL string
Text string
Attachments []string
}
)
func generateBlog() {
fmt.Println("Generating blog dir...")
genBlogDir()
fmt.Println("Generating blog page...")
blogDataSlice := generateBlogPage()
fmt.Println("Generating blog posts...")
generateBlogPosts(blogDataSlice)
fmt.Println("Generating blog rss...")
genRSSBlog(blogDataSlice)
}
func genBlogDir() {
os.RemoveAll("html/en/blog")
err := os.Mkdir("html/en/blog", 0o755)
check(err)
}
func fillLinks(content []byte, post blogData) string {
state := 0 // 1: link text, 2: attachment text, 5 link, 6 attachment
var parsedCont, linkText strings.Builder
for i, char := range content {
switch {
// no state, not in any links
case state == 0:
if char != '{' { // continue if no link is found
parsedCont.WriteByte(char)
continue
}
state = 3
continue
case state == 3:
// check link type
switch char {
case 'l':
state = 1
parsedCont.WriteString("<a href=\"")
continue
case 'a':
state = 2
parsedCont.WriteString("<a href=\"")
parsedCont.WriteString(
"/attachments/" + post.Date + " " + post.Title + "/")
continue
default:
fmt.Printf("ERROR: invalid link identifier at i: %d\n", i)
panic(1)
}
// parsing link text
case state == 1 || state == 2:
if char == '|' {
state += 4
continue
}
linkText.WriteByte(char)
// parsing link
case state == 5 || state == 6:
if char != '}' {
parsedCont.WriteByte(char)
continue
}
parsedCont.WriteString(
"\">" + linkText.String() + "</a>")
state = 0
linkText.Reset()
continue
}
}
return parsedCont.String()
}
func generateBlogPage() []blogData {
// open blog dir
blogDir, err := os.Open("blog")
if err != nil {
fmt.Printf("ERROR opening blog dir: %s\n", err)
}
defer blogDir.Close()
// read blog dir list
postDirList, err := blogDir.Readdirnames(0)
if err != nil {
fmt.Printf("ERROR reading blog list: %s\n", err)
}
// sort in reverse to show latest post first
sort.Sort(sort.Reverse(sort.StringSlice(postDirList)))
var blogDataSlice []blogData
// separate date and title in struct
for _, post := range postDirList {
fmt.Println(post)
blogDataSlice = append(blogDataSlice, blogData{
Date: post[:10],
Title: post[11:],
URL: "/en/blog/" +
strings.ToLower(strings.ReplaceAll(post, " ", "-")),
})
}
// template
tpl, err := template.ParseFiles("templates/blogindex.html.gotmpl")
if err != nil {
fmt.Printf("ERROR creating template: %s\n", err)
}
f, err := os.Create("html/en/blog.html")
if err != nil {
fmt.Printf("ERROR creating html: %s\n", err)
panic(1)
}
err = tpl.ExecuteTemplate(f, "blogindex.html.gotmpl", blogDataSlice)
if err != nil {
fmt.Printf("ERROR executing template: %s\n", err)
}
return blogDataSlice
}
func generateBlogPosts(blogDataSlice []blogData) {
// for each blog
for _, post := range blogDataSlice {
// read file
content, err := os.ReadFile(
"blog/" + post.Date + " " + post.Title + "/post.txt")
if err != nil {
fmt.Printf("ERROR reading post file: %s\n", err)
}
// replace my link format with html style
post.Text = fillLinks(content, post)
if post.Text == "" {
fmt.Printf("ERROR converting links\n")
}
// convert newlines
post.Text = strings.ReplaceAll(post.Text, "\n", "<br>")
// throw content into template
tpl, err := template.ParseFiles("templates/blogpost.html.gotmpl")
if err != nil {
fmt.Printf("ERROR creating template: %s\n", err)
}
f, err := os.Create("html/" + post.URL + ".html")
if err != nil {
fmt.Printf("ERROR creating html: %s\n", err)
panic(1)
}
err = tpl.ExecuteTemplate(f, "blogpost.html.gotmpl", post)
if err != nil {
fmt.Printf("ERROR executing template: %s\n", err)
}
}
}
|