Skip to content

Commit

Permalink
Fix slice on multiline strings
Browse files Browse the repository at this point in the history
  • Loading branch information
danog committed May 26, 2023
1 parent 5cbe290 commit 62c645a
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 9 deletions.
4 changes: 2 additions & 2 deletions cmd/liquid/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
//
// Examples:
//
// echo '{{ "Hello " | append: "World" }}' | liquid
// liquid source.tpl
// echo '{{ "Hello " | append: "World" }}' | liquid
// liquid source.tpl
package main

import (
Expand Down
1 change: 0 additions & 1 deletion engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ func (e *Engine) RegisterBlock(name string, td Renderer) {
// * https://github.com/osteele/liquid/blob/master/filters/filters.go
//
// * https://github.com/osteele/gojekyll/blob/master/filters/filters.go
//
func (e *Engine) RegisterFilter(name string, fn interface{}) {
e.cfg.AddFilter(name, fn)
}
Expand Down
12 changes: 7 additions & 5 deletions filters/standard_filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"strings"
"time"
"unicode"
"unicode/utf8"

"github.com/osteele/liquid/values"
"github.com/osteele/tuesday"
Expand Down Expand Up @@ -147,13 +146,16 @@ func AddStandardFilters(fd FilterDictionary) { // nolint: gocyclo
})
fd.AddFilter("sort_natural", sortNaturalFilter)
fd.AddFilter("slice", func(s string, start int, length func(int) int) string {
// runes aren't bytes; don't use slice
ss := []rune(s)
n := length(1)
if start < 0 {
start = utf8.RuneCountInString(s) + start
start = len(ss) + start
}
end := start + n
if end > len(ss) {
end = len(ss)
}
p := regexp.MustCompile(fmt.Sprintf(`^.{%d}(.{0,%d}).*$`, start, n))
return p.ReplaceAllString(s, "$1")
return string(ss[start:end])
})
fd.AddFilter("split", splitFilter)
fd.AddFilter("strip_html", func(s string) string {
Expand Down
4 changes: 4 additions & 0 deletions filters/standard_filters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,12 @@ var filterTests = []struct {
{`"I strained to see the train through the rain" | remove_first: "rain"`, "I sted to see the train through the rain"},

{`"Liquid" | slice: 0`, "L"},
{`"Liquid
Liquid" | slice: 0`, "L"},
{`"Liquid" | slice: 2`, "q"},
{`"Liquid" | slice: 2, 5`, "quid"},
{`"Liquid
Liquid" | slice: 2, 4`, "quid"},
{`"Liquid" | slice: -3, 2`, "ui"},

{`"a/b/c" | split: '/' | join: '-'`, "a-b-c"},
Expand Down
1 change: 0 additions & 1 deletion liquid.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ Package liquid is a pure Go implementation of Shopify Liquid templates, develope
See the project README https://github.com/osteele/liquid for additional information and implementation status.
The liquid package itself is versioned in gopkg.in. Subpackages have no compatibility guarantees. Except where specifically documented, the “public” entities of subpackages are intended only for use by the liquid package and its subpackages.
*/
package liquid
Expand Down

0 comments on commit 62c645a

Please sign in to comment.