Skip to content

Commit

Permalink
adds an option to set image sort order. #1
Browse files Browse the repository at this point in the history
 - defaults to current behavior, by name
 - option is by directory
 - add `sort-order` key to `bilder.json`
 - `ModTime` for newest first, anything else for sorting by name
  • Loading branch information
fgeller committed Mar 10, 2020
1 parent 86b9aee commit 9bfb8c8
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
1 change: 1 addition & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ It currently supports the following options:
+ `user` *default:* `""`, `pass` *default:* `""`: If both are non-empty strings, bilder will use them as credentials to enable basic authentication for this album.
+ `title` *default:* `""`: Title that should be set for the album, defaults to the directory name.
+ `captions` *default:* `null`: Map object from file name to caption string (consider the demo example below).
+ `sort-order` *default:* `""`: Identifies sort order for images, supported: `ModTime` (newest first), `Name` (by file name, default).

This is the `bilder.json` file in the `kitties` directory of the [demo](https://geller.io/bilder/b/kitties):
```
Expand Down
18 changes: 16 additions & 2 deletions watcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ type imgDetails struct {
Caption string
Path string
ThumbPath string
ModTime time.Time
}

type dirDetails struct {
Expand Down Expand Up @@ -60,6 +61,7 @@ type dirConfig struct {
Title string
Captions map[string]string
User, Pass string
SortOrder string `json:"sort-order"`
}

var (
Expand Down Expand Up @@ -106,6 +108,12 @@ func (a byImgName) Len() int { return len(a) }
func (a byImgName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byImgName) Less(i, j int) bool { return a[i].Path < a[j].Path }

type byImgModTime []*imgDetails

func (a byImgModTime) Len() int { return len(a) }
func (a byImgModTime) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
func (a byImgModTime) Less(i, j int) bool { return a[i].ModTime.Unix() > a[j].ModTime.Unix() }

func (w *watcher) writeIndexes() {
tmpl := template.Must(template.New("dirIndex").Parse(dirIndexTempl))
for d, is := range w.images {
Expand All @@ -114,11 +122,17 @@ func (w *watcher) writeIndexes() {
for _, id := range is {
ids = append(ids, id)
}
sort.Sort(byImgName(ids))
title := d
if cfg, exists := w.configs[d]; exists && cfg.Title != "" {
title = cfg.Title
}

if cfg, exists := w.configs[d]; exists && cfg.SortOrder == "ModTime" {
sort.Sort(byImgModTime(ids))
} else {
sort.Sort(byImgName(ids))
}

dd := dirDetails{
URLPathPrefix: w.urlPathPrefix,
Title: title,
Expand Down Expand Up @@ -307,12 +321,12 @@ func (w *watcher) reloadContents() {
if cfgExists {
cptn = cfg.Captions[f.Name()]
}

details := &imgDetails{
Width: img.Width,
Height: img.Height,
Caption: cptn,
Path: strings.Join([]string{"b", d.Name(), f.Name()}, "/"),
ModTime: f.ModTime(),
}

if w.images == nil {
Expand Down

0 comments on commit 9bfb8c8

Please sign in to comment.