mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-17 21:01:26 +02:00
tocss: Simplify the hugo:vars type handling
Instead of maintaing a list of all CSS units and functions this commit: * Uses 3 regexps to detect typed CSS values (e.g. `24px`) + properly handle numeric Go types. * These regexps may have some false positives -- e.g. strings that needs to be quoted. * For that rare case, you can mark the string with e.g. `"32xxx" | css.Quoted` * For the opposite case: `"32" | css.Unquoted` Updates #10632
This commit is contained in:
41
tpl/css/css.go
Normal file
41
tpl/css/css.go
Normal file
@@ -0,0 +1,41 @@
|
||||
package css
|
||||
|
||||
import (
|
||||
"github.com/gohugoio/hugo/common/types/css"
|
||||
"github.com/gohugoio/hugo/deps"
|
||||
"github.com/gohugoio/hugo/tpl/internal"
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
const name = "css"
|
||||
|
||||
// Namespace provides template functions for the "css" namespace.
|
||||
type Namespace struct {
|
||||
}
|
||||
|
||||
// Quoted returns a string that needs to be quoted in CSS.
|
||||
func (ns *Namespace) Quoted(v any) css.QuotedString {
|
||||
s := cast.ToString(v)
|
||||
return css.QuotedString(s)
|
||||
}
|
||||
|
||||
// Unquoted returns a string that does not need to be quoted in CSS.
|
||||
func (ns *Namespace) Unquoted(v any) css.UnquotedString {
|
||||
s := cast.ToString(v)
|
||||
return css.UnquotedString(s)
|
||||
}
|
||||
|
||||
func init() {
|
||||
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace {
|
||||
ctx := &Namespace{}
|
||||
|
||||
ns := &internal.TemplateFuncsNamespace{
|
||||
Name: name,
|
||||
Context: func(args ...any) (any, error) { return ctx, nil },
|
||||
}
|
||||
|
||||
return ns
|
||||
}
|
||||
|
||||
internal.AddTemplateFuncsNamespace(f)
|
||||
}
|
Reference in New Issue
Block a user