honk/vendor/humungus.tedunangst.com/r/webs/templates/sprintf.go
2022-11-13 16:19:53 +01:00

44 lines
1.4 KiB
Go

//
// Copyright (c) 2019 Ted Unangst <tedu@tedunangst.com>
//
// Permission to use, copy, modify, and distribute this software for any
// purpose with or without fee is hereby granted, provided that the above
// copyright notice and this permission notice appear in all copies.
//
// THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
// WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
// ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
// WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
// ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
// OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
package templates
import (
"fmt"
"html/template"
"io"
)
// fmt.Sprintf, but strings (%s) will be html escaped.
func Sprintf(s string, args ...interface{}) template.HTML {
for i, a := range args {
switch s := a.(type) {
case string:
args[i] = template.HTMLEscapeString(s)
}
}
return template.HTML(fmt.Sprintf(s, args...))
}
// fmt.Fprintf, but strings (%s) will be html escaped.
func Fprintf(w io.Writer, s string, args ...interface{}) (int, error) {
for i, a := range args {
switch s := a.(type) {
case string:
args[i] = template.HTMLEscapeString(s)
}
}
return fmt.Fprintf(w, s, args...)
}