Debian Codegraph @

tpl/encoding/encoding.go

raw
1 // Copyright 2020 The Hugo Authors. All rights reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 // http://www.apache.org/licenses/LICENSE-2.0
7 //
8 // Unless required by applicable law or agreed to in writing, software
9 // distributed under the License is distributed on an "AS IS" BASIS,
10 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11 // See the License for the specific language governing permissions and
12 // limitations under the License.
13
14 // Package encoding provides template functions for encoding content.
15 package encoding
16
17 import (
18 "encoding/base64"
19 "encoding/hex"
20 "encoding/json"
21 "errors"
22 "html/template"
23
24 bp "github.com/gohugoio/hugo/bufferpool"
25 "github.com/gohugoio/hugo/common/hmaps"
26
27 "github.com/mitchellh/mapstructure"
28 "github.com/spf13/cast"
29 )
30
31 // New returns a new instance of the encoding-namespaced template functions.
32 func New() *Namespace {
33 return &Namespace{}
34 }
35
36 // Namespace provides template functions for the "encoding" namespace.
37 type Namespace struct{}
38
39 // Base64Decode returns the base64 decoding of the given content.
40 func (ns *Namespace) Base64Decode(content any) (string, error) {
41 conv, err := cast.ToStringE(content)
42 if err != nil {
43 return "", err
44 }
45
46 dec, err := base64.StdEncoding.DecodeString(conv)
47 return string(dec), err
48 }
49
50 // Base64Encode returns the base64 encoding of the given content.
51 func (ns *Namespace) Base64Encode(content any) (string, error) {
52 conv, err := cast.ToStringE(content)
53 if err != nil {
54 return "", err
55 }
56
57 return base64.StdEncoding.EncodeToString([]byte(conv)), nil
58 }
59
60 // HexDecode returns the hex decoding of the given content.
61 func (ns *Namespace) HexDecode(content any) (string, error) {
62 conv, err := cast.ToStringE(content)
63 if err != nil {
64 return "", err
65 }
66 b, err := hex.DecodeString(conv)
67 if err != nil {
68 return "", err
69 }
70 return string(b), nil
71 }
72
73 // HexEncode returns the hex encoding of the given content.
74 func (ns *Namespace) HexEncode(content any) (string, error) {
75 conv, err := cast.ToStringE(content)
76 if err != nil {
77 return "", err
78 }
79 return hex.EncodeToString([]byte(conv)), nil
80 }
81
82 // Jsonify encodes a given object to JSON. To pretty print the JSON, pass a map
83 // or dictionary of options as the first value in args. Supported options are
84 // "prefix" and "indent". Each JSON element in the output will begin on a new
85 // line beginning with prefix followed by one or more copies of indent according
86 // to the indentation nesting.
87 func (ns *Namespace) Jsonify(args ...any) (template.HTML, error) {
88 var (
89 b []byte
90 err error
91 obj any
92 opts jsonifyOpts
93 )
94
95 switch len(args) {
96 case 0:
97 return "", nil
98 case 1:
99 obj = args[0]
100 case 2:
101 var m map[string]any
102 m, err = hmaps.ToStringMapE(args[0])
103 if err != nil {
104 break
105 }
106 if err = mapstructure.WeakDecode(m, &opts); err != nil {
107 break
108 }
109 obj = args[1]
110 default:
111 err = errors.New("too many arguments to jsonify")
112 }
113
114 if err != nil {
115 return "", err
116 }
117
118 buff := bp.GetBuffer()
119 defer bp.PutBuffer(buff)
120 e := json.NewEncoder(buff)
121 e.SetEscapeHTML(!opts.NoHTMLEscape)
122 e.SetIndent(opts.Prefix, opts.Indent)
123 if err = e.Encode(obj); err != nil {
124 return "", err
125 }
126 b = buff.Bytes()
127 // See https://github.com/golang/go/issues/37083
128 // Hugo changed from MarshalIndent/Marshal. To make the output
129 // the same, we need to trim the trailing newline.
130 b = b[:len(b)-1]
131
132 return template.HTML(b), nil
133 }
134
135 type jsonifyOpts struct {
136 Prefix string
137 Indent string
138 NoHTMLEscape bool
139 }