commit fdda0004f3e92f122abd0534ec57134c0f1d0d8e
parent 9a49380fe4b9788550ae1f0f86346b5b240ea97e
Author: gearsix <gearsix@tuta.io>
Date: Wed, 30 Mar 2022 17:50:50 +0100
added Assets type to Page (for .Assets).
This allows users to navigate assets better. It has '.All' for all
asset paths (a []string as before). '.All' has been split into
'.Image', '.Video', '.Audio' and '.Misc', each being a []*string
(each entry points to an '.All' entry) of the respective types.
Implemented this because I found myself wanting to display a gallery
of images and link a .zip of all the displayed images. I could have
added the .zip to the AssetDir but meant moving it out of that
gallery project folder.
Diffstat:
2 files changed, 22 insertions(+), 3 deletions(-)
diff --git a/content.go b/content.go
@@ -168,7 +168,18 @@ func loadContentFile(p Page, defs map[string]Meta, fpath string, ppath string) (
err = p.NewContentFromFile(fpath)
} else {
a := filepath.Join(ppath, filepath.Base(fpath))
- p.Assets = append(p.Assets, a)
+ p.Assets.All = append(p.Assets.All, a)
+ ref := &p.Asset.all[len(p.Assets.All)-1]
+ mimetype := mime.TypeByExtension(filepath.Ext(fpath))
+ if strings.Contains(mimetype, "image/") {
+ p.Assets.Image = append(p.Assets.Images, ref)
+ } else if strings.Contains(mimetype, "video") {
+ p.Assets.Video = append(p.Assets.Video, ref)
+ } else if strings.Contains(mimetype, "audio") {
+ p.Assets.Audio = append(p.Assets.Audio, ref)
+ } else {
+ p.Assets.Misc = append(p.Assets.Misc, ref)
+ }
}
return p, d, err
}
diff --git a/page.go b/page.go
@@ -41,10 +41,18 @@ type Page struct {
Nav Nav
Meta Meta
Contents []Content
- Assets []string
+ Assets Assets
Updated string
}
+type Assets struct {
+ All []string
+ Audio []*string
+ Image []*string
+ Video []*string
+ Misc []*string
+}
+
// Nav is a struct that provides a set of pointers for navigating a
// across a set of pages. All values are initialised to nil and will only
// be populated manually or by calling `BuildSitemap`.
@@ -80,7 +88,7 @@ func NewPage(path string, updated time.Time) Page {
Nav: Nav{},
Meta: Meta{"Title": titleFromPath(path)},
Contents: make([]Content, 0),
- Assets: make([]string, 0),
+ Assets: Assets{},
Updated: updated.Format(timefmt),
}
}