Styled the categories on the site

This commit is contained in:
Leon Mika 2026-03-22 10:28:33 +11:00
parent f45bdcd83c
commit d9aec4af2c
16 changed files with 176 additions and 59 deletions

View file

@ -6,7 +6,9 @@ import (
"fmt"
"html/template"
"io"
"io/fs"
"iter"
"log"
"os"
"path/filepath"
"strings"
@ -31,11 +33,15 @@ type Builder struct {
func New(site pubmodel.Site, opts Options) (*Builder, error) {
tmpls, err := template.New("").
Funcs(templateFns(site, opts)).
ParseFS(opts.TemplatesFS, tmplNamePostSingle, tmplNamePostList, tmplNameLayoutMain, tmplNameCategoryList, tmplNameCategorySingle)
ParseFS(opts.TemplatesFS, "*.html")
if err != nil {
return nil, err
}
for _, t := range tmpls.Templates() {
log.Printf("Loaded template %s", t.Name())
}
return &Builder{
site: site,
opts: opts,
@ -109,6 +115,9 @@ func (b *Builder) BuildSite(outDir string) error {
return b.writeUploads(buildCtx, b.site.Uploads)
})
// Build static assets
eg.Go(func() error { return b.writeStaticAssets(buildCtx) })
return eg.Wait()
}
@ -432,7 +441,7 @@ func (b *Builder) renderTemplate(w io.Writer, name string, data interface{}) err
func (b *Builder) writeUploads(ctx buildContext, uploads []models.Upload) error {
for _, u := range uploads {
fullPath := filepath.Join(ctx.outDir, "uploads", u.Slug)
fullPath := filepath.Join(ctx.outDir, b.opts.BaseUploads, u.Slug)
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
return err
}
@ -460,3 +469,37 @@ func (b *Builder) writeUploads(ctx buildContext, uploads []models.Upload) error
}
return nil
}
func (b *Builder) writeStaticAssets(ctx buildContext) error {
return fs.WalkDir(b.opts.StaticFS, ".", func(path string, d os.DirEntry, err error) error {
if err != nil {
return err
} else if d.IsDir() {
return nil
}
fullPath := filepath.Join(ctx.outDir, b.opts.BaseStatic, path)
if err := os.MkdirAll(filepath.Dir(fullPath), 0755); err != nil {
return err
}
return func() error {
r, err := b.opts.StaticFS.Open(path)
if err != nil {
return err
}
defer r.Close()
w, err := os.Create(fullPath)
if err != nil {
return err
}
defer w.Close()
if _, err := io.Copy(w, r); err != nil {
return err
}
return nil
}()
})
}

View file

@ -29,12 +29,17 @@ const (
)
type Options struct {
// BasePosts is the base path for posts.
BasePosts string
BasePosts string // BasePosts is the base path for posts.
BaseUploads string // BaseUploads is the base path for uploads.
BaseStatic string // BaseStatic is the base path for static assets.
// TemplatesFS provides the raw templates for rendering the site.
TemplatesFS fs.FS
// StaticFS provides the raw assets for the site. This will be written as is
// from the BaseStatic dir.
StaticFS fs.FS
// FeedItems holds the number of posts to show in the feed.
FeedItems int