66 lines
2.2 KiB
Go
66 lines
2.2 KiB
Go
package routes
|
|
|
|
import (
|
|
"html/template"
|
|
"net/http"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.a71.su/Andrew71/hibiscus-txt/internal/config"
|
|
"git.a71.su/Andrew71/hibiscus-txt/internal/files"
|
|
"git.a71.su/Andrew71/hibiscus-txt/internal/lang"
|
|
"git.a71.su/Andrew71/hibiscus-txt/internal/server/util"
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
// getDays calls getEntries for previous days' entries.
|
|
func getDays(w http.ResponseWriter, r *http.Request) {
|
|
description := template.HTML(
|
|
"<a href=\"#footer\">" + template.HTMLEscapeString(lang.Translate("prompt.days")) + "</a>")
|
|
getEntries(w, r, lang.Translate("title.days"), description, "day", func(files []string) []Entry {
|
|
var filesFormatted []Entry
|
|
for i := range files {
|
|
v := files[len(files)-1-i] // This is suboptimal, but reverse order is better here
|
|
dayString := v
|
|
t, err := time.Parse(time.DateOnly, v)
|
|
if err == nil {
|
|
dayString = t.Format("02 Jan 2006")
|
|
}
|
|
|
|
// Fancy text for today and tomorrow
|
|
// This looks bad, but strings.Title is deprecated, and I'm not importing a golang.org/x package for this...
|
|
// (chances we ever run into tomorrow are really low)
|
|
if v == config.Cfg.TodayDate() {
|
|
dayString = lang.Translate("link.today")
|
|
dayString = strings.ToTitle(string([]rune(dayString)[0])) + string([]rune(dayString)[1:])
|
|
} else if v > config.Cfg.TodayDate() {
|
|
dayString = lang.Translate("link.tomorrow")
|
|
dayString = strings.ToTitle(string([]rune(dayString)[0])) + string([]rune(dayString)[1:])
|
|
}
|
|
filesFormatted = append(filesFormatted, Entry{Title: dayString, Link: "day/" + v})
|
|
}
|
|
return filesFormatted
|
|
})
|
|
}
|
|
|
|
// getDay calls getEntry for a day entry.
|
|
func getDay(w http.ResponseWriter, r *http.Request) {
|
|
dayString := chi.URLParam(r, "day")
|
|
if dayString == "" {
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
util.HandleWrite(w.Write([]byte("day not specified")))
|
|
return
|
|
}
|
|
if dayString == config.Cfg.TodayDate() { // Today can still be edited
|
|
http.Redirect(w, r, "/", http.StatusFound)
|
|
return
|
|
}
|
|
|
|
title := dayString
|
|
t, err := time.Parse(time.DateOnly, dayString)
|
|
if err == nil { // This is low priority so silently fail
|
|
title = t.Format("02 Jan 2006")
|
|
}
|
|
|
|
getEntry(w, r, title, files.DataFile("day/"+dayString), false)
|
|
}
|