intake/web/html/html.go

92 lines
1.7 KiB
Go

package html
import (
"embed"
"encoding/json"
"html/template"
"io"
"log"
"time"
"github.com/Jaculabilis/intake/core"
)
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)
}
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{
"raw": rawHtml,
"tsToDate": tsToDate,
"massDeacVars": massDeactivateVals,
}
//go:embed intake.css
var Stylesheet []byte
//go:embed htmx.org@2.0.4.js
var Htmx []byte
//go:embed *.html
var templates embed.FS
func load(files ...string) *template.Template {
files = append([]string{"layout.html"}, files...)
return template.Must(template.New("layout.html").Funcs(funcs).ParseFS(templates, files...))
}
var home = load("home.html")
type SourceData struct {
Name string
}
type HomeData struct {
Sources []SourceData
}
func Home(writer io.Writer, data HomeData) error {
return home.Execute(writer, data)
}
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)
}