2019-01-02 06:33:26 -05:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
2017-02-17 07:30:50 -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.
|
|
|
|
|
|
|
|
package tplimpl
|
|
|
|
|
|
|
|
import (
|
2022-02-09 12:29:49 -05:00
|
|
|
"bytes"
|
2022-02-17 10:51:19 -05:00
|
|
|
"context"
|
2022-02-09 12:29:49 -05:00
|
|
|
"embed"
|
2022-05-02 10:07:52 -04:00
|
|
|
"fmt"
|
2019-12-10 13:56:44 -05:00
|
|
|
"io"
|
2022-02-09 12:29:49 -05:00
|
|
|
"io/fs"
|
2020-01-15 09:59:56 -05:00
|
|
|
"path/filepath"
|
2019-12-10 13:56:44 -05:00
|
|
|
"reflect"
|
|
|
|
"regexp"
|
2021-04-23 07:40:05 -04:00
|
|
|
"sort"
|
2020-01-15 09:59:56 -05:00
|
|
|
"strings"
|
|
|
|
"sync"
|
2022-05-09 04:05:19 -04:00
|
|
|
"time"
|
2020-01-30 14:02:26 -05:00
|
|
|
"unicode"
|
|
|
|
"unicode/utf8"
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
"github.com/gohugoio/hugo/common/types"
|
2023-01-04 12:24:36 -05:00
|
|
|
"github.com/gohugoio/hugo/output/layouts"
|
2019-11-27 07:42:36 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
2019-12-10 02:02:15 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
"github.com/gohugoio/hugo/output"
|
2019-12-10 02:02:15 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
"github.com/gohugoio/hugo/deps"
|
|
|
|
"github.com/spf13/afero"
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
"github.com/gohugoio/hugo/common/herrors"
|
2018-10-03 08:58:09 -04:00
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
2024-06-08 05:52:22 -04:00
|
|
|
"github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate/parse"
|
2018-05-04 11:53:56 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
htmltemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/htmltemplate"
|
|
|
|
texttemplate "github.com/gohugoio/hugo/tpl/internal/go_templates/texttemplate"
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
"github.com/gohugoio/hugo/identity"
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/tpl"
|
2017-03-27 14:43:49 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
textTmplNamePrefix = "_text/"
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-04-15 09:17:46 -04:00
|
|
|
shortcodesPathPrefix = "shortcodes/"
|
|
|
|
internalPathPrefix = "_internal/"
|
2024-01-30 05:43:20 -05:00
|
|
|
embeddedPathPrefix = "_embedded/"
|
2020-01-15 09:59:56 -05:00
|
|
|
baseFileBase = "baseof"
|
2019-04-15 09:17:46 -04:00
|
|
|
)
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// The identifiers may be truncated in the log, e.g.
|
|
|
|
// "executing "main" at <$scaled.SRelPermalin...>: can't evaluate field SRelPermalink in type *resource.Image"
|
2022-05-12 05:43:20 -04:00
|
|
|
// We need this to identify position in templates with base templates applied.
|
2019-12-10 13:56:44 -05:00
|
|
|
var identifiersRe = regexp.MustCompile(`at \<(.*?)(\.{3})?\>:`)
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
var embeddedTemplatesAliases = map[string][]string{
|
|
|
|
"shortcodes/twitter.html": {"shortcodes/tweet.html"},
|
2019-01-02 06:33:26 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
var (
|
2022-02-15 09:26:18 -05:00
|
|
|
_ tpl.TemplateManager = (*templateExec)(nil)
|
|
|
|
_ tpl.TemplateHandler = (*templateExec)(nil)
|
|
|
|
_ tpl.TemplateFuncGetter = (*templateExec)(nil)
|
|
|
|
_ tpl.TemplateFinder = (*templateExec)(nil)
|
|
|
|
_ tpl.UnusedTemplatesProvider = (*templateExec)(nil)
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
_ tpl.Template = (*templateState)(nil)
|
|
|
|
_ tpl.Info = (*templateState)(nil)
|
|
|
|
)
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-30 14:02:26 -05:00
|
|
|
var baseTemplateDefineRe = regexp.MustCompile(`^{{-?\s*define`)
|
2020-01-23 11:34:19 -05:00
|
|
|
|
2020-01-30 14:02:26 -05:00
|
|
|
// needsBaseTemplate returns true if the first non-comment template block is a
|
|
|
|
// define block.
|
|
|
|
// If a base template does not exist, we will handle that when it's used.
|
2020-01-23 11:34:19 -05:00
|
|
|
func needsBaseTemplate(templ string) bool {
|
2020-01-30 14:02:26 -05:00
|
|
|
idx := -1
|
|
|
|
inComment := false
|
|
|
|
for i := 0; i < len(templ); {
|
|
|
|
if !inComment && strings.HasPrefix(templ[i:], "{{/*") {
|
|
|
|
inComment = true
|
|
|
|
i += 4
|
2022-12-03 20:06:56 -05:00
|
|
|
} else if !inComment && strings.HasPrefix(templ[i:], "{{- /*") {
|
|
|
|
inComment = true
|
|
|
|
i += 6
|
2020-01-30 14:02:26 -05:00
|
|
|
} else if inComment && strings.HasPrefix(templ[i:], "*/}}") {
|
|
|
|
inComment = false
|
|
|
|
i += 4
|
2022-12-03 20:06:56 -05:00
|
|
|
} else if inComment && strings.HasPrefix(templ[i:], "*/ -}}") {
|
|
|
|
inComment = false
|
|
|
|
i += 6
|
2020-01-30 14:02:26 -05:00
|
|
|
} else {
|
|
|
|
r, size := utf8.DecodeRuneInString(templ[i:])
|
|
|
|
if !inComment {
|
|
|
|
if strings.HasPrefix(templ[i:], "{{") {
|
|
|
|
idx = i
|
|
|
|
break
|
|
|
|
} else if !unicode.IsSpace(r) {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
i += size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if idx == -1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return baseTemplateDefineRe.MatchString(templ[idx:])
|
2020-01-23 11:34:19 -05:00
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func newStandaloneTextTemplate(funcs map[string]any) tpl.TemplateParseFinder {
|
2020-01-15 09:59:56 -05:00
|
|
|
return &textTemplateWrapperWithLock{
|
|
|
|
RWMutex: &sync.RWMutex{},
|
|
|
|
Template: texttemplate.New("").Funcs(funcs),
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func newTemplateHandlers(d *deps.Deps) (*tpl.TemplateHandlers, error) {
|
2020-01-15 09:59:56 -05:00
|
|
|
exec, funcs := newTemplateExecuter(d)
|
2022-03-17 17:03:27 -04:00
|
|
|
funcMap := make(map[string]any)
|
2020-01-15 09:59:56 -05:00
|
|
|
for k, v := range funcs {
|
|
|
|
funcMap[k] = v.Interface()
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2022-02-15 09:26:18 -05:00
|
|
|
var templateUsageTracker map[string]templateInfo
|
2023-01-04 12:24:36 -05:00
|
|
|
if d.Conf.PrintUnusedTemplates() {
|
2022-02-15 09:26:18 -05:00
|
|
|
templateUsageTracker = make(map[string]templateInfo)
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
h := &templateHandler{
|
|
|
|
nameBaseTemplateName: make(map[string]string),
|
|
|
|
transformNotFound: make(map[string]*templateState),
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
shortcodes: make(map[string]*shortcodeTemplates),
|
|
|
|
templateInfo: make(map[string]tpl.Info),
|
|
|
|
baseof: make(map[string]templateInfo),
|
|
|
|
needsBaseof: make(map[string]templateInfo),
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2020-07-13 07:40:35 -04:00
|
|
|
main: newTemplateNamespace(funcMap),
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
Deps: d,
|
2023-01-04 12:24:36 -05:00
|
|
|
layoutHandler: layouts.NewLayoutHandler(),
|
2020-01-15 09:59:56 -05:00
|
|
|
layoutsFs: d.BaseFs.Layouts.Fs,
|
2022-07-13 08:47:32 -04:00
|
|
|
layoutTemplateCache: make(map[layoutCacheKey]layoutCacheEntry),
|
2022-02-15 09:26:18 -05:00
|
|
|
|
|
|
|
templateUsageTracker: templateUsageTracker,
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
if err := h.loadEmbedded(); err != nil {
|
|
|
|
return nil, err
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
if err := h.loadTemplates(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2024-10-23 13:26:13 -04:00
|
|
|
if err := h.main.createPrototypes(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
e := &templateExec{
|
|
|
|
d: d,
|
|
|
|
executor: exec,
|
|
|
|
funcs: funcs,
|
|
|
|
templateHandler: h,
|
|
|
|
}
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
if err := e.postTransform(); err != nil {
|
|
|
|
return nil, err
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
return &tpl.TemplateHandlers{
|
|
|
|
Tmpl: e,
|
|
|
|
TxtTmpl: newStandaloneTextTemplate(funcMap),
|
|
|
|
}, nil
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func newTemplateNamespace(funcs map[string]any) *templateNamespace {
|
2020-01-15 09:59:56 -05:00
|
|
|
return &templateNamespace{
|
|
|
|
prototypeHTML: htmltemplate.New("").Funcs(funcs),
|
|
|
|
prototypeText: texttemplate.New("").Funcs(funcs),
|
|
|
|
templateStateMap: &templateStateMap{
|
|
|
|
templates: make(map[string]*templateState),
|
|
|
|
},
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
func newTemplateState(owner *templateState, templ tpl.Template, info templateInfo, id identity.Identity) *templateState {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
if id == nil {
|
|
|
|
id = info
|
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
return &templateState{
|
2024-06-08 05:52:22 -04:00
|
|
|
owner: owner,
|
2020-01-15 09:59:56 -05:00
|
|
|
info: info,
|
|
|
|
typ: info.resolveType(),
|
|
|
|
Template: templ,
|
|
|
|
parseInfo: tpl.DefaultParseInfo,
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
id: id,
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
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
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
type layoutCacheKey struct {
|
2023-01-04 12:24:36 -05:00
|
|
|
d layouts.LayoutDescriptor
|
2020-01-15 09:59:56 -05:00
|
|
|
f string
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
type templateExec struct {
|
|
|
|
d *deps.Deps
|
|
|
|
executor texttemplate.Executer
|
|
|
|
funcs map[string]reflect.Value
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
*templateHandler
|
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t templateExec) Clone(d *deps.Deps) *templateExec {
|
|
|
|
exec, funcs := newTemplateExecuter(d)
|
|
|
|
t.executor = exec
|
|
|
|
t.funcs = funcs
|
|
|
|
t.d = d
|
|
|
|
return &t
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (t *templateExec) Execute(templ tpl.Template, wr io.Writer, data any) error {
|
2022-02-17 10:51:19 -05:00
|
|
|
return t.ExecuteWithContext(context.Background(), templ, wr, data)
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func (t *templateExec) ExecuteWithContext(ctx context.Context, templ tpl.Template, wr io.Writer, data any) error {
|
2020-01-15 09:59:56 -05:00
|
|
|
if rlocker, ok := templ.(types.RLocker); ok {
|
|
|
|
rlocker.RLock()
|
|
|
|
defer rlocker.RUnlock()
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
if t.Metrics != nil {
|
2022-05-09 04:05:19 -04:00
|
|
|
defer t.Metrics.MeasureSince(templ.Name(), time.Now())
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2022-02-15 09:26:18 -05:00
|
|
|
if t.templateUsageTracker != nil {
|
|
|
|
if ts, ok := templ.(*templateState); ok {
|
2023-01-04 12:24:36 -05:00
|
|
|
|
2022-02-15 09:26:18 -05:00
|
|
|
t.templateUsageTrackerMu.Lock()
|
|
|
|
if _, found := t.templateUsageTracker[ts.Name()]; !found {
|
|
|
|
t.templateUsageTracker[ts.Name()] = ts.info
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ts.baseInfo.IsZero() {
|
|
|
|
if _, found := t.templateUsageTracker[ts.baseInfo.name]; !found {
|
|
|
|
t.templateUsageTracker[ts.baseInfo.name] = ts.baseInfo
|
|
|
|
}
|
|
|
|
}
|
|
|
|
t.templateUsageTrackerMu.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-17 10:51:19 -05:00
|
|
|
execErr := t.executor.ExecuteWithContext(ctx, templ, wr, data)
|
2020-01-15 09:59:56 -05:00
|
|
|
if execErr != nil {
|
2024-06-08 05:52:22 -04:00
|
|
|
owner := templ
|
|
|
|
if ts, ok := templ.(*templateState); ok && ts.owner != nil {
|
|
|
|
owner = ts.owner
|
|
|
|
}
|
|
|
|
execErr = t.addFileContext(owner, execErr)
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
return execErr
|
|
|
|
}
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
|
2022-02-15 09:26:18 -05:00
|
|
|
func (t *templateExec) UnusedTemplates() []tpl.FileInfo {
|
|
|
|
if t.templateUsageTracker == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
var unused []tpl.FileInfo
|
|
|
|
|
|
|
|
for _, ti := range t.needsBaseof {
|
|
|
|
if _, found := t.templateUsageTracker[ti.name]; !found {
|
|
|
|
unused = append(unused, ti)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ti := range t.baseof {
|
|
|
|
if _, found := t.templateUsageTracker[ti.name]; !found {
|
|
|
|
unused = append(unused, ti)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, ts := range t.main.templates {
|
|
|
|
ti := ts.info
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
if strings.HasPrefix(ti.name, "_internal/") || ti.meta == nil {
|
2022-02-15 09:26:18 -05:00
|
|
|
continue
|
|
|
|
}
|
2022-02-17 07:04:00 -05:00
|
|
|
|
2022-02-15 09:26:18 -05:00
|
|
|
if _, found := t.templateUsageTracker[ti.name]; !found {
|
|
|
|
unused = append(unused, ti)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Slice(unused, func(i, j int) bool {
|
|
|
|
return unused[i].Name() < unused[j].Name()
|
|
|
|
})
|
|
|
|
|
|
|
|
return unused
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateExec) GetFunc(name string) (reflect.Value, bool) {
|
|
|
|
v, found := t.funcs[name]
|
|
|
|
return v, found
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
type templateHandler struct {
|
2020-01-15 09:59:56 -05:00
|
|
|
main *templateNamespace
|
|
|
|
needsBaseof map[string]templateInfo
|
|
|
|
baseof map[string]templateInfo
|
2019-11-27 07:42:36 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// This is the filesystem to load the templates from. All the templates are
|
|
|
|
// stored in the root of this filesystem.
|
|
|
|
layoutsFs afero.Fs
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
layoutHandler *layouts.LayoutHandler
|
2020-01-15 09:59:56 -05:00
|
|
|
|
2022-07-13 08:47:32 -04:00
|
|
|
layoutTemplateCache map[layoutCacheKey]layoutCacheEntry
|
2020-01-15 09:59:56 -05:00
|
|
|
layoutTemplateCacheMu sync.RWMutex
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
*deps.Deps
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
// Used to get proper filenames in errors
|
|
|
|
nameBaseTemplateName map[string]string
|
|
|
|
|
|
|
|
// Holds name and source of template definitions not found during the first
|
|
|
|
// AST transformation pass.
|
|
|
|
transformNotFound map[string]*templateState
|
|
|
|
|
|
|
|
// shortcodes maps shortcode name to template variants
|
|
|
|
// (language, output format etc.) of that shortcode.
|
|
|
|
shortcodes map[string]*shortcodeTemplates
|
|
|
|
|
|
|
|
// templateInfo maps template name to some additional information about that template.
|
|
|
|
// Note that for shortcodes that same information is embedded in the
|
|
|
|
// shortcodeTemplates type.
|
|
|
|
templateInfo map[string]tpl.Info
|
2022-02-15 09:26:18 -05:00
|
|
|
|
|
|
|
// May be nil.
|
|
|
|
templateUsageTracker map[string]templateInfo
|
|
|
|
templateUsageTrackerMu sync.Mutex
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2022-07-13 08:47:32 -04:00
|
|
|
type layoutCacheEntry struct {
|
|
|
|
found bool
|
|
|
|
templ tpl.Template
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// AddTemplate parses and adds a template to the collection.
|
|
|
|
// Templates with name prefixed with "_text" will be handled as plain
|
|
|
|
// text templates.
|
|
|
|
func (t *templateHandler) AddTemplate(name, tpl string) error {
|
2020-01-15 09:59:56 -05:00
|
|
|
templ, err := t.addTemplateTo(t.newTemplateInfo(name, tpl), t.main)
|
|
|
|
if err == nil {
|
2024-06-08 05:52:22 -04:00
|
|
|
_, err = t.applyTemplateTransformers(t.main, templ)
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
return err
|
2017-04-30 15:52:56 -04:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) Lookup(name string) (tpl.Template, bool) {
|
|
|
|
templ, found := t.main.Lookup(name)
|
|
|
|
if found {
|
|
|
|
return templ, true
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
return nil, false
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (t *templateHandler) LookupLayout(d layouts.LayoutDescriptor, f output.Format) (tpl.Template, bool, error) {
|
2020-01-15 09:59:56 -05:00
|
|
|
key := layoutCacheKey{d, f.Name}
|
|
|
|
t.layoutTemplateCacheMu.RLock()
|
|
|
|
if cacheVal, found := t.layoutTemplateCache[key]; found {
|
|
|
|
t.layoutTemplateCacheMu.RUnlock()
|
2022-07-13 08:47:32 -04:00
|
|
|
return cacheVal.templ, cacheVal.found, cacheVal.err
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2024-06-08 05:52:22 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
t.layoutTemplateCacheMu.RUnlock()
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
t.layoutTemplateCacheMu.Lock()
|
|
|
|
defer t.layoutTemplateCacheMu.Unlock()
|
2019-01-02 06:33:26 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
templ, found, err := t.findLayout(d, f)
|
2022-07-13 08:47:32 -04:00
|
|
|
cacheVal := layoutCacheEntry{found: found, templ: templ, err: err}
|
|
|
|
t.layoutTemplateCache[key] = cacheVal
|
|
|
|
return cacheVal.templ, cacheVal.found, cacheVal.err
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// This currently only applies to shortcodes and what we get here is the
|
|
|
|
// shortcode name.
|
|
|
|
func (t *templateHandler) LookupVariant(name string, variants tpl.TemplateVariants) (tpl.Template, bool, bool) {
|
|
|
|
name = templateBaseName(templateShortcode, name)
|
|
|
|
s, found := t.shortcodes[name]
|
|
|
|
if !found {
|
|
|
|
return nil, false, false
|
2019-04-16 03:13:55 -04:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
sv, found := s.fromVariants(variants)
|
|
|
|
if !found {
|
|
|
|
return nil, false, false
|
2017-04-02 08:20:34 -04:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
more := len(s.variants) > 1
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
return sv.ts, true, more
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2020-07-03 12:02:32 -04:00
|
|
|
// LookupVariants returns all variants of name, nil if none found.
|
|
|
|
func (t *templateHandler) LookupVariants(name string) []tpl.Template {
|
|
|
|
name = templateBaseName(templateShortcode, name)
|
|
|
|
s, found := t.shortcodes[name]
|
|
|
|
if !found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
variants := make([]tpl.Template, len(s.variants))
|
|
|
|
for i := 0; i < len(variants); i++ {
|
|
|
|
variants[i] = s.variants[i].ts
|
|
|
|
}
|
|
|
|
|
|
|
|
return variants
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) HasTemplate(name string) bool {
|
|
|
|
if _, found := t.baseof[name]; found {
|
|
|
|
return true
|
2017-05-06 14:15:28 -04:00
|
|
|
}
|
2020-02-28 06:39:58 -05:00
|
|
|
|
|
|
|
if _, found := t.needsBaseof[name]; found {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
_, found := t.Lookup(name)
|
|
|
|
return found
|
|
|
|
}
|
2017-05-06 14:15:28 -04:00
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
func (t *templateHandler) GetIdentity(name string) (identity.Identity, bool) {
|
|
|
|
if _, found := t.needsBaseof[name]; found {
|
|
|
|
return identity.StringIdentity(name), true
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, found := t.baseof[name]; found {
|
|
|
|
return identity.StringIdentity(name), true
|
|
|
|
}
|
|
|
|
|
|
|
|
tt, found := t.Lookup(name)
|
|
|
|
if !found {
|
|
|
|
return nil, false
|
|
|
|
}
|
|
|
|
return tt.(identity.IdentityProvider).GetIdentity(), found
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
func (t *templateHandler) findLayout(d layouts.LayoutDescriptor, f output.Format) (tpl.Template, bool, error) {
|
|
|
|
d.OutputFormatName = f.Name
|
|
|
|
d.Suffix = f.MediaType.FirstSuffix.Suffix
|
|
|
|
layouts, _ := t.layoutHandler.For(d)
|
2020-01-15 09:59:56 -05:00
|
|
|
for _, name := range layouts {
|
|
|
|
templ, found := t.main.Lookup(name)
|
|
|
|
if found {
|
|
|
|
return templ, true, nil
|
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
overlay, found := t.needsBaseof[name]
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
d.Baseof = true
|
2023-01-04 12:24:36 -05:00
|
|
|
baseLayouts, _ := t.layoutHandler.For(d)
|
2020-01-15 09:59:56 -05:00
|
|
|
var base templateInfo
|
|
|
|
found = false
|
|
|
|
for _, l := range baseLayouts {
|
|
|
|
base, found = t.baseof[l]
|
|
|
|
if found {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
templ, err := t.applyBaseTemplate(overlay, base)
|
|
|
|
if err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
ts := newTemplateState(nil, templ, overlay, identity.Or(base, overlay))
|
2020-01-15 09:59:56 -05:00
|
|
|
|
2020-01-23 11:34:19 -05:00
|
|
|
if found {
|
|
|
|
ts.baseInfo = base
|
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
if _, err := t.applyTemplateTransformers(t.main, ts); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
|
2020-07-13 07:40:35 -04:00
|
|
|
if err := t.extractPartials(ts.Template); err != nil {
|
|
|
|
return nil, false, err
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
return ts, true, nil
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
|
|
|
|
return nil, false, nil
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) newTemplateInfo(name, tpl string) templateInfo {
|
|
|
|
var isText bool
|
2024-01-30 05:43:20 -05:00
|
|
|
var isEmbedded bool
|
|
|
|
|
|
|
|
if strings.HasPrefix(name, embeddedPathPrefix) {
|
|
|
|
isEmbedded = true
|
|
|
|
name = strings.TrimPrefix(name, embeddedPathPrefix)
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
name, isText = t.nameIsText(name)
|
|
|
|
return templateInfo{
|
2024-01-30 05:43:20 -05:00
|
|
|
name: name,
|
|
|
|
isText: isText,
|
|
|
|
isEmbedded: isEmbedded,
|
|
|
|
template: tpl,
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) addFileContext(templ tpl.Template, inerr error) error {
|
|
|
|
if strings.HasPrefix(templ.Name(), "_internal") {
|
2019-12-10 13:56:44 -05:00
|
|
|
return inerr
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
ts, ok := templ.(*templateState)
|
|
|
|
if !ok {
|
2019-12-10 13:56:44 -05:00
|
|
|
return inerr
|
|
|
|
}
|
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
identifiers := t.extractIdentifiers(inerr.Error())
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
checkFilename := func(info templateInfo, inErr error) (error, bool) {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
if info.meta == nil {
|
2020-01-15 09:59:56 -05:00
|
|
|
return inErr, false
|
|
|
|
}
|
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
lineMatcher := func(m herrors.LineMatcher) int {
|
2020-01-15 09:59:56 -05:00
|
|
|
if m.Position.LineNumber != m.LineNumber {
|
2022-05-12 05:43:20 -04:00
|
|
|
return -1
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
for _, id := range identifiers {
|
|
|
|
if strings.Contains(m.Line, id) {
|
2022-05-12 05:43:20 -04:00
|
|
|
// We found the line, but return a 0 to signal to
|
|
|
|
// use the column from the error message.
|
|
|
|
return 0
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
|
|
|
}
|
2022-05-12 05:43:20 -04:00
|
|
|
return -1
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
f, err := info.meta.Open()
|
2020-01-15 09:59:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return inErr, false
|
|
|
|
}
|
|
|
|
defer f.Close()
|
2019-12-10 13:56:44 -05:00
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
fe := herrors.NewFileErrorFromName(inErr, info.meta.Filename)
|
2022-05-12 05:43:20 -04:00
|
|
|
fe.UpdateContent(f, lineMatcher)
|
|
|
|
|
|
|
|
if !fe.ErrorContext().Position.IsValid() {
|
|
|
|
return inErr, false
|
|
|
|
}
|
|
|
|
return fe, true
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2022-05-02 10:07:52 -04:00
|
|
|
inerr = fmt.Errorf("execute of template failed: %w", inerr)
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
if err, ok := checkFilename(ts.info, inerr); ok {
|
2019-12-10 13:56:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
err, _ := checkFilename(ts.baseInfo, inerr)
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
return err
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2019-01-31 05:53:21 -05:00
|
|
|
|
2022-05-12 05:43:20 -04:00
|
|
|
func (t *templateHandler) extractIdentifiers(line string) []string {
|
|
|
|
m := identifiersRe.FindAllStringSubmatch(line, -1)
|
|
|
|
identifiers := make([]string, len(m))
|
|
|
|
for i := 0; i < len(m); i++ {
|
|
|
|
identifiers[i] = m[i][1]
|
|
|
|
}
|
|
|
|
return identifiers
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) addShortcodeVariant(ts *templateState) {
|
|
|
|
name := ts.Name()
|
2019-12-10 13:56:44 -05:00
|
|
|
base := templateBaseName(templateShortcode, name)
|
|
|
|
|
|
|
|
shortcodename, variants := templateNameAndVariants(base)
|
|
|
|
|
|
|
|
templs, found := t.shortcodes[shortcodename]
|
|
|
|
if !found {
|
|
|
|
templs = &shortcodeTemplates{}
|
|
|
|
t.shortcodes[shortcodename] = templs
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
sv := shortcodeVariant{variants: variants, ts: ts}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
|
|
|
i := templs.indexOf(variants)
|
|
|
|
|
|
|
|
if i != -1 {
|
|
|
|
// Only replace if it's an override of an internal template.
|
|
|
|
if !isInternal(name) {
|
|
|
|
templs.variants[i] = sv
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
templs.variants = append(templs.variants, sv)
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
func (t *templateHandler) addTemplateFile(name string, fim hugofs.FileMetaInfo) error {
|
|
|
|
getTemplate := func(fim hugofs.FileMetaInfo) (templateInfo, error) {
|
|
|
|
meta := fim.Meta()
|
|
|
|
f, err := meta.Open()
|
2019-12-10 13:56:44 -05:00
|
|
|
if err != nil {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
return templateInfo{meta: meta}, err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
b, err := io.ReadAll(f)
|
|
|
|
if err != nil {
|
|
|
|
return templateInfo{meta: meta}, err
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
s := removeLeadingBOM(string(b))
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
var isText bool
|
|
|
|
name, isText = t.nameIsText(name)
|
|
|
|
|
|
|
|
return templateInfo{
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
name: name,
|
|
|
|
isText: isText,
|
|
|
|
template: s,
|
|
|
|
meta: meta,
|
2020-01-15 09:59:56 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
tinfo, err := getTemplate(fim)
|
2020-01-15 09:59:56 -05:00
|
|
|
if err != nil {
|
|
|
|
return 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
|
|
|
}
|
|
|
|
|
2020-01-23 11:34:19 -05:00
|
|
|
if isBaseTemplatePath(name) {
|
2020-01-15 09:59:56 -05:00
|
|
|
// Store it for later.
|
|
|
|
t.baseof[name] = tinfo
|
2019-12-10 13:56:44 -05:00
|
|
|
return nil
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
|
|
|
|
2020-01-30 14:02:26 -05:00
|
|
|
needsBaseof := !t.noBaseNeeded(name) && needsBaseTemplate(tinfo.template)
|
2020-01-15 09:59:56 -05:00
|
|
|
if needsBaseof {
|
|
|
|
t.needsBaseof[name] = tinfo
|
2019-12-10 13:56:44 -05:00
|
|
|
return nil
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
templ, err := t.addTemplateTo(tinfo, t.main)
|
|
|
|
if err != nil {
|
|
|
|
return tinfo.errWithFileContext("parse failed", err)
|
|
|
|
}
|
2024-06-08 05:52:22 -04:00
|
|
|
|
|
|
|
if _, err = t.applyTemplateTransformers(t.main, templ); err != nil {
|
|
|
|
return tinfo.errWithFileContext("transform failed", err)
|
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) addTemplateTo(info templateInfo, to *templateNamespace) (*templateState, error) {
|
|
|
|
return to.parse(info)
|
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) applyBaseTemplate(overlay, base templateInfo) (tpl.Template, error) {
|
|
|
|
if overlay.isText {
|
2020-01-23 11:34:19 -05:00
|
|
|
var (
|
|
|
|
templ = t.main.prototypeTextClone.New(overlay.name)
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if !base.IsZero() {
|
|
|
|
templ, err = templ.Parse(base.template)
|
|
|
|
if err != nil {
|
|
|
|
return nil, base.errWithFileContext("parse failed", err)
|
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2020-01-23 11:34:19 -05:00
|
|
|
|
2021-01-17 00:05:58 -05:00
|
|
|
templ, err = texttemplate.Must(templ.Clone()).Parse(overlay.template)
|
2019-12-10 13:56:44 -05:00
|
|
|
if err != nil {
|
2020-01-15 09:59:56 -05:00
|
|
|
return nil, overlay.errWithFileContext("parse failed", err)
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2021-01-17 00:05:58 -05:00
|
|
|
|
|
|
|
// The extra lookup is a workaround, see
|
|
|
|
// * https://github.com/golang/go/issues/16101
|
|
|
|
// * https://github.com/gohugoio/hugo/issues/2549
|
|
|
|
// templ = templ.Lookup(templ.Name())
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
return templ, nil
|
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
|
|
|
}
|
|
|
|
|
2020-01-23 11:34:19 -05:00
|
|
|
var (
|
|
|
|
templ = t.main.prototypeHTMLClone.New(overlay.name)
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
if !base.IsZero() {
|
|
|
|
templ, err = templ.Parse(base.template)
|
|
|
|
if err != nil {
|
|
|
|
return nil, base.errWithFileContext("parse failed", err)
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
templ, err = htmltemplate.Must(templ.Clone()).Parse(overlay.template)
|
|
|
|
if err != nil {
|
|
|
|
return nil, overlay.errWithFileContext("parse failed", err)
|
2019-04-16 03:13:55 -04:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
|
|
|
|
// The extra lookup is a workaround, see
|
|
|
|
// * https://github.com/golang/go/issues/16101
|
|
|
|
// * https://github.com/gohugoio/hugo/issues/2549
|
|
|
|
templ = templ.Lookup(templ.Name())
|
|
|
|
|
|
|
|
return templ, err
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2019-04-16 03:13:55 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) applyTemplateTransformers(ns *templateNamespace, ts *templateState) (*templateContext, error) {
|
|
|
|
c, err := applyTemplateTransformers(ts, ns.newTemplateLookup(ts))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
for k := range c.templateNotFound {
|
|
|
|
t.transformNotFound[k] = ts
|
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
for k, v := range c.deferNodes {
|
|
|
|
if err = t.main.addDeferredTemplate(ts, k, v); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
return c, err
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2023-12-04 06:05:41 -05:00
|
|
|
//go:embed all:embedded/templates/*
|
2022-02-17 07:04:00 -05:00
|
|
|
//go:embed embedded/templates/_default/*
|
2022-05-12 05:43:20 -04:00
|
|
|
//go:embed embedded/templates/_server/*
|
2023-06-26 07:26:36 -04:00
|
|
|
var embeddedTemplatesFs embed.FS
|
2022-02-09 12:29:49 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func (t *templateHandler) loadEmbedded() error {
|
2023-06-26 07:26:36 -04:00
|
|
|
return fs.WalkDir(embeddedTemplatesFs, ".", func(path string, d fs.DirEntry, err error) error {
|
2023-10-18 11:33:00 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-02-09 12:29:49 -05:00
|
|
|
if d == nil || d.IsDir() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-06-26 07:26:36 -04:00
|
|
|
templb, err := embeddedTemplatesFs.ReadFile(path)
|
2022-02-09 12:29:49 -05:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the newlines on Windows in line with how we had it back when we used Go Generate
|
|
|
|
// to write the templates to Go files.
|
|
|
|
templ := string(bytes.ReplaceAll(templb, []byte("\r\n"), []byte("\n")))
|
|
|
|
name := strings.TrimPrefix(filepath.ToSlash(path), "embedded/templates/")
|
2022-02-17 07:04:00 -05:00
|
|
|
templateName := name
|
2022-02-09 12:29:49 -05:00
|
|
|
|
2023-02-18 15:47:35 -05:00
|
|
|
// For the render hooks and the server templates it does not make sense to preserve the
|
|
|
|
// double _internal double book-keeping,
|
2022-02-17 07:04:00 -05:00
|
|
|
// just add it if its now provided by the user.
|
2023-12-04 06:05:41 -05:00
|
|
|
if !strings.Contains(path, "_default/_markup") && !strings.HasPrefix(name, "_server/") && !strings.HasPrefix(name, "partials/_funcs/") {
|
2022-02-17 07:04:00 -05:00
|
|
|
templateName = internalPathPrefix + name
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, found := t.Lookup(templateName); !found {
|
2024-01-30 05:43:20 -05:00
|
|
|
if err := t.AddTemplate(embeddedPathPrefix+templateName, templ); err != nil {
|
2022-02-17 07:04:00 -05:00
|
|
|
return err
|
|
|
|
}
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2022-02-09 12:29:49 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
if aliases, found := embeddedTemplatesAliases[name]; found {
|
2020-01-15 09:59:56 -05:00
|
|
|
// TODO(bep) avoid reparsing these aliases
|
2019-12-10 13:56:44 -05:00
|
|
|
for _, alias := range aliases {
|
2020-01-15 09:59:56 -05:00
|
|
|
alias = internalPathPrefix + alias
|
2024-01-30 05:43:20 -05:00
|
|
|
if err := t.AddTemplate(embeddedPathPrefix+alias, templ); err != nil {
|
2019-12-10 13:56:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-02-09 12:29:49 -05:00
|
|
|
|
|
|
|
return nil
|
|
|
|
})
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) loadTemplates() error {
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
walker := func(path string, fi hugofs.FileMetaInfo) error {
|
|
|
|
if fi.IsDir() {
|
|
|
|
return nil
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
if isDotFile(path) || isBackupFile(path) {
|
2019-12-10 13:56:44 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
name := strings.TrimPrefix(filepath.ToSlash(path), "/")
|
|
|
|
filename := filepath.Base(path)
|
2023-01-04 12:24:36 -05:00
|
|
|
outputFormats := t.Conf.GetConfigSection("outputFormats").(output.Formats)
|
|
|
|
outputFormat, found := outputFormats.FromFilename(filename)
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
if found && outputFormat.IsPlainText {
|
|
|
|
name = textTmplNamePrefix + name
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
if err := t.addTemplateFile(name, fi); err != nil {
|
2019-12-10 13:56:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
if err := helpers.Walk(t.Layouts.Fs, "", walker); err != nil {
|
2022-12-14 06:20:13 -05:00
|
|
|
if !herrors.IsNotExist(err) {
|
2019-12-10 13:56:44 -05:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) nameIsText(name string) (string, bool) {
|
|
|
|
isText := strings.HasPrefix(name, textTmplNamePrefix)
|
|
|
|
if isText {
|
|
|
|
name = strings.TrimPrefix(name, textTmplNamePrefix)
|
2019-04-16 03:13:55 -04:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
return name, isText
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
2019-04-16 03:13:55 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateHandler) noBaseNeeded(name string) bool {
|
|
|
|
if strings.HasPrefix(name, "shortcodes/") || strings.HasPrefix(name, "partials/") {
|
|
|
|
return true
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
return strings.Contains(name, "_markup/")
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
|
|
|
|
2020-07-13 07:40:35 -04:00
|
|
|
func (t *templateHandler) extractPartials(templ tpl.Template) error {
|
|
|
|
templs := templates(templ)
|
|
|
|
for _, templ := range templs {
|
|
|
|
if templ.Name() == "" || !strings.HasPrefix(templ.Name(), "partials/") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
ts := newTemplateState(nil, templ, templateInfo{name: templ.Name()}, nil)
|
2020-07-13 07:40:35 -04:00
|
|
|
ts.typ = templatePartial
|
|
|
|
|
|
|
|
t.main.mu.RLock()
|
|
|
|
_, found := t.main.templates[templ.Name()]
|
|
|
|
t.main.mu.RUnlock()
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
t.main.mu.Lock()
|
|
|
|
// This is a template defined inline.
|
|
|
|
_, err := applyTemplateTransformers(ts, t.main.newTemplateLookup(ts))
|
|
|
|
if err != nil {
|
|
|
|
t.main.mu.Unlock()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
t.main.templates[templ.Name()] = ts
|
|
|
|
t.main.mu.Unlock()
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2019-11-27 07:42:36 -05:00
|
|
|
func (t *templateHandler) postTransform() error {
|
2020-07-01 04:43:17 -04:00
|
|
|
defineCheckedHTML := false
|
|
|
|
defineCheckedText := false
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
for _, v := range t.main.templates {
|
|
|
|
if v.typ == templateShortcode {
|
|
|
|
t.addShortcodeVariant(v)
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
2020-07-01 04:43:17 -04:00
|
|
|
|
|
|
|
if defineCheckedHTML && defineCheckedText {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
isText := isText(v.Template)
|
|
|
|
if isText {
|
|
|
|
if defineCheckedText {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defineCheckedText = true
|
|
|
|
} else {
|
|
|
|
if defineCheckedHTML {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
defineCheckedHTML = true
|
|
|
|
}
|
|
|
|
|
2020-07-13 07:40:35 -04:00
|
|
|
if err := t.extractPartials(v.Template); err != nil {
|
|
|
|
return err
|
2020-07-01 04:43:17 -04:00
|
|
|
}
|
2019-11-27 07:42:36 -05:00
|
|
|
}
|
2019-04-16 03:13:55 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
for name, source := range t.transformNotFound {
|
|
|
|
lookup := t.main.newTemplateLookup(source)
|
|
|
|
templ := lookup(name)
|
|
|
|
if templ != nil {
|
|
|
|
_, err := applyTemplateTransformers(templ, lookup)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2019-04-16 03:13:55 -04:00
|
|
|
}
|
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
2019-11-27 07:42:36 -05:00
|
|
|
|
2021-04-23 07:40:05 -04:00
|
|
|
for _, v := range t.shortcodes {
|
|
|
|
sort.Slice(v.variants, func(i, j int) bool {
|
|
|
|
v1, v2 := v.variants[i], v.variants[j]
|
|
|
|
name1, name2 := v1.ts.Name(), v2.ts.Name()
|
|
|
|
isHTMl1, isHTML2 := strings.HasSuffix(name1, "html"), strings.HasSuffix(name2, "html")
|
|
|
|
|
|
|
|
// There will be a weighted selection later, but make
|
|
|
|
// sure these are sorted to get a stable selection for
|
|
|
|
// output formats missing specific templates.
|
|
|
|
// Prefer HTML.
|
|
|
|
if isHTMl1 || isHTML2 && !(isHTMl1 && isHTML2) {
|
|
|
|
return isHTMl1
|
|
|
|
}
|
|
|
|
|
|
|
|
return name1 < name2
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-04-16 03:13:55 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
type templateNamespace struct {
|
|
|
|
prototypeText *texttemplate.Template
|
|
|
|
prototypeHTML *htmltemplate.Template
|
|
|
|
prototypeTextClone *texttemplate.Template
|
|
|
|
prototypeHTMLClone *htmltemplate.Template
|
2019-11-27 07:42:36 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
*templateStateMap
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
func (t *templateNamespace) getPrototypeText() *texttemplate.Template {
|
|
|
|
if t.prototypeTextClone != nil {
|
|
|
|
return t.prototypeTextClone
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
2024-06-08 05:52:22 -04:00
|
|
|
return t.prototypeText
|
|
|
|
}
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
func (t *templateNamespace) getPrototypeHTML() *htmltemplate.Template {
|
|
|
|
if t.prototypeHTMLClone != nil {
|
|
|
|
return t.prototypeHTMLClone
|
|
|
|
}
|
|
|
|
return t.prototypeHTML
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateNamespace) Lookup(name string) (tpl.Template, bool) {
|
2020-07-13 07:40:35 -04:00
|
|
|
t.mu.RLock()
|
|
|
|
defer t.mu.RUnlock()
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
templ, found := t.templates[name]
|
|
|
|
if !found {
|
|
|
|
return nil, false
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
return templ, found
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateNamespace) createPrototypes() error {
|
|
|
|
t.prototypeTextClone = texttemplate.Must(t.prototypeText.Clone())
|
|
|
|
t.prototypeHTMLClone = htmltemplate.Must(t.prototypeHTML.Clone())
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
return nil
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateNamespace) newTemplateLookup(in *templateState) func(name string) *templateState {
|
|
|
|
return func(name string) *templateState {
|
|
|
|
if templ, found := t.templates[name]; found {
|
|
|
|
if templ.isText() != in.isText() {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return templ
|
|
|
|
}
|
|
|
|
if templ, found := findTemplateIn(name, in); found {
|
2024-06-08 05:52:22 -04:00
|
|
|
return newTemplateState(nil, templ, templateInfo{name: templ.Name()}, nil)
|
2020-01-15 09:59:56 -05:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
func (t *templateNamespace) addDeferredTemplate(owner *templateState, name string, n *parse.ListNode) error {
|
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
|
|
|
|
|
|
|
if _, found := t.templates[name]; found {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var templ tpl.Template
|
|
|
|
|
|
|
|
if owner.isText() {
|
|
|
|
prototype := t.getPrototypeText()
|
|
|
|
tt, err := prototype.New(name).Parse("")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse empty text template %q: %w", name, err)
|
|
|
|
}
|
|
|
|
tt.Tree.Root = n
|
|
|
|
templ = tt
|
|
|
|
} else {
|
|
|
|
prototype := t.getPrototypeHTML()
|
|
|
|
tt, err := prototype.New(name).Parse("")
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to parse empty HTML template %q: %w", name, err)
|
|
|
|
}
|
|
|
|
tt.Tree.Root = n
|
|
|
|
templ = tt
|
|
|
|
}
|
|
|
|
|
|
|
|
dts := newTemplateState(owner, templ, templateInfo{name: name}, nil)
|
|
|
|
t.templates[name] = dts
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateNamespace) parse(info templateInfo) (*templateState, error) {
|
2020-07-13 07:40:35 -04:00
|
|
|
t.mu.Lock()
|
|
|
|
defer t.mu.Unlock()
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
if info.isText {
|
|
|
|
prototype := t.prototypeText
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
templ, err := prototype.New(info.name).Parse(info.template)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
ts := newTemplateState(nil, templ, info, nil)
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
t.templates[info.name] = ts
|
|
|
|
|
|
|
|
return ts, nil
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
prototype := t.prototypeHTML
|
2019-12-10 13:56:44 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
templ, err := prototype.New(info.name).Parse(info.template)
|
2017-02-17 07:30:50 -05:00
|
|
|
if err != nil {
|
2019-12-10 13:56:44 -05:00
|
|
|
return nil, err
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
ts := newTemplateState(nil, templ, info, nil)
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
t.templates[info.name] = ts
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
return ts, nil
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2024-01-30 05:43:20 -05:00
|
|
|
var _ tpl.IsInternalTemplateProvider = (*templateState)(nil)
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
type templateState struct {
|
|
|
|
tpl.Template
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2024-06-08 05:52:22 -04:00
|
|
|
// Set for deferred templates.
|
|
|
|
owner *templateState
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
typ templateType
|
|
|
|
parseInfo tpl.ParseInfo
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
id identity.Identity
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
info templateInfo
|
|
|
|
baseInfo templateInfo // Set when a base template is used.
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2024-01-30 05:43:20 -05:00
|
|
|
func (t *templateState) IsInternalTemplate() bool {
|
|
|
|
return t.info.isEmbedded
|
|
|
|
}
|
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
func (t *templateState) GetIdentity() identity.Identity {
|
|
|
|
return t.id
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateState) ParseInfo() tpl.ParseInfo {
|
|
|
|
return t.parseInfo
|
|
|
|
}
|
2017-03-27 14:43:49 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *templateState) isText() bool {
|
2020-07-01 04:43:17 -04:00
|
|
|
return isText(t.Template)
|
|
|
|
}
|
|
|
|
|
all: Rework page store, add a dynacache, improve partial rebuilds, and some general spring cleaning
There are some breaking changes in this commit, see #11455.
Closes #11455
Closes #11549
This fixes a set of bugs (see issue list) and it is also paying some technical debt accumulated over the years. We now build with Staticcheck enabled in the CI build.
The performance should be about the same as before for regular sized Hugo sites, but it should perform and scale much better to larger data sets, as objects that uses lots of memory (e.g. rendered Markdown, big JSON files read into maps with transform.Unmarshal etc.) will now get automatically garbage collected if needed. Performance on partial rebuilds when running the server in fast render mode should be the same, but the change detection should be much more accurate.
A list of the notable new features:
* A new dependency tracker that covers (almost) all of Hugo's API and is used to do fine grained partial rebuilds when running the server.
* A new and simpler tree document store which allows fast lookups and prefix-walking in all dimensions (e.g. language) concurrently.
* You can now configure an upper memory limit allowing for much larger data sets and/or running on lower specced PCs.
We have lifted the "no resources in sub folders" restriction for branch bundles (e.g. sections).
Memory Limit
* Hugos will, by default, set aside a quarter of the total system memory, but you can set this via the OS environment variable HUGO_MEMORYLIMIT (in gigabytes). This is backed by a partitioned LRU cache used throughout Hugo. A cache that gets dynamically resized in low memory situations, allowing Go's Garbage Collector to free the memory.
New Dependency Tracker: Hugo has had a rule based coarse grained approach to server rebuilds that has worked mostly pretty well, but there have been some surprises (e.g. stale content). This is now revamped with a new dependency tracker that can quickly calculate the delta given a changed resource (e.g. a content file, template, JS file etc.). This handles transitive relations, e.g. $page -> js.Build -> JS import, or $page1.Content -> render hook -> site.GetPage -> $page2.Title, or $page1.Content -> shortcode -> partial -> site.RegularPages -> $page2.Content -> shortcode ..., and should also handle changes to aggregated values (e.g. site.Lastmod) effectively.
This covers all of Hugo's API with 2 known exceptions (a list that may not be fully exhaustive):
Changes to files loaded with template func os.ReadFile may not be handled correctly. We recommend loading resources with resources.Get
Changes to Hugo objects (e.g. Page) passed in the template context to lang.Translate may not be detected correctly. We recommend having simple i18n templates without too much data context passed in other than simple types such as strings and numbers.
Note that the cachebuster configuration (when A changes then rebuild B) works well with the above, but we recommend that you revise that configuration, as it in most situations should not be needed. One example where it is still needed is with TailwindCSS and using changes to hugo_stats.json to trigger new CSS rebuilds.
Document Store: Previously, a little simplified, we split the document store (where we store pages and resources) in a tree per language. This worked pretty well, but the structure made some operations harder than they needed to be. We have now restructured it into one Radix tree for all languages. Internally the language is considered to be a dimension of that tree, and the tree can be viewed in all dimensions concurrently. This makes some operations re. language simpler (e.g. finding translations is just a slice range), but the idea is that it should also be relatively inexpensive to add more dimensions if needed (e.g. role).
Fixes #10169
Fixes #10364
Fixes #10482
Fixes #10630
Fixes #10656
Fixes #10694
Fixes #10918
Fixes #11262
Fixes #11439
Fixes #11453
Fixes #11457
Fixes #11466
Fixes #11540
Fixes #11551
Fixes #11556
Fixes #11654
Fixes #11661
Fixes #11663
Fixes #11664
Fixes #11669
Fixes #11671
Fixes #11807
Fixes #11808
Fixes #11809
Fixes #11815
Fixes #11840
Fixes #11853
Fixes #11860
Fixes #11883
Fixes #11904
Fixes #7388
Fixes #7425
Fixes #7436
Fixes #7544
Fixes #7882
Fixes #7960
Fixes #8255
Fixes #8307
Fixes #8863
Fixes #8927
Fixes #9192
Fixes #9324
2023-12-24 13:11:05 -05:00
|
|
|
func (t *templateState) String() string {
|
|
|
|
return t.Name()
|
|
|
|
}
|
|
|
|
|
2020-07-01 04:43:17 -04:00
|
|
|
func isText(templ tpl.Template) bool {
|
|
|
|
_, isText := templ.(*texttemplate.Template)
|
2020-01-15 09:59:56 -05:00
|
|
|
return isText
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
type templateStateMap struct {
|
2020-07-13 07:40:35 -04:00
|
|
|
mu sync.RWMutex
|
2020-01-15 09:59:56 -05:00
|
|
|
templates map[string]*templateState
|
|
|
|
}
|
Add support for theme composition and inheritance
This commit adds support for theme composition and inheritance in Hugo.
With this, it helps thinking about a theme as a set of ordered components:
```toml
theme = ["my-shortcodes", "base-theme", "hyde"]
```
The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right.
So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`.
Hugo uses two different algorithms to merge the filesystems, depending on the file type:
* For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files.
* For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen.
The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically.
Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure:
* `params` (global and per language)
* `menu` (global and per language)
* `outputformats` and `mediatypes`
The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts.
A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others.
Fixes #4460
Fixes #4450
2018-03-01 09:01:25 -05:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
type textTemplateWrapperWithLock struct {
|
|
|
|
*sync.RWMutex
|
|
|
|
*texttemplate.Template
|
|
|
|
}
|
2018-05-24 05:24:38 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *textTemplateWrapperWithLock) Lookup(name string) (tpl.Template, bool) {
|
|
|
|
t.RLock()
|
|
|
|
templ := t.Template.Lookup(name)
|
|
|
|
t.RUnlock()
|
|
|
|
if templ == nil {
|
|
|
|
return nil, false
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
return &textTemplateWrapperWithLock{
|
|
|
|
RWMutex: t.RWMutex,
|
|
|
|
Template: templ,
|
|
|
|
}, true
|
|
|
|
}
|
2018-05-04 11:53:56 -04:00
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *textTemplateWrapperWithLock) LookupVariant(name string, variants tpl.TemplateVariants) (tpl.Template, bool, bool) {
|
|
|
|
panic("not supported")
|
2019-12-10 13:56:44 -05:00
|
|
|
}
|
2018-10-03 08:58:09 -04:00
|
|
|
|
2020-07-03 12:02:32 -04:00
|
|
|
func (t *textTemplateWrapperWithLock) LookupVariants(name string) []tpl.Template {
|
|
|
|
panic("not supported")
|
|
|
|
}
|
|
|
|
|
2020-01-15 09:59:56 -05:00
|
|
|
func (t *textTemplateWrapperWithLock) Parse(name, tpl string) (tpl.Template, error) {
|
|
|
|
t.Lock()
|
|
|
|
defer t.Unlock()
|
|
|
|
return t.Template.New(name).Parse(tpl)
|
2017-03-27 14:43:49 -04:00
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func isBackupFile(path string) bool {
|
|
|
|
return path[len(path)-1] == '~'
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2020-01-23 11:34:19 -05:00
|
|
|
func isBaseTemplatePath(path string) bool {
|
2019-12-10 13:56:44 -05:00
|
|
|
return strings.Contains(filepath.Base(path), baseFileBase)
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func isDotFile(path string) bool {
|
|
|
|
return filepath.Base(path)[0] == '.'
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
func removeLeadingBOM(s string) string {
|
|
|
|
const bom = '\ufeff'
|
|
|
|
|
|
|
|
for i, r := range s {
|
|
|
|
if i == 0 && r != bom {
|
|
|
|
return s
|
|
|
|
}
|
|
|
|
if i > 0 {
|
|
|
|
return s[i:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return s
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
|
|
|
|
2019-12-10 13:56:44 -05:00
|
|
|
// resolves _internal/shortcodes/param.html => param.html etc.
|
|
|
|
func templateBaseName(typ templateType, name string) string {
|
|
|
|
name = strings.TrimPrefix(name, internalPathPrefix)
|
|
|
|
switch typ {
|
|
|
|
case templateShortcode:
|
|
|
|
return strings.TrimPrefix(name, shortcodesPathPrefix)
|
|
|
|
default:
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2017-02-17 07:30:50 -05:00
|
|
|
}
|
2020-01-15 09:59:56 -05:00
|
|
|
|
|
|
|
func unwrap(templ tpl.Template) tpl.Template {
|
|
|
|
if ts, ok := templ.(*templateState); ok {
|
|
|
|
return ts.Template
|
|
|
|
}
|
|
|
|
return templ
|
|
|
|
}
|
2020-07-01 04:43:17 -04:00
|
|
|
|
|
|
|
func templates(in tpl.Template) []tpl.Template {
|
|
|
|
var templs []tpl.Template
|
|
|
|
in = unwrap(in)
|
|
|
|
if textt, ok := in.(*texttemplate.Template); ok {
|
|
|
|
for _, t := range textt.Templates() {
|
|
|
|
templs = append(templs, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if htmlt, ok := in.(*htmltemplate.Template); ok {
|
|
|
|
for _, t := range htmlt.Templates() {
|
|
|
|
templs = append(templs, t)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return templs
|
|
|
|
}
|