Reimplement archetypes

The old implementation had some issues, mostly related to the context (e.g. name, file paths) passed to the template.

This new implementation is using the exact same code path for evaluating the pages as in a regular build.

This also makes it more robust and easier to reason about in a multilingual setup.

Now, if you are explicit about the target path, Hugo will now always pick the correct mount and language:

```bash
hugo new content/en/posts/my-first-post.md
```

Fixes #9032
Fixes #7589
Fixes #9043
Fixes #9046
Fixes #9047
This commit is contained in:
Bjørn Erik Pedersen
2021-10-13 08:12:06 +02:00
parent 168a3aab46
commit 9185e11eff
19 changed files with 877 additions and 466 deletions

View File

@@ -19,6 +19,8 @@ import (
"regexp"
"runtime"
"github.com/gohugoio/hugo/hugofs/glob"
"github.com/gohugoio/hugo/langs"
"github.com/spf13/afero"
@@ -33,8 +35,7 @@ type SourceSpec struct {
SourceFs afero.Fs
// This is set if the ignoreFiles config is set.
ignoreFilesRe []*regexp.Regexp
shouldInclude func(filename string) bool
Languages map[string]interface{}
DefaultContentLanguage string
@@ -42,7 +43,7 @@ type SourceSpec struct {
}
// NewSourceSpec initializes SourceSpec using languages the given filesystem and PathSpec.
func NewSourceSpec(ps *helpers.PathSpec, fs afero.Fs) *SourceSpec {
func NewSourceSpec(ps *helpers.PathSpec, inclusionFilter *glob.FilenameFilter, fs afero.Fs) *SourceSpec {
cfg := ps.Cfg
defaultLang := cfg.GetString("defaultContentLanguage")
languages := cfg.GetStringMap("languages")
@@ -72,8 +73,19 @@ func NewSourceSpec(ps *helpers.PathSpec, fs afero.Fs) *SourceSpec {
}
}
shouldInclude := func(filename string) bool {
if !inclusionFilter.Match(filename) {
return false
}
for _, r := range regexps {
if r.MatchString(filename) {
return false
}
}
return true
}
return &SourceSpec{ignoreFilesRe: regexps, PathSpec: ps, SourceFs: fs, Languages: languages, DefaultContentLanguage: defaultLang, DisabledLanguages: disabledLangsSet}
return &SourceSpec{shouldInclude: shouldInclude, PathSpec: ps, SourceFs: fs, Languages: languages, DefaultContentLanguage: defaultLang, DisabledLanguages: disabledLangsSet}
}
// IgnoreFile returns whether a given file should be ignored.
@@ -97,24 +109,16 @@ func (s *SourceSpec) IgnoreFile(filename string) bool {
}
}
if len(s.ignoreFilesRe) == 0 {
return false
}
for _, re := range s.ignoreFilesRe {
if re.MatchString(filename) {
return true
}
if !s.shouldInclude(filename) {
return true
}
if runtime.GOOS == "windows" {
// Also check the forward slash variant if different.
unixFilename := filepath.ToSlash(filename)
if unixFilename != filename {
for _, re := range s.ignoreFilesRe {
if re.MatchString(unixFilename) {
return true
}
if !s.shouldInclude(unixFilename) {
return true
}
}
}