From 0130404a1dc663d4aa68d780c9bcb23a4243e68d Mon Sep 17 00:00:00 2001 From: Mitja Felicijan Date: Fri, 21 Jun 2024 17:28:03 +0200 Subject: Added additional filters --- main.go | 65 +++++++++-------------------------------------------------------- 1 file changed, 9 insertions(+), 56 deletions(-) (limited to 'main.go') diff --git a/main.go b/main.go index 2824041..9871d1c 100644 --- a/main.go +++ b/main.go @@ -5,12 +5,10 @@ import ( "fmt" "html/template" "log" - "math/rand" "net/http" "os" "path" "path/filepath" - "reflect" "sort" "strings" "time" @@ -89,50 +87,6 @@ var EmbedTemplatePost string //go:embed "files/index.xml" var EmbedTemplateFeed string -// firstN returns the first n items of a slice. -func firstN(n int, items interface{}) interface{} { - v := reflect.ValueOf(items) - if v.Kind() != reflect.Slice { - panic("firstN: not a slice") - } - if v.Len() < n { - return items - } - return v.Slice(0, n).Interface() -} - -// lastN returns the last n items of any slice. -func lastN(n int, items interface{}) interface{} { - v := reflect.ValueOf(items) - if v.Kind() != reflect.Slice { - panic("lastN: not a slice") - } - l := v.Len() - if l < n { - return items - } - return v.Slice(l-n, l).Interface() -} - -// randomN returns n random items of any slice. -func randomN(n int, items interface{}) interface{} { - v := reflect.ValueOf(items) - if v.Kind() != reflect.Slice { - panic("randomN: not a slice") - } - l := v.Len() - if l < n { - return items - } - rand.Seed(time.Now().UnixNano()) - indices := rand.Perm(l)[:n] - result := reflect.MakeSlice(v.Type(), n, n) - for i, idx := range indices { - result.Index(i).Set(v.Index(idx)) - } - return result.Interface() -} - // Function to clean HTML tags using bluemonday. func cleanHTMLTags(htmlString string) string { p := bluemonday.StrictPolicy() @@ -318,6 +272,13 @@ func buildProject(projectRoot string) { return } + filters := template.FuncMap{ + "first": firstN, + "last": lastN, + "random": randomN, + "filterbytype": filterByType, + } + // Generate HTML files for all pages. for _, page := range pages { outFilepath := path.Join(projectRoot, "public", page.Meta["url"].(string)) @@ -330,11 +291,7 @@ func buildProject(projectRoot string) { templates = append([]string{templatePathname}, templates...) templates = append([]string{baseTemplatePathname}, templates...) - t, err := template.New("base.html").Funcs(template.FuncMap{ - "first": firstN, - "last": lastN, - "random": randomN, - }).ParseFiles(templates...) + t, err := template.New("base.html").Funcs(filters).ParseFiles(templates...) if err != nil { panic(err) } @@ -385,11 +342,7 @@ func buildProject(projectRoot string) { templates = append([]string{templatePathname}, templates...) templates = append([]string{baseTemplatePathname}, templates...) - t, err := template.New("base.html").Funcs(template.FuncMap{ - "first": firstN, - "last": lastN, - "random": randomN, - }).ParseFiles(templates...) + t, err := template.New("base.html").Funcs(filters).ParseFiles(templates...) if err != nil { panic(err) } -- cgit v1.2.3