Skip to content

Commit

Permalink
tpl: Add a warnf template func
Browse files Browse the repository at this point in the history
Fixes #6628
  • Loading branch information
bep committed Dec 21, 2019
1 parent 8a58ebb commit 1773d71
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
17 changes: 11 additions & 6 deletions docs/content/en/functions/errorf.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
---
title: errorf
linktitle: errorf
description: Log ERROR and fail the build from the templates.
title: errorf and warnf
description: Log ERROR or WARNING from the templates.
date: 2017-09-30
publishdate: 2017-09-30
lastmod: 2017-09-30
Expand All @@ -18,12 +17,18 @@ deprecated: false
aliases: []
---

`errorf` will evaluate a format string, then output the result to the ERROR log (and only once per error message to avoid flooding the log).
`errorf` or `warnf` will evaluate a format string, then output the result to the ERROR or WARNING log (and only once per error message to avoid flooding the log).

This will also cause the build to fail (the `hugo` command will `exit -1`).
Any ERROR will also cause the build to fail (the `hugo` command will `exit -1`).

Note that the WARNING will only be printed to the console.

```
{{ errorf "Failed to handle page %q" .Path }}
```

Note that `errorf` supports all the formatting verbs of the [fmt](https://golang.org/pkg/fmt/) package.
```
{{ warnf "You should update the shortcodes in %q" .Path }}
```

Note that `errorf` and `warnf` support all the formatting verbs of the [fmt](https://golang.org/pkg/fmt/) package.
16 changes: 16 additions & 0 deletions docs/data/docs.json
Original file line number Diff line number Diff line change
Expand Up @@ -3053,6 +3053,22 @@
"works!\n"
]
]
},
"Warnf": {
"Description": "Warnf formats according to a format specifier and logs a WARNING.\nIt returns an empty string.",
"Args": [
"format",
"a"
],
"Aliases": [
"warnf"
],
"Examples": [
[
"{{ warnf \"%s.\" \"warning\" }}",
""
]
]
}
},
"hugo": {
Expand Down
13 changes: 12 additions & 1 deletion tpl/fmt/fmt.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,16 @@ import (

// New returns a new instance of the fmt-namespaced template functions.
func New(d *deps.Deps) *Namespace {
return &Namespace{helpers.NewDistinctLogger(d.Log.ERROR)}
return &Namespace{
errorLogger: helpers.NewDistinctLogger(d.Log.ERROR),
warnLogger: helpers.NewDistinctLogger(d.Log.WARN),
}
}

// Namespace provides template functions for the "fmt" namespace.
type Namespace struct {
errorLogger *helpers.DistinctLogger
warnLogger *helpers.DistinctLogger
}

// Print returns string representation of the passed arguments.
Expand All @@ -53,3 +57,10 @@ func (ns *Namespace) Errorf(format string, a ...interface{}) string {
ns.errorLogger.Printf(format, a...)
return _fmt.Sprintf(format, a...)
}

// Warnf formats according to a format specifier and logs a WARNING.
// It returns an empty string.
func (ns *Namespace) Warnf(format string, a ...interface{}) string {
ns.warnLogger.Printf(format, a...)
return ""
}
6 changes: 6 additions & 0 deletions tpl/fmt/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,12 @@ func init() {
},
)

ns.AddMethodMapping(ctx.Warnf,
[]string{"warnf"},
[][2]string{
{`{{ warnf "%s." "warning" }}`, ``},
},
)
return ns
}

Expand Down

0 comments on commit 1773d71

Please sign in to comment.