slugify-filter and tests added.

This commit is contained in:
Florian Schlachter 2014-07-26 17:28:14 +02:00
parent ca44141371
commit f18a69d96f
4 changed files with 34 additions and 0 deletions

1
.gitignore vendored
View File

@ -21,3 +21,4 @@ _testmain.go
*.exe
*.test
.idea

View File

@ -13,6 +13,7 @@ All 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)
## Tags

View File

@ -3,13 +3,19 @@ package pongo2addons
import (
"github.com/flosch/pongo2"
"github.com/extemporalgenome/slug"
"github.com/russross/blackfriday"
)
func init() {
pongo2.RegisterFilter("markdown", filterMarkdown)
pongo2.RegisterFilter("slugify", filterSlugify)
}
func filterMarkdown(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
return pongo2.AsValue(string(blackfriday.MarkdownCommon([]byte(in.String())))), nil
}
func filterSlugify(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, error) {
return pongo2.AsValue(slug.Slug(in.String())), nil
}

26
filters_test.go Normal file
View File

@ -0,0 +1,26 @@
package pongo2addons
import (
"testing"
. "gopkg.in/check.v1"
"github.com/flosch/pongo2"
)
// Hook up gocheck into the "go test" runner.
func Test(t *testing.T) {
TestingT(t)
}
type TestSuite1 struct{}
var _ = Suite(&TestSuite1{})
func (s *TestSuite1) TestFilters(c *C) {
// Markdown
c.Assert(pongo2.RenderTemplateString("{{ \"**test**\"|markdown|safe }}", nil), Equals, "<p><strong>test</strong></p>\n")
// Slugify
c.Assert(pongo2.RenderTemplateString("{{ \"this is ä test!\"|slugify }}", nil), Equals, "this-is-a-test")
}