tpl: Extend Jsonify to support options map
authorCameron Moore <moorereason@gmail.com>
Tue, 24 Mar 2020 02:03:52 +0000 (21:03 -0500)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 7 Apr 2020 18:01:57 +0000 (20:01 +0200)
Add support for prefix and indent options used by json.MarshalIndent
from the Go stdlib.

tpl/encoding/encoding.go
tpl/encoding/encoding_test.go
tpl/encoding/init.go

index 48699b52def79648a76225d2cd35846a8d740051..09e2b94bcce3f9474c37b3e1e95408f4147beac0 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2017 The Hugo Authors. All rights reserved.
+// Copyright 2020 The Hugo Authors. All rights reserved.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -52,8 +52,11 @@ func (ns *Namespace) Base64Encode(content interface{}) (string, error) {
        return base64.StdEncoding.EncodeToString([]byte(conv)), nil
 }
 
-// Jsonify encodes a given object to JSON.  To pretty print the JSON, pass an
-// optional first argument of the indent string, such as "  ".
+// Jsonify encodes a given object to JSON.  To pretty print the JSON, pass a map
+// or dictionary of options as the first argument.  Supported options are
+// "prefix" and "indent".  Each JSON element in the output will begin on a new
+// line beginning with prefix followed by one or more copies of indent according
+// to the indentation nesting.
 func (ns *Namespace) Jsonify(args ...interface{}) (template.HTML, error) {
        var (
                b   []byte
@@ -66,14 +69,14 @@ func (ns *Namespace) Jsonify(args ...interface{}) (template.HTML, error) {
        case 1:
                b, err = json.Marshal(args[0])
        case 2:
-               var indent string
+               var opts map[string]string
 
-               indent, err = cast.ToStringE(args[0])
+               opts, err = cast.ToStringMapStringE(args[0])
                if err != nil {
                        break
                }
 
-               b, err = json.MarshalIndent(args[1], "", indent)
+               b, err = json.MarshalIndent(args[1], opts["prefix"], opts["indent"])
        default:
                err = errors.New("too many arguments to jsonify")
        }
index 2f0988ff37d610b204ed52448439fcb308f8e526..815aa2613110f7db7a636ba8fb5e5a569952566f 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2017 The Hugo Authors. All rights reserved.
+// Copyright 2020 The Hugo Authors. All rights reserved.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -83,20 +83,27 @@ func TestJsonify(t *testing.T) {
        ns := New()
 
        for _, test := range []struct {
-               indent []interface{}
+               opts   interface{}
                v      interface{}
                expect interface{}
        }{
                {nil, []string{"a", "b"}, template.HTML(`["a","b"]`)},
-               {[]interface{}{"  "}, []string{"a", "b"}, template.HTML("[\n  \"a\",\n  \"b\"\n]")},
+               {map[string]string{"indent": "<i>"}, []string{"a", "b"}, template.HTML("[\n<i>\"a\",\n<i>\"b\"\n]")},
+               {map[string]string{"prefix": "<p>"}, []string{"a", "b"}, template.HTML("[\n<p>\"a\",\n<p>\"b\"\n<p>]")},
+               {map[string]string{"prefix": "<p>", "indent": "<i>"}, []string{"a", "b"}, template.HTML("[\n<p><i>\"a\",\n<p><i>\"b\"\n<p>]")},
                {nil, tstNoStringer{}, template.HTML("{}")},
                {nil, nil, template.HTML("null")},
                // errors
                {nil, math.NaN(), false},
-               {[]interface{}{tstNoStringer{}}, []string{"a", "b"}, false},
+               {tstNoStringer{}, []string{"a", "b"}, false},
        } {
+               args := []interface{}{}
 
-               args := append(test.indent, test.v)
+               if test.opts != nil {
+                       args = append(args, test.opts)
+               }
+
+               args = append(args, test.v)
 
                result, err := ns.Jsonify(args...)
 
index 8ec9f1a587e128342e8653ccdcf8a1eb7640fbfc..f97b171826c504760c5aed650321100aba68cf2b 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2017 The Hugo Authors. All rights reserved.
+// Copyright 2020 The Hugo Authors. All rights reserved.
 //
 // Licensed under the Apache License, Version 2.0 (the "License");
 // you may not use this file except in compliance with the License.
@@ -48,7 +48,7 @@ func init() {
                        []string{"jsonify"},
                        [][2]string{
                                {`{{ (slice "A" "B" "C") | jsonify }}`, `["A","B","C"]`},
-                               {`{{ (slice "A" "B" "C") | jsonify "  "}}`, "[\n  \"A\",\n  \"B\",\n  \"C\"\n]"},
+                               {`{{ (slice "A" "B" "C") | jsonify (dict "indent" "  ") }}`, "[\n  \"A\",\n  \"B\",\n  \"C\"\n]"},
                        },
                )