mirror of
https://github.com/gohugoio/hugo.git
synced 2026-08-24 15:28:54 +00:00
@@ -16,6 +16,7 @@ package encoding
|
||||
|
||||
import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"html/template"
|
||||
@@ -56,6 +57,28 @@ func (ns *Namespace) Base64Encode(content any) (string, error) {
|
||||
return base64.StdEncoding.EncodeToString([]byte(conv)), nil
|
||||
}
|
||||
|
||||
// HexDecode returns the hex decoding of the given content.
|
||||
func (ns *Namespace) HexDecode(content any) (string, error) {
|
||||
conv, err := cast.ToStringE(content)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
b, err := hex.DecodeString(conv)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(b), nil
|
||||
}
|
||||
|
||||
// HexEncode returns the hex encoding of the given content.
|
||||
func (ns *Namespace) HexEncode(content any) (string, error) {
|
||||
conv, err := cast.ToStringE(content)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return hex.EncodeToString([]byte(conv)), nil
|
||||
}
|
||||
|
||||
// Jsonify encodes a given object to JSON. To pretty print the JSON, pass a map
|
||||
// or dictionary of options as the first value in args. Supported options are
|
||||
// "prefix" and "indent". Each JSON element in the output will begin on a new
|
||||
|
||||
@@ -77,6 +77,60 @@ func TestBase64Encode(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexDecode(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
ns := New()
|
||||
|
||||
for _, test := range []struct {
|
||||
v any
|
||||
expect any
|
||||
}{
|
||||
{"616263313233213f242a2628292d3d407e", "abc123!?$*&()-=@~"},
|
||||
// errors
|
||||
{t, false},
|
||||
} {
|
||||
|
||||
result, err := ns.HexDecode(test.v)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
continue
|
||||
}
|
||||
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(result, qt.Equals, test.expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestHexEncode(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
ns := New()
|
||||
|
||||
for _, test := range []struct {
|
||||
v any
|
||||
expect any
|
||||
}{
|
||||
{"abc123!?$*&()-=@~", "616263313233213f242a2628292d3d407e"},
|
||||
// errors
|
||||
{t, false},
|
||||
} {
|
||||
|
||||
result, err := ns.HexEncode(test.v)
|
||||
|
||||
if b, ok := test.expect.(bool); ok && !b {
|
||||
c.Assert(err, qt.Not(qt.IsNil))
|
||||
continue
|
||||
}
|
||||
|
||||
c.Assert(err, qt.IsNil)
|
||||
c.Assert(result, qt.Equals, test.expect)
|
||||
}
|
||||
}
|
||||
|
||||
func TestJsonify(t *testing.T) {
|
||||
t.Parallel()
|
||||
c := qt.New(t)
|
||||
|
||||
@@ -46,6 +46,20 @@ func init() {
|
||||
},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.HexDecode,
|
||||
nil,
|
||||
[][2]string{
|
||||
{`{{ "48656c6c6f20776f726c64" | encoding.HexDecode }}`, `Hello world`},
|
||||
},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.HexEncode,
|
||||
nil,
|
||||
[][2]string{
|
||||
{`{{ "Hello world" | encoding.HexEncode }}`, `48656c6c6f20776f726c64`},
|
||||
},
|
||||
)
|
||||
|
||||
ns.AddMethodMapping(ctx.Jsonify,
|
||||
[]string{"jsonify"},
|
||||
[][2]string{
|
||||
|
||||
Reference in New Issue
Block a user