slugify-filter and tests added.
This commit is contained in:
parent
ca44141371
commit
f18a69d96f
1
.gitignore
vendored
1
.gitignore
vendored
@ -21,3 +21,4 @@ _testmain.go
|
||||
|
||||
*.exe
|
||||
*.test
|
||||
.idea
|
||||
|
@ -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
|
||||
|
||||
|
@ -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
26
filters_test.go
Normal 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")
|
||||
}
|
Loading…
Reference in New Issue
Block a user