From becede4bfead952239b9cf7c8eb9fb96d01534ec Mon Sep 17 00:00:00 2001 From: Alan Pearce Date: Tue, 25 Jun 2024 09:14:56 +0200 Subject: refactor/format --- internal/builder/builder.go | 16 ++++++++++++++-- internal/builder/files.go | 40 ++++++++++++++++++++++++---------------- internal/builder/hasher.go | 1 + internal/builder/template.go | 27 ++------------------------- 4 files changed, 41 insertions(+), 43 deletions(-) (limited to 'internal/builder') diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 933679d..d2d86a6 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -137,7 +137,12 @@ func build(outDir string, config config.Config) (*Result, error) { return nil, errors.WithMessage(err, "could not create directory for tags") } log.Debug("rendering tags list") - if err := renderToFile(templates.TagsPage(config, "tags", mapset.Sorted(tags), "/tags"), publicDir, "tags", "index.html"); err != nil { + if err := renderToFile( + templates.TagsPage(config, "tags", mapset.Sorted(tags), "/tags"), + publicDir, + "tags", + "index.html", + ); err != nil { return nil, err } sitemap.AddPath("/tags/", lastMod) @@ -154,7 +159,13 @@ func build(outDir string, config config.Config) (*Result, error) { } log.Debug("rendering tags page", "tag", tag) url := "/tags/" + tag - if err := renderToFile(templates.TagPage(config, tag, matchingPosts, url), publicDir, "tags", tag, "index.html"); err != nil { + if err := renderToFile( + templates.TagPage(config, tag, matchingPosts, url), + publicDir, + "tags", + tag, + "index.html", + ); err != nil { return nil, err } sitemap.AddPath(url, matchingPosts[0].Date) @@ -271,5 +282,6 @@ func BuildSite(ioConfig IOConfig) (*Result, error) { } loadCSS() + return build(ioConfig.Destination, *config) } diff --git a/internal/builder/files.go b/internal/builder/files.go index 20fedf9..a9046d7 100644 --- a/internal/builder/files.go +++ b/internal/builder/files.go @@ -32,6 +32,7 @@ func (mw *MultiWriteCloser) Close() error { lastErr = err } } + return lastErr } @@ -45,6 +46,7 @@ func openFileGz(filename string) (*gzip.Writer, error) { if err != nil { return nil, err } + return gzip.NewWriterLevel(f, gzipLevel) } @@ -54,6 +56,7 @@ func openFileBrotli(filename string) (*brotli.Writer, error) { if err != nil { return nil, err } + return brotli.NewWriterLevel(f, brotliLevel), nil } @@ -70,6 +73,7 @@ func multiOpenFile(filename string) (*MultiWriteCloser, error) { if err != nil { return nil, err } + return &MultiWriteCloser{ writers: []io.WriteCloser{r, gz, br}, multiWriter: io.MultiWriter(r, gz, br), @@ -79,34 +83,38 @@ func multiOpenFile(filename string) (*MultiWriteCloser, error) { func openFileAndVariants(filename string) (io.WriteCloser, error) { if compressFiles { return multiOpenFile(filename) - } else { - return openFileWrite(filename) } + + return openFileWrite(filename) } func copyRecursive(src, dst string) error { return filepath.WalkDir(src, func(path string, d fs.DirEntry, err error) error { + if err != nil { + return err + } rel, err := filepath.Rel(src, path) if err != nil { return err } if d.IsDir() { return mkdirp(dst, rel) - } else { - sf, err := os.Open(path) - if err != nil { - return err - } - defer sf.Close() - df, err := openFileAndVariants(filepath.Join(dst, rel)) - if err != nil { - return err - } - defer df.Close() - if _, err := io.Copy(df, sf); err != nil { - return err - } } + + sf, err := os.Open(path) + if err != nil { + return err + } + defer sf.Close() + df, err := openFileAndVariants(filepath.Join(dst, rel)) + if err != nil { + return err + } + defer df.Close() + if _, err := io.Copy(df, sf); err != nil { + return err + } + return nil }) } diff --git a/internal/builder/hasher.go b/internal/builder/hasher.go index dbc29f8..f0f9167 100644 --- a/internal/builder/hasher.go +++ b/internal/builder/hasher.go @@ -8,5 +8,6 @@ import ( func hash(s string) string { shasum := sha256.New() shasum.Write([]byte(s)) + return "sha256-" + base64.StdEncoding.EncodeToString(shasum.Sum(nil)) } diff --git a/internal/builder/template.go b/internal/builder/template.go index 5ec963a..9d81582 100644 --- a/internal/builder/template.go +++ b/internal/builder/template.go @@ -10,14 +10,11 @@ import ( "website/internal/atom" "website/internal/config" "website/internal/content" - "website/internal/log" "github.com/PuerkitoBio/goquery" - "github.com/a-h/htmlformat" "github.com/antchfx/xmlquery" "github.com/antchfx/xpath" "github.com/pkg/errors" - "golang.org/x/net/html" ) var ( @@ -84,6 +81,7 @@ func renderRobotsTXT(config config.Config) (io.Reader, error) { } w.Close() }() + return r, nil } @@ -209,27 +207,6 @@ func getHTMLStyleHash(filenames ...string) (string, error) { return "", err } html := doc.Find("head > style").Text() - return hash(html), nil -} - -func renderHTML(doc *goquery.Document) io.Reader { - r, w := io.Pipe() - - go func() { - _, err := w.Write([]byte("\n")) - if err != nil { - log.Error("error writing doctype", "error", err) - w.CloseWithError(err) - } - err = htmlformat.Nodes(w, []*html.Node{doc.Children().Get(0)}) - if err != nil { - log.Error("error rendering html", "error", err) - w.CloseWithError(err) - return - } - defer w.Close() - }() - - return r + return hash(html), nil } -- cgit 1.4.1