Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix slice on multiline strings #82

Merged
merged 1 commit into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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