From 62c645a48acd0a3af136ddf20115eba0f1c035ff Mon Sep 17 00:00:00 2001 From: Daniil Gentili Date: Fri, 26 May 2023 23:35:00 +0200 Subject: [PATCH] Fix slice on multiline strings --- cmd/liquid/main.go | 4 ++-- engine.go | 1 - filters/standard_filters.go | 12 +++++++----- filters/standard_filters_test.go | 4 ++++ liquid.go | 1 - 5 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cmd/liquid/main.go b/cmd/liquid/main.go index 2b59d00..62f382d 100644 --- a/cmd/liquid/main.go +++ b/cmd/liquid/main.go @@ -4,8 +4,8 @@ // // Examples: // -// echo '{{ "Hello " | append: "World" }}' | liquid -// liquid source.tpl +// echo '{{ "Hello " | append: "World" }}' | liquid +// liquid source.tpl package main import ( diff --git a/engine.go b/engine.go index d143364..9991b1e 100644 --- a/engine.go +++ b/engine.go @@ -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) } diff --git a/filters/standard_filters.go b/filters/standard_filters.go index fa1e6ac..6eb9458 100644 --- a/filters/standard_filters.go +++ b/filters/standard_filters.go @@ -12,7 +12,6 @@ import ( "strings" "time" "unicode" - "unicode/utf8" "github.com/osteele/liquid/values" "github.com/osteele/tuesday" @@ -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 { diff --git a/filters/standard_filters_test.go b/filters/standard_filters_test.go index 17bc031..0b5b17d 100644 --- a/filters/standard_filters_test.go +++ b/filters/standard_filters_test.go @@ -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"}, diff --git a/liquid.go b/liquid.go index 341e11b..249492d 100644 --- a/liquid.go +++ b/liquid.go @@ -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