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)
}
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("" + linkText.String() + "")
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", "
")
// 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)
}
}
}