2014-07-20 00:42:24 +02:00
|
|
|
package pongo2addons
|
|
|
|
|
|
|
|
import (
|
2014-07-28 14:40:58 +02:00
|
|
|
"errors"
|
|
|
|
"time"
|
|
|
|
|
2014-07-20 00:42:24 +02:00
|
|
|
"github.com/flosch/pongo2"
|
|
|
|
|
2014-07-26 17:28:14 +02:00
|
|
|
"github.com/extemporalgenome/slug"
|
2014-07-20 00:42:24 +02:00
|
|
|
"github.com/russross/blackfriday"
|
2014-07-28 14:40:58 +02:00
|
|
|
"github.com/flosch/go-humanize"
|
2014-07-20 00:42:24 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
pongo2.RegisterFilter("markdown", filterMarkdown)
|
2014-07-26 17:28:14 +02:00
|
|
|
pongo2.RegisterFilter("slugify", filterSlugify)
|
2014-07-28 14:40:58 +02:00
|
|
|
pongo2.RegisterFilter("filesizeformat", filterFilesizeformat)
|
|
|
|
pongo2.RegisterFilter("timeuntil", filterTimeuntilTimesince)
|
|
|
|
pongo2.RegisterFilter("timesince", filterTimeuntilTimesince)
|
2014-07-28 16:21:38 +02:00
|
|
|
pongo2.RegisterFilter("naturaltime", filterTimeuntilTimesince)
|
|
|
|
pongo2.RegisterFilter("intcomma", filterIntcomma)
|
2014-07-28 16:24:44 +02:00
|
|
|
pongo2.RegisterFilter("ordinal", filterOrdinal)
|
2014-07-20 00:42:24 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func filterMarkdown(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
|
|
|
return pongo2.AsValue(string(blackfriday.MarkdownCommon([]byte(in.String())))), nil
|
|
|
|
}
|
2014-07-26 17:28:14 +02:00
|
|
|
|
|
|
|
func filterSlugify(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
|
|
|
return pongo2.AsValue(slug.Slug(in.String())), nil
|
|
|
|
}
|
2014-07-28 14:40:58 +02:00
|
|
|
|
|
|
|
func filterFilesizeformat(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
|
|
|
return pongo2.AsValue(humanize.IBytes(uint64(in.Integer()))), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
|
|
|
basetime, is_time := in.Interface().(time.Time)
|
|
|
|
if !is_time {
|
|
|
|
return nil, errors.New("timeuntil-value is not a time.Time-instance.")
|
|
|
|
}
|
|
|
|
var paramtime time.Time
|
|
|
|
if !param.IsNil() {
|
|
|
|
paramtime, is_time = param.Interface().(time.Time)
|
|
|
|
if !is_time {
|
|
|
|
return nil, errors.New("timeuntil-parameter is not a time.Time-instance.")
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
paramtime = time.Now()
|
|
|
|
}
|
|
|
|
|
|
|
|
return pongo2.AsValue(humanize.TimeDuration(basetime.Sub(paramtime))), nil
|
|
|
|
}
|
2014-07-28 16:21:38 +02:00
|
|
|
|
|
|
|
func filterIntcomma(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
|
|
|
return pongo2.AsValue(humanize.Comma(int64(in.Integer()))), nil
|
|
|
|
}
|
2014-07-28 16:24:44 +02:00
|
|
|
|
|
|
|
func filterOrdinal(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
|
|
|
|
return pongo2.AsValue(humanize.Ordinal(in.Integer())), nil
|
|
|
|
}
|