@bookshop/hugo-engine 3.3.2 → 3.4.0-alpha.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (96) hide show
  1. package/.nyc_output/{396c364e-fec4-4f27-9ff2-8b236061ef8f.json → 0f70a16f-6a79-48c7-8b16-3b4665e247b2.json} +0 -0
  2. package/.nyc_output/{4ef43740-059b-4329-96cd-0643fd8fe8d8.json → 6285d5fa-be3d-49e4-8f2f-82157d95dcd8.json} +0 -0
  3. package/.nyc_output/{656275f4-66d6-4d27-bfab-c2f7a3087670.json → 8b5e1314-6e86-4197-a3ae-3c40e4c2f39e.json} +0 -0
  4. package/.nyc_output/{6aec5420-0b83-4260-b300-4d595dfd0660.json → c5490eb0-70c0-468f-a43f-e58c75dd34c8.json} +0 -0
  5. package/.nyc_output/{874bd79b-45da-4726-9afc-1f6cab44be1f.json → ee1a9bec-d318-4aea-af93-7269debcf6df.json} +0 -0
  6. package/.nyc_output/processinfo/{396c364e-fec4-4f27-9ff2-8b236061ef8f.json → 0f70a16f-6a79-48c7-8b16-3b4665e247b2.json} +1 -1
  7. package/.nyc_output/processinfo/6285d5fa-be3d-49e4-8f2f-82157d95dcd8.json +1 -0
  8. package/.nyc_output/processinfo/{4ef43740-059b-4329-96cd-0643fd8fe8d8.json → 8b5e1314-6e86-4197-a3ae-3c40e4c2f39e.json} +1 -1
  9. package/.nyc_output/processinfo/{656275f4-66d6-4d27-bfab-c2f7a3087670.json → c5490eb0-70c0-468f-a43f-e58c75dd34c8.json} +1 -1
  10. package/.nyc_output/processinfo/{874bd79b-45da-4726-9afc-1f6cab44be1f.json → ee1a9bec-d318-4aea-af93-7269debcf6df.json} +1 -1
  11. package/.nyc_output/processinfo/index.json +1 -1
  12. package/bookshop-hugo-templates/bookshop.html +56 -0
  13. package/bookshop-hugo-templates/bookshop_bindings.html +29 -0
  14. package/bookshop-hugo-templates/bookshop_component_browser.html +30 -0
  15. package/bookshop-hugo-templates/bookshop_partial.html +42 -0
  16. package/bookshop-hugo-templates/bookshop_scss.html +9 -0
  17. package/bookshop-hugo-templates/errors/bad_bookshop_tag.html +39 -0
  18. package/bookshop-hugo-templates/errors/err.html +6 -0
  19. package/bookshop-hugo-templates/helpers/component.html +25 -0
  20. package/bookshop-hugo-templates/helpers/component_key.html +11 -0
  21. package/bookshop-hugo-templates/helpers/partial.html +25 -0
  22. package/bookshop-hugo-templates/helpers/partial_key.html +9 -0
  23. package/full-hugo-renderer/build.sh +34 -0
  24. package/full-hugo-renderer/go.mod +87 -0
  25. package/{hugo-renderer → full-hugo-renderer}/go.sum +204 -12
  26. package/full-hugo-renderer/hugo_renderer.wasm +0 -0
  27. package/full-hugo-renderer/hugo_renderer.wasm.gz +0 -0
  28. package/full-hugo-renderer/main.go +215 -0
  29. package/{hugo-renderer → full-hugo-renderer}/wasm_exec.js +6 -2
  30. package/lib/builder.js +2 -2
  31. package/lib/engine.js +109 -42
  32. package/lib/hugoIdentifierParser.js +5 -0
  33. package/lib/translateTextTemplate.js +3 -0
  34. package/lib/translateTextTemplate.test.js +22 -16
  35. package/package.json +4 -4
  36. package/.nyc_output/processinfo/6aec5420-0b83-4260-b300-4d595dfd0660.json +0 -1
  37. package/hugo-renderer/build.sh +0 -66
  38. package/hugo-renderer/deps/bookshop_modified_deps.go +0 -6
  39. package/hugo-renderer/go.mod +0 -19
  40. package/hugo-renderer/helpers/bookshop_modified_content.go +0 -98
  41. package/hugo-renderer/helpers/bookshop_modified_general.go +0 -33
  42. package/hugo-renderer/helpers/emoji.go +0 -96
  43. package/hugo-renderer/hugo_renderer.wasm +0 -0
  44. package/hugo-renderer/hugo_renderer.wasm.gz +0 -0
  45. package/hugo-renderer/main.go +0 -42
  46. package/hugo-renderer/tpl/bookshop_engine/bookshop_func_importer.go +0 -35
  47. package/hugo-renderer/tpl/bookshop_engine/bookshop_render.go +0 -64
  48. package/hugo-renderer/tpl/bookshop_library/bookshop_components.go +0 -129
  49. package/hugo-renderer/tpl/bookshop_library/bookshop_data.go +0 -53
  50. package/hugo-renderer/tpl/cast/cast.go +0 -63
  51. package/hugo-renderer/tpl/cast/init.go +0 -57
  52. package/hugo-renderer/tpl/collections/append.go +0 -37
  53. package/hugo-renderer/tpl/collections/apply.go +0 -131
  54. package/hugo-renderer/tpl/collections/collections.go +0 -768
  55. package/hugo-renderer/tpl/collections/complement.go +0 -55
  56. package/hugo-renderer/tpl/collections/index.go +0 -133
  57. package/hugo-renderer/tpl/collections/init.go +0 -214
  58. package/hugo-renderer/tpl/collections/merge.go +0 -126
  59. package/hugo-renderer/tpl/collections/reflect_helpers.go +0 -216
  60. package/hugo-renderer/tpl/collections/sort.go +0 -183
  61. package/hugo-renderer/tpl/collections/symdiff.go +0 -68
  62. package/hugo-renderer/tpl/collections/where.go +0 -517
  63. package/hugo-renderer/tpl/compare/compare.go +0 -324
  64. package/hugo-renderer/tpl/compare/init.go +0 -85
  65. package/hugo-renderer/tpl/crypto/crypto.go +0 -109
  66. package/hugo-renderer/tpl/crypto/init.go +0 -65
  67. package/hugo-renderer/tpl/debug/debug.go +0 -40
  68. package/hugo-renderer/tpl/debug/init.go +0 -45
  69. package/hugo-renderer/tpl/encoding/encoding.go +0 -90
  70. package/hugo-renderer/tpl/encoding/init.go +0 -59
  71. package/hugo-renderer/tpl/fmt/fmt.go +0 -66
  72. package/hugo-renderer/tpl/fmt/init.go +0 -77
  73. package/hugo-renderer/tpl/inflect/inflect.go +0 -79
  74. package/hugo-renderer/tpl/inflect/init.go +0 -60
  75. package/hugo-renderer/tpl/internal/bookshop_modified_templatefuncsRegistry.go +0 -88
  76. package/hugo-renderer/tpl/math/init.go +0 -134
  77. package/hugo-renderer/tpl/math/math.go +0 -164
  78. package/hugo-renderer/tpl/math/round.go +0 -61
  79. package/hugo-renderer/tpl/partials/init.go +0 -55
  80. package/hugo-renderer/tpl/partials/partials.go +0 -88
  81. package/hugo-renderer/tpl/path/init.go +0 -60
  82. package/hugo-renderer/tpl/path/path.go +0 -159
  83. package/hugo-renderer/tpl/reflect/init.go +0 -51
  84. package/hugo-renderer/tpl/reflect/reflect.go +0 -36
  85. package/hugo-renderer/tpl/safe/init.go +0 -80
  86. package/hugo-renderer/tpl/safe/safe.go +0 -72
  87. package/hugo-renderer/tpl/site/bookshop_site.go +0 -67
  88. package/hugo-renderer/tpl/site/init.go +0 -41
  89. package/hugo-renderer/tpl/strings/init.go +0 -229
  90. package/hugo-renderer/tpl/strings/regexp.go +0 -125
  91. package/hugo-renderer/tpl/strings/strings.go +0 -500
  92. package/hugo-renderer/tpl/strings/truncate.go +0 -157
  93. package/hugo-renderer/tpl/transform/bookshop_unmarshal.go +0 -39
  94. package/hugo-renderer/tpl/transform/init.go +0 -113
  95. package/hugo-renderer/tpl/transform/transform.go +0 -116
  96. package/hugo-renderer/update-from-hugo.sh +0 -19
@@ -1,90 +0,0 @@
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
- "github.com/gohugoio/hugo/common/maps"
24
- "github.com/spf13/cast"
25
- )
26
-
27
- // New returns a new instance of the encoding-namespaced template functions.
28
- func New() *Namespace {
29
- return &Namespace{}
30
- }
31
-
32
- // Namespace provides template functions for the "encoding" namespace.
33
- type Namespace struct{}
34
-
35
- // Base64Decode returns the base64 decoding of the given content.
36
- func (ns *Namespace) Base64Decode(content interface{}) (string, error) {
37
- conv, err := cast.ToStringE(content)
38
- if err != nil {
39
- return "", err
40
- }
41
-
42
- dec, err := base64.StdEncoding.DecodeString(conv)
43
- return string(dec), err
44
- }
45
-
46
- // Base64Encode returns the base64 encoding of the given content.
47
- func (ns *Namespace) Base64Encode(content interface{}) (string, error) {
48
- conv, err := cast.ToStringE(content)
49
- if err != nil {
50
- return "", err
51
- }
52
-
53
- return base64.StdEncoding.EncodeToString([]byte(conv)), nil
54
- }
55
-
56
- // Jsonify encodes a given object to JSON. To pretty print the JSON, pass a map
57
- // or dictionary of options as the first argument. Supported options are
58
- // "prefix" and "indent". Each JSON element in the output will begin on a new
59
- // line beginning with prefix followed by one or more copies of indent according
60
- // to the indentation nesting.
61
- func (ns *Namespace) Jsonify(args ...interface{}) (template.HTML, error) {
62
- var (
63
- b []byte
64
- err error
65
- )
66
-
67
- switch len(args) {
68
- case 0:
69
- return "", nil
70
- case 1:
71
- b, err = json.Marshal(args[0])
72
- case 2:
73
- var opts map[string]string
74
-
75
- opts, err = maps.ToStringMapStringE(args[0])
76
- if err != nil {
77
- break
78
- }
79
-
80
- b, err = json.MarshalIndent(args[1], opts["prefix"], opts["indent"])
81
- default:
82
- err = errors.New("too many arguments to jsonify")
83
- }
84
-
85
- if err != nil {
86
- return "", err
87
- }
88
-
89
- return template.HTML(b), nil
90
- }
@@ -1,59 +0,0 @@
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
15
-
16
- import (
17
- "hugo-renderer/deps"
18
- "hugo-renderer/tpl/internal"
19
- )
20
-
21
- const name = "encoding"
22
-
23
- func init() {
24
- f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
25
- ctx := New()
26
-
27
- ns := &internal.TemplateFuncsNamespace{
28
- Name: name,
29
- Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
30
- }
31
-
32
- ns.AddMethodMapping(ctx.Base64Decode,
33
- []string{"base64Decode"},
34
- [][2]string{
35
- {`{{ "SGVsbG8gd29ybGQ=" | base64Decode }}`, `Hello world`},
36
- {`{{ 42 | base64Encode | base64Decode }}`, `42`},
37
- },
38
- )
39
-
40
- ns.AddMethodMapping(ctx.Base64Encode,
41
- []string{"base64Encode"},
42
- [][2]string{
43
- {`{{ "Hello world" | base64Encode }}`, `SGVsbG8gd29ybGQ=`},
44
- },
45
- )
46
-
47
- ns.AddMethodMapping(ctx.Jsonify,
48
- []string{"jsonify"},
49
- [][2]string{
50
- {`{{ (slice "A" "B" "C") | jsonify }}`, `["A","B","C"]`},
51
- {`{{ (slice "A" "B" "C") | jsonify (dict "indent" " ") }}`, "[\n \"A\",\n \"B\",\n \"C\"\n]"},
52
- },
53
- )
54
-
55
- return ns
56
- }
57
-
58
- internal.AddTemplateFuncsNamespace(f)
59
- }
@@ -1,66 +0,0 @@
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
-
20
- "hugo-renderer/deps"
21
- )
22
-
23
- // Bookshop: This file has been stripped due to irrelevancy with live editing.
24
-
25
- // New returns a new instance of the fmt-namespaced template functions.
26
- func New(d *deps.Deps) *Namespace {
27
- return &Namespace{}
28
- }
29
-
30
- // Namespace provides template functions for the "fmt" namespace.
31
- type Namespace struct {
32
- }
33
-
34
- // Print returns string representation of the passed arguments.
35
- func (ns *Namespace) Print(a ...interface{}) string {
36
- return _fmt.Sprint(a...)
37
- }
38
-
39
- // Printf returns a formatted string representation of the passed arguments.
40
- func (ns *Namespace) Printf(format string, a ...interface{}) string {
41
- return _fmt.Sprintf(format, a...)
42
- }
43
-
44
- // Println returns string representation of the passed arguments ending with a newline.
45
- func (ns *Namespace) Println(a ...interface{}) string {
46
- return _fmt.Sprintln(a...)
47
- }
48
-
49
- // Errorf formats according to a format specifier and logs an ERROR.
50
- // It returns an empty string.
51
- func (ns *Namespace) Errorf(format string, a ...interface{}) string {
52
- return "" // TODO(bookshop): NYI
53
- }
54
-
55
- // Erroridf formats according to a format specifier and logs an ERROR and
56
- // an information text that the error with the given ID can be suppressed in config.
57
- // It returns an empty string.
58
- func (ns *Namespace) Erroridf(id, format string, a ...interface{}) string {
59
- return "" // TODO(bookshop): NYI
60
- }
61
-
62
- // Warnf formats according to a format specifier and logs a WARNING.
63
- // It returns an empty string.
64
- func (ns *Namespace) Warnf(format string, a ...interface{}) string {
65
- return "" // TODO(bookshop): NYI
66
- }
@@ -1,77 +0,0 @@
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
15
-
16
- import (
17
- "hugo-renderer/deps"
18
- "hugo-renderer/tpl/internal"
19
- )
20
-
21
- const name = "fmt"
22
-
23
- func init() {
24
- f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
25
- ctx := New(d)
26
-
27
- ns := &internal.TemplateFuncsNamespace{
28
- Name: name,
29
- Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
30
- }
31
-
32
- ns.AddMethodMapping(ctx.Print,
33
- []string{"print"},
34
- [][2]string{
35
- {`{{ print "works!" }}`, `works!`},
36
- },
37
- )
38
-
39
- ns.AddMethodMapping(ctx.Println,
40
- []string{"println"},
41
- [][2]string{
42
- {`{{ println "works!" }}`, "works!\n"},
43
- },
44
- )
45
-
46
- ns.AddMethodMapping(ctx.Printf,
47
- []string{"printf"},
48
- [][2]string{
49
- {`{{ printf "%s!" "works" }}`, `works!`},
50
- },
51
- )
52
-
53
- ns.AddMethodMapping(ctx.Errorf,
54
- []string{"errorf"},
55
- [][2]string{
56
- {`{{ errorf "%s." "failed" }}`, ``},
57
- },
58
- )
59
-
60
- ns.AddMethodMapping(ctx.Erroridf,
61
- []string{"erroridf"},
62
- [][2]string{
63
- {`{{ erroridf "my-err-id" "%s." "failed" }}`, ``},
64
- },
65
- )
66
-
67
- ns.AddMethodMapping(ctx.Warnf,
68
- []string{"warnf"},
69
- [][2]string{
70
- {`{{ warnf "%s." "warning" }}`, ``},
71
- },
72
- )
73
- return ns
74
- }
75
-
76
- internal.AddTemplateFuncsNamespace(f)
77
- }
@@ -1,79 +0,0 @@
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 inflect provides template functions for the inflection of words.
15
- package inflect
16
-
17
- import (
18
- "strconv"
19
- "strings"
20
-
21
- _inflect "github.com/gobuffalo/flect"
22
- "github.com/spf13/cast"
23
- )
24
-
25
- // New returns a new instance of the inflect-namespaced template functions.
26
- func New() *Namespace {
27
- return &Namespace{}
28
- }
29
-
30
- // Namespace provides template functions for the "inflect" namespace.
31
- type Namespace struct{}
32
-
33
- // Humanize returns the humanized form of a single parameter.
34
- //
35
- // If the parameter is either an integer or a string containing an integer
36
- // value, the behavior is to add the appropriate ordinal.
37
- //
38
- // Example: "my-first-post" -> "My first post"
39
- // Example: "103" -> "103rd"
40
- // Example: 52 -> "52nd"
41
- func (ns *Namespace) Humanize(in interface{}) (string, error) {
42
- word, err := cast.ToStringE(in)
43
- if err != nil {
44
- return "", err
45
- }
46
-
47
- if word == "" {
48
- return "", nil
49
- }
50
-
51
- _, ok := in.(int) // original param was literal int value
52
- _, err = strconv.Atoi(word) // original param was string containing an int value
53
- if ok || err == nil {
54
- return _inflect.Ordinalize(word), nil
55
- }
56
-
57
- str := _inflect.Humanize(word)
58
- return _inflect.Humanize(strings.ToLower(str)), nil
59
- }
60
-
61
- // Pluralize returns the plural form of a single word.
62
- func (ns *Namespace) Pluralize(in interface{}) (string, error) {
63
- word, err := cast.ToStringE(in)
64
- if err != nil {
65
- return "", err
66
- }
67
-
68
- return _inflect.Pluralize(word), nil
69
- }
70
-
71
- // Singularize returns the singular form of a single word.
72
- func (ns *Namespace) Singularize(in interface{}) (string, error) {
73
- word, err := cast.ToStringE(in)
74
- if err != nil {
75
- return "", err
76
- }
77
-
78
- return _inflect.Singularize(word), nil
79
- }
@@ -1,60 +0,0 @@
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 inflect
15
-
16
- import (
17
- "hugo-renderer/deps"
18
- "hugo-renderer/tpl/internal"
19
- )
20
-
21
- const name = "inflect"
22
-
23
- func init() {
24
- f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
25
- ctx := New()
26
-
27
- ns := &internal.TemplateFuncsNamespace{
28
- Name: name,
29
- Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
30
- }
31
-
32
- ns.AddMethodMapping(ctx.Humanize,
33
- []string{"humanize"},
34
- [][2]string{
35
- {`{{ humanize "my-first-post" }}`, `My first post`},
36
- {`{{ humanize "myCamelPost" }}`, `My camel post`},
37
- {`{{ humanize "52" }}`, `52nd`},
38
- {`{{ humanize 103 }}`, `103rd`},
39
- },
40
- )
41
-
42
- ns.AddMethodMapping(ctx.Pluralize,
43
- []string{"pluralize"},
44
- [][2]string{
45
- {`{{ "cat" | pluralize }}`, `cats`},
46
- },
47
- )
48
-
49
- ns.AddMethodMapping(ctx.Singularize,
50
- []string{"singularize"},
51
- [][2]string{
52
- {`{{ "cats" | singularize }}`, `cat`},
53
- },
54
- )
55
-
56
- return ns
57
- }
58
-
59
- internal.AddTemplateFuncsNamespace(f)
60
- }
@@ -1,88 +0,0 @@
1
- package internal
2
-
3
- import (
4
- "path/filepath"
5
- "reflect"
6
- "runtime"
7
- "strings"
8
-
9
- "hugo-renderer/deps"
10
- )
11
-
12
- // TemplateFuncsNamespaceRegistry describes a registry of functions that provide
13
- // namespaces.
14
- var TemplateFuncsNamespaceRegistry []func(d *deps.Deps) *TemplateFuncsNamespace
15
-
16
- // AddTemplateFuncsNamespace adds a given function to a registry.
17
- func AddTemplateFuncsNamespace(ns func(d *deps.Deps) *TemplateFuncsNamespace) {
18
- TemplateFuncsNamespaceRegistry = append(TemplateFuncsNamespaceRegistry, ns)
19
- }
20
-
21
- // TemplateFuncsNamespace represents a template function namespace.
22
- type TemplateFuncsNamespace struct {
23
- // The namespace name, "strings", "lang", etc.
24
- Name string
25
-
26
- // This is the method receiver.
27
- Context func(v ...interface{}) (interface{}, error)
28
-
29
- // Additional info, aliases and examples, per method name.
30
- MethodMappings map[string]TemplateFuncMethodMapping
31
- }
32
-
33
- // TemplateFuncsNamespaces is a slice of TemplateFuncsNamespace.
34
- type TemplateFuncsNamespaces []*TemplateFuncsNamespace
35
-
36
- // TemplateFuncMethodMapping represents a mapping of functions to methods for a
37
- // given namespace.
38
- type TemplateFuncMethodMapping struct {
39
- Method interface{}
40
-
41
- // Any template funcs aliases. This is mainly motivated by keeping
42
- // backwards compatibility, but some new template funcs may also make
43
- // sense to give short and snappy aliases.
44
- // Note that these aliases are global and will be merged, so the last
45
- // key will win.
46
- Aliases []string
47
-
48
- // A slice of input/expected examples.
49
- // We keep it a the namespace level for now, but may find a way to keep track
50
- // of the single template func, for documentation purposes.
51
- // Some of these, hopefully just a few, may depend on some test data to run.
52
- Examples [][2]string
53
- }
54
-
55
- func methodToName(m interface{}) string {
56
- name := runtime.FuncForPC(reflect.ValueOf(m).Pointer()).Name()
57
- name = filepath.Ext(name)
58
- name = strings.TrimPrefix(name, ".")
59
- name = strings.TrimSuffix(name, "-fm")
60
- return name
61
- }
62
-
63
- // AddMethodMapping adds a method to a template function namespace.
64
- func (t *TemplateFuncsNamespace) AddMethodMapping(m interface{}, aliases []string, examples [][2]string) {
65
- if t.MethodMappings == nil {
66
- t.MethodMappings = make(map[string]TemplateFuncMethodMapping)
67
- }
68
-
69
- name := methodToName(m)
70
-
71
- // sanity check
72
- for _, e := range examples {
73
- if e[0] == "" {
74
- panic(t.Name + ": Empty example for " + name)
75
- }
76
- }
77
- for _, a := range aliases {
78
- if a == "" {
79
- panic(t.Name + ": Empty alias for " + name)
80
- }
81
- }
82
-
83
- t.MethodMappings[name] = TemplateFuncMethodMapping{
84
- Method: m,
85
- Aliases: aliases,
86
- Examples: examples,
87
- }
88
- }
@@ -1,134 +0,0 @@
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
- "hugo-renderer/deps"
18
- "hugo-renderer/tpl/internal"
19
- )
20
-
21
- const name = "math"
22
-
23
- func init() {
24
- f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
25
- ctx := New()
26
-
27
- ns := &internal.TemplateFuncsNamespace{
28
- Name: name,
29
- Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
30
- }
31
-
32
- ns.AddMethodMapping(ctx.Add,
33
- []string{"add"},
34
- [][2]string{
35
- {"{{add 1 2}}", "3"},
36
- },
37
- )
38
-
39
- ns.AddMethodMapping(ctx.Ceil,
40
- nil,
41
- [][2]string{
42
- {"{{math.Ceil 2.1}}", "3"},
43
- },
44
- )
45
-
46
- ns.AddMethodMapping(ctx.Div,
47
- []string{"div"},
48
- [][2]string{
49
- {"{{div 6 3}}", "2"},
50
- },
51
- )
52
-
53
- ns.AddMethodMapping(ctx.Floor,
54
- nil,
55
- [][2]string{
56
- {"{{math.Floor 1.9}}", "1"},
57
- },
58
- )
59
-
60
- ns.AddMethodMapping(ctx.Log,
61
- nil,
62
- [][2]string{
63
- {"{{math.Log 1}}", "0"},
64
- },
65
- )
66
-
67
- ns.AddMethodMapping(ctx.Max,
68
- nil,
69
- [][2]string{
70
- {"{{math.Max 1 2 }}", "2"},
71
- },
72
- )
73
-
74
- ns.AddMethodMapping(ctx.Min,
75
- nil,
76
- [][2]string{
77
- {"{{math.Min 1 2 }}", "1"},
78
- },
79
- )
80
-
81
- ns.AddMethodMapping(ctx.Mod,
82
- []string{"mod"},
83
- [][2]string{
84
- {"{{mod 15 3}}", "0"},
85
- },
86
- )
87
-
88
- ns.AddMethodMapping(ctx.ModBool,
89
- []string{"modBool"},
90
- [][2]string{
91
- {"{{modBool 15 3}}", "true"},
92
- },
93
- )
94
-
95
- ns.AddMethodMapping(ctx.Mul,
96
- []string{"mul"},
97
- [][2]string{
98
- {"{{mul 2 3}}", "6"},
99
- },
100
- )
101
-
102
- ns.AddMethodMapping(ctx.Pow,
103
- []string{"pow"},
104
- [][2]string{
105
- {"{{math.Pow 2 3}}", "8"},
106
- },
107
- )
108
-
109
- ns.AddMethodMapping(ctx.Round,
110
- nil,
111
- [][2]string{
112
- {"{{math.Round 1.5}}", "2"},
113
- },
114
- )
115
-
116
- ns.AddMethodMapping(ctx.Sqrt,
117
- nil,
118
- [][2]string{
119
- {"{{math.Sqrt 81}}", "9"},
120
- },
121
- )
122
-
123
- ns.AddMethodMapping(ctx.Sub,
124
- []string{"sub"},
125
- [][2]string{
126
- {"{{sub 3 2}}", "1"},
127
- },
128
- )
129
-
130
- return ns
131
- }
132
-
133
- internal.AddTemplateFuncsNamespace(f)
134
- }