diff --git a/.gitignore b/.gitignore index 8365624..5738814 100644 --- a/.gitignore +++ b/.gitignore @@ -21,3 +21,4 @@ _testmain.go *.exe *.test +.idea diff --git a/README.md b/README.md index 9954d89..059eb9d 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/filter_markdown.go b/filters.go similarity index 60% rename from filter_markdown.go rename to filters.go index 6d40363..6f51e0b 100644 --- a/filter_markdown.go +++ b/filters.go @@ -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 +} diff --git a/filters_test.go b/filters_test.go new file mode 100644 index 0000000..e9bd01c --- /dev/null +++ b/filters_test.go @@ -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, "

test

\n") + + // Slugify + c.Assert(pongo2.RenderTemplateString("{{ \"this is รค test!\"|slugify }}", nil), Equals, "this-is-a-test") +}