summaryrefslogtreecommitdiffstats
path: root/tpl
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-01-30 09:23:21 +0100
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2024-01-30 20:12:03 +0100
commit4e84f57efb57f5c8a850e4c1d562a0bcc7bd1700 (patch)
tree678ed4f283b67339c341ad355306ca0d5d771ad8 /tpl
parentf31a6db797c9251a362ef9f8ad4c03fb608b5ac0 (diff)
Add warnidf template function
Also rename config `ignoreErrors` => `ignoreLogs` But the old still works. Closes #9189
Diffstat (limited to 'tpl')
-rw-r--r--tpl/data/data.go4
-rw-r--r--tpl/fmt/fmt.go12
-rw-r--r--tpl/fmt/fmt_integration_test.go29
-rw-r--r--tpl/fmt/init.go7
4 files changed, 39 insertions, 13 deletions
diff --git a/tpl/data/data.go b/tpl/data/data.go
index 7eb730c4c..78f1f3f48 100644
--- a/tpl/data/data.go
+++ b/tpl/data/data.go
@@ -94,7 +94,7 @@ func (ns *Namespace) GetCSV(sep string, args ...any) (d [][]string, err error) {
if security.IsAccessDenied(err) {
return nil, err
}
- ns.deps.Log.Errorsf(constants.ErrRemoteGetCSV, "Failed to get CSV resource %q: %s", url, err)
+ ns.deps.Log.Erroridf(constants.ErrRemoteGetCSV, "Failed to get CSV resource %q: %s", url, err)
return nil, nil
}
@@ -132,7 +132,7 @@ func (ns *Namespace) GetJSON(args ...any) (any, error) {
if security.IsAccessDenied(err) {
return nil, err
}
- ns.deps.Log.Errorsf(constants.ErrRemoteGetJSON, "Failed to get JSON resource %q: %s", url, err)
+ ns.deps.Log.Erroridf(constants.ErrRemoteGetJSON, "Failed to get JSON resource %q: %s", url, err)
return nil, nil
}
diff --git a/tpl/fmt/fmt.go b/tpl/fmt/fmt.go
index 4f18758e0..04dbd339c 100644
--- a/tpl/fmt/fmt.go
+++ b/tpl/fmt/fmt.go
@@ -68,9 +68,7 @@ func (ns *Namespace) Errorf(format string, args ...any) string {
// an information text that the error with the given id can be suppressed in config.
// It returns an empty string.
func (ns *Namespace) Erroridf(id, format string, args ...any) string {
- format += "\nYou can suppress this error by adding the following to your site configuration:\nignoreErrors = ['%s']"
- args = append(args, id)
- ns.logger.Errorsf(id, format, args...)
+ ns.logger.Erroridf(id, format, args...)
return ""
}
@@ -81,6 +79,14 @@ func (ns *Namespace) Warnf(format string, args ...any) string {
return ""
}
+// Warnidf formats args according to a format specifier and logs an WARNING and
+// an information text that the warning with the given id can be suppressed in config.
+// It returns an empty string.
+func (ns *Namespace) Warnidf(id, format string, args ...any) string {
+ ns.logger.Warnidf(id, format, args...)
+ return ""
+}
+
// Warnmf is epxermimental and subject to change at any time.
func (ns *Namespace) Warnmf(m any, format string, args ...any) string {
return ns.logmf(ns.logger.Warn(), m, format, args...)
diff --git a/tpl/fmt/fmt_integration_test.go b/tpl/fmt/fmt_integration_test.go
index 40bfefcdc..74322770e 100644
--- a/tpl/fmt/fmt_integration_test.go
+++ b/tpl/fmt/fmt_integration_test.go
@@ -16,6 +16,7 @@ package fmt_test
import (
"testing"
+ qt "github.com/frankban/quicktest"
"github.com/gohugoio/hugo/hugolib"
)
@@ -32,13 +33,25 @@ ignoreErrors = ['error-b']
{{ erroridf "error-b" "%s" "b"}}
`
- b := hugolib.NewIntegrationTestBuilder(
- hugolib.IntegrationTestConfig{
- T: t,
- TxtarString: files,
- },
- )
+ b, err := hugolib.TestE(t, files)
- b.BuildE()
- b.AssertLogMatches(`^ERROR a\nYou can suppress this error by adding the following to your site configuration:\nignoreErrors = \['error-a'\]\n$`)
+ b.Assert(err, qt.IsNotNil)
+ b.AssertLogMatches(`^ERROR a\nYou can suppress this error by adding the following to your site configuration:\nignoreLogs = \['error-a'\]\n$`)
+}
+
+func TestWarnidf(t *testing.T) {
+ t.Parallel()
+
+ files := `
+-- hugo.toml --
+disableKinds = ['page','rss','section','sitemap','taxonomy','term']
+ignoreLogs = ['warning-b']
+-- layouts/index.html --
+{{ warnidf "warning-a" "%s" "a"}}
+{{ warnidf "warning-b" "%s" "b"}}
+ `
+
+ b := hugolib.Test(t, files, hugolib.TestOptWarn())
+ b.AssertLogContains("WARN a", "You can suppress this warning", "ignoreLogs", "['warning-a']")
+ b.AssertLogNotContains("['warning-b']")
}
diff --git a/tpl/fmt/init.go b/tpl/fmt/init.go
index 8000627e2..701bd3b6a 100644
--- a/tpl/fmt/init.go
+++ b/tpl/fmt/init.go
@@ -66,6 +66,13 @@ func init() {
},
)
+ ns.AddMethodMapping(ctx.Warnidf,
+ []string{"warnidf"},
+ [][2]string{
+ {`{{ warnidf "my-warn-id" "%s." "warning" }}`, ``},
+ },
+ )
+
ns.AddMethodMapping(ctx.Warnf,
[]string{"warnf"},
[][2]string{