filters added: filesizeformat,timesince,timeuntil

This commit is contained in:
Florian Schlachter 2014-07-28 14:40:58 +02:00
parent e3924f065e
commit c3efa8fc22
3 changed files with 52 additions and 1 deletions

View File

@ -15,7 +15,9 @@ All additional filters/tags will be registered automatically.
## Filters
- **markdown** (parses markdown text and outputs HTML; **hint**: use the **safe**-filter to make the output not being escaped)
- **slugify** (creates a slug for a given input)
- **[slugify](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#slugify)** (creates a slug for a given input)
- **[filesizeformat](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#filesizeformat)** (human-readable filesize; takes bytes as input)
- **[timesince](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#timesince)/[timeuntil](https://docs.djangoproject.com/en/1.6/ref/templates/builtins/#timeuntil)** (human-readable time duration indicator)
## Tags

View File

@ -1,15 +1,22 @@
package pongo2addons
import (
"errors"
"time"
"github.com/flosch/pongo2"
"github.com/extemporalgenome/slug"
"github.com/russross/blackfriday"
"github.com/flosch/go-humanize"
)
func init() {
pongo2.RegisterFilter("markdown", filterMarkdown)
pongo2.RegisterFilter("slugify", filterSlugify)
pongo2.RegisterFilter("filesizeformat", filterFilesizeformat)
pongo2.RegisterFilter("timeuntil", filterTimeuntilTimesince)
pongo2.RegisterFilter("timesince", filterTimeuntilTimesince)
}
func filterMarkdown(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
@ -19,3 +26,25 @@ func filterMarkdown(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error
func filterSlugify(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
return pongo2.AsValue(slug.Slug(in.String())), nil
}
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
}

View File

@ -1,6 +1,7 @@
package pongo2addons
import (
"time"
"testing"
. "gopkg.in/check.v1"
@ -23,4 +24,23 @@ func (s *TestSuite1) TestFilters(c *C) {
// Slugify
c.Assert(pongo2.RenderTemplateString("{{ \"this is ä test!\"|slugify }}", nil), Equals, "this-is-a-test")
// Filesizeformat
c.Assert(pongo2.RenderTemplateString("{{ 123456789|filesizeformat }}", nil), Equals, "118MiB")
// Timesince/timeuntil
base_date := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
future_date := base_date.Add(24 * 7 * 4 * time.Hour + 2 * time.Hour)
c.Assert(pongo2.RenderTemplateString("{{ future_date|timeuntil:base_date }}",
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "4 weeks from now")
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
future_date = base_date.Add(2 * time.Hour)
c.Assert(pongo2.RenderTemplateString("{{ future_date|timeuntil:base_date }}",
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours from now")
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
future_date = base_date.Add(2 * time.Hour)
c.Assert(pongo2.RenderTemplateString("{{ base_date|timesince:future_date }}",
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours ago")
}