comment
: (`string`) The comment character used in the CSV. If set, lines beginning with the comment character without preceding whitespace are ignored.
+lazyQuotes
+: (`bool`) If true, a quote may appear in an unquoted field and a non-doubled quote may appear in a quoted field. Default is `false`.
+
```go-html-template
{{ $csv := "a;b;c" | transform.Unmarshal (dict "delimiter" ";") }}
```
"fmt"
"log"
"regexp"
+ "strconv"
"strings"
"github.com/gohugoio/hugo/common/herrors"
// Comment, if not 0, is the comment character used in the CSV decoder. Lines beginning with the
// Comment character without preceding whitespace are ignored.
Comment rune
+
+ // If true, a quote may appear in an unquoted field and a non-doubled quote
+ // may appear in a quoted field. It defaults to false.
+ LazyQuotes bool
}
// OptionsKey is used in cache keys.
var sb strings.Builder
sb.WriteRune(d.Delimiter)
sb.WriteRune(d.Comment)
+ sb.WriteString(strconv.FormatBool(d.LazyQuotes))
return sb.String()
}
r := csv.NewReader(bytes.NewReader(data))
r.Comma = d.Delimiter
r.Comment = d.Comment
+ r.LazyQuotes = d.LazyQuotes
records, err := r.ReadAll()
if err != nil {
_, err := b.BuildE()
b.Assert(err.Error(), qt.Contains, "error calling highlight: invalid Highlight option: 0")
}
+
+// Issue #11884
+func TestUnmarshalCSVLazyDecoding(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+disableKinds = ['page','rss','section','sitemap','taxonomy','term']
+-- assets/pets.csv --
+name,description,age
+Spot,a nice dog,3
+Rover,"a big dog",5
+Felix,a "malicious" cat,7
+Bella,"an "evil" cat",9
+Scar,"a "dead cat",11
+-- layouts/index.html --
+{{ $opts := dict "lazyQuotes" true }}
+{{ $data := resources.Get "pets.csv" | transform.Unmarshal $opts }}
+{{ printf "%v" $data | safeHTML }}
+ `
+ b := hugolib.Test(t, files)
+
+ b.AssertFileContent("public/index.html", `
+[[name description age] [Spot a nice dog 3] [Rover a big dog 5] [Felix a "malicious" cat 7] [Bella an "evil" cat 9] [Scar a "dead cat 11]]
+ `)
+}