hibiscus/internal/server/routes/notes.go

56 lines
1.8 KiB
Go
Raw Permalink Normal View History

2024-10-23 14:11:02 +03:00
package routes
2024-10-21 16:46:25 +03:00
import (
"html/template"
"net/http"
"net/url"
"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
// getNotes calls getEntries for all notes.
func getNotes(w http.ResponseWriter, r *http.Request) {
2024-10-21 16:46:25 +03:00
// This is suboptimal, but will do...
description := template.HTML(
"<a href=\"#\" onclick='newNote(\"" + template.HTMLEscapeString(lang.Translate("prompt.notes")) + "\")'>" + template.HTMLEscapeString(lang.Translate("button.notes")) + "</a>" +
" <noscript>(" + template.HTMLEscapeString(lang.Translate("noscript.notes")) + ")</noscript>")
2024-10-23 14:11:02 +03:00
getEntries(w, r, lang.Translate("title.notes"), description, "notes", func(files []string) []Entry {
2024-10-21 16:46:25 +03:00
var filesFormatted []Entry
for _, v := range files {
// titleString := strings.Replace(v, "-", " ", -1) // This would be cool, but what if I need a hyphen?
filesFormatted = append(filesFormatted, Entry{Title: v, Link: "notes/" + v})
}
return filesFormatted
})
}
2024-10-23 14:11:02 +03:00
// getNote calls getEntry for a note.
func getNote(w http.ResponseWriter, r *http.Request) {
2024-10-21 16:46:25 +03:00
noteString := chi.URLParam(r, "note")
if noteString == "" {
w.WriteHeader(http.StatusBadRequest)
2024-10-23 14:11:02 +03:00
util.HandleWrite(w.Write([]byte("note not specified")))
2024-10-21 16:46:25 +03:00
return
}
// Handle non-latin note names
if decodedNote, err := url.QueryUnescape(noteString); err == nil {
noteString = decodedNote
}
2024-10-23 14:11:02 +03:00
getEntry(w, r, noteString, files.DataFile("notes/"+noteString), true)
2024-10-21 16:46:25 +03:00
}
2024-10-23 14:11:02 +03:00
// postNote calls postEntry for a note.
func postNote(w http.ResponseWriter, r *http.Request) {
2024-10-21 16:46:25 +03:00
noteString := chi.URLParam(r, "note")
if noteString == "" {
w.WriteHeader(http.StatusBadRequest)
2024-10-23 14:11:02 +03:00
util.HandleWrite(w.Write([]byte("note not specified")))
2024-10-21 16:46:25 +03:00
return
}
2024-10-23 14:11:02 +03:00
postEntry(files.DataFile("notes/"+noteString), w, r)
2024-10-21 16:46:25 +03:00
}