intake/web/html/html.go

92 lines
1.7 KiB
Go
Raw Permalink Normal View History

2025-01-24 00:18:41 +00:00
package html
import (
"embed"
2025-01-30 06:21:17 +00:00
"encoding/json"
2025-01-24 00:18:41 +00:00
"html/template"
"io"
2025-01-30 06:21:17 +00:00
"log"
"time"
"github.com/Jaculabilis/intake/core"
2025-01-24 00:18:41 +00:00
)
func rawHtml(str string) template.HTML {
return template.HTML(str)
}
func tsToDate(t int) string {
tm := time.Unix(int64(t), 0).UTC()
return tm.Format(time.DateTime)
}
2025-01-30 06:21:17 +00:00
func massDeactivateVals(items []core.Item) string {
var shorts []string
for _, item := range items {
shorts = append(shorts, core.FormatAsShort(item))
}
massDeac := struct {
Items []string `json:"items"`
}{shorts}
vals, err := json.Marshal(massDeac)
if err != nil {
log.Printf("error serializing mass deactivate list: %v", err)
}
return string(vals)
}
var funcs = template.FuncMap{
2025-01-30 06:21:17 +00:00
"raw": rawHtml,
"tsToDate": tsToDate,
"massDeacVars": massDeactivateVals,
}
2025-01-24 15:15:54 +00:00
//go:embed intake.css
var Stylesheet []byte
2025-01-28 04:34:07 +00:00
//go:embed htmx.org@2.0.4.js
var Htmx []byte
2025-01-24 00:18:41 +00:00
//go:embed *.html
var templates embed.FS
2025-01-28 03:35:13 +00:00
func load(files ...string) *template.Template {
files = append([]string{"layout.html"}, files...)
return template.Must(template.New("layout.html").Funcs(funcs).ParseFS(templates, files...))
2025-01-24 00:18:41 +00:00
}
var home = load("home.html")
type SourceData struct {
Name string
}
type HomeData struct {
Sources []SourceData
}
func Home(writer io.Writer, data HomeData) error {
2025-01-24 00:18:41 +00:00
return home.Execute(writer, data)
}
2025-01-28 03:35:13 +00:00
var feed = load("feed.html", "item.html")
type FeedData struct {
Items []core.Item
}
func Feed(writer io.Writer, data FeedData) error {
return feed.Execute(writer, data)
}
var item = load("itemPage.html", "item.html")
type ItemData struct {
Item core.Item
Open bool
}
func Item(writer io.Writer, data ItemData) error {
return item.Execute(writer, data)
}