Added first, last, random filters

Author Mitja Felicijan <mitja.felicijan@gmail.com> 2024-06-18 18:43:56 +0200
Committer Mitja Felicijan <mitja.felicijan@gmail.com> 2024-06-18 18:43:56 +0200
Commit b738f38af8551021f77ad759425063d06e3c80de (patch)
-rw-r--r-- README.md 29
-rw-r--r-- main.go 58
2 files changed, 85 insertions, 2 deletions
diff --git a/README.md b/README.md
...
158
})
158
})
159
```
159
```
160
  
160
  
  
161
## Special filters
  
162
  
  
163
- first (gets first N posts)
  
164
- last (gets last N posts)
  
165
- random (gets random N posts)
  
166
  
  
167
```html
  
168
<!-- First 10 pages -->
  
169
{{ range first 10 .Pages }}
  
170
  {{ if and (eq .Type "post") (not .Draft) }}
  
171
    <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
  
172
  {{ end }}
  
173
{{ end }}
  
174
  
  
175
<!-- Last 10 pages -->
  
176
{{ range last 10 .Pages }}
  
177
  {{ if and (eq .Type "post") (not .Draft) }}
  
178
    <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
  
179
  {{ end }}
  
180
{{ end }}
  
181
  
  
182
<!-- Random 10 pages -->
  
183
{{ range random 10 .Pages }}
  
184
  {{ if and (eq .Type "post") (not .Draft) }}
  
185
    <li><a href="{{ .RelPermalink }}">{{ .Title }}</a></li>
  
186
  {{ end }}
  
187
{{ end }}
  
188
```
  
189
  
161
## License
190
## License
162
  
191
  
163
[jbmafp](https://github.com/mitjafelicijan/jbmafp) was written by [Mitja
192
[jbmafp](https://github.com/mitjafelicijan/jbmafp) was written by [Mitja
...
diff --git a/main.go b/main.go
...
5
	"fmt"
5
	"fmt"
6
	"html/template"
6
	"html/template"
7
	"log"
7
	"log"
  
8
	"math/rand"
8
	"net/http"
9
	"net/http"
9
	"os"
10
	"os"
10
	"path"
11
	"path"
11
	"path/filepath"
12
	"path/filepath"
  
13
	"reflect"
12
	"sort"
14
	"sort"
13
	"strings"
15
	"strings"
14
	"time"
16
	"time"
...
86
  
88
  
87
//go:embed "files/index.xml"
89
//go:embed "files/index.xml"
88
var EmbedTemplateFeed string
90
var EmbedTemplateFeed string
  
91
  
  
92
// firstN returns the first n items of a slice.
  
93
func firstN(n int, items interface{}) interface{} {
  
94
	v := reflect.ValueOf(items)
  
95
	if v.Kind() != reflect.Slice {
  
96
		panic("firstN: not a slice")
  
97
	}
  
98
	if v.Len() < n {
  
99
		return items
  
100
	}
  
101
	return v.Slice(0, n).Interface()
  
102
}
  
103
  
  
104
// lastN returns the last n items of any slice.
  
105
func lastN(n int, items interface{}) interface{} {
  
106
	v := reflect.ValueOf(items)
  
107
	if v.Kind() != reflect.Slice {
  
108
		panic("lastN: not a slice")
  
109
	}
  
110
	l := v.Len()
  
111
	if l < n {
  
112
		return items
  
113
	}
  
114
	return v.Slice(l-n, l).Interface()
  
115
}
  
116
  
  
117
// randomN returns n random items of any slice.
  
118
func randomN(n int, items interface{}) interface{} {
  
119
	v := reflect.ValueOf(items)
  
120
	if v.Kind() != reflect.Slice {
  
121
		panic("randomN: not a slice")
  
122
	}
  
123
	l := v.Len()
  
124
	if l < n {
  
125
		return items
  
126
	}
  
127
	rand.Seed(time.Now().UnixNano())
  
128
	indices := rand.Perm(l)[:n]
  
129
	result := reflect.MakeSlice(v.Type(), n, n)
  
130
	for i, idx := range indices {
  
131
		result.Index(i).Set(v.Index(idx))
  
132
	}
  
133
	return result.Interface()
  
134
}
89
  
135
  
90
// Function to clean HTML tags using bluemonday.
136
// Function to clean HTML tags using bluemonday.
91
func cleanHTMLTags(htmlString string) string {
137
func cleanHTMLTags(htmlString string) string {
...
284
			templates = append([]string{templatePathname}, templates...)
330
			templates = append([]string{templatePathname}, templates...)
285
			templates = append([]string{baseTemplatePathname}, templates...)
331
			templates = append([]string{baseTemplatePathname}, templates...)
286
  
332
  
287
			t, err := template.ParseFiles(templates...)
333
			t, err := template.New("base.html").Funcs(template.FuncMap{
  
334
				"first":  firstN,
  
335
				"last":   lastN,
  
336
				"random": randomN,
  
337
			}).ParseFiles(templates...)
288
			if err != nil {
338
			if err != nil {
289
				panic(err)
339
				panic(err)
290
			}
340
			}
...
335
		templates = append([]string{templatePathname}, templates...)
385
		templates = append([]string{templatePathname}, templates...)
336
		templates = append([]string{baseTemplatePathname}, templates...)
386
		templates = append([]string{baseTemplatePathname}, templates...)
337
  
387
  
338
		t, err := template.ParseFiles(templates...)
388
		t, err := template.New("base.html").Funcs(template.FuncMap{
  
389
			"first":  firstN,
  
390
			"last":   lastN,
  
391
			"random": randomN,
  
392
		}).ParseFiles(templates...)
339
		if err != nil {
393
		if err != nil {
340
			panic(err)
394
			panic(err)
341
		}
395
		}
...