diff options
author | Bjørn Erik Pedersen <[email protected]> | 2023-02-21 18:32:09 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <[email protected]> | 2023-02-22 13:26:10 +0100 |
commit | ecf3cd514fe0e4c848513053721ee9573108f666 (patch) | |
tree | 4b2815053abe072e0218aa82d92025b5cab6a6fc /tpl | |
parent | a1a9c08b5ffee8432d9ed23143233d5f00b50608 (diff) | |
download | hugo-ecf3cd514fe0e4c848513053721ee9573108f666.tar.gz hugo-ecf3cd514fe0e4c848513053721ee9573108f666.zip |
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
Diffstat (limited to 'tpl')
-rw-r--r-- | tpl/css/css.go | 41 | ||||
-rw-r--r-- | tpl/tplimpl/template_funcs.go | 1 |
2 files changed, 42 insertions, 0 deletions
diff --git a/tpl/css/css.go b/tpl/css/css.go new file mode 100644 index 000000000..e1783334e --- /dev/null +++ b/tpl/css/css.go @@ -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) +} diff --git a/tpl/tplimpl/template_funcs.go b/tpl/tplimpl/template_funcs.go index e664bd6c5..b8102c75d 100644 --- a/tpl/tplimpl/template_funcs.go +++ b/tpl/tplimpl/template_funcs.go @@ -36,6 +36,7 @@ import ( _ "github.com/gohugoio/hugo/tpl/collections" _ "github.com/gohugoio/hugo/tpl/compare" _ "github.com/gohugoio/hugo/tpl/crypto" + _ "github.com/gohugoio/hugo/tpl/css" _ "github.com/gohugoio/hugo/tpl/data" _ "github.com/gohugoio/hugo/tpl/debug" _ "github.com/gohugoio/hugo/tpl/diagrams" |