genblog, main: fill links
crispy-caesus crispy@crispy-caesus.eu
Fri, 20 Mar 2026 22:54:18 +0100
2 files changed,
62 insertions(+),
1 deletions(-)
M
cmd/server/genblog.go
→
cmd/server/genblog.go
@@ -25,6 +25,62 @@ fmt.Println("Generating blog posts...")
generateBlogPosts(blogDataSlice) } +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")@@ -79,7 +135,10 @@ "blog/" + post.Date + " " + post.Title + "/post.txt")
if err != nil { fmt.Printf("ERROR reading post file: %s\n", err) } - post.Text = string(content) + post.Text = fillLinks(content, post) + if post.Text == "" { + fmt.Printf("ERROR converting links\n") + } post.Text = strings.ReplaceAll(post.Text, "\n", "<br>") tpl, err := template.ParseFiles("templates/blogpost.gotmpl")
M
cmd/server/main.go
→
cmd/server/main.go
@@ -24,8 +24,10 @@ return
} fs := http.FileServer(http.Dir("static")) + blogfs := http.FileServer(http.Dir("blog")) http.Handle("/static/", http.StripPrefix("/static/", fs)) + http.Handle("/attachments/", http.StripPrefix("/attachments/", blogfs)) http.HandleFunc("/", handler)