-
Notifications
You must be signed in to change notification settings - Fork 59
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
174 additions
and
122 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
package filters | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/osteele/liquid/expressions" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func init() { | ||
DefineStandardFilters() | ||
} | ||
|
||
var filterTests = []struct{ in, expected string }{ | ||
// Jekyll extensions | ||
{`obj | inspect`, `{"a":1}`}, | ||
|
||
// filters | ||
// product_price | default: 2.99 }} | ||
|
||
// list filters | ||
// site.pages | map: 'category' | compact | join "," %} | ||
// {% assign my_array = "apples, oranges, peaches, plums" | split: ", " %}my_array.first }} | ||
{`"John, Paul, George, Ringo" | split: ", " | join: " and "`, "John and Paul and George and Ringo"}, | ||
{`animals | sort | join: ", "`, "Sally Snake, giraffe, octopus, zebra"}, | ||
{`sort_prop | sort: "weight" | inspect`, `[{"weight":null},{"weight":1},{"weight":3},{"weight":5}]`}, | ||
|
||
// last, map, slice, sort_natural, reverse, size, uniq | ||
|
||
// string filters | ||
// "/my/fancy/url" | append: ".html" | ||
// {% assign filename = "/index.html" %}"website.com" | append: filename | ||
|
||
// "title" | capitalize | ||
// "my great title" | capitalize | ||
|
||
// "Parker Moore" | downcase | ||
|
||
// "Have you read 'James & the Giant Peach'?" | escape | ||
// "1 < 2 & 3" | escape_once | ||
// "1 < 2 & 3" | escape_once | ||
|
||
// lstrip, newline_to_br, prepend, remove, remove_first, replace, replace_first | ||
// rstrip, split, strip, strip_html, strip_newlines, truncate, truncatewords, upcase | ||
// url_decode, url_encode | ||
|
||
// number filters | ||
// -17 | abs | ||
// 4 | abs | ||
// "-19.86" | abs | ||
|
||
// 1.2 | ceil | ||
// 2.0 | ceil | ||
// 183.357 | ceil | ||
// "3.5" | ceil | ||
|
||
// 16 | divided_by: 4 | ||
// 5 | divided_by: 3 | ||
// 20 | divided_by: 7.0 | ||
|
||
// 1.2 | floor | ||
// 2.0 | floor | ||
// 183.357 | floor | ||
// minus, modulo, plus, round,times | ||
|
||
// date filters | ||
// article.published_at | date: "%a, %b %d, %y" | ||
// article.published_at | date: "%Y" | ||
// "March 14, 2016" | date: "%b %d, %y" | ||
// "now" | date: "%Y-%m-%d %H:%M" } | ||
} | ||
|
||
var filterTestContext = expressions.NewContext(map[string]interface{}{ | ||
"x": 123, | ||
"obj": map[string]interface{}{ | ||
"a": 1, | ||
}, | ||
"animals": []string{"zebra", "octopus", "giraffe", "Sally Snake"}, | ||
"pages": []map[string]interface{}{ | ||
{"category": "business"}, | ||
{"category": "celebrities"}, | ||
{}, | ||
{"category": "lifestyle"}, | ||
{"category": "sports"}, | ||
{}, | ||
{"category": "technology"}, | ||
}, | ||
"sort_prop": []map[string]interface{}{ | ||
{"weight": 1}, | ||
{"weight": 5}, | ||
{"weight": 3}, | ||
{"weight": nil}, | ||
}, | ||
"ar": []string{"first", "second", "third"}, | ||
"page": map[string]interface{}{ | ||
"title": "Introduction", | ||
}, | ||
}) | ||
|
||
func TestFilters(t *testing.T) { | ||
for i, test := range filterTests { | ||
t.Run(fmt.Sprintf("%02d", i), func(t *testing.T) { | ||
val, err := expressions.EvaluateExpr(test.in, filterTestContext) | ||
require.NoErrorf(t, err, test.in) | ||
actual := fmt.Sprintf("%s", val) | ||
require.Equalf(t, test.expected, actual, test.in) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
// The filters package defines the standard Liquid filters. | ||
package filters | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"strings" | ||
|
||
"github.com/osteele/liquid/expressions" | ||
"github.com/osteele/liquid/generics" | ||
) | ||
|
||
// DefineStandardFilters defines the standard Liquid filters. | ||
func DefineStandardFilters() { | ||
// lists | ||
expressions.DefineFilter("join", joinFilter) | ||
expressions.DefineFilter("sort", sortFilter) | ||
|
||
// strings | ||
expressions.DefineFilter("split", splitFilter) | ||
|
||
// Jekyll | ||
expressions.DefineFilter("inspect", json.Marshal) | ||
} | ||
|
||
func joinFilter(in []interface{}, sep interface{}) interface{} { | ||
a := make([]string, len(in)) | ||
s := ", " | ||
if sep != nil { | ||
s = fmt.Sprint(sep) | ||
} | ||
for i, x := range in { | ||
a[i] = fmt.Sprint(x) | ||
} | ||
return strings.Join(a, s) | ||
} | ||
|
||
func sortFilter(in []interface{}, key interface{}) []interface{} { | ||
out := make([]interface{}, len(in)) | ||
for i, v := range in { | ||
out[i] = v | ||
} | ||
if key == nil { | ||
generics.Sort(out) | ||
} else { | ||
generics.SortByProperty(out, key.(string)) | ||
} | ||
return out | ||
} | ||
|
||
func splitFilter(in, sep string) interface{} { | ||
return strings.Split(in, sep) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters