From c3efa8fc22ab775d368c569026a2f85496f2d4ae Mon Sep 17 00:00:00 2001 From: Florian Schlachter Date: Mon, 28 Jul 2014 14:40:58 +0200 Subject: [PATCH] filters added: filesizeformat,timesince,timeuntil --- README.md | 4 +++- filters.go | 29 +++++++++++++++++++++++++++++ filters_test.go | 20 ++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 6551653..dd8a000 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/filters.go b/filters.go index 6f51e0b..eae8e81 100644 --- a/filters.go +++ b/filters.go @@ -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 +} diff --git a/filters_test.go b/filters_test.go index e9bd01c..39fc772 100644 --- a/filters_test.go +++ b/filters_test.go @@ -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") }