mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
tpl: Improve godoc
This commit is contained in:
parent
a6d545854a
commit
6eea32bd6b
8 changed files with 142 additions and 141 deletions
|
@ -29,18 +29,18 @@ func New() *Namespace {
|
||||||
type Namespace struct {
|
type Namespace struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToInt converts the given value to an int.
|
// ToInt converts v to an int.
|
||||||
func (ns *Namespace) ToInt(v any) (int, error) {
|
func (ns *Namespace) ToInt(v any) (int, error) {
|
||||||
v = convertTemplateToString(v)
|
v = convertTemplateToString(v)
|
||||||
return _cast.ToIntE(v)
|
return _cast.ToIntE(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToString converts the given value to a string.
|
// ToString converts v to a string.
|
||||||
func (ns *Namespace) ToString(v any) (string, error) {
|
func (ns *Namespace) ToString(v any) (string, error) {
|
||||||
return _cast.ToStringE(v)
|
return _cast.ToStringE(v)
|
||||||
}
|
}
|
||||||
|
|
||||||
// ToFloat converts the given value to a float.
|
// ToFloat converts v to a float.
|
||||||
func (ns *Namespace) ToFloat(v any) (float64, error) {
|
func (ns *Namespace) ToFloat(v any) (float64, error) {
|
||||||
v = convertTemplateToString(v)
|
v = convertTemplateToString(v)
|
||||||
return _cast.ToFloat64E(v)
|
return _cast.ToFloat64E(v)
|
||||||
|
|
|
@ -47,39 +47,39 @@ type Namespace struct {
|
||||||
distinctLogger loggers.IgnorableLogger
|
distinctLogger loggers.IgnorableLogger
|
||||||
}
|
}
|
||||||
|
|
||||||
// Print returns string representation of the passed arguments.
|
// Print returns a string representation args.
|
||||||
func (ns *Namespace) Print(a ...any) string {
|
func (ns *Namespace) Print(args ...any) string {
|
||||||
return _fmt.Sprint(a...)
|
return _fmt.Sprint(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Printf returns a formatted string representation of the passed arguments.
|
// Printf returns a formatted string representation of args.
|
||||||
func (ns *Namespace) Printf(format string, a ...any) string {
|
func (ns *Namespace) Printf(format string, args ...any) string {
|
||||||
return _fmt.Sprintf(format, a...)
|
return _fmt.Sprintf(format, args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Println returns string representation of the passed arguments ending with a newline.
|
// Println returns string representation of args ending with a newline.
|
||||||
func (ns *Namespace) Println(a ...any) string {
|
func (ns *Namespace) Println(args ...any) string {
|
||||||
return _fmt.Sprintln(a...)
|
return _fmt.Sprintln(args...)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Errorf formats according to a format specifier and logs an ERROR.
|
// Errorf formats args according to a format specifier and logs an ERROR.
|
||||||
// It returns an empty string.
|
// It returns an empty string.
|
||||||
func (ns *Namespace) Errorf(format string, a ...any) string {
|
func (ns *Namespace) Errorf(format string, args ...any) string {
|
||||||
ns.distinctLogger.Errorf(format, a...)
|
ns.distinctLogger.Errorf(format, args...)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Erroridf formats according to a format specifier and logs an ERROR and
|
// Erroridf formats args according to a format specifier and logs an ERROR and
|
||||||
// an information text that the error with the given ID can be suppressed in config.
|
// an information text that the error with the given ID can be suppressed in config.
|
||||||
// It returns an empty string.
|
// It returns an empty string.
|
||||||
func (ns *Namespace) Erroridf(id, format string, a ...any) string {
|
func (ns *Namespace) Erroridf(id, format string, args ...any) string {
|
||||||
ns.distinctLogger.Errorsf(id, format, a...)
|
ns.distinctLogger.Errorsf(id, format, args...)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
||||||
// Warnf formats according to a format specifier and logs a WARNING.
|
// Warnf formats args according to a format specifier and logs a WARNING.
|
||||||
// It returns an empty string.
|
// It returns an empty string.
|
||||||
func (ns *Namespace) Warnf(format string, a ...any) string {
|
func (ns *Namespace) Warnf(format string, args ...any) string {
|
||||||
ns.distinctLogger.Warnf(format, a...)
|
ns.distinctLogger.Warnf(format, args...)
|
||||||
return ""
|
return ""
|
||||||
}
|
}
|
||||||
|
|
|
@ -32,14 +32,14 @@ func New() *Namespace {
|
||||||
// Namespace provides template functions for the "math" namespace.
|
// Namespace provides template functions for the "math" namespace.
|
||||||
type Namespace struct{}
|
type Namespace struct{}
|
||||||
|
|
||||||
// Add adds two numbers.
|
// Add adds the two addends n1 and n2.
|
||||||
func (ns *Namespace) Add(a, b any) (any, error) {
|
func (ns *Namespace) Add(n1, n2 any) (any, error) {
|
||||||
return _math.DoArithmetic(a, b, '+')
|
return _math.DoArithmetic(n1, n2, '+')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ceil returns the least integer value greater than or equal to x.
|
// Ceil returns the least integer value greater than or equal to n.
|
||||||
func (ns *Namespace) Ceil(x any) (float64, error) {
|
func (ns *Namespace) Ceil(n any) (float64, error) {
|
||||||
xf, err := cast.ToFloat64E(x)
|
xf, err := cast.ToFloat64E(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New("Ceil operator can't be used with non-float value")
|
return 0, errors.New("Ceil operator can't be used with non-float value")
|
||||||
}
|
}
|
||||||
|
@ -47,14 +47,14 @@ func (ns *Namespace) Ceil(x any) (float64, error) {
|
||||||
return math.Ceil(xf), nil
|
return math.Ceil(xf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Div divides two numbers.
|
// Div divides n1 by n2.
|
||||||
func (ns *Namespace) Div(a, b any) (any, error) {
|
func (ns *Namespace) Div(n1, n2 any) (any, error) {
|
||||||
return _math.DoArithmetic(a, b, '/')
|
return _math.DoArithmetic(n1, n2, '/')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Floor returns the greatest integer value less than or equal to x.
|
// Floor returns the greatest integer value less than or equal to n.
|
||||||
func (ns *Namespace) Floor(x any) (float64, error) {
|
func (ns *Namespace) Floor(n any) (float64, error) {
|
||||||
xf, err := cast.ToFloat64E(x)
|
xf, err := cast.ToFloat64E(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New("Floor operator can't be used with non-float value")
|
return 0, errors.New("Floor operator can't be used with non-float value")
|
||||||
}
|
}
|
||||||
|
@ -62,9 +62,9 @@ func (ns *Namespace) Floor(x any) (float64, error) {
|
||||||
return math.Floor(xf), nil
|
return math.Floor(xf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Log returns the natural logarithm of a number.
|
// Log returns the natural logarithm of the number n.
|
||||||
func (ns *Namespace) Log(a any) (float64, error) {
|
func (ns *Namespace) Log(n any) (float64, error) {
|
||||||
af, err := cast.ToFloat64E(a)
|
af, err := cast.ToFloat64E(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New("Log operator can't be used with non integer or float value")
|
return 0, errors.New("Log operator can't be used with non integer or float value")
|
||||||
}
|
}
|
||||||
|
@ -72,10 +72,10 @@ func (ns *Namespace) Log(a any) (float64, error) {
|
||||||
return math.Log(af), nil
|
return math.Log(af), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Max returns the greater of two numbers.
|
// Max returns the greater of the two numbers n1 or n2.
|
||||||
func (ns *Namespace) Max(a, b any) (float64, error) {
|
func (ns *Namespace) Max(n1, n2 any) (float64, error) {
|
||||||
af, erra := cast.ToFloat64E(a)
|
af, erra := cast.ToFloat64E(n1)
|
||||||
bf, errb := cast.ToFloat64E(b)
|
bf, errb := cast.ToFloat64E(n2)
|
||||||
|
|
||||||
if erra != nil || errb != nil {
|
if erra != nil || errb != nil {
|
||||||
return 0, errors.New("Max operator can't be used with non-float value")
|
return 0, errors.New("Max operator can't be used with non-float value")
|
||||||
|
@ -84,10 +84,10 @@ func (ns *Namespace) Max(a, b any) (float64, error) {
|
||||||
return math.Max(af, bf), nil
|
return math.Max(af, bf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Min returns the smaller of two numbers.
|
// Min returns the smaller of two numbers n1 or n2.
|
||||||
func (ns *Namespace) Min(a, b any) (float64, error) {
|
func (ns *Namespace) Min(n1, n2 any) (float64, error) {
|
||||||
af, erra := cast.ToFloat64E(a)
|
af, erra := cast.ToFloat64E(n1)
|
||||||
bf, errb := cast.ToFloat64E(b)
|
bf, errb := cast.ToFloat64E(n2)
|
||||||
|
|
||||||
if erra != nil || errb != nil {
|
if erra != nil || errb != nil {
|
||||||
return 0, errors.New("Min operator can't be used with non-float value")
|
return 0, errors.New("Min operator can't be used with non-float value")
|
||||||
|
@ -96,10 +96,10 @@ func (ns *Namespace) Min(a, b any) (float64, error) {
|
||||||
return math.Min(af, bf), nil
|
return math.Min(af, bf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mod returns a % b.
|
// Mod returns n1 % n2.
|
||||||
func (ns *Namespace) Mod(a, b any) (int64, error) {
|
func (ns *Namespace) Mod(n1, n2 any) (int64, error) {
|
||||||
ai, erra := cast.ToInt64E(a)
|
ai, erra := cast.ToInt64E(n1)
|
||||||
bi, errb := cast.ToInt64E(b)
|
bi, errb := cast.ToInt64E(n2)
|
||||||
|
|
||||||
if erra != nil || errb != nil {
|
if erra != nil || errb != nil {
|
||||||
return 0, errors.New("modulo operator can't be used with non integer value")
|
return 0, errors.New("modulo operator can't be used with non integer value")
|
||||||
|
@ -112,9 +112,9 @@ func (ns *Namespace) Mod(a, b any) (int64, error) {
|
||||||
return ai % bi, nil
|
return ai % bi, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// ModBool returns the boolean of a % b. If a % b == 0, return true.
|
// ModBool returns the boolean of n1 % n2. If n1 % n2 == 0, return true.
|
||||||
func (ns *Namespace) ModBool(a, b any) (bool, error) {
|
func (ns *Namespace) ModBool(n1, n2 any) (bool, error) {
|
||||||
res, err := ns.Mod(a, b)
|
res, err := ns.Mod(n1, n2)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return false, err
|
return false, err
|
||||||
}
|
}
|
||||||
|
@ -122,15 +122,15 @@ func (ns *Namespace) ModBool(a, b any) (bool, error) {
|
||||||
return res == int64(0), nil
|
return res == int64(0), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Mul multiplies two numbers.
|
// Mul multiplies the two numbers n1 and n2.
|
||||||
func (ns *Namespace) Mul(a, b any) (any, error) {
|
func (ns *Namespace) Mul(n1, n2 any) (any, error) {
|
||||||
return _math.DoArithmetic(a, b, '*')
|
return _math.DoArithmetic(n1, n2, '*')
|
||||||
}
|
}
|
||||||
|
|
||||||
// Pow returns a raised to the power of b.
|
// Pow returns n1 raised to the power of n2.
|
||||||
func (ns *Namespace) Pow(a, b any) (float64, error) {
|
func (ns *Namespace) Pow(n1, n2 any) (float64, error) {
|
||||||
af, erra := cast.ToFloat64E(a)
|
af, erra := cast.ToFloat64E(n1)
|
||||||
bf, errb := cast.ToFloat64E(b)
|
bf, errb := cast.ToFloat64E(n2)
|
||||||
|
|
||||||
if erra != nil || errb != nil {
|
if erra != nil || errb != nil {
|
||||||
return 0, errors.New("Pow operator can't be used with non-float value")
|
return 0, errors.New("Pow operator can't be used with non-float value")
|
||||||
|
@ -139,9 +139,9 @@ func (ns *Namespace) Pow(a, b any) (float64, error) {
|
||||||
return math.Pow(af, bf), nil
|
return math.Pow(af, bf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Round returns the nearest integer, rounding half away from zero.
|
// Round returns the integer nearest to n, rounding half away from zero.
|
||||||
func (ns *Namespace) Round(x any) (float64, error) {
|
func (ns *Namespace) Round(n any) (float64, error) {
|
||||||
xf, err := cast.ToFloat64E(x)
|
xf, err := cast.ToFloat64E(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New("Round operator can't be used with non-float value")
|
return 0, errors.New("Round operator can't be used with non-float value")
|
||||||
}
|
}
|
||||||
|
@ -149,9 +149,9 @@ func (ns *Namespace) Round(x any) (float64, error) {
|
||||||
return _round(xf), nil
|
return _round(xf), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sqrt returns the square root of a number.
|
// Sqrt returns the square root of the number n.
|
||||||
func (ns *Namespace) Sqrt(a any) (float64, error) {
|
func (ns *Namespace) Sqrt(n any) (float64, error) {
|
||||||
af, err := cast.ToFloat64E(a)
|
af, err := cast.ToFloat64E(n)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, errors.New("Sqrt operator can't be used with non integer or float value")
|
return 0, errors.New("Sqrt operator can't be used with non integer or float value")
|
||||||
}
|
}
|
||||||
|
@ -159,9 +159,9 @@ func (ns *Namespace) Sqrt(a any) (float64, error) {
|
||||||
return math.Sqrt(af), nil
|
return math.Sqrt(af), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Sub subtracts two numbers.
|
// Sub subtracts n2 from n1.
|
||||||
func (ns *Namespace) Sub(a, b any) (any, error) {
|
func (ns *Namespace) Sub(n1, n2 any) (any, error) {
|
||||||
return _math.DoArithmetic(a, b, '-')
|
return _math.DoArithmetic(n1, n2, '-')
|
||||||
}
|
}
|
||||||
|
|
||||||
var counter uint64
|
var counter uint64
|
||||||
|
|
|
@ -30,44 +30,44 @@ func New() *Namespace {
|
||||||
// Namespace provides template functions for the "safe" namespace.
|
// Namespace provides template functions for the "safe" namespace.
|
||||||
type Namespace struct{}
|
type Namespace struct{}
|
||||||
|
|
||||||
// CSS returns a given string as html/template CSS content.
|
// CSS returns the string s as html/template CSS content.
|
||||||
func (ns *Namespace) CSS(a any) (template.CSS, error) {
|
func (ns *Namespace) CSS(s any) (template.CSS, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
return template.CSS(s), err
|
return template.CSS(ss), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTML returns a given string as html/template HTML content.
|
// HTML returns the string s as html/template HTML content.
|
||||||
func (ns *Namespace) HTML(a any) (template.HTML, error) {
|
func (ns *Namespace) HTML(s any) (template.HTML, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
return template.HTML(s), err
|
return template.HTML(ss), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTMLAttr returns a given string as html/template HTMLAttr content.
|
// HTMLAttr returns the string s as html/template HTMLAttr content.
|
||||||
func (ns *Namespace) HTMLAttr(a any) (template.HTMLAttr, error) {
|
func (ns *Namespace) HTMLAttr(s any) (template.HTMLAttr, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
return template.HTMLAttr(s), err
|
return template.HTMLAttr(ss), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// JS returns the given string as a html/template JS content.
|
// JS returns the given string as a html/template JS content.
|
||||||
func (ns *Namespace) JS(a any) (template.JS, error) {
|
func (ns *Namespace) JS(s any) (template.JS, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
return template.JS(s), err
|
return template.JS(ss), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// JSStr returns the given string as a html/template JSStr content.
|
// JSStr returns the given string as a html/template JSStr content.
|
||||||
func (ns *Namespace) JSStr(a any) (template.JSStr, error) {
|
func (ns *Namespace) JSStr(s any) (template.JSStr, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
return template.JSStr(s), err
|
return template.JSStr(ss), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// URL returns a given string as html/template URL content.
|
// URL returns the string s as html/template URL content.
|
||||||
func (ns *Namespace) URL(a any) (template.URL, error) {
|
func (ns *Namespace) URL(s any) (template.URL, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
return template.URL(s), err
|
return template.URL(ss), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// SanitizeURL returns a given string as html/template URL content.
|
// SanitizeURL returns the string s as html/template URL content.
|
||||||
func (ns *Namespace) SanitizeURL(a any) (string, error) {
|
func (ns *Namespace) SanitizeURL(s any) (string, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
return helpers.SanitizeURL(s), err
|
return helpers.SanitizeURL(ss), err
|
||||||
}
|
}
|
||||||
|
|
|
@ -372,7 +372,7 @@ func (ns *Namespace) Title(s any) (string, error) {
|
||||||
return ns.titleFunc(ss), nil
|
return ns.titleFunc(ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// FirstUpper returns a string with the first character as upper case.
|
// FirstUpper converts s making the first character upper case.
|
||||||
func (ns *Namespace) FirstUpper(s any) (string, error) {
|
func (ns *Namespace) FirstUpper(s any) (string, error) {
|
||||||
ss, err := cast.ToStringE(s)
|
ss, err := cast.ToStringE(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -404,8 +404,8 @@ func (ns *Namespace) ToUpper(s any) (string, error) {
|
||||||
return strings.ToUpper(ss), nil
|
return strings.ToUpper(ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Trim returns a string with all leading and trailing characters defined
|
// Trim returns converts the strings s removing all leading and trailing characters defined
|
||||||
// contained in cutset removed.
|
// contained.
|
||||||
func (ns *Namespace) Trim(s, cutset any) (string, error) {
|
func (ns *Namespace) Trim(s, cutset any) (string, error) {
|
||||||
ss, err := cast.ToStringE(s)
|
ss, err := cast.ToStringE(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -484,7 +484,7 @@ func (ns *Namespace) TrimSuffix(suffix, s any) (string, error) {
|
||||||
return strings.TrimSuffix(ss, sx), nil
|
return strings.TrimSuffix(ss, sx), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Repeat returns a new string consisting of count copies of the string s.
|
// Repeat returns a new string consisting of n copies of the string s.
|
||||||
func (ns *Namespace) Repeat(n, s any) (string, error) {
|
func (ns *Namespace) Repeat(n, s any) (string, error) {
|
||||||
ss, err := cast.ToStringE(s)
|
ss, err := cast.ToStringE(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
|
@ -57,9 +57,8 @@ func (ns *Namespace) AsTime(v any, args ...any) (any, error) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Format converts the textual representation of the datetime string into
|
// Format converts the textual representation of the datetime string in v into
|
||||||
// the other form or returns it of the time.Time value. These are formatted
|
// time.Time if needed and formats it with the given layout.
|
||||||
// with the layout string
|
|
||||||
func (ns *Namespace) Format(layout string, v any) (string, error) {
|
func (ns *Namespace) Format(layout string, v any) (string, error) {
|
||||||
t, err := htime.ToTimeInDefaultLocationE(v, ns.location)
|
t, err := htime.ToTimeInDefaultLocationE(v, ns.location)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -74,19 +73,19 @@ func (ns *Namespace) Now() _time.Time {
|
||||||
return _time.Now()
|
return _time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
// ParseDuration parses a duration string.
|
// ParseDuration parses the duration string s.
|
||||||
// A duration string is a possibly signed sequence of
|
// A duration string is a possibly signed sequence of
|
||||||
// decimal numbers, each with optional fraction and a unit suffix,
|
// decimal numbers, each with optional fraction and a unit suffix,
|
||||||
// such as "300ms", "-1.5h" or "2h45m".
|
// such as "300ms", "-1.5h" or "2h45m".
|
||||||
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
// Valid time units are "ns", "us" (or "µs"), "ms", "s", "m", "h".
|
||||||
// See https://golang.org/pkg/time/#ParseDuration
|
// See https://golang.org/pkg/time/#ParseDuration
|
||||||
func (ns *Namespace) ParseDuration(in any) (_time.Duration, error) {
|
func (ns *Namespace) ParseDuration(s any) (_time.Duration, error) {
|
||||||
s, err := cast.ToStringE(in)
|
ss, err := cast.ToStringE(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return 0, err
|
return 0, err
|
||||||
}
|
}
|
||||||
|
|
||||||
return _time.ParseDuration(s)
|
return _time.ParseDuration(ss)
|
||||||
}
|
}
|
||||||
|
|
||||||
var durationUnits = map[string]_time.Duration{
|
var durationUnits = map[string]_time.Duration{
|
||||||
|
|
|
@ -90,9 +90,9 @@ func (ns *Namespace) HighlightCodeBlock(ctx hooks.CodeblockContext, opts ...any)
|
||||||
return hl.HighlightCodeBlock(ctx, optsv)
|
return hl.HighlightCodeBlock(ctx, optsv)
|
||||||
}
|
}
|
||||||
|
|
||||||
// CanHighlight returns whether the given language is supported by the Chroma highlighter.
|
// CanHighlight returns whether the given code language is supported by the Chroma highlighter.
|
||||||
func (ns *Namespace) CanHighlight(lang string) bool {
|
func (ns *Namespace) CanHighlight(language string) bool {
|
||||||
return lexers.Get(lang) != nil
|
return lexers.Get(language) != nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTMLEscape returns a copy of s with reserved HTML characters escaped.
|
// HTMLEscape returns a copy of s with reserved HTML characters escaped.
|
||||||
|
@ -105,7 +105,7 @@ func (ns *Namespace) HTMLEscape(s any) (string, error) {
|
||||||
return html.EscapeString(ss), nil
|
return html.EscapeString(ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// HTMLUnescape returns a copy of with HTML escape requences converted to plain
|
// HTMLUnescape returns a copy of s with HTML escape requences converted to plain
|
||||||
// text.
|
// text.
|
||||||
func (ns *Namespace) HTMLUnescape(s any) (string, error) {
|
func (ns *Namespace) HTMLUnescape(s any) (string, error) {
|
||||||
ss, err := cast.ToStringE(s)
|
ss, err := cast.ToStringE(s)
|
||||||
|
@ -116,7 +116,7 @@ func (ns *Namespace) HTMLUnescape(s any) (string, error) {
|
||||||
return html.UnescapeString(ss), nil
|
return html.UnescapeString(ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Markdownify renders a given input from Markdown to HTML.
|
// Markdownify renders s from Markdown to HTML.
|
||||||
func (ns *Namespace) Markdownify(s any) (template.HTML, error) {
|
func (ns *Namespace) Markdownify(s any) (template.HTML, error) {
|
||||||
|
|
||||||
home := ns.deps.Site.Home()
|
home := ns.deps.Site.Home()
|
||||||
|
@ -144,6 +144,7 @@ func (ns *Namespace) Plainify(s any) (string, error) {
|
||||||
return helpers.StripHTML(ss), nil
|
return helpers.StripHTML(ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// For internal use.
|
||||||
func (ns *Namespace) Reset() {
|
func (ns *Namespace) Reset() {
|
||||||
ns.cache.Clear()
|
ns.cache.Clear()
|
||||||
}
|
}
|
||||||
|
|
|
@ -40,14 +40,14 @@ type Namespace struct {
|
||||||
multihost bool
|
multihost bool
|
||||||
}
|
}
|
||||||
|
|
||||||
// AbsURL takes a given string and converts it to an absolute URL.
|
// AbsURL takes the string s and converts it to an absolute URL.
|
||||||
func (ns *Namespace) AbsURL(a any) (template.HTML, error) {
|
func (ns *Namespace) AbsURL(s any) (template.HTML, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return template.HTML(ns.deps.PathSpec.AbsURL(s, false)), nil
|
return template.HTML(ns.deps.PathSpec.AbsURL(ss, false)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Parse parses rawurl into a URL structure. The rawurl may be relative or
|
// Parse parses rawurl into a URL structure. The rawurl may be relative or
|
||||||
|
@ -61,38 +61,39 @@ func (ns *Namespace) Parse(rawurl any) (*url.URL, error) {
|
||||||
return url.Parse(s)
|
return url.Parse(s)
|
||||||
}
|
}
|
||||||
|
|
||||||
// RelURL takes a given string and prepends the relative path according to a
|
// RelURL takes the string s and prepends the relative path according to a
|
||||||
// page's position in the project directory structure.
|
// page's position in the project directory structure.
|
||||||
func (ns *Namespace) RelURL(a any) (template.HTML, error) {
|
func (ns *Namespace) RelURL(s any) (template.HTML, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
return template.HTML(ns.deps.PathSpec.RelURL(s, false)), nil
|
return template.HTML(ns.deps.PathSpec.RelURL(ss, false)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// URLize returns the given argument formatted as URL.
|
// URLize returns the the strings s formatted as an URL.
|
||||||
func (ns *Namespace) URLize(a any) (string, error) {
|
func (ns *Namespace) URLize(s any) (string, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
return ns.deps.PathSpec.URLize(s), nil
|
return ns.deps.PathSpec.URLize(ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Anchorize creates sanitized anchor names that are compatible with Blackfriday.
|
// Anchorize creates sanitized anchor name version of the string s that is compatible
|
||||||
func (ns *Namespace) Anchorize(a any) (string, error) {
|
// with how your configured markdown renderer does it.
|
||||||
s, err := cast.ToStringE(a)
|
func (ns *Namespace) Anchorize(s any) (string, error) {
|
||||||
|
ss, err := cast.ToStringE(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", nil
|
return "", nil
|
||||||
}
|
}
|
||||||
return ns.deps.ContentSpec.SanitizeAnchorName(s), nil
|
return ns.deps.ContentSpec.SanitizeAnchorName(ss), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ref returns the absolute URL path to a given content item.
|
// Ref returns the absolute URL path to a given content item from Page p.
|
||||||
func (ns *Namespace) Ref(in any, args any) (template.HTML, error) {
|
func (ns *Namespace) Ref(p any, args any) (template.HTML, error) {
|
||||||
p, ok := in.(urls.RefLinker)
|
pp, ok := p.(urls.RefLinker)
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", errors.New("invalid Page received in Ref")
|
return "", errors.New("invalid Page received in Ref")
|
||||||
}
|
}
|
||||||
|
@ -100,13 +101,13 @@ func (ns *Namespace) Ref(in any, args any) (template.HTML, error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
s, err := p.Ref(argsm)
|
s, err := pp.Ref(argsm)
|
||||||
return template.HTML(s), err
|
return template.HTML(s), err
|
||||||
}
|
}
|
||||||
|
|
||||||
// RelRef returns the relative URL path to a given content item.
|
// RelRef returns the relative URL path to a given content item from Page p.
|
||||||
func (ns *Namespace) RelRef(in any, args any) (template.HTML, error) {
|
func (ns *Namespace) RelRef(p any, args any) (template.HTML, error) {
|
||||||
p, ok := in.(urls.RefLinker)
|
pp, ok := p.(urls.RefLinker)
|
||||||
if !ok {
|
if !ok {
|
||||||
return "", errors.New("invalid Page received in RelRef")
|
return "", errors.New("invalid Page received in RelRef")
|
||||||
}
|
}
|
||||||
|
@ -115,7 +116,7 @@ func (ns *Namespace) RelRef(in any, args any) (template.HTML, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err := p.RelRef(argsm)
|
s, err := pp.RelRef(argsm)
|
||||||
return template.HTML(s), err
|
return template.HTML(s), err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -163,25 +164,25 @@ func (ns *Namespace) refArgsToMap(args any) (map[string]any, error) {
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RelLangURL takes a given string and prepends the relative path according to a
|
// RelLangURL takes the string s and prepends the relative path according to a
|
||||||
// page's position in the project directory structure and the current language.
|
// page's position in the project directory structure and the current language.
|
||||||
func (ns *Namespace) RelLangURL(a any) (template.HTML, error) {
|
func (ns *Namespace) RelLangURL(s any) (template.HTML, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return template.HTML(ns.deps.PathSpec.RelURL(s, !ns.multihost)), nil
|
return template.HTML(ns.deps.PathSpec.RelURL(ss, !ns.multihost)), nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// AbsLangURL takes a given string and converts it to an absolute URL according
|
// AbsLangURL the string s and converts it to an absolute URL according
|
||||||
// to a page's position in the project directory structure and the current
|
// to a page's position in the project directory structure and the current
|
||||||
// language.
|
// language.
|
||||||
func (ns *Namespace) AbsLangURL(a any) (template.HTML, error) {
|
func (ns *Namespace) AbsLangURL(s any) (template.HTML, error) {
|
||||||
s, err := cast.ToStringE(a)
|
ss, err := cast.ToStringE(s)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
return template.HTML(ns.deps.PathSpec.AbsURL(s, !ns.multihost)), nil
|
return template.HTML(ns.deps.PathSpec.AbsURL(ss, !ns.multihost)), nil
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue