@bookshop/hugo-engine 3.3.3 → 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/{25f8c04a-7fe3-4d10-b99e-0688e18203ec.json → 0f70a16f-6a79-48c7-8b16-3b4665e247b2.json} +0 -0
  2. package/.nyc_output/{2c3e7d19-6b44-48be-857a-ceba6bb9bf99.json → 6285d5fa-be3d-49e4-8f2f-82157d95dcd8.json} +0 -0
  3. package/.nyc_output/{5429d932-0ea0-4757-b731-0a647f4adc5f.json → 8b5e1314-6e86-4197-a3ae-3c40e4c2f39e.json} +0 -0
  4. package/.nyc_output/{723f5a1e-49f4-48c2-8a96-069bb758bb7f.json → c5490eb0-70c0-468f-a43f-e58c75dd34c8.json} +0 -0
  5. package/.nyc_output/{e4df4511-7e66-4fa6-84ed-6bd96e3027fe.json → ee1a9bec-d318-4aea-af93-7269debcf6df.json} +0 -0
  6. package/.nyc_output/processinfo/{25f8c04a-7fe3-4d10-b99e-0688e18203ec.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/{2c3e7d19-6b44-48be-857a-ceba6bb9bf99.json → 8b5e1314-6e86-4197-a3ae-3c40e4c2f39e.json} +1 -1
  9. package/.nyc_output/processinfo/{5429d932-0ea0-4757-b731-0a647f4adc5f.json → c5490eb0-70c0-468f-a43f-e58c75dd34c8.json} +1 -1
  10. package/.nyc_output/processinfo/{723f5a1e-49f4-48c2-8a96-069bb758bb7f.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/e4df4511-7e66-4fa6-84ed-6bd96e3027fe.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,164 +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 provides template functions for mathematical operations.
15
- package math
16
-
17
- import (
18
- "errors"
19
- "math"
20
-
21
- _math "github.com/gohugoio/hugo/common/math"
22
-
23
- "github.com/spf13/cast"
24
- )
25
-
26
- // New returns a new instance of the math-namespaced template functions.
27
- func New() *Namespace {
28
- return &Namespace{}
29
- }
30
-
31
- // Namespace provides template functions for the "math" namespace.
32
- type Namespace struct{}
33
-
34
- // Add adds two numbers.
35
- func (ns *Namespace) Add(a, b interface{}) (interface{}, error) {
36
- return _math.DoArithmetic(a, b, '+')
37
- }
38
-
39
- // Ceil returns the least integer value greater than or equal to x.
40
- func (ns *Namespace) Ceil(x interface{}) (float64, error) {
41
- xf, err := cast.ToFloat64E(x)
42
- if err != nil {
43
- return 0, errors.New("Ceil operator can't be used with non-float value")
44
- }
45
-
46
- return math.Ceil(xf), nil
47
- }
48
-
49
- // Div divides two numbers.
50
- func (ns *Namespace) Div(a, b interface{}) (interface{}, error) {
51
- return _math.DoArithmetic(a, b, '/')
52
- }
53
-
54
- // Floor returns the greatest integer value less than or equal to x.
55
- func (ns *Namespace) Floor(x interface{}) (float64, error) {
56
- xf, err := cast.ToFloat64E(x)
57
- if err != nil {
58
- return 0, errors.New("Floor operator can't be used with non-float value")
59
- }
60
-
61
- return math.Floor(xf), nil
62
- }
63
-
64
- // Log returns the natural logarithm of a number.
65
- func (ns *Namespace) Log(a interface{}) (float64, error) {
66
- af, err := cast.ToFloat64E(a)
67
- if err != nil {
68
- return 0, errors.New("Log operator can't be used with non integer or float value")
69
- }
70
-
71
- return math.Log(af), nil
72
- }
73
-
74
- // Max returns the greater of two numbers.
75
- func (ns *Namespace) Max(a, b interface{}) (float64, error) {
76
- af, erra := cast.ToFloat64E(a)
77
- bf, errb := cast.ToFloat64E(b)
78
-
79
- if erra != nil || errb != nil {
80
- return 0, errors.New("Max operator can't be used with non-float value")
81
- }
82
-
83
- return math.Max(af, bf), nil
84
- }
85
-
86
- // Min returns the smaller of two numbers.
87
- func (ns *Namespace) Min(a, b interface{}) (float64, error) {
88
- af, erra := cast.ToFloat64E(a)
89
- bf, errb := cast.ToFloat64E(b)
90
-
91
- if erra != nil || errb != nil {
92
- return 0, errors.New("Min operator can't be used with non-float value")
93
- }
94
-
95
- return math.Min(af, bf), nil
96
- }
97
-
98
- // Mod returns a % b.
99
- func (ns *Namespace) Mod(a, b interface{}) (int64, error) {
100
- ai, erra := cast.ToInt64E(a)
101
- bi, errb := cast.ToInt64E(b)
102
-
103
- if erra != nil || errb != nil {
104
- return 0, errors.New("modulo operator can't be used with non integer value")
105
- }
106
-
107
- if bi == 0 {
108
- return 0, errors.New("the number can't be divided by zero at modulo operation")
109
- }
110
-
111
- return ai % bi, nil
112
- }
113
-
114
- // ModBool returns the boolean of a % b. If a % b == 0, return true.
115
- func (ns *Namespace) ModBool(a, b interface{}) (bool, error) {
116
- res, err := ns.Mod(a, b)
117
- if err != nil {
118
- return false, err
119
- }
120
-
121
- return res == int64(0), nil
122
- }
123
-
124
- // Mul multiplies two numbers.
125
- func (ns *Namespace) Mul(a, b interface{}) (interface{}, error) {
126
- return _math.DoArithmetic(a, b, '*')
127
- }
128
-
129
- // Pow returns a raised to the power of b.
130
- func (ns *Namespace) Pow(a, b interface{}) (float64, error) {
131
- af, erra := cast.ToFloat64E(a)
132
- bf, errb := cast.ToFloat64E(b)
133
-
134
- if erra != nil || errb != nil {
135
- return 0, errors.New("Pow operator can't be used with non-float value")
136
- }
137
-
138
- return math.Pow(af, bf), nil
139
- }
140
-
141
- // Round returns the nearest integer, rounding half away from zero.
142
- func (ns *Namespace) Round(x interface{}) (float64, error) {
143
- xf, err := cast.ToFloat64E(x)
144
- if err != nil {
145
- return 0, errors.New("Round operator can't be used with non-float value")
146
- }
147
-
148
- return _round(xf), nil
149
- }
150
-
151
- // Sqrt returns the square root of a number.
152
- func (ns *Namespace) Sqrt(a interface{}) (float64, error) {
153
- af, err := cast.ToFloat64E(a)
154
- if err != nil {
155
- return 0, errors.New("Sqrt operator can't be used with non integer or float value")
156
- }
157
-
158
- return math.Sqrt(af), nil
159
- }
160
-
161
- // Sub subtracts two numbers.
162
- func (ns *Namespace) Sub(a, b interface{}) (interface{}, error) {
163
- return _math.DoArithmetic(a, b, '-')
164
- }
@@ -1,61 +0,0 @@
1
- // Copyright 2009 The Go Authors. All rights reserved.
2
- // Use of this source code is governed by a BSD-style
3
- // license that can be found in the LICENSE file.
4
-
5
- // According to https://github.com/golang/go/issues/20100, the Go stdlib will
6
- // include math.Round beginning with Go 1.10.
7
- //
8
- // The following implementation was taken from https://golang.org/cl/43652.
9
-
10
- package math
11
-
12
- import "math"
13
-
14
- const (
15
- mask = 0x7FF
16
- shift = 64 - 11 - 1
17
- bias = 1023
18
- )
19
-
20
- // Round returns the nearest integer, rounding half away from zero.
21
- //
22
- // Special cases are:
23
- // Round(±0) = ±0
24
- // Round(±Inf) = ±Inf
25
- // Round(NaN) = NaN
26
- func _round(x float64) float64 {
27
- // Round is a faster implementation of:
28
- //
29
- // func Round(x float64) float64 {
30
- // t := Trunc(x)
31
- // if Abs(x-t) >= 0.5 {
32
- // return t + Copysign(1, x)
33
- // }
34
- // return t
35
- // }
36
- const (
37
- signMask = 1 << 63
38
- fracMask = 1<<shift - 1
39
- half = 1 << (shift - 1)
40
- one = bias << shift
41
- )
42
-
43
- bits := math.Float64bits(x)
44
- e := uint(bits>>shift) & mask
45
- if e < bias {
46
- // Round abs(x) < 1 including denormals.
47
- bits &= signMask // +-0
48
- if e == bias-1 {
49
- bits |= one // +-1
50
- }
51
- } else if e < bias+shift {
52
- // Round any abs(x) >= 1 containing a fractional component [0,1).
53
- //
54
- // Numbers with larger exponents are returned unchanged since they
55
- // must be either an integer, infinity, or NaN.
56
- e -= bias
57
- bits += half >> e
58
- bits &^= fracMask >> e
59
- }
60
- return math.Float64frombits(bits)
61
- }
@@ -1,55 +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 partials
15
-
16
- import (
17
- "hugo-renderer/deps"
18
- "hugo-renderer/tpl/internal"
19
- )
20
-
21
- const name = "partials"
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.Include,
33
- []string{"partial"},
34
- [][2]string{
35
- {`{{ partial "header.html" . }}`, `<title>Hugo Rocks!</title>`},
36
- },
37
- )
38
-
39
- // TODO(bep) we need the return to be a valid identifier, but
40
- // should consider another way of adding it.
41
- ns.AddMethodMapping(func() string { return "" },
42
- []string{"return"},
43
- [][2]string{},
44
- )
45
-
46
- ns.AddMethodMapping(ctx.IncludeCached,
47
- []string{"partialCached"},
48
- [][2]string{},
49
- )
50
-
51
- return ns
52
- }
53
-
54
- internal.AddTemplateFuncsNamespace(f)
55
- }
@@ -1,88 +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 partials provides template functions for working with reusable
15
- // templates.
16
- package partials
17
-
18
- import (
19
- "bytes"
20
- "fmt"
21
- "html/template"
22
- "strings"
23
-
24
- library "hugo-renderer/tpl/bookshop_library"
25
-
26
- "hugo-renderer/deps"
27
- )
28
-
29
- // Bookshop: This file has been stripped to only support Bookshop partials when live editing.
30
-
31
- // New returns a new instance of the templates-namespaced template functions.
32
- func New(deps *deps.Deps) *Namespace {
33
- return &Namespace{}
34
- }
35
-
36
- // Namespace provides template functions for the "templates" namespace.
37
- type Namespace struct {
38
- }
39
-
40
- // Include executes the named partial.
41
- // If the partial contains a return statement, that value will be returned.
42
- // Else, the rendered output will be returned:
43
- // A string if the partial is a text/template, or template.HTML when html/template.
44
- func (ns *Namespace) Include(tagname string, contextList ...interface{}) (interface{}, error) {
45
- // TODO(bookshop): Fallback to this
46
- _ = strings.TrimPrefix(name, "partials/")
47
-
48
- var context interface{}
49
- if len(contextList) > 0 {
50
- context = contextList[0]
51
- }
52
-
53
- var name string
54
-
55
- switch tagname {
56
- case "bookshop":
57
- name, context = library.UnwrapBookshopComponent(context)
58
- case "bookshop_partial":
59
- name, context = library.UnwrapBookshopPartial(context)
60
- default:
61
- panic("Can't render standard includes yet")
62
- }
63
-
64
- templ, found := library.RetrieveBookshopPartial(name)
65
-
66
- if !found {
67
- // TODO(bookshop): Fallback to the name above
68
- return "", fmt.Errorf("partial %q not found", name)
69
- }
70
-
71
- // TODO(bookshop): Large section of code was omitted
72
- // meaning partials with return values won't work.
73
-
74
- buf := bytes.NewBufferString("")
75
- if err := templ.Execute(buf, context); err != nil {
76
- return "", err
77
- }
78
-
79
- return template.HTML(buf.String()), nil
80
- }
81
-
82
- // IncludeCached executes and caches partial templates. The cache is created with name+variants as the key.
83
- func (ns *Namespace) IncludeCached(name string, context interface{}, variants ...interface{}) (interface{}, error) {
84
- // Bookshop skips cached partials
85
- // because partial rendering is implemented at a higher
86
- // asbtraction level
87
- return ns.Include(name, context)
88
- }
@@ -1,60 +0,0 @@
1
- // Copyright 2018 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 path
15
-
16
- import (
17
- "fmt"
18
- "path/filepath"
19
-
20
- "hugo-renderer/deps"
21
- "hugo-renderer/tpl/internal"
22
- )
23
-
24
- const name = "path"
25
-
26
- func init() {
27
- f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
28
- ctx := New(d)
29
-
30
- ns := &internal.TemplateFuncsNamespace{
31
- Name: name,
32
- Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
33
- }
34
-
35
- ns.AddMethodMapping(ctx.Split,
36
- nil,
37
- [][2]string{
38
- {`{{ "/my/path/filename.txt" | path.Split }}`, `/my/path/|filename.txt`},
39
- {fmt.Sprintf(`{{ %q | path.Split }}`, filepath.FromSlash("/my/path/filename.txt")), `/my/path/|filename.txt`},
40
- },
41
- )
42
-
43
- testDir := filepath.Join("my", "path")
44
- testFile := filepath.Join(testDir, "filename.txt")
45
-
46
- ns.AddMethodMapping(ctx.Join,
47
- nil,
48
- [][2]string{
49
- {fmt.Sprintf(`{{ slice %q "filename.txt" | path.Join }}`, testDir), `my/path/filename.txt`},
50
- {`{{ path.Join "my" "path" "filename.txt" }}`, `my/path/filename.txt`},
51
- {fmt.Sprintf(`{{ %q | path.Ext }}`, testFile), `.txt`},
52
- {fmt.Sprintf(`{{ %q | path.Base }}`, testFile), `filename.txt`},
53
- {fmt.Sprintf(`{{ %q | path.Dir }}`, testFile), `my/path`},
54
- },
55
- )
56
-
57
- return ns
58
- }
59
- internal.AddTemplateFuncsNamespace(f)
60
- }
@@ -1,159 +0,0 @@
1
- // Copyright 2018 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 path provides template functions for manipulating paths.
15
- package path
16
-
17
- import (
18
- "fmt"
19
- _path "path"
20
- "path/filepath"
21
-
22
- "hugo-renderer/deps"
23
-
24
- "github.com/spf13/cast"
25
- )
26
-
27
- // New returns a new instance of the path-namespaced template functions.
28
- func New(deps *deps.Deps) *Namespace {
29
- return &Namespace{
30
- deps: deps,
31
- }
32
- }
33
-
34
- // Namespace provides template functions for the "os" namespace.
35
- type Namespace struct {
36
- deps *deps.Deps
37
- }
38
-
39
- // DirFile holds the result from path.Split.
40
- type DirFile struct {
41
- Dir string
42
- File string
43
- }
44
-
45
- // Used in test.
46
- func (df DirFile) String() string {
47
- return fmt.Sprintf("%s|%s", df.Dir, df.File)
48
- }
49
-
50
- // Ext returns the file name extension used by path.
51
- // The extension is the suffix beginning at the final dot
52
- // in the final slash-separated element of path;
53
- // it is empty if there is no dot.
54
- // The input path is passed into filepath.ToSlash converting any Windows slashes
55
- // to forward slashes.
56
- func (ns *Namespace) Ext(path interface{}) (string, error) {
57
- spath, err := cast.ToStringE(path)
58
- if err != nil {
59
- return "", err
60
- }
61
- spath = filepath.ToSlash(spath)
62
- return _path.Ext(spath), nil
63
- }
64
-
65
- // Dir returns all but the last element of path, typically the path's directory.
66
- // After dropping the final element using Split, the path is Cleaned and trailing
67
- // slashes are removed.
68
- // If the path is empty, Dir returns ".".
69
- // If the path consists entirely of slashes followed by non-slash bytes, Dir
70
- // returns a single slash. In any other case, the returned path does not end in a
71
- // slash.
72
- // The input path is passed into filepath.ToSlash converting any Windows slashes
73
- // to forward slashes.
74
- func (ns *Namespace) Dir(path interface{}) (string, error) {
75
- spath, err := cast.ToStringE(path)
76
- if err != nil {
77
- return "", err
78
- }
79
- spath = filepath.ToSlash(spath)
80
- return _path.Dir(spath), nil
81
- }
82
-
83
- // Base returns the last element of path.
84
- // Trailing slashes are removed before extracting the last element.
85
- // If the path is empty, Base returns ".".
86
- // If the path consists entirely of slashes, Base returns "/".
87
- // The input path is passed into filepath.ToSlash converting any Windows slashes
88
- // to forward slashes.
89
- func (ns *Namespace) Base(path interface{}) (string, error) {
90
- spath, err := cast.ToStringE(path)
91
- if err != nil {
92
- return "", err
93
- }
94
- spath = filepath.ToSlash(spath)
95
- return _path.Base(spath), nil
96
- }
97
-
98
- // Split splits path immediately following the final slash,
99
- // separating it into a directory and file name component.
100
- // If there is no slash in path, Split returns an empty dir and
101
- // file set to path.
102
- // The input path is passed into filepath.ToSlash converting any Windows slashes
103
- // to forward slashes.
104
- // The returned values have the property that path = dir+file.
105
- func (ns *Namespace) Split(path interface{}) (DirFile, error) {
106
- spath, err := cast.ToStringE(path)
107
- if err != nil {
108
- return DirFile{}, err
109
- }
110
- spath = filepath.ToSlash(spath)
111
- dir, file := _path.Split(spath)
112
-
113
- return DirFile{Dir: dir, File: file}, nil
114
- }
115
-
116
- // Join joins any number of path elements into a single path, adding a
117
- // separating slash if necessary. All the input
118
- // path elements are passed into filepath.ToSlash converting any Windows slashes
119
- // to forward slashes.
120
- // The result is Cleaned; in particular,
121
- // all empty strings are ignored.
122
- func (ns *Namespace) Join(elements ...interface{}) (string, error) {
123
- var pathElements []string
124
- for _, elem := range elements {
125
- switch v := elem.(type) {
126
- case []string:
127
- for _, e := range v {
128
- pathElements = append(pathElements, filepath.ToSlash(e))
129
- }
130
- case []interface{}:
131
- for _, e := range v {
132
- elemStr, err := cast.ToStringE(e)
133
- if err != nil {
134
- return "", err
135
- }
136
- pathElements = append(pathElements, filepath.ToSlash(elemStr))
137
- }
138
- default:
139
- elemStr, err := cast.ToStringE(elem)
140
- if err != nil {
141
- return "", err
142
- }
143
- pathElements = append(pathElements, filepath.ToSlash(elemStr))
144
- }
145
- }
146
- return _path.Join(pathElements...), nil
147
- }
148
-
149
- // Clean replaces the separators used with standard slashes and then
150
- // extraneous slashes are removed.
151
- func (ns *Namespace) Clean(path interface{}) (string, error) {
152
- spath, err := cast.ToStringE(path)
153
-
154
- if err != nil {
155
- return "", err
156
- }
157
- spath = filepath.ToSlash(spath)
158
- return _path.Clean(spath), nil
159
- }
@@ -1,51 +0,0 @@
1
- // Copyright 2018 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 reflect provides template functions for run-time object reflection.
15
- package reflect
16
-
17
- import (
18
- "hugo-renderer/deps"
19
- "hugo-renderer/tpl/internal"
20
- )
21
-
22
- const name = "reflect"
23
-
24
- func init() {
25
- f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
26
- ctx := New()
27
-
28
- ns := &internal.TemplateFuncsNamespace{
29
- Name: name,
30
- Context: func(args ...interface{}) (interface{}, error) { return ctx, nil },
31
- }
32
-
33
- ns.AddMethodMapping(ctx.IsMap,
34
- nil,
35
- [][2]string{
36
- {`{{ if reflect.IsMap (dict "a" 1) }}Map{{ end }}`, `Map`},
37
- },
38
- )
39
-
40
- ns.AddMethodMapping(ctx.IsSlice,
41
- nil,
42
- [][2]string{
43
- {`{{ if reflect.IsSlice (slice 1 2 3) }}Slice{{ end }}`, `Slice`},
44
- },
45
- )
46
-
47
- return ns
48
- }
49
-
50
- internal.AddTemplateFuncsNamespace(f)
51
- }
@@ -1,36 +0,0 @@
1
- // Copyright 2018 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 reflect
15
-
16
- import (
17
- "reflect"
18
- )
19
-
20
- // New returns a new instance of the reflect-namespaced template functions.
21
- func New() *Namespace {
22
- return &Namespace{}
23
- }
24
-
25
- // Namespace provides template functions for the "reflect" namespace.
26
- type Namespace struct{}
27
-
28
- // IsMap reports whether v is a map.
29
- func (ns *Namespace) IsMap(v interface{}) bool {
30
- return reflect.ValueOf(v).Kind() == reflect.Map
31
- }
32
-
33
- // IsSlice reports whether v is a slice.
34
- func (ns *Namespace) IsSlice(v interface{}) bool {
35
- return reflect.ValueOf(v).Kind() == reflect.Slice
36
- }