Merge pull request #5 from tucnak/patch-1

Filter random for slices
This commit is contained in:
Florian Schlachter 2017-09-02 02:11:13 +02:00 committed by GitHub
commit 86b9b7b805
3 changed files with 31 additions and 2 deletions

View File

@ -3,7 +3,7 @@
[![Build Status](https://travis-ci.org/flosch/pongo2-addons.svg?branch=master)](https://travis-ci.org/flosch/pongo2-addons) [![Build Status](https://travis-ci.org/flosch/pongo2-addons.svg?branch=master)](https://travis-ci.org/flosch/pongo2-addons)
[![Gratipay](http://img.shields.io/badge/gratipay-support%20pongo-brightgreen.svg)](https://gratipay.com/flosch/) [![Gratipay](http://img.shields.io/badge/gratipay-support%20pongo-brightgreen.svg)](https://gratipay.com/flosch/)
Official filter and tag add-ons for [pongo2](https://github.com/flosch/pongo2). Since this package uses Official filter and tag add-ons for [pongo2](https://github.com/flosch/pongo2). Since this package uses
3rd-party-libraries, it's in its own package. 3rd-party-libraries, it's in its own package.
## How to install and use ## How to install and use
@ -24,10 +24,11 @@ All additional filters/tags will be registered automatically.
- **[filesizeformat](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#filesizeformat)** (human-readable filesize; takes bytes as input) - **[filesizeformat](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#filesizeformat)** (human-readable filesize; takes bytes as input)
- **[slugify](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slugify)** (creates a slug for a given input) - **[slugify](https://docs.djangoproject.com/en/dev/ref/templates/builtins/#slugify)** (creates a slug for a given input)
- **truncatesentences** / **truncatesentences_html** (returns the first X sentences [like truncatechars/truncatewords]; please provide X as a parameter) - **truncatesentences** / **truncatesentences_html** (returns the first X sentences [like truncatechars/truncatewords]; please provide X as a parameter)
- **random** (returns a random element of the input slice)
- Markup - Markup
- **markdown** - **markdown**
- Humanize - Humanize
- **[intcomma](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#intcomma)** (put decimal marks into the number) - **[intcomma](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#intcomma)** (put decimal marks into the number)
- **[ordinal](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#ordinal)** (convert integer to its ordinal as string) - **[ordinal](https://docs.djangoproject.com/en/dev/ref/contrib/humanize/#ordinal)** (convert integer to its ordinal as string)

View File

@ -3,6 +3,7 @@ package pongo2addons
import ( import (
"bytes" "bytes"
"fmt" "fmt"
"math/rand"
"regexp" "regexp"
"strings" "strings"
"time" "time"
@ -17,11 +18,14 @@ import (
) )
func init() { func init() {
rand.Seed(time.Now().UTC().UnixNano())
// Regulars // Regulars
pongo2.RegisterFilter("slugify", filterSlugify) pongo2.RegisterFilter("slugify", filterSlugify)
pongo2.RegisterFilter("filesizeformat", filterFilesizeformat) pongo2.RegisterFilter("filesizeformat", filterFilesizeformat)
pongo2.RegisterFilter("truncatesentences", filterTruncatesentences) pongo2.RegisterFilter("truncatesentences", filterTruncatesentences)
pongo2.RegisterFilter("truncatesentences_html", filterTruncatesentencesHTML) pongo2.RegisterFilter("truncatesentences_html", filterTruncatesentencesHTML)
pongo2.RegisterFilter("random", filterRandom)
// Markup // Markup
pongo2.RegisterFilter("markdown", filterMarkdown) pongo2.RegisterFilter("markdown", filterMarkdown)
@ -218,6 +222,24 @@ func filterTruncatesentencesHTML(in *pongo2.Value, param *pongo2.Value) (*pongo2
return pongo2.AsSafeValue(newOutput.String()), nil return pongo2.AsSafeValue(newOutput.String()), nil
} }
func filterRandom(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
if !in.CanSlice() {
return nil, &pongo2.Error{
Sender: "filter:random",
OrigError: errors.New("input is not sliceable"),
}
}
if in.Len() <= 0 {
return nil, &pongo2.Error{
Sender: "filter:random",
OrigError: errors.New("input slice is empty"),
}
}
return in.Index(rand.Intn(in.Len())), nil
}
func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
basetime, isTime := in.Interface().(time.Time) basetime, isTime := in.Interface().(time.Time)
if !isTime { if !isTime {

View File

@ -89,4 +89,10 @@ func (s *TestSuite1) TestFilters(c *C) {
c.Assert(getResult("{{ text|truncatesentences_html:3 }}", pongo2.Context{ c.Assert(getResult("{{ text|truncatesentences_html:3 }}", pongo2.Context{
"text": `<div class="test"><ul><li>This is a first sentence with a 4.50 number.</li><li>The second one is even more fun! Isn't it?</li><li>Last sentence, okay.</li></ul></div>`}), "text": `<div class="test"><ul><li>This is a first sentence with a 4.50 number.</li><li>The second one is even more fun! Isn't it?</li><li>Last sentence, okay.</li></ul></div>`}),
Equals, `<div class="test"><ul><li>This is a first sentence with a 4.50 number.</li><li>The second one is even more fun! Isn't it?</li></ul></div>`) Equals, `<div class="test"><ul><li>This is a first sentence with a 4.50 number.</li><li>The second one is even more fun! Isn't it?</li></ul></div>`)
// Random
c.Assert(getResult("{{ array|random }}",
pongo2.Context{"array": []int{42}}),
Equals, "42")
} }