2024-10-23 14:11:02 +03:00
|
|
|
package routes
|
2024-10-21 16:46:25 +03:00
|
|
|
|
|
|
|
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"
|
2024-10-23 14:11:02 +03:00
|
|
|
"git.a71.su/Andrew71/hibiscus-txt/internal/server/util"
|
2024-10-21 16:46:25 +03:00
|
|
|
"github.com/go-chi/chi/v5"
|
|
|
|
)
|
|
|
|
|
2024-10-23 14:11:02 +03:00
|
|
|
// getDays calls getEntries for previous days' entries.
|
|
|
|
func getDays(w http.ResponseWriter, r *http.Request) {
|
2024-10-21 16:46:25 +03:00
|
|
|
description := template.HTML(
|
|
|
|
"<a href=\"#footer\">" + template.HTMLEscapeString(lang.Translate("prompt.days")) + "</a>")
|
2024-10-23 14:11:02 +03:00
|
|
|
getEntries(w, r, lang.Translate("title.days"), description, "day", func(files []string) []Entry {
|
2024-10-21 16:46:25 +03:00
|
|
|
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
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-10-23 14:11:02 +03:00
|
|
|
// getDay calls getEntry for a day entry.
|
|
|
|
func getDay(w http.ResponseWriter, r *http.Request) {
|
2024-10-21 16:46:25 +03:00
|
|
|
dayString := chi.URLParam(r, "day")
|
|
|
|
if dayString == "" {
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
2024-10-23 14:11:02 +03:00
|
|
|
util.HandleWrite(w.Write([]byte("day not specified")))
|
2024-10-21 16:46:25 +03:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2024-10-23 14:11:02 +03:00
|
|
|
getEntry(w, r, title, files.DataFile("day/"+dayString), false)
|
2024-10-21 16:46:25 +03:00
|
|
|
}
|