jorge/markup/filters.go

222 lines
6.2 KiB
Go
Raw Normal View History

package markup
2024-02-15 17:53:45 +01:00
import (
2024-02-15 20:12:24 +01:00
"bytes"
2024-02-15 17:53:45 +01:00
"fmt"
"net/url"
"path/filepath"
2024-02-15 17:53:45 +01:00
"reflect"
2024-02-16 19:13:52 +01:00
"regexp"
"strings"
2024-02-15 17:53:45 +01:00
2024-02-15 19:25:57 +01:00
"encoding/xml"
2024-02-15 20:12:24 +01:00
"time"
2024-02-16 01:52:23 +01:00
"github.com/elliotchance/orderedmap/v2"
2024-02-15 19:25:57 +01:00
"github.com/osteele/liquid"
2024-02-15 17:53:45 +01:00
"github.com/osteele/liquid/evaluator"
"github.com/osteele/liquid/expressions"
2024-02-15 20:12:24 +01:00
"github.com/yuin/goldmark"
"github.com/osteele/liquid/render"
2024-02-15 17:53:45 +01:00
)
2024-02-15 19:25:57 +01:00
// a lot of the filters and tags available at jekyll aren't default liquid manually adding them here
// copied from https://github.com/osteele/gojekyll/blob/f1794a874890bfb601cae767a0cce15d672e9058/filters/filters.go
// MIT License: https://github.com/osteele/gojekyll/blob/f1794a874890bfb601cae767a0cce15d672e9058/LICENSE
func loadJekyllFilters(e *liquid.Engine, siteUrl string, includesDir string) {
2024-02-15 19:25:57 +01:00
e.RegisterFilter("filter", filter)
e.RegisterFilter("group_by", groupByFilter)
e.RegisterFilter("group_by_exp", groupByExpFilter)
e.RegisterFilter("sort", sortFilter)
e.RegisterFilter("keys", keysFilter)
2024-02-15 19:25:57 +01:00
e.RegisterFilter("where", whereFilter)
e.RegisterFilter("where_exp", whereExpFilter)
2024-02-16 19:13:52 +01:00
e.RegisterFilter("normalize_whitespace", func(s string) string {
wsPattern := regexp.MustCompile(`(?s:[\s\n]+)`)
return wsPattern.ReplaceAllString(s, " ")
})
2024-02-25 15:35:08 +01:00
e.RegisterFilter("markdownify", func(s string) (string, error) {
// using goldmark here instead of balckfriday, to avoid an extra dependency
2024-02-15 20:12:24 +01:00
var buf bytes.Buffer
err := goldmark.Convert([]byte(s), &buf)
2024-02-25 15:35:08 +01:00
return buf.String(), err
2024-02-15 20:12:24 +01:00
})
2024-02-25 15:35:08 +01:00
e.RegisterFilter("xml_escape", func(s string) (string, error) {
// using goldmark here instead of balckfriday, to avoid an extra dependency
var buf bytes.Buffer
err := xml.EscapeText(&buf, []byte(s))
return buf.String(), err
})
e.RegisterFilter("absolute_url", func(path string) (string, error) {
return url.JoinPath(siteUrl, path)
2024-02-15 19:25:57 +01:00
})
e.RegisterFilter("date_to_rfc822", func(date time.Time) string {
return date.Format(time.RFC822)
// Out: Mon, 07 Nov 2008 13:07:54 -0800
})
e.RegisterFilter("date_to_string", func(date time.Time) string {
return date.Format("02 Jan 2006")
// Out: 07 Nov 2008
})
e.RegisterFilter("date_to_long_string", func(date time.Time) string {
return date.Format("02 January 2006")
// Out: 07 November 2008
})
e.RegisterFilter("date_to_xmlschema", func(date time.Time) string {
return date.Format("2006-01-02T15:04:05-07:00")
// Out: 2008-11-07T13:07:54-08:00
})
e.RegisterTag("include", func(rc render.Context) (string, error) {
return includeFromDir(includesDir, rc)
})
2024-02-15 19:25:57 +01:00
}
2024-02-15 17:53:45 +01:00
func filter(values []map[string]interface{}, key string) []interface{} {
var result []interface{}
for _, value := range values {
if _, ok := value[key]; ok {
result = append(result, value)
}
}
return result
}
func groupByExpFilter(array []map[string]interface{}, name string, expr expressions.Closure) ([]map[string]interface{}, error) {
rt := reflect.ValueOf(array)
if !(rt.Kind() != reflect.Array || rt.Kind() == reflect.Slice) {
return nil, nil
}
2024-02-16 01:52:23 +01:00
groups := orderedmap.NewOrderedMap[interface{}, []interface{}]()
2024-02-15 17:53:45 +01:00
for i := 0; i < rt.Len(); i++ {
item := rt.Index(i).Interface()
key, err := expr.Bind(name, item).Evaluate()
if err != nil {
return nil, err
}
2024-02-16 01:52:23 +01:00
if group, found := groups.Get(key); found {
groups.Set(key, append(group, item))
2024-02-15 17:53:45 +01:00
} else {
2024-02-16 01:52:23 +01:00
groups.Set(key, []interface{}{item})
2024-02-15 17:53:45 +01:00
}
}
var result []map[string]interface{}
2024-02-16 01:52:23 +01:00
for _, k := range groups.Keys() {
v, _ := groups.Get(k)
2024-02-15 17:53:45 +01:00
result = append(result, map[string]interface{}{"name": k, "items": v})
}
return result, nil
}
2024-02-16 01:52:23 +01:00
// TODO use ordered map
2024-02-15 17:53:45 +01:00
func groupByFilter(array []map[string]interface{}, property string) []map[string]interface{} {
rt := reflect.ValueOf(array)
if !(rt.Kind() != reflect.Array || rt.Kind() == reflect.Slice) {
return nil
}
2024-02-16 01:52:23 +01:00
groups := orderedmap.NewOrderedMap[interface{}, []interface{}]()
2024-02-15 17:53:45 +01:00
for i := 0; i < rt.Len(); i++ {
irt := rt.Index(i)
if irt.Kind() == reflect.Map && irt.Type().Key().Kind() == reflect.String {
krt := irt.MapIndex(reflect.ValueOf(property))
if krt.IsValid() && krt.CanInterface() {
key := krt.Interface()
2024-02-16 01:52:23 +01:00
if group, found := groups.Get(key); found {
groups.Set(key, append(group, irt.Interface()))
2024-02-15 17:53:45 +01:00
} else {
2024-02-16 01:52:23 +01:00
groups.Set(key, []interface{}{irt.Interface()})
2024-02-15 17:53:45 +01:00
}
}
}
}
var result []map[string]interface{}
2024-02-16 01:52:23 +01:00
for _, k := range groups.Keys() {
v, _ := groups.Get(k)
2024-02-15 17:53:45 +01:00
result = append(result, map[string]interface{}{"name": k, "items": v})
}
return result
}
func keysFilter(m map[string]interface{}) []string {
keys := make([]string, len(m))
i := 0
for k := range m {
keys[i] = k
i++
}
return keys
}
2024-02-15 17:53:45 +01:00
func sortFilter(array []interface{}, key interface{}, nilFirst func(bool) bool) []interface{} {
nf := nilFirst(true)
result := make([]interface{}, len(array))
copy(result, array)
if key == nil {
evaluator.Sort(result)
} else {
// TODO error if key is not a string
evaluator.SortByProperty(result, key.(string), nf)
}
return result
}
func whereExpFilter(array []interface{}, name string, expr expressions.Closure) ([]interface{}, error) {
rt := reflect.ValueOf(array)
if rt.Kind() != reflect.Array && rt.Kind() != reflect.Slice {
return nil, nil
}
var result []interface{}
for i := 0; i < rt.Len(); i++ {
item := rt.Index(i).Interface()
value, err := expr.Bind(name, item).Evaluate()
if err != nil {
return nil, err
}
if value != nil && value != false {
result = append(result, item)
}
}
return result, nil
}
func whereFilter(array []map[string]interface{}, key string, value interface{}) []interface{} {
rt := reflect.ValueOf(array)
if rt.Kind() != reflect.Array && rt.Kind() != reflect.Slice {
return nil
}
var result []interface{}
for i := 0; i < rt.Len(); i++ {
item := rt.Index(i)
if item.Kind() == reflect.Map && item.Type().Key().Kind() == reflect.String {
attr := item.MapIndex(reflect.ValueOf(key))
if attr.IsValid() && fmt.Sprint(attr) == value {
result = append(result, item.Interface())
}
}
}
return result
}
func includeFromDir(dir string, rc render.Context) (string, error) {
argsline, err := rc.ExpandTagArg()
if err != nil {
return "", err
}
args := strings.Split(argsline, " ")
if err != nil {
return "", err
}
if len(args) != 1 {
return "", fmt.Errorf("parse error")
}
filename := filepath.Join(dir, args[0])
return rc.RenderFile(filename, map[string]interface{}{})
}