2021-10-16 10:24:49 -04:00
|
|
|
// Copyright 2021 The Hugo Authors. All rights reserved.
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-08-12 10:43:37 -04:00
|
|
|
package glob
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
|
|
|
|
import (
|
2021-10-16 10:24:49 -04:00
|
|
|
"os"
|
2019-08-12 10:43:37 -04:00
|
|
|
"path"
|
|
|
|
"path/filepath"
|
2021-10-13 02:12:06 -04:00
|
|
|
"runtime"
|
2019-01-02 05:58:32 -05:00
|
|
|
"strings"
|
|
|
|
"sync"
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
|
2019-01-02 05:58:32 -05:00
|
|
|
"github.com/gobwas/glob"
|
2019-08-12 10:43:37 -04:00
|
|
|
"github.com/gobwas/glob/syntax"
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
)
|
|
|
|
|
2021-10-16 10:24:49 -04:00
|
|
|
const filepathSeparator = string(os.PathSeparator)
|
|
|
|
|
2021-10-13 02:12:06 -04:00
|
|
|
var (
|
|
|
|
isWindows = runtime.GOOS == "windows"
|
|
|
|
defaultGlobCache = &globCache{
|
2022-09-21 11:01:54 -04:00
|
|
|
isWindows: isWindows,
|
|
|
|
cache: make(map[string]globErr),
|
2021-10-13 02:12:06 -04:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2020-10-05 14:01:52 -04:00
|
|
|
type globErr struct {
|
|
|
|
glob glob.Glob
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2021-10-13 02:12:06 -04:00
|
|
|
type globCache struct {
|
|
|
|
// Config
|
2022-09-21 11:01:54 -04:00
|
|
|
isWindows bool
|
2019-01-02 05:58:32 -05:00
|
|
|
|
2021-10-13 02:12:06 -04:00
|
|
|
// Cache
|
|
|
|
sync.RWMutex
|
|
|
|
cache map[string]globErr
|
2021-02-14 13:24:13 -05:00
|
|
|
}
|
|
|
|
|
2021-10-13 02:12:06 -04:00
|
|
|
func (gc *globCache) GetGlob(pattern string) (glob.Glob, error) {
|
2020-10-05 14:01:52 -04:00
|
|
|
var eg globErr
|
2019-01-02 05:58:32 -05:00
|
|
|
|
2021-10-13 02:12:06 -04:00
|
|
|
gc.RLock()
|
2020-10-05 14:01:52 -04:00
|
|
|
var found bool
|
2021-10-13 02:12:06 -04:00
|
|
|
eg, found = gc.cache[pattern]
|
|
|
|
gc.RUnlock()
|
2020-10-05 14:01:52 -04:00
|
|
|
if found {
|
|
|
|
return eg.glob, eg.err
|
2019-01-02 05:58:32 -05:00
|
|
|
}
|
|
|
|
|
2021-10-13 02:12:06 -04:00
|
|
|
var g glob.Glob
|
2020-10-05 14:01:52 -04:00
|
|
|
var err error
|
|
|
|
|
2021-10-13 02:12:06 -04:00
|
|
|
pattern = filepath.ToSlash(pattern)
|
2022-09-21 11:01:54 -04:00
|
|
|
g, err = glob.Compile(strings.ToLower(pattern), '/')
|
2021-10-13 02:12:06 -04:00
|
|
|
|
|
|
|
eg = globErr{
|
|
|
|
globDecorator{
|
2022-09-21 11:01:54 -04:00
|
|
|
g: g,
|
|
|
|
isWindows: gc.isWindows},
|
2021-10-13 02:12:06 -04:00
|
|
|
err,
|
|
|
|
}
|
|
|
|
|
|
|
|
gc.Lock()
|
|
|
|
gc.cache[pattern] = eg
|
|
|
|
gc.Unlock()
|
2020-10-05 14:01:52 -04:00
|
|
|
|
|
|
|
return eg.glob, eg.err
|
Add Hugo Piper with SCSS support and much more
Before this commit, you would have to use page bundles to do image processing etc. in Hugo.
This commit adds
* A new `/assets` top-level project or theme dir (configurable via `assetDir`)
* A new template func, `resources.Get` which can be used to "get a resource" that can be further processed.
This means that you can now do this in your templates (or shortcodes):
```bash
{{ $sunset := (resources.Get "images/sunset.jpg").Fill "300x200" }}
```
This also adds a new `extended` build tag that enables powerful SCSS/SASS support with source maps. To compile this from source, you will also need a C compiler installed:
```
HUGO_BUILD_TAGS=extended mage install
```
Note that you can use output of the SCSS processing later in a non-SCSSS-enabled Hugo.
The `SCSS` processor is a _Resource transformation step_ and it can be chained with the many others in a pipeline:
```bash
{{ $css := resources.Get "styles.scss" | resources.ToCSS | resources.PostCSS | resources.Minify | resources.Fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
The transformation funcs above have aliases, so it can be shortened to:
```bash
{{ $css := resources.Get "styles.scss" | toCSS | postCSS | minify | fingerprint }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" integrity="{{ $styles.Data.Digest }}" media="screen">
```
A quick tip would be to avoid the fingerprinting part, and possibly also the not-superfast `postCSS` when you're doing development, as it allows Hugo to be smarter about the rebuilding.
Documentation will follow, but have a look at the demo repo in https://github.com/bep/hugo-sass-test
New functions to create `Resource` objects:
* `resources.Get` (see above)
* `resources.FromString`: Create a Resource from a string.
New `Resource` transformation funcs:
* `resources.ToCSS`: Compile `SCSS` or `SASS` into `CSS`.
* `resources.PostCSS`: Process your CSS with PostCSS. Config file support (project or theme or passed as an option).
* `resources.Minify`: Currently supports `css`, `js`, `json`, `html`, `svg`, `xml`.
* `resources.Fingerprint`: Creates a fingerprinted version of the given Resource with Subresource Integrity..
* `resources.Concat`: Concatenates a list of Resource objects. Think of this as a poor man's bundler.
* `resources.ExecuteAsTemplate`: Parses and executes the given Resource and data context (e.g. .Site) as a Go template.
Fixes #4381
Fixes #4903
Fixes #4858
2018-02-20 04:02:14 -05:00
|
|
|
}
|
2019-08-12 10:43:37 -04:00
|
|
|
|
2023-05-21 08:25:16 -04:00
|
|
|
// Or creates a new Glob from the given globs.
|
|
|
|
func Or(globs ...glob.Glob) glob.Glob {
|
|
|
|
return globSlice{globs: globs}
|
|
|
|
}
|
|
|
|
|
|
|
|
// MatchesFunc is a convenience type to create a glob.Glob from a function.
|
|
|
|
type MatchesFunc func(s string) bool
|
|
|
|
|
|
|
|
func (m MatchesFunc) Match(s string) bool {
|
|
|
|
return m(s)
|
|
|
|
}
|
|
|
|
|
|
|
|
type globSlice struct {
|
|
|
|
globs []glob.Glob
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g globSlice) Match(s string) bool {
|
|
|
|
for _, g := range g.globs {
|
|
|
|
if g.Match(s) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2021-10-13 02:12:06 -04:00
|
|
|
type globDecorator struct {
|
|
|
|
// On Windows we may get filenames with Windows slashes to match,
|
2023-02-18 15:47:35 -05:00
|
|
|
// which we need to normalize.
|
2021-10-13 02:12:06 -04:00
|
|
|
isWindows bool
|
|
|
|
|
|
|
|
g glob.Glob
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g globDecorator) Match(s string) bool {
|
|
|
|
if g.isWindows {
|
|
|
|
s = filepath.ToSlash(s)
|
|
|
|
}
|
2022-09-23 07:54:09 -04:00
|
|
|
s = strings.ToLower(s)
|
2021-10-13 02:12:06 -04:00
|
|
|
return g.g.Match(s)
|
|
|
|
}
|
|
|
|
|
2022-09-21 11:01:54 -04:00
|
|
|
type globDecoratorDouble struct {
|
2022-09-23 07:54:09 -04:00
|
|
|
lowerCase glob.Glob
|
2022-09-21 11:01:54 -04:00
|
|
|
originalCase glob.Glob
|
|
|
|
}
|
|
|
|
|
|
|
|
func (g globDecoratorDouble) Match(s string) bool {
|
|
|
|
return g.lowerCase.Match(s) || g.originalCase.Match(s)
|
|
|
|
}
|
|
|
|
|
2021-10-13 02:12:06 -04:00
|
|
|
func GetGlob(pattern string) (glob.Glob, error) {
|
|
|
|
return defaultGlobCache.GetGlob(pattern)
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:43:37 -04:00
|
|
|
func NormalizePath(p string) string {
|
2022-09-23 07:54:09 -04:00
|
|
|
return strings.ToLower(NormalizePathNoLower(p))
|
2019-08-12 10:43:37 -04:00
|
|
|
}
|
|
|
|
|
2022-09-23 07:54:09 -04:00
|
|
|
func NormalizePathNoLower(p string) string {
|
2022-09-21 11:01:54 -04:00
|
|
|
return strings.Trim(path.Clean(filepath.ToSlash(p)), "/.")
|
|
|
|
}
|
|
|
|
|
2019-08-12 10:43:37 -04:00
|
|
|
// ResolveRootDir takes a normalized path on the form "assets/**.json" and
|
|
|
|
// determines any root dir, i.e. any start path without any wildcards.
|
|
|
|
func ResolveRootDir(p string) string {
|
|
|
|
parts := strings.Split(path.Dir(p), "/")
|
|
|
|
var roots []string
|
|
|
|
for _, part := range parts {
|
2019-08-13 06:35:04 -04:00
|
|
|
if HasGlobChar(part) {
|
2019-08-12 10:43:37 -04:00
|
|
|
break
|
|
|
|
}
|
|
|
|
roots = append(roots, part)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(roots) == 0 {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
return strings.Join(roots, "/")
|
|
|
|
}
|
2019-08-13 06:35:04 -04:00
|
|
|
|
|
|
|
// FilterGlobParts removes any string with glob wildcard.
|
|
|
|
func FilterGlobParts(a []string) []string {
|
|
|
|
b := a[:0]
|
|
|
|
for _, x := range a {
|
|
|
|
if !HasGlobChar(x) {
|
|
|
|
b = append(b, x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return b
|
|
|
|
}
|
|
|
|
|
|
|
|
// HasGlobChar returns whether s contains any glob wildcards.
|
|
|
|
func HasGlobChar(s string) bool {
|
|
|
|
for i := 0; i < len(s); i++ {
|
|
|
|
if syntax.Special(s[i]) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|