Merge pull request #3 from flosch/pr/2

Adapt to latest changes in pongo2
This commit is contained in:
Florian Schlachter 2017-02-13 21:57:55 +01:00 committed by GitHub
commit ada12dc6c9
3 changed files with 84 additions and 77 deletions

View File

@ -1,10 +1,10 @@
language: go language: go
go: go:
- 1.3 - 1.7
- tip - tip
install: install:
- go get code.google.com/p/go.tools/cmd/cover - go get golang.org/x/tools/cmd/cover
- go get gopkg.in/check.v1 - go get gopkg.in/check.v1
- go get github.com/flosch/pongo2 - go get github.com/flosch/pongo2
- go get github.com/russross/blackfriday - go get github.com/russross/blackfriday

View File

@ -12,6 +12,7 @@ import (
"github.com/extemporalgenome/slug" "github.com/extemporalgenome/slug"
"github.com/flosch/go-humanize" "github.com/flosch/go-humanize"
"github.com/juju/errors"
"github.com/russross/blackfriday" "github.com/russross/blackfriday"
) )
@ -20,7 +21,7 @@ func init() {
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)
// Markup // Markup
pongo2.RegisterFilter("markdown", filterMarkdown) pongo2.RegisterFilter("markdown", filterMarkdown)
@ -58,9 +59,9 @@ func filterTruncatesentences(in *pongo2.Value, param *pongo2.Value) (*pongo2.Val
} }
// Taken from pongo2/filters_builtin.go // Taken from pongo2/filters_builtin.go
func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func() bool, fn func(c rune, s int, idx int) int, finalize func()) { func filterTruncateHTMLHelper(value string, newOutput *bytes.Buffer, cond func() bool, fn func(c rune, s int, idx int) int, finalize func()) {
vLen := len(value) vLen := len(value)
tag_stack := make([]string, 0) tagStack := make([]string, 0)
idx := 0 idx := 0
for idx < vLen && !cond() { for idx < vLen && !cond() {
@ -71,17 +72,17 @@ func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func(
} }
if c == '<' { if c == '<' {
new_output.WriteRune(c) newOutput.WriteRune(c)
idx += s // consume "<" idx += s // consume "<"
if idx+1 < vLen { if idx+1 < vLen {
if value[idx] == '/' { if value[idx] == '/' {
// Close tag // Close tag
new_output.WriteString("/") newOutput.WriteString("/")
tag := "" tag := ""
idx += 1 // consume "/" idx++ // consume "/"
for idx < vLen { for idx < vLen {
c2, size2 := utf8.DecodeRuneInString(value[idx:]) c2, size2 := utf8.DecodeRuneInString(value[idx:])
@ -99,21 +100,21 @@ func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func(
idx += size2 idx += size2
} }
if len(tag_stack) > 0 { if len(tagStack) > 0 {
// Ideally, the close tag is TOP of tag stack // Ideally, the close tag is TOP of tag stack
// In malformed HTML, it must not be, so iterate through the stack and remove the tag // In malformed HTML, it must not be, so iterate through the stack and remove the tag
for i := len(tag_stack) - 1; i >= 0; i-- { for i := len(tagStack) - 1; i >= 0; i-- {
if tag_stack[i] == tag { if tagStack[i] == tag {
// Found the tag // Found the tag
tag_stack[i] = tag_stack[len(tag_stack)-1] tagStack[i] = tagStack[len(tagStack)-1]
tag_stack = tag_stack[:len(tag_stack)-1] tagStack = tagStack[:len(tagStack)-1]
break break
} }
} }
} }
new_output.WriteString(tag) newOutput.WriteString(tag)
new_output.WriteString(">") newOutput.WriteString(">")
} else { } else {
// Open tag // Open tag
@ -127,7 +128,7 @@ func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func(
continue continue
} }
new_output.WriteRune(c2) newOutput.WriteRune(c2)
// End of tag found // End of tag found
if c2 == '>' { if c2 == '>' {
@ -147,7 +148,7 @@ func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func(
} }
// Add tag to stack // Add tag to stack
tag_stack = append(tag_stack, tag) tagStack = append(tagStack, tag)
} }
} }
} else { } else {
@ -157,14 +158,14 @@ func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func(
finalize() finalize()
for i := len(tag_stack) - 1; i >= 0; i-- { for i := len(tagStack) - 1; i >= 0; i-- {
tag := tag_stack[i] tag := tagStack[i]
// Close everything from the regular tag stack // Close everything from the regular tag stack
new_output.WriteString(fmt.Sprintf("</%s>", tag)) newOutput.WriteString(fmt.Sprintf("</%s>", tag))
} }
} }
func filterTruncatesentencesHtml(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { func filterTruncatesentencesHTML(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
count := param.Integer() count := param.Integer()
if count <= 0 { if count <= 0 {
return pongo2.AsValue(""), nil return pongo2.AsValue(""), nil
@ -173,15 +174,15 @@ func filterTruncatesentencesHtml(in *pongo2.Value, param *pongo2.Value) (*pongo2
value := in.String() value := in.String()
newLen := max(param.Integer(), 0) newLen := max(param.Integer(), 0)
new_output := bytes.NewBuffer(nil) newOutput := bytes.NewBuffer(nil)
sentencefilter := 0 sentencefilter := 0
filterTruncateHtmlHelper(value, new_output, func() bool { filterTruncateHTMLHelper(value, newOutput, func() bool {
return sentencefilter >= newLen return sentencefilter >= newLen
}, func(_ rune, _ int, idx int) int { }, func(_ rune, _ int, idx int) int {
// Get next word // Get next word
word_found := false wordFound := false
for idx < len(value) { for idx < len(value) {
c2, size2 := utf8.DecodeRuneInString(value[idx:]) c2, size2 := utf8.DecodeRuneInString(value[idx:])
@ -195,7 +196,7 @@ func filterTruncatesentencesHtml(in *pongo2.Value, param *pongo2.Value) (*pongo2
return idx return idx
} }
new_output.WriteRune(c2) newOutput.WriteRune(c2)
idx += size2 idx += size2
if (c2 == '.' && !(idx+1 < len(value) && value[idx+1] >= '0' && value[idx+1] <= '9')) || if (c2 == '.' && !(idx+1 < len(value) && value[idx+1] >= '0' && value[idx+1] <= '9')) ||
@ -203,35 +204,35 @@ func filterTruncatesentencesHtml(in *pongo2.Value, param *pongo2.Value) (*pongo2
// Sentence ends here, stop capturing it now // Sentence ends here, stop capturing it now
break break
} else { } else {
word_found = true wordFound = true
} }
} }
if word_found { if wordFound {
sentencefilter++ sentencefilter++
} }
return idx return idx
}, func() {}) }, func() {})
return pongo2.AsSafeValue(new_output.String()), nil return pongo2.AsSafeValue(newOutput.String()), 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, is_time := in.Interface().(time.Time) basetime, isTime := in.Interface().(time.Time)
if !is_time { if !isTime {
return nil, &pongo2.Error{ return nil, &pongo2.Error{
Sender: "filter:timeuntil/timesince", Sender: "filter:timeuntil/timesince",
ErrorMsg: "time-value is not a time.Time-instance.", OrigError: errors.New("time-value is not a time.Time-instance"),
} }
} }
var paramtime time.Time var paramtime time.Time
if !param.IsNil() { if !param.IsNil() {
paramtime, is_time = param.Interface().(time.Time) paramtime, isTime = param.Interface().(time.Time)
if !is_time { if !isTime {
return nil, &pongo2.Error{ return nil, &pongo2.Error{
Sender: "filter:timeuntil/timesince", Sender: "filter:timeuntil/timesince",
ErrorMsg: "time-parameter is not a time.Time-instance.", OrigError: errors.New("time-parameter is not a time.Time-instance"),
} }
} }
} else { } else {
@ -250,28 +251,28 @@ func filterOrdinal(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo
} }
func filterNaturalday(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) { func filterNaturalday(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *pongo2.Error) {
basetime, is_time := in.Interface().(time.Time) basetime, isTime := in.Interface().(time.Time)
if !is_time { if !isTime {
return nil, &pongo2.Error{ return nil, &pongo2.Error{
Sender: "filter:naturalday", Sender: "filter:naturalday",
ErrorMsg: "naturalday-value is not a time.Time-instance.", OrigError: errors.New("naturalday-value is not a time.Time-instance"),
} }
} }
var reference_time time.Time var referenceTime time.Time
if !param.IsNil() { if !param.IsNil() {
reference_time, is_time = param.Interface().(time.Time) referenceTime, isTime = param.Interface().(time.Time)
if !is_time { if !isTime {
return nil, &pongo2.Error{ return nil, &pongo2.Error{
Sender: "filter:naturalday", Sender: "filter:naturalday",
ErrorMsg: "naturalday-parameter is not a time.Time-instance.", OrigError: errors.New("naturalday-parameter is not a time.Time-instance"),
} }
} }
} else { } else {
reference_time = time.Now() referenceTime = time.Now()
} }
d := reference_time.Sub(basetime) / time.Hour d := referenceTime.Sub(basetime) / time.Hour
switch { switch {
case d >= 0 && d < 24: case d >= 0 && d < 24:

View File

@ -14,73 +14,79 @@ func Test(t *testing.T) {
TestingT(t) TestingT(t)
} }
// A wrapprt of pongo2.RenderTemplateString
func getResult(s string, ctx pongo2.Context) string {
result, _ := pongo2.RenderTemplateString(s, ctx)
return result
}
type TestSuite1 struct{} type TestSuite1 struct{}
var _ = Suite(&TestSuite1{}) var _ = Suite(&TestSuite1{})
func (s *TestSuite1) TestFilters(c *C) { func (s *TestSuite1) TestFilters(c *C) {
// Markdown // Markdown
c.Assert(pongo2.RenderTemplateString("{{ \"**test**\"|markdown }}", nil), Equals, "<p><strong>test</strong></p>\n") c.Assert(getResult("{{ \"**test**\"|markdown }}", nil), Equals, "<p><strong>test</strong></p>\n")
// Slugify // Slugify
c.Assert(pongo2.RenderTemplateString("{{ \"this is ä test!\"|slugify }}", nil), Equals, "this-is-a-test") c.Assert(getResult("{{ \"this is ä test!\"|slugify }}", nil), Equals, "this-is-a-test")
// Filesizeformat // Filesizeformat
c.Assert(pongo2.RenderTemplateString("{{ 123456789|filesizeformat }}", nil), Equals, "118MiB") c.Assert(getResult("{{ 123456789|filesizeformat }}", nil), Equals, "118MiB")
// Timesince/timeuntil // Timesince/timeuntil
base_date := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) baseDate := 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) futureDate := baseDate.Add(24*7*4*time.Hour + 2*time.Hour)
c.Assert(pongo2.RenderTemplateString("{{ future_date|timeuntil:base_date }}", c.Assert(getResult("{{ future_date|timeuntil:base_date }}",
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "4 weeks from now") pongo2.Context{"base_date": baseDate, "future_date": futureDate}), Equals, "4 weeks from now")
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) baseDate = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
future_date = base_date.Add(2 * time.Hour) futureDate = baseDate.Add(2 * time.Hour)
c.Assert(pongo2.RenderTemplateString("{{ future_date|timeuntil:base_date }}", c.Assert(getResult("{{ future_date|timeuntil:base_date }}",
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours from now") pongo2.Context{"base_date": baseDate, "future_date": futureDate}), Equals, "2 hours from now")
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) baseDate = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
future_date = base_date.Add(2 * time.Hour) futureDate = baseDate.Add(2 * time.Hour)
c.Assert(pongo2.RenderTemplateString("{{ base_date|timesince:future_date }}", c.Assert(getResult("{{ base_date|timesince:future_date }}",
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "2 hours ago") pongo2.Context{"base_date": baseDate, "future_date": futureDate}), Equals, "2 hours ago")
// Natural time // Natural time
base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) baseDate = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
future_date = base_date.Add(4 * time.Second) futureDate = baseDate.Add(4 * time.Second)
c.Assert(pongo2.RenderTemplateString("{{ base_date|naturaltime:future_date }}", c.Assert(getResult("{{ base_date|naturaltime:future_date }}",
pongo2.Context{"base_date": base_date, "future_date": future_date}), Equals, "4 seconds ago") pongo2.Context{"base_date": baseDate, "future_date": futureDate}), Equals, "4 seconds ago")
// Naturalday // Naturalday
today := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) today := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC)
yesterday := today.Add(-24 * time.Hour) yesterday := today.Add(-24 * time.Hour)
tomorrow := today.Add(24 * time.Hour) tomorrow := today.Add(24 * time.Hour)
today_plus_3 := today.Add(3 * 24 * time.Hour) todayPlus3 := today.Add(3 * 24 * time.Hour)
c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}", c.Assert(getResult("{{ date|naturalday:today }}",
pongo2.Context{"date": today, "today": today}), Equals, "today") pongo2.Context{"date": today, "today": today}), Equals, "today")
c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}", c.Assert(getResult("{{ date|naturalday:today }}",
pongo2.Context{"date": yesterday, "today": today}), Equals, "yesterday") pongo2.Context{"date": yesterday, "today": today}), Equals, "yesterday")
c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}", c.Assert(getResult("{{ date|naturalday:today }}",
pongo2.Context{"date": tomorrow, "today": today}), Equals, "tomorrow") pongo2.Context{"date": tomorrow, "today": today}), Equals, "tomorrow")
c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}", c.Assert(getResult("{{ date|naturalday:today }}",
pongo2.Context{"date": today_plus_3, "today": today}), Equals, "3 days from now") pongo2.Context{"date": todayPlus3, "today": today}), Equals, "3 days from now")
// Intcomma // Intcomma
c.Assert(pongo2.RenderTemplateString("{{ 123456789|intcomma }}", nil), Equals, "123,456,789") c.Assert(getResult("{{ 123456789|intcomma }}", nil), Equals, "123,456,789")
// Ordinal // Ordinal
c.Assert(pongo2.RenderTemplateString("{{ 1|ordinal }} {{ 2|ordinal }} {{ 3|ordinal }} {{ 18241|ordinal }}", nil), c.Assert(getResult("{{ 1|ordinal }} {{ 2|ordinal }} {{ 3|ordinal }} {{ 18241|ordinal }}", nil),
Equals, "1st 2nd 3rd 18241st") Equals, "1st 2nd 3rd 18241st")
// Truncatesentences // Truncatesentences
c.Assert(pongo2.RenderTemplateString("{{ text|truncatesentences:3|safe }}", pongo2.Context{ c.Assert(getResult("{{ text|truncatesentences:3|safe }}", pongo2.Context{
"text": `This is a first sentence with a 4.50 number. The second one is even more fun! Isn't it? Last sentence, okay.`}), "text": `This is a first sentence with a 4.50 number. The second one is even more fun! Isn't it? Last sentence, okay.`}),
Equals, "This is a first sentence with a 4.50 number. The second one is even more fun! Isn't it?") Equals, "This is a first sentence with a 4.50 number. The second one is even more fun! Isn't it?")
// Truncatesentences_html // Truncatesentences_html
c.Assert(pongo2.RenderTemplateString("{{ text|truncatesentences_html:2 }}", pongo2.Context{ c.Assert(getResult("{{ text|truncatesentences_html:2 }}", 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!</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!</li></ul></div>`)
c.Assert(pongo2.RenderTemplateString("{{ 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>`)
} }