github.com/anakojm/hugo-katex@v0.0.0-20231023141351-42d6f5de9c0b/tpl/fmt/fmt.go (about)

     1  // Copyright 2017 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 fmt provides template functions for formatting strings.
    15  package fmt
    16  
    17  import (
    18  	_fmt "fmt"
    19  	"sort"
    20  
    21  	"github.com/bep/logg"
    22  	"github.com/gohugoio/hugo/common/loggers"
    23  	"github.com/gohugoio/hugo/deps"
    24  	"github.com/spf13/cast"
    25  )
    26  
    27  // New returns a new instance of the fmt-namespaced template functions.
    28  func New(d *deps.Deps) *Namespace {
    29  	ns := &Namespace{
    30  		logger: d.Log,
    31  	}
    32  
    33  	d.BuildStartListeners.Add(func() {
    34  		ns.logger.Reset()
    35  	})
    36  
    37  	return ns
    38  }
    39  
    40  // Namespace provides template functions for the "fmt" namespace.
    41  type Namespace struct {
    42  	logger loggers.Logger
    43  }
    44  
    45  // Print returns a string representation of args.
    46  func (ns *Namespace) Print(args ...any) string {
    47  	return _fmt.Sprint(args...)
    48  }
    49  
    50  // Printf returns string representation of args formatted with the layouut in format.
    51  func (ns *Namespace) Printf(format string, args ...any) string {
    52  	return _fmt.Sprintf(format, args...)
    53  }
    54  
    55  // Println returns string representation of args  ending with a newline.
    56  func (ns *Namespace) Println(args ...any) string {
    57  	return _fmt.Sprintln(args...)
    58  }
    59  
    60  // Errorf formats args according to a format specifier and logs an ERROR.
    61  // It returns an empty string.
    62  func (ns *Namespace) Errorf(format string, args ...any) string {
    63  	ns.logger.Errorf(format, args...)
    64  	return ""
    65  }
    66  
    67  // Erroridf formats args according to a format specifier and logs an ERROR and
    68  // an information text that the error with the given id can be suppressed in config.
    69  // It returns an empty string.
    70  func (ns *Namespace) Erroridf(id, format string, args ...any) string {
    71  	ns.logger.Errorsf(id, format, args...)
    72  	return ""
    73  }
    74  
    75  // Warnf formats args according to a format specifier and logs a WARNING.
    76  // It returns an empty string.
    77  func (ns *Namespace) Warnf(format string, args ...any) string {
    78  	ns.logger.Warnf(format, args...)
    79  	return ""
    80  }
    81  
    82  // Warnmf is epxermimental and subject to change at any time.
    83  func (ns *Namespace) Warnmf(m any, format string, args ...any) string {
    84  	return ns.logmf(ns.logger.Warn(), m, format, args...)
    85  }
    86  
    87  // Errormf is epxermimental and subject to change at any time.
    88  func (ns *Namespace) Errormf(m any, format string, args ...any) string {
    89  	return ns.logmf(ns.logger.Error(), m, format, args...)
    90  }
    91  
    92  func (ns *Namespace) logmf(l logg.LevelLogger, m any, format string, args ...any) string {
    93  	mm := cast.ToStringMap(m)
    94  	fields := make(logg.Fields, len(mm))
    95  	i := 0
    96  	for k, v := range mm {
    97  		fields[i] = logg.Field{Name: k, Value: v}
    98  		i++
    99  	}
   100  	// Sort the fields to make the output deterministic.
   101  	sort.Slice(fields, func(i, j int) bool {
   102  		return fields[i].Name < fields[j].Name
   103  	})
   104  
   105  	l.WithFields(fields).Logf(format, args...)
   106  
   107  	return ""
   108  }