diff options
author | Alan Pearce | 2025-01-26 14:34:58 +0100 |
---|---|---|
committer | Alan Pearce | 2025-01-26 14:34:58 +0100 |
commit | 88932b8dc1d2a73195dcb1a9c4b82e15acd908c4 (patch) | |
tree | d549721a78d65fdcb104c709fb50dad7651870f7 | |
parent | 33b1a776e087ddd012e35ef3a7efa3ec9fd575b8 (diff) | |
download | website-88932b8dc1d2a73195dcb1a9c4b82e15acd908c4.tar.lz website-88932b8dc1d2a73195dcb1a9c4b82e15acd908c4.tar.zst website-88932b8dc1d2a73195dcb1a9c4b82e15acd908c4.zip |
move / -> /index.html convention to storage layer
-rw-r--r-- | internal/builder/builder.go | 10 | ||||
-rw-r--r-- | internal/storage/files/writer.go | 16 |
2 files changed, 17 insertions, 9 deletions
diff --git a/internal/builder/builder.go b/internal/builder/builder.go index 68eec14..df66f87 100644 --- a/internal/builder/builder.go +++ b/internal/builder/builder.go @@ -77,7 +77,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, log.Debug("rendering tags list") if err := storage.RenderToFile( templates.TagsPage(config, "tags", mapset.Sorted(tags), "/tags"), - path.Join("tags", "index.html"), + "tags/", ); err != nil { return nil, err } @@ -94,7 +94,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, url := "/tags/" + tag if err := storage.RenderToFile( templates.TagPage(config, tag, matchingPosts, url), - path.Join("tags", tag, "index.html"), + path.Join("tags", tag)+"/", ); err != nil { return nil, err } @@ -118,7 +118,7 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, log.Debug("rendering list page") if err := storage.RenderToFile( templates.ListPage(config, posts, "/post"), - path.Join("post", "index.html"), + "post/", ); err != nil { return nil, err } @@ -160,14 +160,14 @@ func build(ioConfig *IOConfig, config *config.Config, log *log.Logger) (*Result, if err != nil { return nil, err } - if err := storage.RenderToFile(templates.Homepage(config, posts, content), "index.html"); err != nil { + if err := storage.RenderToFile(templates.Homepage(config, posts, content), "/"); err != nil { return nil, err } // it would be nice to set LastMod here, but using the latest post // date would be wrong as the homepage has its own content file // without a date, which could be newer sitemap.AddPath("/", time.Time{}) - f, err := storage.Open("index.html") + f, err := storage.Open("/") if err != nil { return nil, err } diff --git a/internal/storage/files/writer.go b/internal/storage/files/writer.go index 9831435..f0fe6a9 100644 --- a/internal/storage/files/writer.go +++ b/internal/storage/files/writer.go @@ -7,6 +7,7 @@ import ( "io/fs" "os" "path/filepath" + "strings" "go.alanpearce.eu/x/log" @@ -70,11 +71,11 @@ func (f *Files) CopyRecursive(src string) error { } func (f *Files) Open(filename string) (io.ReadCloser, error) { - return os.Open(filepath.Join(f.outputDirectory, filename)) + return os.Open(f.join(filename)) } func (f *Files) OutputToFile(output io.Reader, filename string) error { - fn := filepath.Join(f.outputDirectory, filename) + fn := f.join(filename) if err := f.mkdirp(filepath.Dir(filename)); err != nil { return err } @@ -93,7 +94,7 @@ func (f *Files) OutputToFile(output io.Reader, filename string) error { } func (f *Files) RenderToFile(component templ.Component, filename string) error { - fn := filepath.Join(f.outputDirectory, filename) + fn := f.join(filename) if err := f.mkdirp(filepath.Dir(filename)); err != nil { return err } @@ -112,7 +113,7 @@ func (f *Files) RenderToFile(component templ.Component, filename string) error { } func (f *Files) WriterToFile(writer io.WriterTo, filename string) error { - fn := filepath.Join(f.outputDirectory, filename) + fn := f.join(filename) if err := f.mkdirp(filepath.Dir(filename)); err != nil { return err } @@ -209,3 +210,10 @@ func (f *Files) mkdirp(dir string) error { return errors.WithMessage(err, "could not create directory") } + +func (f *Files) join(filename string) string { + if strings.HasSuffix(filename, "/") { + filename = filename + "index.html" + } + return filepath.Join(f.outputDirectory, filename) +} |