| 1 |
package css |
| 2 |
|
| 3 |
import ( |
| 4 |
"context" |
| 5 |
"errors" |
| 6 |
"fmt" |
| 7 |
"sync" |
| 8 |
|
| 9 |
"github.com/gohugoio/hugo/common/hashing" |
| 10 |
"github.com/gohugoio/hugo/common/hmaps" |
| 11 |
"github.com/gohugoio/hugo/common/hugio" |
| 12 |
"github.com/gohugoio/hugo/common/hugo" |
| 13 |
"github.com/gohugoio/hugo/common/paths" |
| 14 |
"github.com/gohugoio/hugo/common/types/css" |
| 15 |
"github.com/gohugoio/hugo/deps" |
| 16 |
"github.com/gohugoio/hugo/identity" |
| 17 |
"github.com/gohugoio/hugo/markup/highlight" |
| 18 |
"github.com/gohugoio/hugo/markup/markup_config" |
| 19 |
"github.com/gohugoio/hugo/resources" |
| 20 |
"github.com/gohugoio/hugo/resources/resource" |
| 21 |
"github.com/gohugoio/hugo/resources/resource_transformers/babel" |
| 22 |
"github.com/gohugoio/hugo/resources/resource_transformers/cssjs" |
| 23 |
jstransform "github.com/gohugoio/hugo/resources/resource_transformers/js" |
| 24 |
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/dartsass" |
| 25 |
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/sass" |
| 26 |
"github.com/gohugoio/hugo/resources/resource_transformers/tocss/scss" |
| 27 |
"github.com/gohugoio/hugo/tpl/internal" |
| 28 |
"github.com/gohugoio/hugo/tpl/internal/resourcehelpers" |
| 29 |
"github.com/mitchellh/mapstructure" |
| 30 |
"github.com/spf13/cast" |
| 31 |
) |
| 32 |
|
| 33 |
const name = "css" |
| 34 |
|
| 35 |
// Namespace provides template functions for the "css" namespace. |
| 36 |
type Namespace struct { |
| 37 |
d *deps.Deps |
| 38 |
scssClientLibSass *scss.Client |
| 39 |
postcssClient *cssjs.PostCSSClient |
| 40 |
tailwindcssClient *cssjs.TailwindCSSClient |
| 41 |
babelClient *babel.Client |
| 42 |
jsTransformClient *jstransform.Client |
| 43 |
chromaStylesCache *hmaps.Cache[uint64, resource.Resource] |
| 44 |
highlightConfig highlight.Config |
| 45 |
|
| 46 |
// The Dart Client requires a os/exec process, so only |
| 47 |
// create it if we really need it. |
| 48 |
// This is mostly to avoid creating one per site build test. |
| 49 |
scssClientDartSassInit sync.Once |
| 50 |
scssClientDartSass *dartsass.Client |
| 51 |
} |
| 52 |
|
| 53 |
// ChromaStyles generates a CSS stylesheet for the Chroma code highlighter |
| 54 |
// and returns it as a Resource. This stylesheet is needed if |
| 55 |
// markup.highlight.noClasses is disabled in config. |
| 56 |
// The style defaults to the markup.highlight.style config setting. |
| 57 |
func (ns *Namespace) ChromaStyles(opts any) (resource.Resource, error) { |
| 58 |
key := hashing.HashUint64(opts) |
| 59 |
return ns.chromaStylesCache.GetOrCreate(key, func() (resource.Resource, error) { |
| 60 |
m, err := hmaps.ToStringMapE(opts) |
| 61 |
if err != nil { |
| 62 |
return nil, err |
| 63 |
} |
| 64 |
var o struct { |
| 65 |
TargetPath string |
| 66 |
highlight.ChromaStylesOptions `mapstructure:",squash"` |
| 67 |
} |
| 68 |
|
| 69 |
if err := mapstructure.WeakDecode(m, &o); err != nil { |
| 70 |
return nil, err |
| 71 |
} |
| 72 |
if o.TargetPath == "" { |
| 73 |
return nil, errors.New("targetPath cannot be empty") |
| 74 |
} |
| 75 |
if o.Style == "" { |
| 76 |
o.Style = ns.highlightConfig.Style |
| 77 |
} |
| 78 |
css, err := highlight.ChromaStylesCSS(o.ChromaStylesOptions) |
| 79 |
if err != nil { |
| 80 |
return nil, err |
| 81 |
} |
| 82 |
|
| 83 |
return ns.d.ResourceSpec.NewResource( |
| 84 |
resources.ResourceSourceDescriptor{ |
| 85 |
LazyPublish: true, |
| 86 |
GroupIdentity: identity.Anonymous, |
| 87 |
OpenReadSeekCloser: func() (hugio.ReadSeekCloser, error) { |
| 88 |
return hugio.NewReadSeekerNoOpCloserFromString(css), nil |
| 89 |
}, |
| 90 |
TargetPath: o.TargetPath, |
| 91 |
}) |
| 92 |
}) |
| 93 |
} |
| 94 |
|
| 95 |
// Quoted returns a string that needs to be quoted in CSS. |
| 96 |
func (ns *Namespace) Quoted(v any) css.QuotedString { |
| 97 |
s := cast.ToString(v) |
| 98 |
return css.QuotedString(s) |
| 99 |
} |
| 100 |
|
| 101 |
// Unquoted returns a string that does not need to be quoted in CSS. |
| 102 |
func (ns *Namespace) Unquoted(v any) css.UnquotedString { |
| 103 |
s := cast.ToString(v) |
| 104 |
return css.UnquotedString(s) |
| 105 |
} |
| 106 |
|
| 107 |
// PostCSS processes the given Resource with PostCSS. |
| 108 |
func (ns *Namespace) PostCSS(args ...any) (resource.Resource, error) { |
| 109 |
if len(args) > 2 { |
| 110 |
return nil, errors.New("must not provide more arguments than resource object and options") |
| 111 |
} |
| 112 |
|
| 113 |
r, m, err := resourcehelpers.ResolveArgs(args) |
| 114 |
if err != nil { |
| 115 |
return nil, err |
| 116 |
} |
| 117 |
|
| 118 |
return ns.postcssClient.Process(r, m) |
| 119 |
} |
| 120 |
|
| 121 |
// TailwindCSS processes the given Resource with tailwindcss. |
| 122 |
func (ns *Namespace) TailwindCSS(args ...any) (resource.Resource, error) { |
| 123 |
if len(args) > 2 { |
| 124 |
return nil, errors.New("must not provide more arguments than resource object and options") |
| 125 |
} |
| 126 |
|
| 127 |
r, m, err := resourcehelpers.ResolveArgs(args) |
| 128 |
if err != nil { |
| 129 |
return nil, err |
| 130 |
} |
| 131 |
|
| 132 |
return ns.tailwindcssClient.Process(r, m) |
| 133 |
} |
| 134 |
|
| 135 |
// Sass processes the given Resource with SASS. |
| 136 |
func (ns *Namespace) Sass(args ...any) (resource.Resource, error) { |
| 137 |
if len(args) > 2 { |
| 138 |
return nil, errors.New("must not provide more arguments than resource object and options") |
| 139 |
} |
| 140 |
|
| 141 |
var ( |
| 142 |
r resources.ResourceTransformer |
| 143 |
m map[string]any |
| 144 |
targetPath string |
| 145 |
err error |
| 146 |
ok bool |
| 147 |
transpiler = sass.TranspilerLibSass // Deprecated default. Will be dartsass in future versions. See below. |
| 148 |
) |
| 149 |
|
| 150 |
r, targetPath, ok = resourcehelpers.ResolveIfFirstArgIsString(args) |
| 151 |
|
| 152 |
if !ok { |
| 153 |
r, m, err = resourcehelpers.ResolveArgs(args) |
| 154 |
if err != nil { |
| 155 |
return nil, err |
| 156 |
} |
| 157 |
} |
| 158 |
|
| 159 |
if m != nil { |
| 160 |
if t, _, found := hmaps.LookupEqualFold(m, "transpiler"); found { |
| 161 |
switch t { |
| 162 |
case sass.TranspilerDart: |
| 163 |
transpiler = cast.ToString(t) |
| 164 |
case sass.TranspilerLibSass: |
| 165 |
hugo.Deprecate("css.Sass: libsass", "Use dartsass instead. See https://gohugo.io/functions/css/sass/#dart-sass", "v0.153.0") |
| 166 |
transpiler = cast.ToString(t) |
| 167 |
default: |
| 168 |
return nil, fmt.Errorf("unsupported transpiler %q; valid values are %q or %q", t, sass.TranspilerLibSass, sass.TranspilerDart) |
| 169 |
} |
| 170 |
} |
| 171 |
} |
| 172 |
|
| 173 |
if transpiler == sass.TranspilerLibSass { |
| 174 |
var options scss.Options |
| 175 |
if targetPath != "" { |
| 176 |
options.TargetPath = paths.ToSlashTrimLeading(targetPath) |
| 177 |
} else if m != nil { |
| 178 |
options, err = scss.DecodeOptions(m) |
| 179 |
if err != nil { |
| 180 |
return nil, err |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
return ns.scssClientLibSass.ToCSS(r, options) |
| 185 |
} |
| 186 |
|
| 187 |
if m == nil { |
| 188 |
m = make(map[string]any) |
| 189 |
} |
| 190 |
if targetPath != "" { |
| 191 |
m["targetPath"] = targetPath |
| 192 |
} |
| 193 |
|
| 194 |
client, err := ns.getscssClientDartSass() |
| 195 |
if err != nil { |
| 196 |
return nil, err |
| 197 |
} |
| 198 |
|
| 199 |
return client.ToCSS(r, m) |
| 200 |
} |
| 201 |
|
| 202 |
// Build processes the given CSS Resource with ESBuild. |
| 203 |
// Note that this method is identical to the one in the js Namespace. |
| 204 |
func (ns *Namespace) Build(args ...any) (resource.Resource, error) { |
| 205 |
var ( |
| 206 |
r resources.ResourceTransformer |
| 207 |
m map[string]any |
| 208 |
targetPath string |
| 209 |
err error |
| 210 |
ok bool |
| 211 |
) |
| 212 |
|
| 213 |
r, targetPath, ok = resourcehelpers.ResolveIfFirstArgIsString(args) |
| 214 |
|
| 215 |
if !ok { |
| 216 |
r, m, err = resourcehelpers.ResolveArgs(args) |
| 217 |
if err != nil { |
| 218 |
return nil, err |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
if targetPath != "" { |
| 223 |
m = map[string]any{"targetPath": targetPath} |
| 224 |
} |
| 225 |
|
| 226 |
return ns.jsTransformClient.Process(r, m) |
| 227 |
} |
| 228 |
|
| 229 |
func init() { |
| 230 |
f := func(d *deps.Deps) *internal.TemplateFuncsNamespace { |
| 231 |
scssClient, err := scss.New(d.BaseFs.Assets, d.ResourceSpec) |
| 232 |
if err != nil { |
| 233 |
panic(err) |
| 234 |
} |
| 235 |
ctx := &Namespace{ |
| 236 |
d: d, |
| 237 |
scssClientLibSass: scssClient, |
| 238 |
postcssClient: cssjs.NewPostCSSClient(d.ResourceSpec), |
| 239 |
tailwindcssClient: cssjs.NewTailwindCSSClient(d.ResourceSpec), |
| 240 |
babelClient: babel.New(d.ResourceSpec), |
| 241 |
jsTransformClient: jstransform.New(d.BaseFs.Assets, d.ResourceSpec, true), |
| 242 |
chromaStylesCache: hmaps.NewCacheWithOptions[uint64, resource.Resource](hmaps.CacheOptions{Size: 10}), |
| 243 |
highlightConfig: d.Conf.GetConfigSection("markup").(markup_config.Config).Highlight, |
| 244 |
} |
| 245 |
|
| 246 |
ns := &internal.TemplateFuncsNamespace{ |
| 247 |
Name: name, |
| 248 |
Context: func(cctx context.Context, args ...any) (any, error) { return ctx, nil }, |
| 249 |
} |
| 250 |
|
| 251 |
ns.AddMethodMapping(ctx.ChromaStyles, |
| 252 |
nil, |
| 253 |
[][2]string{}, |
| 254 |
) |
| 255 |
|
| 256 |
ns.AddMethodMapping(ctx.PostCSS, |
| 257 |
[]string{"postCSS"}, |
| 258 |
[][2]string{}, |
| 259 |
) |
| 260 |
|
| 261 |
ns.AddMethodMapping(ctx.Quoted, |
| 262 |
nil, |
| 263 |
[][2]string{}, |
| 264 |
) |
| 265 |
|
| 266 |
ns.AddMethodMapping(ctx.Sass, |
| 267 |
[]string{"toCSS"}, |
| 268 |
[][2]string{}, |
| 269 |
) |
| 270 |
|
| 271 |
ns.AddMethodMapping(ctx.Build, |
| 272 |
nil, |
| 273 |
[][2]string{}, |
| 274 |
) |
| 275 |
|
| 276 |
ns.AddMethodMapping(ctx.TailwindCSS, |
| 277 |
nil, |
| 278 |
[][2]string{}, |
| 279 |
) |
| 280 |
|
| 281 |
ns.AddMethodMapping(ctx.Unquoted, |
| 282 |
nil, |
| 283 |
[][2]string{}, |
| 284 |
) |
| 285 |
|
| 286 |
return ns |
| 287 |
} |
| 288 |
|
| 289 |
internal.AddTemplateFuncsNamespace(f) |
| 290 |
} |
| 291 |
|
| 292 |
func (ns *Namespace) getscssClientDartSass() (*dartsass.Client, error) { |
| 293 |
var err error |
| 294 |
ns.scssClientDartSassInit.Do(func() { |
| 295 |
ns.scssClientDartSass, err = dartsass.New(ns.d.BaseFs.Assets, ns.d.ResourceSpec) |
| 296 |
if err != nil { |
| 297 |
return |
| 298 |
} |
| 299 |
ns.d.BuildClosers.Add(ns.scssClientDartSass) |
| 300 |
}) |
| 301 |
|
| 302 |
return ns.scssClientDartSass, err |
| 303 |
} |