2014-07-20 00:42:24 +02:00
|
|
|
package pongo2addons
|
|
|
|
|
|
|
|
import (
|
2014-07-31 18:16:55 +02:00
|
|
|
"bytes"
|
2020-08-05 05:24:39 +02:00
|
|
|
"errors"
|
2014-07-31 18:16:55 +02:00
|
|
|
"fmt"
|
2017-09-02 01:39:48 +02:00
|
|
|
"math/rand"
|
2014-07-30 22:58:02 +02:00
|
|
|
"regexp"
|
|
|
|
"strings"
|
2014-07-31 18:16:55 +02:00
|
|
|
"time"
|
|
|
|
"unicode/utf8"
|
2014-07-28 14:40:58 +02:00
|
|
|
|
|
|
|
"github.com/flosch/go-humanize"
|
2022-05-06 14:34:48 +02:00
|
|
|
"github.com/flosch/pongo2"
|
2022-05-06 14:52:58 +02:00
|
|
|
"github.com/gosimple/slug"
|
2020-08-05 05:24:39 +02:00
|
|
|
"github.com/russross/blackfriday/v2"
|
2014-07-20 00:42:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2017-09-02 01:58:18 +02:00
|
|
|
rand.Seed(time.Now().UTC().UnixNano())
|
|
|
|
|
2014-07-30 22:58:02 +02:00
|
|
|
// Regulars
|
2014-07-26 17:28:14 +02:00
|
|
|
pongo2.RegisterFilter("slugify", filterSlugify)
|
2014-07-28 14:40:58 +02:00
|
|
|
pongo2.RegisterFilter("filesizeformat", filterFilesizeformat)
|
2014-07-30 22:58:02 +02:00
|
|
|
pongo2.RegisterFilter("truncatesentences", filterTruncatesentences)
|
2017-02-13 12:42:10 +01:00
|
|
|
pongo2.RegisterFilter("truncatesentences_html", filterTruncatesentencesHTML)
|
2017-09-02 01:39:48 +02:00
|
|
|
pongo2.RegisterFilter("random", filterRandom)
|
2014-07-30 22:58:02 +02:00
|
|
|
|
|
|
|
// Markup
|
|
|
|
pongo2.RegisterFilter("markdown", filterMarkdown)
|
|
|
|
|
|
|
|
// Humanize
|
2014-07-28 14:40:58 +02:00
|
|
|
pongo2.RegisterFilter("timeuntil", filterTimeuntilTimesince)
|
|
|
|
pongo2.RegisterFilter("timesince", filterTimeuntilTimesince)
|
2014-07-28 16:21:38 +02:00
|
|
|
pongo2.RegisterFilter("naturaltime", filterTimeuntilTimesince)
|
2014-07-28 16:54:04 +02:00
|
|
|
pongo2.RegisterFilter("naturalday", filterNaturalday)
|
2014-07-28 16:21:38 +02:00
|
|
|
pongo2.RegisterFilter("intcomma", filterIntcomma)
|
2014-07-28 16:24:44 +02:00
|
|
|
pongo2.RegisterFilter("ordinal", filterOrdinal)
|
2014-07-20 00:42:24 +02:00
|
|
|
}
|
|
|
|
|
2014-10-02 04:30:52 +02:00
|
|
|
func filterMarkdown(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
2020-08-05 05:24:39 +02:00
|
|
|
return pongo2.AsSafeValue(string(blackfriday.Run([]byte(in.String())))), nil
|
2014-07-20 00:42:24 +02:00
|
|
|
}
|
2014-07-26 17:28:14 +02:00
|
|
|
|
2014-10-02 04:30:52 +02:00
|
|
|
func filterSlugify(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
2022-05-06 14:52:58 +02:00
|
|
|
return pongo2.AsValue(slug.Make(in.String())), nil
|
2014-07-26 17:28:14 +02:00
|
|
|
}
|
2014-07-28 14:40:58 +02:00
|
|
|
|
2014-10-02 04:30:52 +02:00
|
|
|
func filterFilesizeformat(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
2014-07-28 14:40:58 +02:00
|
|
|
return pongo2.AsValue(humanize.IBytes(uint64(in.Integer()))), nil
|
|
|
|
}
|
|
|
|
|
2014-07-30 22:58:02 +02:00
|
|
|
var filterTruncatesentencesRe = regexp.MustCompile(`(?U:.*[\w]{3,}.*([\d][\.!?][\D]|[\D][\.!?][\s]|[\n$]))`)
|
|
|
|
|
2014-10-02 04:30:52 +02:00
|
|
|
func filterTruncatesentences(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
2014-07-30 22:58:02 +02:00
|
|
|
count := param.Integer()
|
|
|
|
if count <= 0 {
|
|
|
|
return pongo2.AsValue(""), nil
|
|
|
|
}
|
|
|
|
sentencens := filterTruncatesentencesRe.FindAllString(strings.TrimSpace(in.String()), -1)
|
|
|
|
return pongo2.AsValue(strings.TrimSpace(strings.Join(sentencens[:min(count, len(sentencens))], ""))), nil
|
|
|
|
}
|
|
|
|
|
2014-07-31 18:16:55 +02:00
|
|
|
// Taken from pongo2/filters_builtin.go
|
2017-02-13 12:42:10 +01:00
|
|
|
func filterTruncateHTMLHelper(value string, newOutput *bytes.Buffer, cond func() bool, fn func(c rune, s int, idx int) int, finalize func()) {
|
2014-07-31 18:16:55 +02:00
|
|
|
vLen := len(value)
|
2017-02-13 12:42:10 +01:00
|
|
|
tagStack := make([]string, 0)
|
2014-07-31 18:16:55 +02:00
|
|
|
idx := 0
|
|
|
|
|
|
|
|
for idx < vLen && !cond() {
|
|
|
|
c, s := utf8.DecodeRuneInString(value[idx:])
|
|
|
|
if c == utf8.RuneError {
|
|
|
|
idx += s
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if c == '<' {
|
2017-02-13 12:42:10 +01:00
|
|
|
newOutput.WriteRune(c)
|
2014-07-31 18:16:55 +02:00
|
|
|
idx += s // consume "<"
|
|
|
|
|
|
|
|
if idx+1 < vLen {
|
|
|
|
if value[idx] == '/' {
|
|
|
|
// Close tag
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
newOutput.WriteString("/")
|
2014-07-31 18:16:55 +02:00
|
|
|
|
|
|
|
tag := ""
|
2017-02-13 12:42:10 +01:00
|
|
|
idx++ // consume "/"
|
2014-07-31 18:16:55 +02:00
|
|
|
|
|
|
|
for idx < vLen {
|
|
|
|
c2, size2 := utf8.DecodeRuneInString(value[idx:])
|
|
|
|
if c2 == utf8.RuneError {
|
|
|
|
idx += size2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// End of tag found
|
|
|
|
if c2 == '>' {
|
|
|
|
idx++ // consume ">"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
tag += string(c2)
|
|
|
|
idx += size2
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
if len(tagStack) > 0 {
|
2014-07-31 18:16:55 +02:00
|
|
|
// Ideally, the close tag is TOP of tag stack
|
|
|
|
// In malformed HTML, it must not be, so iterate through the stack and remove the tag
|
2017-02-13 12:42:10 +01:00
|
|
|
for i := len(tagStack) - 1; i >= 0; i-- {
|
|
|
|
if tagStack[i] == tag {
|
2014-07-31 18:16:55 +02:00
|
|
|
// Found the tag
|
2017-02-13 12:42:10 +01:00
|
|
|
tagStack[i] = tagStack[len(tagStack)-1]
|
|
|
|
tagStack = tagStack[:len(tagStack)-1]
|
2014-07-31 18:16:55 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
newOutput.WriteString(tag)
|
|
|
|
newOutput.WriteString(">")
|
2014-07-31 18:16:55 +02:00
|
|
|
} else {
|
|
|
|
// Open tag
|
|
|
|
|
|
|
|
tag := ""
|
|
|
|
|
|
|
|
params := false
|
|
|
|
for idx < vLen {
|
|
|
|
c2, size2 := utf8.DecodeRuneInString(value[idx:])
|
|
|
|
if c2 == utf8.RuneError {
|
|
|
|
idx += size2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
newOutput.WriteRune(c2)
|
2014-07-31 18:16:55 +02:00
|
|
|
|
|
|
|
// End of tag found
|
|
|
|
if c2 == '>' {
|
|
|
|
idx++ // consume ">"
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
if !params {
|
|
|
|
if c2 == ' ' {
|
|
|
|
params = true
|
|
|
|
} else {
|
|
|
|
tag += string(c2)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
idx += size2
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add tag to stack
|
2017-02-13 12:42:10 +01:00
|
|
|
tagStack = append(tagStack, tag)
|
2014-07-31 18:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
idx = fn(c, s, idx)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
finalize()
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
for i := len(tagStack) - 1; i >= 0; i-- {
|
|
|
|
tag := tagStack[i]
|
2014-07-31 18:16:55 +02:00
|
|
|
// Close everything from the regular tag stack
|
2017-02-13 12:42:10 +01:00
|
|
|
newOutput.WriteString(fmt.Sprintf("</%s>", tag))
|
2014-07-31 18:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
func filterTruncatesentencesHTML(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
2014-07-31 18:16:55 +02:00
|
|
|
count := param.Integer()
|
|
|
|
if count <= 0 {
|
|
|
|
return pongo2.AsValue(""), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
value := in.String()
|
|
|
|
newLen := max(param.Integer(), 0)
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
newOutput := bytes.NewBuffer(nil)
|
2014-07-31 18:16:55 +02:00
|
|
|
|
|
|
|
sentencefilter := 0
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
filterTruncateHTMLHelper(value, newOutput, func() bool {
|
2014-07-31 18:16:55 +02:00
|
|
|
return sentencefilter >= newLen
|
|
|
|
}, func(_ rune, _ int, idx int) int {
|
|
|
|
// Get next word
|
2017-02-13 12:42:10 +01:00
|
|
|
wordFound := false
|
2014-07-31 18:16:55 +02:00
|
|
|
|
|
|
|
for idx < len(value) {
|
|
|
|
c2, size2 := utf8.DecodeRuneInString(value[idx:])
|
|
|
|
if c2 == utf8.RuneError {
|
|
|
|
idx += size2
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if c2 == '<' {
|
|
|
|
// HTML tag start, don't consume it
|
|
|
|
return idx
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
newOutput.WriteRune(c2)
|
2014-07-31 18:16:55 +02:00
|
|
|
idx += size2
|
|
|
|
|
|
|
|
if (c2 == '.' && !(idx+1 < len(value) && value[idx+1] >= '0' && value[idx+1] <= '9')) ||
|
|
|
|
c2 == '!' || c2 == '?' || c2 == '\n' {
|
|
|
|
// Sentence ends here, stop capturing it now
|
|
|
|
break
|
|
|
|
} else {
|
2017-02-13 12:42:10 +01:00
|
|
|
wordFound = true
|
2014-07-31 18:16:55 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
if wordFound {
|
2014-07-31 18:16:55 +02:00
|
|
|
sentencefilter++
|
|
|
|
}
|
|
|
|
|
|
|
|
return idx
|
|
|
|
}, func() {})
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
return pongo2.AsSafeValue(newOutput.String()), nil
|
2014-07-31 18:16:55 +02:00
|
|
|
}
|
|
|
|
|
2017-09-02 01:39:48 +02:00
|
|
|
func filterRandom(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
|
|
|
if !in.CanSlice() {
|
2017-09-02 01:51:18 +02:00
|
|
|
return nil, &pongo2.Error{
|
|
|
|
Sender: "filter:random",
|
|
|
|
OrigError: errors.New("input is not sliceable"),
|
2017-09-02 01:39:48 +02:00
|
|
|
}
|
|
|
|
}
|
2017-09-02 01:51:18 +02:00
|
|
|
|
2017-09-02 01:39:48 +02:00
|
|
|
if in.Len() <= 0 {
|
2017-09-02 01:51:18 +02:00
|
|
|
return nil, &pongo2.Error{
|
|
|
|
Sender: "filter:random",
|
|
|
|
OrigError: errors.New("input slice is empty"),
|
|
|
|
}
|
2017-09-02 01:39:48 +02:00
|
|
|
}
|
2017-09-02 01:51:18 +02:00
|
|
|
|
2017-09-02 01:39:48 +02:00
|
|
|
return in.Index(rand.Intn(in.Len())), nil
|
|
|
|
}
|
|
|
|
|
2014-10-02 04:30:52 +02:00
|
|
|
func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
2017-02-13 12:42:10 +01:00
|
|
|
basetime, isTime := in.Interface().(time.Time)
|
|
|
|
if !isTime {
|
2014-10-02 04:30:52 +02:00
|
|
|
return nil, &pongo2.Error{
|
2017-02-13 11:53:32 +01:00
|
|
|
Sender: "filter:timeuntil/timesince",
|
2017-02-13 12:42:10 +01:00
|
|
|
OrigError: errors.New("time-value is not a time.Time-instance"),
|
2014-10-02 04:30:52 +02:00
|
|
|
}
|
2014-07-28 14:40:58 +02:00
|
|
|
}
|
|
|
|
var paramtime time.Time
|
|
|
|
if !param.IsNil() {
|
2017-02-13 12:42:10 +01:00
|
|
|
paramtime, isTime = param.Interface().(time.Time)
|
|
|
|
if !isTime {
|
2014-10-02 04:30:52 +02:00
|
|
|
return nil, &pongo2.Error{
|
2017-02-13 11:53:32 +01:00
|
|
|
Sender: "filter:timeuntil/timesince",
|
2017-02-13 12:42:10 +01:00
|
|
|
OrigError: errors.New("time-parameter is not a time.Time-instance"),
|
2014-10-02 04:30:52 +02:00
|
|
|
}
|
2014-07-28 14:40:58 +02:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
paramtime = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return pongo2.AsValue(humanize.TimeDuration(basetime.Sub(paramtime))), nil
|
|
|
|
}
|
2014-07-28 16:21:38 +02:00
|
|
|
|
2014-10-02 04:30:52 +02:00
|
|
|
func filterIntcomma(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
2014-07-28 16:21:38 +02:00
|
|
|
return pongo2.AsValue(humanize.Comma(int64(in.Integer()))), nil
|
|
|
|
}
|
2014-07-28 16:24:44 +02:00
|
|
|
|
2014-10-02 04:30:52 +02:00
|
|
|
func filterOrdinal(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
2014-07-28 16:24:44 +02:00
|
|
|
return pongo2.AsValue(humanize.Ordinal(in.Integer())), nil
|
|
|
|
}
|
2014-07-28 16:54:04 +02:00
|
|
|
|
2014-10-02 04:30:52 +02:00
|
|
|
func filterNaturalday(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
|
2017-02-13 12:42:10 +01:00
|
|
|
basetime, isTime := in.Interface().(time.Time)
|
|
|
|
if !isTime {
|
2014-10-02 04:30:52 +02:00
|
|
|
return nil, &pongo2.Error{
|
2017-02-13 11:53:32 +01:00
|
|
|
Sender: "filter:naturalday",
|
2017-02-13 12:42:10 +01:00
|
|
|
OrigError: errors.New("naturalday-value is not a time.Time-instance"),
|
2014-10-02 04:30:52 +02:00
|
|
|
}
|
2014-07-28 16:54:04 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
var referenceTime time.Time
|
2014-07-28 16:54:04 +02:00
|
|
|
if !param.IsNil() {
|
2017-02-13 12:42:10 +01:00
|
|
|
referenceTime, isTime = param.Interface().(time.Time)
|
|
|
|
if !isTime {
|
2014-10-02 04:30:52 +02:00
|
|
|
return nil, &pongo2.Error{
|
2017-02-13 11:53:32 +01:00
|
|
|
Sender: "filter:naturalday",
|
2017-02-13 12:42:10 +01:00
|
|
|
OrigError: errors.New("naturalday-parameter is not a time.Time-instance"),
|
2014-10-02 04:30:52 +02:00
|
|
|
}
|
2014-07-28 16:54:04 +02:00
|
|
|
}
|
|
|
|
} else {
|
2017-02-13 12:42:10 +01:00
|
|
|
referenceTime = time.Now()
|
2014-07-28 16:54:04 +02:00
|
|
|
}
|
|
|
|
|
2017-02-13 12:42:10 +01:00
|
|
|
d := referenceTime.Sub(basetime) / time.Hour
|
2014-07-28 16:54:04 +02:00
|
|
|
|
|
|
|
switch {
|
|
|
|
case d >= 0 && d < 24:
|
|
|
|
// Today
|
|
|
|
return pongo2.AsValue("today"), nil
|
|
|
|
case d >= 24:
|
|
|
|
return pongo2.AsValue("yesterday"), nil
|
|
|
|
case d < 0 && d >= -24:
|
|
|
|
return pongo2.AsValue("tomorrow"), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Default behaviour
|
|
|
|
return pongo2.ApplyFilter("naturaltime", in, param)
|
|
|
|
}
|