| 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 math |
| 15 |
|
| 16 |
import ( |
| 17 |
"context" |
| 18 |
"runtime" |
| 19 |
|
| 20 |
"github.com/gohugoio/hugo/deps" |
| 21 |
"github.com/gohugoio/hugo/tpl/internal" |
| 22 |
) |
| 23 |
|
| 24 |
const name = "math" |
| 25 |
|
| 26 |
func init() { |
| 27 |
mathAtan1 := "0.7853981633974483" |
| 28 |
mathTan1 := "1.557407724654902" |
| 29 |
if runtime.GOARCH == "s390x" { |
| 30 |
mathAtan1 = "0.7853981633974484" |
| 31 |
mathTan1 = "1.5574077246549018" |
| 32 |
} |
| 33 |
|
| 34 |
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { |
| 35 |
ctx := New(d) |
| 36 |
|
| 37 |
ns := &internal.TemplateFuncsNamespace{ |
| 38 |
Name: name, |
| 39 |
Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil }, |
| 40 |
} |
| 41 |
|
| 42 |
ns.AddMethodMapping(ctx.Abs, |
| 43 |
nil, |
| 44 |
[][2]string{ |
| 45 |
{"{{ math.Abs -2.1 }}", "2.1"}, |
| 46 |
}, |
| 47 |
) |
| 48 |
|
| 49 |
ns.AddMethodMapping(ctx.Acos, |
| 50 |
nil, |
| 51 |
[][2]string{ |
| 52 |
{"{{ math.Acos 1 }}", "0"}, |
| 53 |
}, |
| 54 |
) |
| 55 |
|
| 56 |
ns.AddMethodMapping(ctx.Add, |
| 57 |
[]string{"add"}, |
| 58 |
[][2]string{ |
| 59 |
{"{{ add 1 2 }}", "3"}, |
| 60 |
}, |
| 61 |
) |
| 62 |
|
| 63 |
ns.AddMethodMapping(ctx.Asin, |
| 64 |
nil, |
| 65 |
[][2]string{ |
| 66 |
{"{{ math.Asin 1 }}", "1.5707963267948966"}, |
| 67 |
}, |
| 68 |
) |
| 69 |
|
| 70 |
ns.AddMethodMapping(ctx.Atan, |
| 71 |
nil, |
| 72 |
[][2]string{ |
| 73 |
{"{{ math.Atan 1 }}", mathAtan1}, |
| 74 |
}, |
| 75 |
) |
| 76 |
|
| 77 |
ns.AddMethodMapping(ctx.Atan2, |
| 78 |
nil, |
| 79 |
[][2]string{ |
| 80 |
{"{{ math.Atan2 1 2 }}", "0.4636476090008061"}, |
| 81 |
}, |
| 82 |
) |
| 83 |
|
| 84 |
ns.AddMethodMapping(ctx.Ceil, |
| 85 |
nil, |
| 86 |
[][2]string{ |
| 87 |
{"{{ math.Ceil 2.1 }}", "3"}, |
| 88 |
}, |
| 89 |
) |
| 90 |
|
| 91 |
ns.AddMethodMapping(ctx.Cos, |
| 92 |
nil, |
| 93 |
[][2]string{ |
| 94 |
{"{{ math.Cos 1 }}", "0.5403023058681398"}, |
| 95 |
}, |
| 96 |
) |
| 97 |
|
| 98 |
ns.AddMethodMapping(ctx.Counter, |
| 99 |
nil, |
| 100 |
[][2]string{}, |
| 101 |
) |
| 102 |
|
| 103 |
ns.AddMethodMapping(ctx.Div, |
| 104 |
[]string{"div"}, |
| 105 |
[][2]string{ |
| 106 |
{"{{ div 6 3 }}", "2"}, |
| 107 |
}, |
| 108 |
) |
| 109 |
|
| 110 |
ns.AddMethodMapping(ctx.Floor, |
| 111 |
nil, |
| 112 |
[][2]string{ |
| 113 |
{"{{ math.Floor 1.9 }}", "1"}, |
| 114 |
}, |
| 115 |
) |
| 116 |
|
| 117 |
ns.AddMethodMapping(ctx.Log, |
| 118 |
nil, |
| 119 |
[][2]string{ |
| 120 |
{"{{ math.Log 1 }}", "0"}, |
| 121 |
}, |
| 122 |
) |
| 123 |
|
| 124 |
ns.AddMethodMapping(ctx.Max, |
| 125 |
nil, |
| 126 |
[][2]string{ |
| 127 |
{"{{ math.Max 1 2 }}", "2"}, |
| 128 |
}, |
| 129 |
) |
| 130 |
|
| 131 |
ns.AddMethodMapping(ctx.MaxInt64, |
| 132 |
nil, |
| 133 |
[][2]string{ |
| 134 |
{"{{ math.MaxInt64 }}", "9223372036854775807"}, |
| 135 |
}, |
| 136 |
) |
| 137 |
|
| 138 |
ns.AddMethodMapping(ctx.Min, |
| 139 |
nil, |
| 140 |
[][2]string{ |
| 141 |
{"{{ math.Min 1 2 }}", "1"}, |
| 142 |
}, |
| 143 |
) |
| 144 |
|
| 145 |
ns.AddMethodMapping(ctx.Mod, |
| 146 |
[]string{"mod"}, |
| 147 |
[][2]string{ |
| 148 |
{"{{ mod 15 3 }}", "0"}, |
| 149 |
}, |
| 150 |
) |
| 151 |
|
| 152 |
ns.AddMethodMapping(ctx.ModBool, |
| 153 |
[]string{"modBool"}, |
| 154 |
[][2]string{ |
| 155 |
{"{{ modBool 15 3 }}", "true"}, |
| 156 |
}, |
| 157 |
) |
| 158 |
|
| 159 |
ns.AddMethodMapping(ctx.Mul, |
| 160 |
[]string{"mul"}, |
| 161 |
[][2]string{ |
| 162 |
{"{{ mul 2 3 }}", "6"}, |
| 163 |
}, |
| 164 |
) |
| 165 |
|
| 166 |
ns.AddMethodMapping(ctx.Pi, |
| 167 |
nil, |
| 168 |
[][2]string{ |
| 169 |
{"{{ math.Pi }}", "3.141592653589793"}, |
| 170 |
}, |
| 171 |
) |
| 172 |
|
| 173 |
ns.AddMethodMapping(ctx.Pow, |
| 174 |
[]string{"pow"}, |
| 175 |
[][2]string{ |
| 176 |
{"{{ math.Pow 2 3 }}", "8"}, |
| 177 |
}, |
| 178 |
) |
| 179 |
|
| 180 |
ns.AddMethodMapping(ctx.Product, |
| 181 |
nil, |
| 182 |
[][2]string{}, |
| 183 |
) |
| 184 |
|
| 185 |
ns.AddMethodMapping(ctx.Rand, |
| 186 |
nil, |
| 187 |
[][2]string{ |
| 188 |
{"{{ math.Rand }}", "0.6312770459590062"}, |
| 189 |
}, |
| 190 |
) |
| 191 |
|
| 192 |
ns.AddMethodMapping(ctx.Round, |
| 193 |
nil, |
| 194 |
[][2]string{ |
| 195 |
{"{{ math.Round 1.5 }}", "2"}, |
| 196 |
}, |
| 197 |
) |
| 198 |
|
| 199 |
ns.AddMethodMapping(ctx.Sin, |
| 200 |
nil, |
| 201 |
[][2]string{ |
| 202 |
{"{{ math.Sin 1 }}", "0.8414709848078965"}, |
| 203 |
}, |
| 204 |
) |
| 205 |
|
| 206 |
ns.AddMethodMapping(ctx.Sqrt, |
| 207 |
nil, |
| 208 |
[][2]string{ |
| 209 |
{"{{ math.Sqrt 81 }}", "9"}, |
| 210 |
}, |
| 211 |
) |
| 212 |
|
| 213 |
ns.AddMethodMapping(ctx.Sub, |
| 214 |
[]string{"sub"}, |
| 215 |
[][2]string{ |
| 216 |
{"{{ sub 3 2 }}", "1"}, |
| 217 |
}, |
| 218 |
) |
| 219 |
|
| 220 |
ns.AddMethodMapping(ctx.Sum, |
| 221 |
nil, |
| 222 |
[][2]string{}, |
| 223 |
) |
| 224 |
|
| 225 |
ns.AddMethodMapping(ctx.Tan, |
| 226 |
nil, |
| 227 |
[][2]string{ |
| 228 |
{"{{ math.Tan 1 }}", mathTan1}, |
| 229 |
}, |
| 230 |
) |
| 231 |
|
| 232 |
ns.AddMethodMapping(ctx.ToDegrees, |
| 233 |
nil, |
| 234 |
[][2]string{ |
| 235 |
{"{{ math.ToDegrees 1.5707963267948966 }}", "90"}, |
| 236 |
}, |
| 237 |
) |
| 238 |
|
| 239 |
ns.AddMethodMapping(ctx.ToRadians, |
| 240 |
nil, |
| 241 |
[][2]string{ |
| 242 |
{"{{ math.ToRadians 90 }}", "1.5707963267948966"}, |
| 243 |
}, |
| 244 |
) |
| 245 |
|
| 246 |
return ns |
| 247 |
} |
| 248 |
|
| 249 |
internal.AddTemplateFuncsNamespace(f) |
| 250 |
} |