From bd3adb0e5efb9ea6dcd3a4c810b28da743c416a5 Mon Sep 17 00:00:00 2001 From: Yuta Hayashibe Date: Mon, 13 Feb 2017 19:53:32 +0900 Subject: [PATCH 1/4] Fix for 6d9b0dd of pongo2 --- filters.go | 17 +++++++++-------- filters_test.go | 38 ++++++++++++++++++++++---------------- 2 files changed, 31 insertions(+), 24 deletions(-) diff --git a/filters.go b/filters.go index af1a5d1..eea9d45 100644 --- a/filters.go +++ b/filters.go @@ -2,6 +2,7 @@ package pongo2addons import ( "bytes" + "errors" "fmt" "regexp" "strings" @@ -221,8 +222,8 @@ func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Va basetime, is_time := in.Interface().(time.Time) if !is_time { return nil, &pongo2.Error{ - Sender: "filter:timeuntil/timesince", - ErrorMsg: "time-value is not a time.Time-instance.", + Sender: "filter:timeuntil/timesince", + OrigError: errors.New("time-value is not a time.Time-instance."), } } var paramtime time.Time @@ -230,8 +231,8 @@ func filterTimeuntilTimesince(in *pongo2.Value, param *pongo2.Value) (*pongo2.Va paramtime, is_time = param.Interface().(time.Time) if !is_time { return nil, &pongo2.Error{ - Sender: "filter:timeuntil/timesince", - ErrorMsg: "time-parameter is not a time.Time-instance.", + Sender: "filter:timeuntil/timesince", + OrigError: errors.New("time-parameter is not a time.Time-instance."), } } } else { @@ -253,8 +254,8 @@ func filterNaturalday(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *po basetime, is_time := in.Interface().(time.Time) if !is_time { return nil, &pongo2.Error{ - Sender: "filter:naturalday", - ErrorMsg: "naturalday-value is not a time.Time-instance.", + Sender: "filter:naturalday", + OrigError: errors.New("naturalday-value is not a time.Time-instance."), } } @@ -263,8 +264,8 @@ func filterNaturalday(in *pongo2.Value, param *pongo2.Value) (*pongo2.Value, *po reference_time, is_time = param.Interface().(time.Time) if !is_time { return nil, &pongo2.Error{ - Sender: "filter:naturalday", - ErrorMsg: "naturalday-parameter is not a time.Time-instance.", + Sender: "filter:naturalday", + OrigError: errors.New("naturalday-parameter is not a time.Time-instance."), } } } else { diff --git a/filters_test.go b/filters_test.go index f38aeff..ae9dae0 100644 --- a/filters_test.go +++ b/filters_test.go @@ -14,40 +14,46 @@ func Test(t *testing.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{} var _ = Suite(&TestSuite1{}) func (s *TestSuite1) TestFilters(c *C) { // Markdown - c.Assert(pongo2.RenderTemplateString("{{ \"**test**\"|markdown }}", nil), Equals, "

test

\n") + c.Assert(getResult("{{ \"**test**\"|markdown }}", nil), Equals, "

test

\n") // 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 - c.Assert(pongo2.RenderTemplateString("{{ 123456789|filesizeformat }}", nil), Equals, "118MiB") + c.Assert(getResult("{{ 123456789|filesizeformat }}", nil), Equals, "118MiB") // Timesince/timeuntil base_date := 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) - 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") base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) future_date = base_date.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") base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) future_date = base_date.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") // Natural time base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) future_date = base_date.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") // Naturalday @@ -55,32 +61,32 @@ func (s *TestSuite1) TestFilters(c *C) { yesterday := today.Add(-24 * time.Hour) tomorrow := today.Add(24 * time.Hour) today_plus_3 := 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") - c.Assert(pongo2.RenderTemplateString("{{ date|naturalday:today }}", + c.Assert(getResult("{{ date|naturalday:today }}", 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") - 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") // Intcomma - c.Assert(pongo2.RenderTemplateString("{{ 123456789|intcomma }}", nil), Equals, "123,456,789") + c.Assert(getResult("{{ 123456789|intcomma }}", nil), Equals, "123,456,789") // 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") // 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.`}), Equals, "This is a first sentence with a 4.50 number. The second one is even more fun! Isn't it?") // Truncatesentences_html - c.Assert(pongo2.RenderTemplateString("{{ text|truncatesentences_html:2 }}", pongo2.Context{ + c.Assert(getResult("{{ text|truncatesentences_html:2 }}", pongo2.Context{ "text": `
`}), Equals, `
`) - c.Assert(pongo2.RenderTemplateString("{{ text|truncatesentences_html:3 }}", pongo2.Context{ + c.Assert(getResult("{{ text|truncatesentences_html:3 }}", pongo2.Context{ "text": `
`}), Equals, `
`) } From d82344e1c3d2b710828bb2146ea3685abc0d6859 Mon Sep 17 00:00:00 2001 From: Florian Schlachter Date: Mon, 13 Feb 2017 12:42:10 +0100 Subject: [PATCH 2/4] Make linter happy and use github.com/juju/errors --- filters.go | 84 ++++++++++++++++++++++++------------------------- filters_test.go | 28 ++++++++--------- 2 files changed, 56 insertions(+), 56 deletions(-) diff --git a/filters.go b/filters.go index eea9d45..0091981 100644 --- a/filters.go +++ b/filters.go @@ -2,7 +2,6 @@ package pongo2addons import ( "bytes" - "errors" "fmt" "regexp" "strings" @@ -13,6 +12,7 @@ import ( "github.com/extemporalgenome/slug" "github.com/flosch/go-humanize" + "github.com/juju/errors" "github.com/russross/blackfriday" ) @@ -21,7 +21,7 @@ func init() { pongo2.RegisterFilter("slugify", filterSlugify) pongo2.RegisterFilter("filesizeformat", filterFilesizeformat) pongo2.RegisterFilter("truncatesentences", filterTruncatesentences) - pongo2.RegisterFilter("truncatesentences_html", filterTruncatesentencesHtml) + pongo2.RegisterFilter("truncatesentences_html", filterTruncatesentencesHTML) // Markup pongo2.RegisterFilter("markdown", filterMarkdown) @@ -59,9 +59,9 @@ func filterTruncatesentences(in *pongo2.Value, param *pongo2.Value) (*pongo2.Val } // 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) - tag_stack := make([]string, 0) + tagStack := make([]string, 0) idx := 0 for idx < vLen && !cond() { @@ -72,17 +72,17 @@ func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func( } if c == '<' { - new_output.WriteRune(c) + newOutput.WriteRune(c) idx += s // consume "<" if idx+1 < vLen { if value[idx] == '/' { // Close tag - new_output.WriteString("/") + newOutput.WriteString("/") tag := "" - idx += 1 // consume "/" + idx++ // consume "/" for idx < vLen { c2, size2 := utf8.DecodeRuneInString(value[idx:]) @@ -100,21 +100,21 @@ func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func( idx += size2 } - if len(tag_stack) > 0 { + if len(tagStack) > 0 { // 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 - for i := len(tag_stack) - 1; i >= 0; i-- { - if tag_stack[i] == tag { + for i := len(tagStack) - 1; i >= 0; i-- { + if tagStack[i] == tag { // Found the tag - tag_stack[i] = tag_stack[len(tag_stack)-1] - tag_stack = tag_stack[:len(tag_stack)-1] + tagStack[i] = tagStack[len(tagStack)-1] + tagStack = tagStack[:len(tagStack)-1] break } } } - new_output.WriteString(tag) - new_output.WriteString(">") + newOutput.WriteString(tag) + newOutput.WriteString(">") } else { // Open tag @@ -128,7 +128,7 @@ func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func( continue } - new_output.WriteRune(c2) + newOutput.WriteRune(c2) // End of tag found if c2 == '>' { @@ -148,7 +148,7 @@ func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func( } // Add tag to stack - tag_stack = append(tag_stack, tag) + tagStack = append(tagStack, tag) } } } else { @@ -158,14 +158,14 @@ func filterTruncateHtmlHelper(value string, new_output *bytes.Buffer, cond func( finalize() - for i := len(tag_stack) - 1; i >= 0; i-- { - tag := tag_stack[i] + for i := len(tagStack) - 1; i >= 0; i-- { + tag := tagStack[i] // Close everything from the regular tag stack - new_output.WriteString(fmt.Sprintf("", tag)) + newOutput.WriteString(fmt.Sprintf("", 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() if count <= 0 { return pongo2.AsValue(""), nil @@ -174,15 +174,15 @@ func filterTruncatesentencesHtml(in *pongo2.Value, param *pongo2.Value) (*pongo2 value := in.String() newLen := max(param.Integer(), 0) - new_output := bytes.NewBuffer(nil) + newOutput := bytes.NewBuffer(nil) sentencefilter := 0 - filterTruncateHtmlHelper(value, new_output, func() bool { + filterTruncateHTMLHelper(value, newOutput, func() bool { return sentencefilter >= newLen }, func(_ rune, _ int, idx int) int { // Get next word - word_found := false + wordFound := false for idx < len(value) { c2, size2 := utf8.DecodeRuneInString(value[idx:]) @@ -196,7 +196,7 @@ func filterTruncatesentencesHtml(in *pongo2.Value, param *pongo2.Value) (*pongo2 return idx } - new_output.WriteRune(c2) + newOutput.WriteRune(c2) idx += size2 if (c2 == '.' && !(idx+1 < len(value) && value[idx+1] >= '0' && value[idx+1] <= '9')) || @@ -204,35 +204,35 @@ func filterTruncatesentencesHtml(in *pongo2.Value, param *pongo2.Value) (*pongo2 // Sentence ends here, stop capturing it now break } else { - word_found = true + wordFound = true } } - if word_found { + if wordFound { sentencefilter++ } return idx }, 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) { - basetime, is_time := in.Interface().(time.Time) - if !is_time { + basetime, isTime := in.Interface().(time.Time) + if !isTime { return nil, &pongo2.Error{ Sender: "filter:timeuntil/timesince", - OrigError: errors.New("time-value is not a time.Time-instance."), + OrigError: errors.New("time-value is not a time.Time-instance"), } } var paramtime time.Time if !param.IsNil() { - paramtime, is_time = param.Interface().(time.Time) - if !is_time { + paramtime, isTime = param.Interface().(time.Time) + if !isTime { return nil, &pongo2.Error{ Sender: "filter:timeuntil/timesince", - OrigError: errors.New("time-parameter is not a time.Time-instance."), + OrigError: errors.New("time-parameter is not a time.Time-instance"), } } } else { @@ -251,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) { - basetime, is_time := in.Interface().(time.Time) - if !is_time { + basetime, isTime := in.Interface().(time.Time) + if !isTime { return nil, &pongo2.Error{ Sender: "filter:naturalday", - OrigError: errors.New("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() { - reference_time, is_time = param.Interface().(time.Time) - if !is_time { + referenceTime, isTime = param.Interface().(time.Time) + if !isTime { return nil, &pongo2.Error{ Sender: "filter:naturalday", - OrigError: errors.New("naturalday-parameter is not a time.Time-instance."), + OrigError: errors.New("naturalday-parameter is not a time.Time-instance"), } } } else { - reference_time = time.Now() + referenceTime = time.Now() } - d := reference_time.Sub(basetime) / time.Hour + d := referenceTime.Sub(basetime) / time.Hour switch { case d >= 0 && d < 24: diff --git a/filters_test.go b/filters_test.go index ae9dae0..b2b45ca 100644 --- a/filters_test.go +++ b/filters_test.go @@ -35,32 +35,32 @@ func (s *TestSuite1) TestFilters(c *C) { c.Assert(getResult("{{ 123456789|filesizeformat }}", nil), Equals, "118MiB") // Timesince/timeuntil - base_date := 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) + baseDate := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) + futureDate := baseDate.Add(24*7*4*time.Hour + 2*time.Hour) 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) - future_date = base_date.Add(2 * time.Hour) + baseDate = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) + futureDate = baseDate.Add(2 * time.Hour) 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) - future_date = base_date.Add(2 * time.Hour) + baseDate = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) + futureDate = baseDate.Add(2 * time.Hour) 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 - base_date = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) - future_date = base_date.Add(4 * time.Second) + baseDate = time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) + futureDate = baseDate.Add(4 * time.Second) 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 today := time.Date(2014, time.February, 1, 8, 30, 00, 00, time.UTC) yesterday := 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(getResult("{{ date|naturalday:today }}", pongo2.Context{"date": today, "today": today}), Equals, "today") c.Assert(getResult("{{ date|naturalday:today }}", @@ -68,7 +68,7 @@ func (s *TestSuite1) TestFilters(c *C) { c.Assert(getResult("{{ date|naturalday:today }}", pongo2.Context{"date": tomorrow, "today": today}), Equals, "tomorrow") 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 c.Assert(getResult("{{ 123456789|intcomma }}", nil), Equals, "123,456,789") From e8e71bd51ff8cd15c2b99973c9a03a25f4801662 Mon Sep 17 00:00:00 2001 From: Florian Schlachter Date: Mon, 13 Feb 2017 12:43:45 +0100 Subject: [PATCH 3/4] travis: test against Go 1.7 --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index 746ebb9..cedb85a 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: go go: - - 1.3 + - 1.7 - tip install: - go get code.google.com/p/go.tools/cmd/cover From e9edcae0c98a1afdfa968dafd7e84b1b528c656d Mon Sep 17 00:00:00 2001 From: Florian Schlachter Date: Mon, 13 Feb 2017 21:49:22 +0100 Subject: [PATCH 4/4] Fix import path for cover tool --- .travis.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.travis.yml b/.travis.yml index cedb85a..0f75eb4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,7 +4,7 @@ go: - 1.7 - tip 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 github.com/flosch/pongo2 - go get github.com/russross/blackfriday