Files
pages/internal/server/render.go
T
Anya Tokugawa c1175d389f Implement Knowledge City platform, organizer and DAV integration
Capture accumulated application, UI, CLI, regression tests and design documentation changes, including task project mapping and default CalDAV list naming.

Keep deployment data, backups, local credentials and build artifacts out of Git. Move the Compose enrollment token to an ignored local environment file.
2026-09-27 14:44:43 +09:00

144 lines
2.9 KiB
Go

package server
import (
"html/template"
"net/http"
"net/url"
"strings"
"time"
)
func mergeTemplateData(
data any,
wikiName string,
theme string,
sThemeCSS string,
) map[string]any {
out := map[string]any{
"WikiName": wikiName,
"Theme": theme,
"ThemeCSS": sThemeCSS,
"Campus": nil,
"Domain": nil,
}
if data == nil {
return out
}
m, ok := data.(map[string]any)
if !ok {
out["Data"] = data
return out
}
for k, v := range m {
out[k] = v
}
return out
}
func (s *Server) render(
w http.ResponseWriter,
name string,
data any,
) {
domain := s.currentDomain()
campus := s.currentCampus()
if values, ok := data.(map[string]any); ok {
if context, ok := values["Context"].(*RequestContext); ok {
if context.Domain != nil {
domain = context.Domain
}
if context.Campus != nil {
campus = context.Campus
}
}
}
domainUUID := ""
if domain != nil {
domainUUID = domain.UUID
}
nonce := ""
if safeProjectionTemplate(name) {
var err error
nonce, err = setProjectionCSP(w, s.externalResourcePolicy(domainUUID))
if err != nil {
http.Error(w, "could not secure HTML projection", 500)
return
}
if name == "public_periodical.gohtml" {
if values, ok := data.(map[string]any); ok {
if origin, ok := values["PublicMediaOrigin"].(string); ok {
u, err := url.Parse(origin)
if err == nil && (u.Scheme == "https" || u.Scheme == "http") && u.Host != "" && u.User == nil && u.Path == "" && !strings.ContainsAny(origin, " ;\r\n\t") {
w.Header().Set("Content-Security-Policy", strings.Replace(w.Header().Get("Content-Security-Policy"), "img-src 'self'", "img-src 'self' "+origin, 1))
}
}
}
}
}
templates, err := s.templates.Clone()
if err != nil {
http.Error(w, err.Error(), 500)
return
}
// Template helpers run for each row/cell. Resolve configuration once per
// render instead of issuing locale/timezone queries for every invocation.
locale := s.getLocale(domainUUID)
location := s.getLocation(domainUUID)
templates = templates.Funcs(template.FuncMap{
"cspNonce": func() string { return nonce },
"T": func(key string) string {
return translate(locale, key)
},
"formatUnix": func(ts int64) string {
return time.Unix(ts, 0).
In(location).
Format("2006-01-02 15:04:05 MST")
},
"formatUnixMS": func(ts int64) string {
if ts <= 0 {
return "-"
}
return time.UnixMilli(ts).In(location).Format("2006-01-02 15:04")
},
})
err = templates.ExecuteTemplate(
w,
name,
func() map[string]any {
m :=
mergeTemplateData(
data,
s.getWikiName(domainUUID),
s.getTheme(domainUUID),
s.getThemeCSS(domainUUID),
)
m["Domain"] = domain
m["Campus"] = campus
if _, ok := m["Timezone"]; !ok {
m["Timezone"] = location.String()
}
return m
}(),
)
if err != nil {
http.Error(
w,
err.Error(),
500,
)
return
}
}