diff options
author | Runxi Yu <me@runxiyu.org> | 2025-03-07 20:59:37 +0800 |
---|---|---|
committer | Runxi Yu <me@runxiyu.org> | 2025-03-07 21:03:59 +0800 |
commit | ea0a7f238d933d20718cbc3544962358aa4645b1 (patch) | |
tree | 06bf3aad217cc755908def0d597f79786914547c | |
parent | css: Don't bold table headers unless it's a .title-row (diff) | |
download | forge-ea0a7f238d933d20718cbc3544962358aa4645b1.tar.gz forge-ea0a7f238d933d20718cbc3544962358aa4645b1.tar.zst forge-ea0a7f238d933d20718cbc3544962358aa4645b1.zip |
http/templates: Minify templates
-rw-r--r-- | go.mod | 2 | ||||
-rw-r--r-- | go.sum | 9 | ||||
-rw-r--r-- | resources.go | 33 |
3 files changed, 40 insertions, 4 deletions
@@ -10,6 +10,7 @@ require ( github.com/jackc/pgx/v5 v5.7.2 github.com/microcosm-cc/bluemonday v1.0.27 github.com/niklasfasching/go-org v1.7.0 + github.com/tdewolff/minify/v2 v2.21.3 github.com/yuin/goldmark v1.7.8 go.lindenii.runxiyu.org/lindenii-common v0.0.0-20250220024148-a3640b47317b golang.org/x/crypto v0.36.0 @@ -35,6 +36,7 @@ require ( github.com/kevinburke/ssh_config v1.2.0 // indirect github.com/pjbgf/sha1cd v0.3.2 // indirect github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect + github.com/tdewolff/parse/v2 v2.7.19 // indirect golang.org/x/exp v0.0.0-20250305212735-054e65f0b394 // indirect golang.org/x/net v0.37.0 // indirect golang.org/x/sync v0.12.0 // indirect @@ -41,8 +41,6 @@ github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399 h1:eMj github.com/go-git/go-git-fixtures/v4 v4.3.2-0.20231010084843-55a94097c399/go.mod h1:1OCfN199q1Jm3HZlxleg+Dw/mwps2Wbk9frAWm+4FII= github.com/go-git/go-git-fixtures/v5 v5.0.0-20241203230421-0753e18f8f03 h1:LumE+tQdnYW24a9RoO08w64LHTzkNkdUqBD/0QPtlEY= github.com/go-git/go-git-fixtures/v5 v5.0.0-20241203230421-0753e18f8f03/go.mod h1:hMKrMnUE4W0SJ7bFyM00dyz/HoknZoptGWzrj6M+dEM= -github.com/go-git/go-git/v5 v5.12.1-0.20250306065839-cc0ed3acb41b h1:oKgLff/2/aGGeTJA6jZZTS0ptie++sSkjTw2KETnLNU= -github.com/go-git/go-git/v5 v5.12.1-0.20250306065839-cc0ed3acb41b/go.mod h1:sFBXkITeIW/teSFKDcfpPHF95h2YPURY4rZCdzzEEmo= github.com/go-git/go-git/v5 v5.12.1-0.20250306131248-f7eb53c2ef7e h1:AkZEV+Im2c8zzqkXrcpxQE/RRSxi51MFjMpGCFCFAH4= github.com/go-git/go-git/v5 v5.12.1-0.20250306131248-f7eb53c2ef7e/go.mod h1:1FgBmxmy7D4Qz3VI3gypi8qPD/UMP3eAjwQ9Vz4q5qM= github.com/golang/groupcache v0.0.0-20241129210726-2c02b8208cf8 h1:f+oWsMOmNPc8JmEHVZIycC7hBoQxHH9pNKQORJNozsQ= @@ -92,6 +90,13 @@ github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81P github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/stretchr/testify v1.10.0 h1:Xv5erBjTwe/5IxqUQTdXv5kgmIvbHo3QQyRwhJsOfJA= github.com/stretchr/testify v1.10.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/tdewolff/minify/v2 v2.21.3 h1:KmhKNGrN/dGcvb2WDdB5yA49bo37s+hcD8RiF+lioV8= +github.com/tdewolff/minify/v2 v2.21.3/go.mod h1:iGxHaGiONAnsYuo8CRyf8iPUcqRJVB/RhtEcTpqS7xw= +github.com/tdewolff/parse/v2 v2.7.19 h1:7Ljh26yj+gdLFEq/7q9LT4SYyKtwQX4ocNrj45UCePg= +github.com/tdewolff/parse/v2 v2.7.19/go.mod h1:3FbJWZp3XT9OWVN3Hmfp0p/a08v4h8J9W1aghka0soA= +github.com/tdewolff/test v1.0.11-0.20231101010635-f1265d231d52/go.mod h1:6DAvZliBAAnD7rhVgwaM7DE5/d9NMOAJ09SqYqeK4QE= +github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739 h1:IkjBCtQOOjIn03u/dMQK9g+Iw9ewps4mCl1nB8Sscbo= +github.com/tdewolff/test v1.0.11-0.20240106005702-7de5f7df4739/go.mod h1:XPuWBzvdUzhCuxWO1ojpXsyzsA5bFoS3tO/Q3kFuTG8= github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY= github.com/yuin/goldmark v1.7.8 h1:iERMLn0/QJeHFhxSt3p6PeN9mGnvIKSpG9YYorDMnic= github.com/yuin/goldmark v1.7.8/go.mod h1:uzxRWxtg69N339t3louHJ7+O03ezfj6PlliRlaOzY1E= diff --git a/resources.go b/resources.go index 7ada53f..a25ddd6 100644 --- a/resources.go +++ b/resources.go @@ -8,6 +8,9 @@ import ( "html/template" "io/fs" "net/http" + + "github.com/tdewolff/minify/v2" + "github.com/tdewolff/minify/v2/html" ) // We embed all source for easy AGPL compliance. @@ -33,12 +36,38 @@ var resources_fs embed.FS var templates *template.Template func load_templates() (err error) { - templates, err = template.New("templates").Funcs(template.FuncMap{ + m := minify.New() + m.Add("text/html", &html.Minifier{ TemplateDelims: [2]string{"{{", "}}"}, KeepDefaultAttrVals: true }) + + templates = template.New("templates").Funcs(template.FuncMap{ "first_line": first_line, "base_name": base_name, "path_escape": path_escape, "query_escape": query_escape, - }).ParseFS(resources_fs, "templates/*") + }) + + err = fs.WalkDir(resources_fs, "templates", func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } + if !d.IsDir() { + content, err := fs.ReadFile(resources_fs, path) + if err != nil { + return err + } + + minified, err := m.Bytes("text/html", content) + if err != nil { + return err + } + + _, err = templates.Parse(string(minified)) + if err != nil { + return err + } + } + return nil + }) return err } |