about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorAlan Pearce2024-04-18 12:31:48 +0200
committerAlan Pearce2024-04-18 12:37:41 +0200
commitc6917416ca0dcc36af98b7039fd19c57d8dcedb5 (patch)
tree5caf8d8ab4fccbaf1be3ca1d878678660e5b6992
parent100034677d2b4c4678c050ab08889b581c889840 (diff)
downloadwebsite-c6917416ca0dcc36af98b7039fd19c57d8dcedb5.tar.lz
website-c6917416ca0dcc36af98b7039fd19c57d8dcedb5.tar.zst
website-c6917416ca0dcc36af98b7039fd19c57d8dcedb5.zip
prettify HTML
-rw-r--r--cmd/build/build.go23
-rw-r--r--go.mod4
-rw-r--r--go.sum17
-rw-r--r--server.go1
4 files changed, 19 insertions, 26 deletions
diff --git a/cmd/build/build.go b/cmd/build/build.go
index 0436c20..4254bf8 100644
--- a/cmd/build/build.go
+++ b/cmd/build/build.go
@@ -21,15 +21,16 @@ import (
 
 	"github.com/BurntSushi/toml"
 	"github.com/PuerkitoBio/goquery"
+	"github.com/a-h/htmlformat"
 	"github.com/adrg/frontmatter"
 	"github.com/antchfx/xmlquery"
 	"github.com/antchfx/xpath"
 	mapset "github.com/deckarep/golang-set/v2"
-	iohelper "github.com/jf-tech/go-corelib/ios"
 	"github.com/pkg/errors"
 	"github.com/yuin/goldmark"
 	"github.com/yuin/goldmark/extension"
-	"github.com/yuin/goldmark/renderer/html"
+	htmlrenderer "github.com/yuin/goldmark/renderer/html"
+	"golang.org/x/net/html"
 )
 
 type PostMatter struct {
@@ -79,7 +80,7 @@ func readPosts(root string, inputDir string, outputDir string) ([]Post, Tags, er
 	urlReplacer := strings.NewReplacer(root, "", ".md", "/")
 	markdown := goldmark.New(
 		goldmark.WithRendererOptions(
-			html.WithUnsafe(),
+			htmlrenderer.WithUnsafe(),
 		),
 		goldmark.WithExtensions(
 			extension.GFM,
@@ -154,18 +155,20 @@ func layout(filename string, config config.Config, pageTitle string) (*goquery.D
 }
 
 func renderHTML(doc *goquery.Document) io.Reader {
-	r := bytes.NewBufferString("<!doctype html>\n")
-	// hello go? HTML 5 does not have self-closing tags
-	replacer := iohelper.NewBytesReplacingReader(r, []byte("/>"), []byte(" >"))
+	r, w := io.Pipe()
 
 	// TODO: return errors to main thread
 	go func() {
-		err := goquery.Render(r, doc.Children())
+		w.Write([]byte("<!doctype html>\n"))
+		err := htmlformat.Nodes(w, []*html.Node{doc.Children().Get(0)})
 		if err != nil {
 			slog.Error("error rendering html", "error", err)
+			w.CloseWithError(err)
+			return
 		}
+		defer w.Close()
 	}()
-	return replacer
+	return r
 }
 
 func renderPost(post Post, config config.Config) (r io.Reader, err error) {
@@ -255,7 +258,7 @@ func renderHomepage(config config.Config, posts []Post) (io.Reader, error) {
 	doc.Find(".title").AddClass("p-name u-url")
 	var buf bytes.Buffer
 
-	md := goldmark.New(goldmark.WithRendererOptions(html.WithUnsafe()))
+	md := goldmark.New(goldmark.WithRendererOptions(htmlrenderer.WithUnsafe()))
 	if err := md.Convert(*index, &buf); err != nil {
 		return nil, err
 	}
@@ -386,7 +389,7 @@ func mkdirp(dirs ...string) error {
 }
 
 func outputToFile(output io.Reader, filename ...string) error {
-	file, err := os.OpenFile(path.Join(filename...), os.O_WRONLY|os.O_CREATE, 00644)
+	file, err := os.OpenFile(path.Join(filename...), os.O_WRONLY|os.O_CREATE, 0644)
 	if err != nil {
 		return errors.WithMessage(err, "could not open output file")
 	}
diff --git a/go.mod b/go.mod
index 912c6e8..041b3cb 100644
--- a/go.mod
+++ b/go.mod
@@ -5,6 +5,7 @@ go 1.22.1
 require (
 	github.com/BurntSushi/toml v1.2.1
 	github.com/PuerkitoBio/goquery v1.9.1
+	github.com/a-h/htmlformat v0.0.0-20231108124658-5bd994fe268e
 	github.com/adrg/frontmatter v0.2.0
 	github.com/ansrivas/fiberprometheus/v2 v2.6.1
 	github.com/antchfx/xmlquery v1.4.0
@@ -13,10 +14,10 @@ require (
 	github.com/getsentry/sentry-go v0.27.0
 	github.com/gofiber/contrib/fibersentry v1.0.4
 	github.com/gofiber/fiber/v2 v2.52.4
-	github.com/jf-tech/go-corelib v0.0.1
 	github.com/pkg/errors v0.9.1
 	github.com/shengyanli1982/law v0.1.12
 	github.com/yuin/goldmark v1.7.1
+	golang.org/x/net v0.21.0
 )
 
 require (
@@ -44,7 +45,6 @@ require (
 	github.com/valyala/bytebufferpool v1.0.0 // indirect
 	github.com/valyala/fasthttp v1.52.0 // indirect
 	github.com/valyala/tcplisten v1.0.0 // indirect
-	golang.org/x/net v0.21.0 // indirect
 	golang.org/x/sys v0.19.0 // indirect
 	golang.org/x/text v0.14.0 // indirect
 	google.golang.org/protobuf v1.30.0 // indirect
diff --git a/go.sum b/go.sum
index 4af81db..406d9a5 100644
--- a/go.sum
+++ b/go.sum
@@ -3,6 +3,8 @@ github.com/BurntSushi/toml v1.2.1 h1:9F2/+DoOYIOksmaJFPw1tGFy1eDnIJXg+UHjuD8lTak
 github.com/BurntSushi/toml v1.2.1/go.mod h1:CxXYINrC8qIiEnFrOxCa7Jy5BFHlXnUU2pbicEuybxQ=
 github.com/PuerkitoBio/goquery v1.9.1 h1:mTL6XjbJTZdpfL+Gwl5U2h1l9yEkJjhmlTeV9VPW7UI=
 github.com/PuerkitoBio/goquery v1.9.1/go.mod h1:cW1n6TmIMDoORQU5IU/P1T3tGFunOeXEpGP2WHRwkbY=
+github.com/a-h/htmlformat v0.0.0-20231108124658-5bd994fe268e h1:Eog54DQpku7NpPNff9wzQYT61TGu9jjq5N8UhAkqIgw=
+github.com/a-h/htmlformat v0.0.0-20231108124658-5bd994fe268e/go.mod h1:FMIm5afKmEfarNbIXOaPHFY8X7fo+fRQB6I9MPG2nB0=
 github.com/adrg/frontmatter v0.2.0 h1:/DgnNe82o03riBd1S+ZDjd43wAmC6W35q67NHeLkPd4=
 github.com/adrg/frontmatter v0.2.0/go.mod h1:93rQCj3z3ZlwyxxpQioRKC1wDLto4aXHrbqIsnH9wmE=
 github.com/andybalholm/brotli v1.1.0 h1:eLKJA0d02Lf0mVpIDgYnqXcUn0GqVmEFny3VuID1U3M=
@@ -13,16 +15,12 @@ github.com/ansrivas/fiberprometheus/v2 v2.6.1 h1:wac3pXaE6BYYTF04AC6K0ktk6vCD+Mn
 github.com/ansrivas/fiberprometheus/v2 v2.6.1/go.mod h1:MloIKvy4yN6hVqlRpJ/jDiR244YnWJaQC0FIqS8A+MY=
 github.com/antchfx/xmlquery v1.4.0 h1:xg2HkfcRK2TeTbdb0m1jxCYnvsPaGY/oeZWTGqX/0hA=
 github.com/antchfx/xmlquery v1.4.0/go.mod h1:Ax2aeaeDjfIw3CwXKDQ0GkwZ6QlxoChlIBP+mGnDFjI=
-github.com/antchfx/xpath v1.1.10/go.mod h1:Yee4kTMuNiPYJ7nSNorELQMr1J33uOpXDMByNYhvtNk=
 github.com/antchfx/xpath v1.3.0 h1:nTMlzGAK3IJ0bPpME2urTuFL76o4A96iYvoKFHRXJgc=
 github.com/antchfx/xpath v1.3.0/go.mod h1:i54GszH55fYfBmoZXapTHN8T8tkcHfRgLyVwwqzXNcs=
 github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
 github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
-github.com/bradleyjkemp/cupaloy v2.3.0+incompatible h1:UafIjBvWQmS9i/xRg+CamMrnLTKNzo+bdmT/oH34c2Y=
-github.com/bradleyjkemp/cupaloy v2.3.0+incompatible/go.mod h1:Au1Xw1sgaJ5iSFktEhYsS0dbQiS1B0/XMXl+42y9Ilk=
 github.com/cespare/xxhash/v2 v2.2.0 h1:DC2CZ1Ep5Y4k3ZQ899DldepgrayRUGE6BBZ/cd9Cj44=
 github.com/cespare/xxhash/v2 v2.2.0/go.mod h1:VGX0DQ3Q6kWi7AoAeZDth3/j3BFtOZR5XLFGgcrjCOs=
-github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
 github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
 github.com/deckarep/golang-set/v2 v2.6.0 h1:XfcQbWM1LlMB8BsJ8N9vW5ehnnPVIw0je80NsVHagjM=
@@ -44,13 +42,10 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
 github.com/golang/protobuf v1.5.3 h1:KhyjKVUg7Usr/dYsdSqoFveMYd5ko72D+zANwlG1mmg=
 github.com/golang/protobuf v1.5.3/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
 github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
-github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
-github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
+github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
+github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
 github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
 github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
-github.com/hashicorp/golang-lru v0.5.4/go.mod h1:iADmTwqILo4mZ8BN3D2Q6+9jd8WM5uGBxy+E8yxSoD4=
-github.com/jf-tech/go-corelib v0.0.1 h1:U7/V7QnnhzXBFCGAPVPuSiQi1nCO380Gw2SxJVffU3o=
-github.com/jf-tech/go-corelib v0.0.1/go.mod h1:+sIxnA2kmfH2extpn0QQb2Ow72HPfO1m3efu2+6Kqa4=
 github.com/klauspost/compress v1.17.8 h1:YcnTYrq7MikUT7k0Yb5eceMmALQPYBW/Xltxn0NAMnU=
 github.com/klauspost/compress v1.17.8/go.mod h1:Di0epgTjJY877eYKx5yC51cX2A2Vl2ibi7bDH9ttBbw=
 github.com/kr/pretty v0.3.1 h1:flRD4NNwYAUpkphVc1HcthR4KEIFJ65n8Mw5qdRn3LE=
@@ -90,13 +85,10 @@ github.com/rogpeppe/go-internal v1.10.0 h1:TMyTOH3F/DB16zRVcYyreMH6GnZZrwQVAoYjR
 github.com/rogpeppe/go-internal v1.10.0/go.mod h1:UQnix2H7Ngw/k4C5ijL5+65zddjncjaFoBhdsK/akog=
 github.com/shengyanli1982/law v0.1.12 h1:Kesu+0KSKQ4cpVih5OmOYJxNUpSIKDTqH1jWNqxmB+Y=
 github.com/shengyanli1982/law v0.1.12/go.mod h1:20k9YnOTwilUB4X5Z4S7TIX5Ek1Ok4xfx8V8ZxIWlyM=
-github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
-github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
 github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
 github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
 github.com/tinylib/msgp v1.1.8 h1:FCXC1xanKO4I8plpHGH2P7koL/RzZs12l/+r7vakfm0=
 github.com/tinylib/msgp v1.1.8/go.mod h1:qkpG+2ldGg4xRFmx+jfTvZPxfGFhi64BcnL9vkCm/Tw=
-github.com/tkuchiki/go-timezone v0.2.0/go.mod h1:b1Ean9v2UXtxSq4TZF0i/TU9NuoWa9hOzOKoGCV2zqY=
 github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
 github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
 github.com/valyala/fasthttp v1.52.0 h1:wqBQpxH71XW0e2g+Og4dzQM8pk34aFYlA1Ga8db7gU0=
@@ -166,6 +158,5 @@ gopkg.in/check.v1 v1.0.0-20201130134442-10cb98267c6c/go.mod h1:JHkPIbrfpd72SG/EV
 gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
 gopkg.in/yaml.v2 v2.4.0 h1:D8xgwECY7CYvx+Y2n4sBz93Jn9JRvxdiyyo8CTfuKaY=
 gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
-gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
 gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
 gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
diff --git a/server.go b/server.go
index 2a1eed0..c588efb 100644
--- a/server.go
+++ b/server.go
@@ -24,7 +24,6 @@ import (
 	"github.com/shengyanli1982/law"
 )
 
-// TODO prettify HTML
 // TODO purge CSS
 // TODO HTTP2 https://github.com/dgrr/http2