Files
LuPhoto/internal/site/site.go
T
2026-11-04 05:10:46 -08:00

210 lines
5.0 KiB
Go

package site
import (
"embed"
"fmt"
"html/template"
"io"
"net/url"
"unicode"
"github.com/luciochen233/luphoto/internal/process"
)
//go:embed templates/*.html
var templateFiles embed.FS
var templates = template.Must(template.New("site").Funcs(template.FuncMap{"lang": Lang}).ParseFS(templateFiles, "templates/*.html"))
func Lang(text string) string {
for _, r := range text {
if unicode.Is(unicode.Han, r) {
return "zh"
}
}
return ""
}
const (
SiteName = "Photos · Lucio"
SiteDescription = "Photographs by Guoliang Chen · 陈国梁 · Lucio."
)
type Tiles struct {
Format string `json:"format"`
TileSize int `json:"tile_size"`
Overlap int `json:"overlap"`
Path string `json:"path"`
Scrambled bool `json:"scrambled,omitempty"`
Cell int `json:"cell,omitempty"`
}
type Photo struct {
process.Pyramid
Title string `json:"title,omitempty"`
Caption string `json:"caption,omitempty"`
Alt string `json:"alt,omitempty"`
Slides int `json:"slides"`
EXIF *process.EXIF `json:"exif,omitempty"`
}
type Manifest struct {
Build string `json:"build"`
Slug string `json:"slug"`
Title string `json:"title"`
Subtitle string `json:"subtitle,omitempty"`
Date string `json:"date,omitempty"`
Order *int `json:"order,omitempty"`
Hidden bool `json:"hidden,omitempty"`
CollapsedFocusY float64 `json:"collapsed_focus_y"`
ShowEXIF bool `json:"show_exif"`
Tiles Tiles `json:"tiles"`
Cover *process.Pyramid `json:"cover"`
OG string `json:"og,omitempty"`
Photos []Photo `json:"photos"`
}
type IndexAlbum struct {
Slug string `json:"slug"`
Title string `json:"title"`
Subtitle string `json:"subtitle,omitempty"`
Date string `json:"date,omitempty"`
CollapsedFocusY float64 `json:"collapsed_focus_y"`
Photos int `json:"photos"`
Manifest string `json:"manifest"`
Cover *process.Pyramid `json:"cover"`
OG string `json:"og,omitempty"`
}
type Index struct {
Build string `json:"build"`
Tiles Tiles `json:"tiles"`
Albums []IndexAlbum `json:"albums"`
}
func IndexURL(build string) string {
return "/index." + build + ".json"
}
func AlbumPath(slug string) string {
return "/a/" + url.PathEscape(slug) + "/"
}
func ManifestURL(build, slug string) string {
return AlbumPath(slug) + "manifest." + build + ".json"
}
func ManifestName(build string) string {
return "manifest." + build + ".json"
}
func IndexName(build string) string {
return "index." + build + ".json"
}
func OGPath(hash string) string {
if hash == "" {
return ""
}
return "/og/" + hash + ".jpg"
}
type Page struct {
Assets Assets
BudgetMS int
MaxDPR float64
Concurrency int
BlockContextMenu bool
BaseURL string
}
type head struct {
Page
Build string
Title string
Description string
Path string
Image string
IndexURL string
}
func (h head) Absolute(path string) string {
if path == "" {
return ""
}
return h.BaseURL + path
}
type indexPage struct {
head
Albums []IndexAlbum
}
type albumPage struct {
head
AlbumTitle string
Slug string
Subtitle string
Date string
Photos int
ManifestURL string
}
func RenderIndex(w io.Writer, index *Index, page Page) error {
image := ""
if len(index.Albums) > 0 {
image = OGPath(index.Albums[0].OG)
}
return templates.ExecuteTemplate(w, "index.html", indexPage{
head: head{
Page: page,
Build: index.Build,
Title: SiteName,
Description: SiteDescription,
Path: "/",
Image: image,
IndexURL: IndexURL(index.Build),
},
Albums: index.Albums,
})
}
func RenderAlbum(w io.Writer, m *Manifest, page Page) error {
description := m.Subtitle
if description == "" {
description = fmt.Sprintf("%d photographs by Lucio", len(m.Photos))
if m.Date != "" {
description += ", " + m.Date
}
description += "."
}
return templates.ExecuteTemplate(w, "album.html", albumPage{
head: head{
Page: page,
Build: m.Build,
Title: m.Title + " · Lucio",
Description: description,
Path: AlbumPath(m.Slug),
Image: OGPath(m.OG),
IndexURL: IndexURL(m.Build),
},
AlbumTitle: m.Title,
Slug: m.Slug,
Subtitle: m.Subtitle,
Date: m.Date,
Photos: len(m.Photos),
ManifestURL: ManifestURL(m.Build, m.Slug),
})
}
func RenderNotFound(w io.Writer, build string, page Page) error {
return templates.ExecuteTemplate(w, "404.html", head{
Page: page,
Build: build,
Title: "Not found · Lucio",
Description: SiteDescription,
Path: "/404.html",
IndexURL: IndexURL(build),
})
}