intake/web/html/html.go

74 lines
1.2 KiB
Go
Raw Normal View History

2025-01-24 00:18:41 +00:00
package html
import (
"embed"
"html/template"
"io"
"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)
}
var funcs = template.FuncMap{
"raw": rawHtml,
"tsToDate": tsToDate,
}
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)
}