1
0
mirror of https://github.com/danog/liquid.git synced 2024-11-30 10:48:59 +01:00

More filters

This commit is contained in:
Oliver Steele 2017-06-28 15:33:13 -04:00
parent af8c48608e
commit c433c0894e
3 changed files with 34 additions and 14 deletions

View File

@ -49,6 +49,7 @@ var filterTests = []struct {
{`"apples, oranges, peaches, plums" | split: ", " | size`, 4},
// string filters
// TODO escape, truncatewords, url_decode, url_encode
{`"Take my protein pills and put my helmet on" | replace: "my", "your"`, "Take your protein pills and put your helmet on"},
{`"Take my protein pills and put my helmet on" | replace_first: "my", "your"`, "Take your protein pills and put my helmet on"},
{`"/my/fancy/url" | append: ".html"`, "/my/fancy/url.html"},
@ -56,10 +57,9 @@ var filterTests = []struct {
{`"title" | capitalize`, "Title"},
{`"my great title" | capitalize`, "My great title"},
{`"Parker Moore" | downcase`, "parker moore"},
{`"Parker Moore" | upcase`, "PARKER MOORE"},
{`" So much room for activities! " | strip`, "So much room for activities!"},
{`" So much room for activities! " | lstrip`, "So much room for activities! "},
{`" So much room for activities! " | rstrip`, " So much room for activities!"},
{`"Have you read 'James & the Giant Peach'?" | escape`, "Have you read 'James & the Giant Peach'?"},
{`"1 < 2 & 3" | escape_once`, "1 &lt; 2 &amp; 3"},
{`"1 &lt; 2 &amp; 3" | escape_once`, "1 &lt; 2 &amp; 3"},
{`"apples, oranges, and bananas" | prepend: "Some fruit: "`, "Some fruit: apples, oranges, and bananas"},
{`"I strained to see the train through the rain" | remove: "rain"`, "I sted to see the t through the "},
{`"I strained to see the train through the rain" | remove_first: "rain"`, "I sted to see the train through the rain"},
@ -67,13 +67,14 @@ var filterTests = []struct {
{`"Liquid" | slice: 2`, "q"},
{`"Liquid" | slice: 2, 5`, "quid"},
{`"Liquid" | slice: -3, 2`, "ui"},
{`"Have <em>you</em> read <strong>Ulysses</strong>?" | strip_html`, "Have you read Ulysses?"},
{`"Ground control to Major Tom." | truncate: 20`, "Ground control to..."},
{`"Ground control to Major Tom." | truncate: 25, ", and so on"`, "Ground control, and so on"},
{`"Ground control to Major Tom." | truncate: 20, ""`, "Ground control to Ma"},
// TODO escape, newline_to_br, strip_html, strip_newlines, truncatewords, url_decode, url_encode
// {`"Have you read 'James & the Giant Peach'?" | escape`, ""},
// {`"1 < 2 & 3" | escape_once`, ""},
// {`"1 &lt; 2 &amp; 3" | escape_once`, ""},
{`"Parker Moore" | upcase`, "PARKER MOORE"},
{`" So much room for activities! " | strip`, "So much room for activities!"},
{`" So much room for activities! " | lstrip`, "So much room for activities! "},
{`" So much room for activities! " | rstrip`, " So much room for activities!"},
// number filters
{`-17 | abs`, 17},

View File

@ -4,7 +4,9 @@ package filters
import (
"encoding/json"
"fmt"
"html"
"math"
"regexp"
"strings"
"time"
"unicode"
@ -89,11 +91,14 @@ func DefineStandardFilters() {
expressions.DefineFilter("downcase", func(s, suffix string) string {
return strings.ToLower(s)
})
// expressions.DefineFilter("escape", func(s, suffix string) string {
// buf := new(bytes.Buffer)
// template.HTMLEscape(buf, []byte(s))
// return buf.String()
// })
expressions.DefineFilter("escape", html.EscapeString)
expressions.DefineFilter("escape_once", func(s, suffix string) string {
return html.EscapeString(html.UnescapeString(s))
})
// TODO test case for this
expressions.DefineFilter("newline_to_br", func(s string) string {
return strings.Replace(s, "\n", "<br />", -1)
})
expressions.DefineFilter("prepend", func(s, prefix string) string {
return prefix + s
})
@ -126,6 +131,14 @@ func DefineStandardFilters() {
return s[start : start+n]
})
expressions.DefineFilter("split", splitFilter)
expressions.DefineFilter("strip_html", func(s string) string {
// TODO this probably isn't sufficient
return regexp.MustCompile(`<.*?>`).ReplaceAllString(s, "")
})
// TODO test case for this
expressions.DefineFilter("strip_newlines", func(s string) string {
return strings.Replace(s, "\n", "", -1)
})
expressions.DefineFilter("strip", strings.TrimSpace)
expressions.DefineFilter("lstrip", func(s string) string {
return strings.TrimLeftFunc(s, unicode.IsSpace)

View File

@ -57,8 +57,14 @@ func Convert(value interface{}, t reflect.Type) reflect.Value {
var dateLayouts = []string{
"2006-01-02 15:04:05 -07:00",
"January 2, 2006",
"2006-01-02 15:04:05 -4",
"2006-01-02 15:04:05",
"2006-01-02 15:04",
"2006-01-02",
"January 2, 2006",
"January 2 2006",
"Jan 2, 2006",
"Jan 2 2006",
}
// ParseTime tries a few heuristics to parse a date from a string