@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,80 +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 safe
15
-
16
- import (
17
- "hugo-renderer/deps"
18
- "hugo-renderer/tpl/internal"
19
- )
20
-
21
- const name = "safe"
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.CSS,
33
- []string{"safeCSS"},
34
- [][2]string{
35
- {`{{ "Bat&Man" | safeCSS | safeCSS }}`, `Bat&Man`},
36
- },
37
- )
38
-
39
- ns.AddMethodMapping(ctx.HTML,
40
- []string{"safeHTML"},
41
- [][2]string{
42
- {`{{ "Bat&Man" | safeHTML | safeHTML }}`, `Bat&Man`},
43
- {`{{ "Bat&Man" | safeHTML }}`, `Bat&Man`},
44
- },
45
- )
46
-
47
- ns.AddMethodMapping(ctx.HTMLAttr,
48
- []string{"safeHTMLAttr"},
49
- [][2]string{},
50
- )
51
-
52
- ns.AddMethodMapping(ctx.JS,
53
- []string{"safeJS"},
54
- [][2]string{
55
- {`{{ "(1*2)" | safeJS | safeJS }}`, `(1*2)`},
56
- },
57
- )
58
-
59
- ns.AddMethodMapping(ctx.JSStr,
60
- []string{"safeJSStr"},
61
- [][2]string{},
62
- )
63
-
64
- ns.AddMethodMapping(ctx.URL,
65
- []string{"safeURL"},
66
- [][2]string{
67
- {`{{ "http://gohugo.io" | safeURL | safeURL }}`, `http://gohugo.io`},
68
- },
69
- )
70
-
71
- ns.AddMethodMapping(ctx.SanitizeURL,
72
- []string{"sanitizeURL", "sanitizeurl"},
73
- [][2]string{},
74
- )
75
-
76
- return ns
77
- }
78
-
79
- internal.AddTemplateFuncsNamespace(f)
80
- }
@@ -1,72 +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 safe provides template functions for escaping untrusted content or
15
- // encapsulating trusted content.
16
- package safe
17
-
18
- import (
19
- "html/template"
20
-
21
- "github.com/spf13/cast"
22
- )
23
-
24
- // New returns a new instance of the safe-namespaced template functions.
25
- func New() *Namespace {
26
- return &Namespace{}
27
- }
28
-
29
- // Namespace provides template functions for the "safe" namespace.
30
- type Namespace struct{}
31
-
32
- // CSS returns a given string as html/template CSS content.
33
- func (ns *Namespace) CSS(a interface{}) (template.CSS, error) {
34
- s, err := cast.ToStringE(a)
35
- return template.CSS(s), err
36
- }
37
-
38
- // HTML returns a given string as html/template HTML content.
39
- func (ns *Namespace) HTML(a interface{}) (template.HTML, error) {
40
- s, err := cast.ToStringE(a)
41
- return template.HTML(s), err
42
- }
43
-
44
- // HTMLAttr returns a given string as html/template HTMLAttr content.
45
- func (ns *Namespace) HTMLAttr(a interface{}) (template.HTMLAttr, error) {
46
- s, err := cast.ToStringE(a)
47
- return template.HTMLAttr(s), err
48
- }
49
-
50
- // JS returns the given string as a html/template JS content.
51
- func (ns *Namespace) JS(a interface{}) (template.JS, error) {
52
- s, err := cast.ToStringE(a)
53
- return template.JS(s), err
54
- }
55
-
56
- // JSStr returns the given string as a html/template JSStr content.
57
- func (ns *Namespace) JSStr(a interface{}) (template.JSStr, error) {
58
- s, err := cast.ToStringE(a)
59
- return template.JSStr(s), err
60
- }
61
-
62
- // URL returns a given string as html/template URL content.
63
- func (ns *Namespace) URL(a interface{}) (template.URL, error) {
64
- s, err := cast.ToStringE(a)
65
- return template.URL(s), err
66
- }
67
-
68
- // SanitizeURL returns a given string as html/template URL content.
69
- func (ns *Namespace) SanitizeURL(a interface{}) (string, error) {
70
- s, err := cast.ToStringE(a)
71
- return s, err // TODO(bookshop): NYI
72
- }
@@ -1,67 +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 site stubs out some of the Hugo site functions for use in Bookshop live editing.
15
- package site
16
-
17
- import (
18
- "hugo-renderer/deps"
19
- library "hugo-renderer/tpl/bookshop_library"
20
- )
21
-
22
- func New(deps *deps.Deps) *Namespace {
23
- return &Namespace{
24
- deps: deps,
25
- }
26
- }
27
-
28
- type Namespace struct {
29
- deps *deps.Deps
30
- }
31
-
32
- // Data returns any data that was present in the _cloudcannon/info.json
33
- // This will require `data_config: true` in the site cloudcannon.config.yml
34
- func (ns *Namespace) Data() (map[string]interface{}, error) {
35
- data := library.RetrieveHugoBookshopData()
36
-
37
- return data.Data, nil
38
- }
39
-
40
- // site.Title should have been stashed inside a Bookshop meta comment
41
- func (ns *Namespace) Title() (interface{}, error) {
42
- meta := library.RetrieveHugoBookshopMeta()
43
-
44
- return meta["title"], nil
45
- }
46
-
47
- // site.Copyright should have been stashed inside a Bookshop meta comment
48
- func (ns *Namespace) Copyright() (interface{}, error) {
49
- meta := library.RetrieveHugoBookshopMeta()
50
-
51
- return meta["copyright"], nil
52
- }
53
-
54
- // site.BaseURL should have been stashed inside a Bookshop meta comment
55
- func (ns *Namespace) BaseURL() (interface{}, error) {
56
- meta := library.RetrieveHugoBookshopMeta()
57
-
58
- return meta["baseurl"], nil
59
- }
60
-
61
- // Pending mock out of the params function
62
- func (ns *Namespace) Params() (map[string]interface{}, error) {
63
- params := make(map[string]interface{})
64
- params["env_bookshop_live"] = true
65
-
66
- return params, nil
67
- }
@@ -1,41 +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 site
15
-
16
- import (
17
- "hugo-renderer/deps"
18
- "hugo-renderer/tpl/internal"
19
- )
20
-
21
- const name = "site"
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.Data,
33
- []string{},
34
- [][2]string{},
35
- )
36
-
37
- return ns
38
- }
39
-
40
- internal.AddTemplateFuncsNamespace(f)
41
- }
@@ -1,229 +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 strings
15
-
16
- import (
17
- "hugo-renderer/deps"
18
- "hugo-renderer/tpl/internal"
19
- )
20
-
21
- const name = "strings"
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.Chomp,
33
- []string{"chomp"},
34
- [][2]string{
35
- {`{{chomp "<p>Blockhead</p>\n" | safeHTML }}`, `<p>Blockhead</p>`},
36
- },
37
- )
38
-
39
- ns.AddMethodMapping(ctx.CountRunes,
40
- []string{"countrunes"},
41
- [][2]string{},
42
- )
43
-
44
- ns.AddMethodMapping(ctx.RuneCount,
45
- nil,
46
- [][2]string{},
47
- )
48
-
49
- ns.AddMethodMapping(ctx.CountWords,
50
- []string{"countwords"},
51
- [][2]string{},
52
- )
53
-
54
- ns.AddMethodMapping(ctx.Count,
55
- nil,
56
- [][2]string{
57
- {`{{"aabab" | strings.Count "a" }}`, `3`},
58
- },
59
- )
60
-
61
- ns.AddMethodMapping(ctx.Contains,
62
- nil,
63
- [][2]string{
64
- {`{{ strings.Contains "abc" "b" }}`, `true`},
65
- {`{{ strings.Contains "abc" "d" }}`, `false`},
66
- },
67
- )
68
-
69
- ns.AddMethodMapping(ctx.ContainsAny,
70
- nil,
71
- [][2]string{
72
- {`{{ strings.ContainsAny "abc" "bcd" }}`, `true`},
73
- {`{{ strings.ContainsAny "abc" "def" }}`, `false`},
74
- },
75
- )
76
-
77
- ns.AddMethodMapping(ctx.FindRE,
78
- []string{"findRE"},
79
- [][2]string{
80
- {
81
- `{{ findRE "[G|g]o" "Hugo is a static side generator written in Go." "1" }}`,
82
- `[go]`,
83
- },
84
- },
85
- )
86
-
87
- ns.AddMethodMapping(ctx.HasPrefix,
88
- []string{"hasPrefix"},
89
- [][2]string{
90
- {`{{ hasPrefix "Hugo" "Hu" }}`, `true`},
91
- {`{{ hasPrefix "Hugo" "Fu" }}`, `false`},
92
- },
93
- )
94
-
95
- ns.AddMethodMapping(ctx.ToLower,
96
- []string{"lower"},
97
- [][2]string{
98
- {`{{lower "BatMan"}}`, `batman`},
99
- },
100
- )
101
-
102
- ns.AddMethodMapping(ctx.Replace,
103
- []string{"replace"},
104
- [][2]string{
105
- {
106
- `{{ replace "Batman and Robin" "Robin" "Catwoman" }}`,
107
- `Batman and Catwoman`,
108
- },
109
- {
110
- `{{ replace "aabbaabb" "a" "z" 2 }}`,
111
- `zzbbaabb`,
112
- },
113
- },
114
- )
115
-
116
- ns.AddMethodMapping(ctx.ReplaceRE,
117
- []string{"replaceRE"},
118
- [][2]string{
119
- {
120
- `{{ replaceRE "a+b" "X" "aabbaabbab" }}`,
121
- `XbXbX`,
122
- },
123
- {
124
- `{{ replaceRE "a+b" "X" "aabbaabbab" 1 }}`,
125
- `Xbaabbab`,
126
- },
127
- },
128
- )
129
-
130
- ns.AddMethodMapping(ctx.SliceString,
131
- []string{"slicestr"},
132
- [][2]string{
133
- {`{{slicestr "BatMan" 0 3}}`, `Bat`},
134
- {`{{slicestr "BatMan" 3}}`, `Man`},
135
- },
136
- )
137
-
138
- ns.AddMethodMapping(ctx.Split,
139
- []string{"split"},
140
- [][2]string{},
141
- )
142
-
143
- ns.AddMethodMapping(ctx.Substr,
144
- []string{"substr"},
145
- [][2]string{
146
- {`{{substr "BatMan" 0 -3}}`, `Bat`},
147
- {`{{substr "BatMan" 3 3}}`, `Man`},
148
- },
149
- )
150
-
151
- ns.AddMethodMapping(ctx.Trim,
152
- []string{"trim"},
153
- [][2]string{
154
- {`{{ trim "++Batman--" "+-" }}`, `Batman`},
155
- },
156
- )
157
-
158
- ns.AddMethodMapping(ctx.TrimLeft,
159
- nil,
160
- [][2]string{
161
- {`{{ "aabbaa" | strings.TrimLeft "a" }}`, `bbaa`},
162
- },
163
- )
164
-
165
- ns.AddMethodMapping(ctx.TrimPrefix,
166
- nil,
167
- [][2]string{
168
- {`{{ "aabbaa" | strings.TrimPrefix "a" }}`, `abbaa`},
169
- {`{{ "aabbaa" | strings.TrimPrefix "aa" }}`, `bbaa`},
170
- },
171
- )
172
-
173
- ns.AddMethodMapping(ctx.TrimRight,
174
- nil,
175
- [][2]string{
176
- {`{{ "aabbaa" | strings.TrimRight "a" }}`, `aabb`},
177
- },
178
- )
179
-
180
- ns.AddMethodMapping(ctx.TrimSuffix,
181
- nil,
182
- [][2]string{
183
- {`{{ "aabbaa" | strings.TrimSuffix "a" }}`, `aabba`},
184
- {`{{ "aabbaa" | strings.TrimSuffix "aa" }}`, `aabb`},
185
- },
186
- )
187
-
188
- ns.AddMethodMapping(ctx.Title,
189
- []string{"title"},
190
- [][2]string{
191
- {`{{title "Bat man"}}`, `Bat Man`},
192
- {`{{title "somewhere over the rainbow"}}`, `Somewhere Over the Rainbow`},
193
- },
194
- )
195
-
196
- ns.AddMethodMapping(ctx.FirstUpper,
197
- nil,
198
- [][2]string{
199
- {`{{ "hugo rocks!" | strings.FirstUpper }}`, `Hugo rocks!`},
200
- },
201
- )
202
-
203
- ns.AddMethodMapping(ctx.Truncate,
204
- []string{"truncate"},
205
- [][2]string{
206
- {`{{ "this is a very long text" | truncate 10 " ..." }}`, `this is a ...`},
207
- {`{{ "With [Markdown](/markdown) inside." | markdownify | truncate 14 }}`, `With <a href="/markdown">Markdown …</a>`},
208
- },
209
- )
210
-
211
- ns.AddMethodMapping(ctx.Repeat,
212
- nil,
213
- [][2]string{
214
- {`{{ "yo" | strings.Repeat 4 }}`, `yoyoyoyo`},
215
- },
216
- )
217
-
218
- ns.AddMethodMapping(ctx.ToUpper,
219
- []string{"upper"},
220
- [][2]string{
221
- {`{{upper "BatMan"}}`, `BATMAN`},
222
- },
223
- )
224
-
225
- return ns
226
- }
227
-
228
- internal.AddTemplateFuncsNamespace(f)
229
- }
@@ -1,125 +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 strings
15
-
16
- import (
17
- "regexp"
18
- "sync"
19
-
20
- "github.com/spf13/cast"
21
- )
22
-
23
- // FindRE returns a list of strings that match the regular expression. By default all matches
24
- // will be included. The number of matches can be limited with an optional third parameter.
25
- func (ns *Namespace) FindRE(expr string, content interface{}, limit ...interface{}) ([]string, error) {
26
- re, err := reCache.Get(expr)
27
- if err != nil {
28
- return nil, err
29
- }
30
-
31
- conv, err := cast.ToStringE(content)
32
- if err != nil {
33
- return nil, err
34
- }
35
-
36
- if len(limit) == 0 {
37
- return re.FindAllString(conv, -1), nil
38
- }
39
-
40
- lim, err := cast.ToIntE(limit[0])
41
- if err != nil {
42
- return nil, err
43
- }
44
-
45
- return re.FindAllString(conv, lim), nil
46
- }
47
-
48
- // ReplaceRE returns a copy of s, replacing all matches of the regular
49
- // expression pattern with the replacement text repl. The number of replacements
50
- // can be limited with an optional fourth parameter.
51
- func (ns *Namespace) ReplaceRE(pattern, repl, s interface{}, n ...interface{}) (_ string, err error) {
52
- sp, err := cast.ToStringE(pattern)
53
- if err != nil {
54
- return
55
- }
56
-
57
- sr, err := cast.ToStringE(repl)
58
- if err != nil {
59
- return
60
- }
61
-
62
- ss, err := cast.ToStringE(s)
63
- if err != nil {
64
- return
65
- }
66
-
67
- nn := -1
68
- if len(n) > 0 {
69
- nn, err = cast.ToIntE(n[0])
70
- if err != nil {
71
- return
72
- }
73
- }
74
-
75
- re, err := reCache.Get(sp)
76
- if err != nil {
77
- return "", err
78
- }
79
-
80
- return re.ReplaceAllStringFunc(ss, func(str string) string {
81
- if nn == 0 {
82
- return str
83
- }
84
-
85
- nn -= 1
86
- return re.ReplaceAllString(str, sr)
87
- }), nil
88
- }
89
-
90
- // regexpCache represents a cache of regexp objects protected by a mutex.
91
- type regexpCache struct {
92
- mu sync.RWMutex
93
- re map[string]*regexp.Regexp
94
- }
95
-
96
- // Get retrieves a regexp object from the cache based upon the pattern.
97
- // If the pattern is not found in the cache, create one
98
- func (rc *regexpCache) Get(pattern string) (re *regexp.Regexp, err error) {
99
- var ok bool
100
-
101
- if re, ok = rc.get(pattern); !ok {
102
- re, err = regexp.Compile(pattern)
103
- if err != nil {
104
- return nil, err
105
- }
106
- rc.set(pattern, re)
107
- }
108
-
109
- return re, nil
110
- }
111
-
112
- func (rc *regexpCache) get(key string) (re *regexp.Regexp, ok bool) {
113
- rc.mu.RLock()
114
- re, ok = rc.re[key]
115
- rc.mu.RUnlock()
116
- return
117
- }
118
-
119
- func (rc *regexpCache) set(key string, re *regexp.Regexp) {
120
- rc.mu.Lock()
121
- rc.re[key] = re
122
- rc.mu.Unlock()
123
- }
124
-
125
- var reCache = regexpCache{re: make(map[string]*regexp.Regexp)}