github.com/linchen2chris/hugo@v0.0.0-20230307053224-cec209389705/tpl/encoding/encoding.go (about)

     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/json"
    20  	"errors"
    21  	"html/template"
    22  
    23  	bp "github.com/gohugoio/hugo/bufferpool"
    24  
    25  	"github.com/gohugoio/hugo/common/maps"
    26  	"github.com/mitchellh/mapstructure"
    27  	"github.com/spf13/cast"
    28  )
    29  
    30  // New returns a new instance of the encoding-namespaced template functions.
    31  func New() *Namespace {
    32  	return &Namespace{}
    33  }
    34  
    35  // Namespace provides template functions for the "encoding" namespace.
    36  type Namespace struct{}
    37  
    38  // Base64Decode returns the base64 decoding of the given content.
    39  func (ns *Namespace) Base64Decode(content any) (string, error) {
    40  	conv, err := cast.ToStringE(content)
    41  	if err != nil {
    42  		return "", err
    43  	}
    44  
    45  	dec, err := base64.StdEncoding.DecodeString(conv)
    46  	return string(dec), err
    47  }
    48  
    49  // Base64Encode returns the base64 encoding of the given content.
    50  func (ns *Namespace) Base64Encode(content any) (string, error) {
    51  	conv, err := cast.ToStringE(content)
    52  	if err != nil {
    53  		return "", err
    54  	}
    55  
    56  	return base64.StdEncoding.EncodeToString([]byte(conv)), nil
    57  }
    58  
    59  // Jsonify encodes a given object to JSON.  To pretty print the JSON, pass a map
    60  // or dictionary of options as the first value in args.  Supported options are
    61  // "prefix" and "indent".  Each JSON element in the output will begin on a new
    62  // line beginning with prefix followed by one or more copies of indent according
    63  // to the indentation nesting.
    64  func (ns *Namespace) Jsonify(args ...any) (template.HTML, error) {
    65  	var (
    66  		b    []byte
    67  		err  error
    68  		obj  any
    69  		opts jsonifyOpts
    70  	)
    71  
    72  	switch len(args) {
    73  	case 0:
    74  		return "", nil
    75  	case 1:
    76  		obj = args[0]
    77  	case 2:
    78  		var m map[string]any
    79  		m, err = maps.ToStringMapE(args[0])
    80  		if err != nil {
    81  			break
    82  		}
    83  		if err = mapstructure.WeakDecode(m, &opts); err != nil {
    84  			break
    85  		}
    86  		obj = args[1]
    87  	default:
    88  		err = errors.New("too many arguments to jsonify")
    89  	}
    90  
    91  	if err != nil {
    92  		return "", err
    93  	}
    94  
    95  	buff := bp.GetBuffer()
    96  	defer bp.PutBuffer(buff)
    97  	e := json.NewEncoder(buff)
    98  	e.SetEscapeHTML(!opts.NoHTMLEscape)
    99  	e.SetIndent(opts.Prefix, opts.Indent)
   100  	if err = e.Encode(obj); err != nil {
   101  		return "", err
   102  	}
   103  	b = buff.Bytes()
   104  	// See https://github.com/golang/go/issues/37083
   105  	// Hugo changed from MarshalIndent/Marshal. To make the output
   106  	// the same, we need to trim the trailing newline.
   107  	b = b[:len(b)-1]
   108  
   109  	return template.HTML(b), nil
   110  }
   111  
   112  type jsonifyOpts struct {
   113  	Prefix       string
   114  	Indent       string
   115  	NoHTMLEscape bool
   116  }