Files
wire/internal/ramblings/ramblings.go
2026-05-25 16:11:20 -04:00

75 lines
1.3 KiB
Go

package ramblings
import (
"html/template"
"log"
"net/http"
"os"
"path/filepath"
"strings"
"golang.org/x/text/cases"
"golang.org/x/text/language"
)
type Ramblings []Rambles
type Rambles struct {
Title string
Link string
Content template.HTML
}
type RamblingsParser struct {
path string
}
func NewParser(path string) *RamblingsParser {
rp := RamblingsParser{
path: path,
}
return &rp
}
func (rp *RamblingsParser) GetRamblings() (rs Ramblings) {
entries, err := os.ReadDir(rp.path)
if err != nil {
log.Fatal(err)
}
for _, ent := range entries {
if ent.IsDir() {
continue
}
title := ent.Name()
title = strings.ReplaceAll(title, "_", " ")
title = strings.ReplaceAll(title, "-", " ")
ext := filepath.Ext(ent.Name())
title = strings.TrimSuffix(title ,ext)
caser := cases.Title(language.AmericanEnglish)
title = caser.String(title)
fp := filepath.Join(rp.path, ent.Name())
contents, err := os.ReadFile(fp)
if err != nil {
log.Fatal(err)
}
rs = append(rs, Rambles{
Title: title,
Link: template.HTMLEscapeString(ent.Name()),
Content: template.HTML(string(contents)),
})
}
return
}
func NewHandler(ramble Rambles) func(w http.ResponseWriter, r *http.Request) {
return func(w http.ResponseWriter, r *http.Request) {
w.Write([]byte(ramble.Content))
}
}