hugo

Fork of github.com/gohugoio/hugo with reverse pagination support

git clone git://git.shimmy1996.com/hugo.git

init.go (2399B)

    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 time
   15 
   16 import (
   17 	"errors"
   18 
   19 	"github.com/gohugoio/hugo/deps"
   20 	"github.com/gohugoio/hugo/langs"
   21 	"github.com/gohugoio/hugo/tpl/internal"
   22 )
   23 
   24 const name = "time"
   25 
   26 func init() {
   27 	f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
   28 		if d.Language == nil {
   29 			panic("Language must be set")
   30 		}
   31 		ctx := New(langs.GetTimeFormatter(d.Language), langs.GetLocation(d.Language))
   32 
   33 		ns := &internal.TemplateFuncsNamespace{
   34 			Name: name,
   35 			Context: func(args ...any) (any, error) {
   36 				// Handle overlapping "time" namespace and func.
   37 				//
   38 				// If no args are passed to `time`, assume namespace usage and
   39 				// return namespace context.
   40 				//
   41 				// If args are passed, call AsTime().
   42 
   43 				switch len(args) {
   44 				case 0:
   45 					return ctx, nil
   46 				case 1:
   47 					return ctx.AsTime(args[0])
   48 				case 2:
   49 					return ctx.AsTime(args[0], args[1])
   50 
   51 				// 3 or more arguments. Currently not supported.
   52 				default:
   53 					return nil, errors.New("Invalid arguments supplied to `time`. Refer to time documentation: https://gohugo.io/functions/time/")
   54 				}
   55 			},
   56 		}
   57 
   58 		ns.AddMethodMapping(ctx.Format,
   59 			[]string{"dateFormat"},
   60 			[][2]string{
   61 				{`dateFormat: {{ dateFormat "Monday, Jan 2, 2006" "2015-01-21" }}`, `dateFormat: Wednesday, Jan 21, 2015`},
   62 			},
   63 		)
   64 
   65 		ns.AddMethodMapping(ctx.Now,
   66 			[]string{"now"},
   67 			[][2]string{},
   68 		)
   69 
   70 		ns.AddMethodMapping(ctx.AsTime,
   71 			nil,
   72 			[][2]string{
   73 				{`{{ (time "2015-01-21").Year }}`, `2015`},
   74 			},
   75 		)
   76 
   77 		ns.AddMethodMapping(ctx.Duration,
   78 			[]string{"duration"},
   79 			[][2]string{
   80 				{`{{ mul 60 60 | duration "second" }}`, `1h0m0s`},
   81 			},
   82 		)
   83 
   84 		ns.AddMethodMapping(ctx.ParseDuration,
   85 			nil,
   86 			[][2]string{
   87 				{`{{ "1h12m10s" | time.ParseDuration }}`, `1h12m10s`},
   88 			},
   89 		)
   90 
   91 		return ns
   92 	}
   93 
   94 	internal.AddTemplateFuncsNamespace(f)
   95 }