Make linter happy and use github.com/juju/errors
This commit is contained in:
parent
bd3adb0e5e
commit
d82344e1c3
84
filters.go
84
filters.go
@ -2,7 +2,6 @@ package pongo2addons
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
@ -13,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"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -21,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)
|
||||||
@ -59,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() {
|
||||||
@ -72,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:])
|
||||||
@ -100,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
|
||||||
|
|
||||||
@ -128,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 == '>' {
|
||||||
@ -148,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 {
|
||||||
@ -158,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
|
||||||
@ -174,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:])
|
||||||
@ -196,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')) ||
|
||||||
@ -204,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",
|
||||||
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
|
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",
|
||||||
OrigError: errors.New("time-parameter is not a time.Time-instance."),
|
OrigError: errors.New("time-parameter is not a time.Time-instance"),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} 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) {
|
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",
|
||||||
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() {
|
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",
|
||||||
OrigError: errors.New("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:
|
||||||
|
@ -35,32 +35,32 @@ func (s *TestSuite1) TestFilters(c *C) {
|
|||||||
c.Assert(getResult("{{ 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(getResult("{{ 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(getResult("{{ 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(getResult("{{ 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(getResult("{{ 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(getResult("{{ 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(getResult("{{ date|naturalday:today }}",
|
c.Assert(getResult("{{ date|naturalday:today }}",
|
||||||
@ -68,7 +68,7 @@ func (s *TestSuite1) TestFilters(c *C) {
|
|||||||
c.Assert(getResult("{{ 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(getResult("{{ 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(getResult("{{ 123456789|intcomma }}", nil), Equals, "123,456,789")
|
c.Assert(getResult("{{ 123456789|intcomma }}", nil), Equals, "123,456,789")
|
||||||
|
Loading…
Reference in New Issue
Block a user