Welcome to mirror list, hosted at ThFree Co, Russian Federation.

github.com/gohugoio/hugo.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-10-13 09:12:06 +0300
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2021-10-16 16:22:03 +0300
commit9185e11effa682ea1ef7dc98f2943743671023a6 (patch)
treef89d4138ddffd163a2afcd814ed2c26d3c66c4c9 /hugolib
parent168a3aab4622786ccd0943137fce3912707f2a46 (diff)
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
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/content_factory.go181
-rw-r--r--hugolib/content_factory_test.go60
-rw-r--r--hugolib/filesystems/basefs.go36
-rw-r--r--hugolib/hugo_sites.go7
-rw-r--r--hugolib/pages_capture_test.go2
-rw-r--r--hugolib/site.go8
6 files changed, 288 insertions, 6 deletions
diff --git a/hugolib/content_factory.go b/hugolib/content_factory.go
new file mode 100644
index 000000000..b94608e86
--- /dev/null
+++ b/hugolib/content_factory.go
@@ -0,0 +1,181 @@
+// Copyright 2021 The Hugo Authors. All rights reserved.
+//
+// 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 hugolib
+
+import (
+ "io"
+ "path/filepath"
+ "strings"
+ "time"
+
+ "github.com/gohugoio/hugo/helpers"
+
+ "github.com/gohugoio/hugo/source"
+
+ "github.com/gohugoio/hugo/resources/page"
+
+ "github.com/pkg/errors"
+ "github.com/spf13/afero"
+)
+
+// ContentFactory creates content files from archetype templates.
+type ContentFactory struct {
+ h *HugoSites
+
+ // We parse the archetype templates as Go templates, so we need
+ // to replace any shortcode with a temporary placeholder.
+ shortocdeReplacerPre *strings.Replacer
+ shortocdeReplacerPost *strings.Replacer
+}
+
+// AppplyArchetypeFilename archetypeFilename to w as a template using the given Page p as the foundation for the data context.
+func (f ContentFactory) AppplyArchetypeFilename(w io.Writer, p page.Page, archetypeKind, archetypeFilename string) error {
+
+ fi, err := f.h.SourceFilesystems.Archetypes.Fs.Stat(archetypeFilename)
+ if err != nil {
+ return err
+ }
+
+ if fi.IsDir() {
+ return errors.Errorf("archetype directory (%q) not supported", archetypeFilename)
+ }
+
+ templateSource, err := afero.ReadFile(f.h.SourceFilesystems.Archetypes.Fs, archetypeFilename)
+ if err != nil {
+ return errors.Wrapf(err, "failed to read archetype file %q: %s", archetypeFilename, err)
+
+ }
+
+ return f.AppplyArchetypeTemplate(w, p, archetypeKind, string(templateSource))
+
+}
+
+// AppplyArchetypeFilename templateSource to w as a template using the given Page p as the foundation for the data context.
+func (f ContentFactory) AppplyArchetypeTemplate(w io.Writer, p page.Page, archetypeKind, templateSource string) error {
+ ps := p.(*pageState)
+ if archetypeKind == "" {
+ archetypeKind = p.Type()
+ }
+
+ d := &archetypeFileData{
+ Type: archetypeKind,
+ Date: time.Now().Format(time.RFC3339),
+ Page: p,
+ File: p.File(),
+ }
+
+ templateSource = f.shortocdeReplacerPre.Replace(templateSource)
+
+ templ, err := ps.s.TextTmpl().Parse("archetype.md", string(templateSource))
+ if err != nil {
+ return errors.Wrapf(err, "failed to parse archetype template: %s", err)
+ }
+
+ result, err := executeToString(ps.s.Tmpl(), templ, d)
+ if err != nil {
+ return errors.Wrapf(err, "failed to execute archetype template: %s", err)
+ }
+
+ _, err = io.WriteString(w, f.shortocdeReplacerPost.Replace(result))
+
+ return err
+
+}
+
+func (f ContentFactory) SectionFromFilename(filename string) string {
+ filename = filepath.Clean(filename)
+ rel, _ := f.h.AbsProjectContentDir(filename)
+ if rel == "" {
+ return ""
+ }
+
+ parts := strings.Split(helpers.ToSlashTrimLeading(rel), "/")
+ if len(parts) < 2 {
+ return ""
+ }
+ return parts[0]
+}
+
+// CreateContentPlaceHolder creates a content placeholder file inside the
+// best matching content directory.
+func (f ContentFactory) CreateContentPlaceHolder(filename string) (string, error) {
+ filename = filepath.Clean(filename)
+ _, abs := f.h.AbsProjectContentDir(filename)
+
+ contentDir := filepath.Dir(abs)
+ if err := f.h.Fs.Source.MkdirAll(contentDir, 0777); err != nil {
+ return "", err
+ }
+
+ // This will be overwritten later, just write a placholder to get
+ // the paths correct.
+ placeholder := `---
+title: "Content Placeholder"
+_build:
+ render: never
+ list: never
+ publishResources: false
+---
+
+`
+
+ if err := afero.WriteFile(f.h.Fs.Source, abs, []byte(placeholder), 0777); err != nil {
+ return "", err
+ }
+
+ return abs, nil
+}
+
+// NewContentFactory creates a new ContentFactory for h.
+func NewContentFactory(h *HugoSites) ContentFactory {
+ return ContentFactory{
+ h: h,
+ shortocdeReplacerPre: strings.NewReplacer(
+ "{{<", "{x{<",
+ "{{%", "{x{%",
+ ">}}", ">}x}",
+ "%}}", "%}x}"),
+ shortocdeReplacerPost: strings.NewReplacer(
+ "{x{<", "{{<",
+ "{x{%", "{{%",
+ ">}x}", ">}}",
+ "%}x}", "%}}"),
+ }
+}
+
+// archetypeFileData represents the data available to an archetype template.
+type archetypeFileData struct {
+ // The archetype content type, either given as --kind option or extracted
+ // from the target path's section, i.e. "blog/mypost.md" will resolve to
+ // "blog".
+ Type string
+
+ // The current date and time as a RFC3339 formatted string, suitable for use in front matter.
+ Date string
+
+ // The temporary page. Note that only the file path information is relevant at this stage.
+ Page page.Page
+
+ // File is the same as Page.File, embedded here for historic reasons.
+ // TODO(bep) make this a method.
+ source.File
+}
+
+func (f *archetypeFileData) Site() page.Site {
+ return f.Page.Site()
+}
+
+func (f *archetypeFileData) Name() string {
+ return f.Page.File().ContentBaseName()
+}
diff --git a/hugolib/content_factory_test.go b/hugolib/content_factory_test.go
new file mode 100644
index 000000000..50cc783f6
--- /dev/null
+++ b/hugolib/content_factory_test.go
@@ -0,0 +1,60 @@
+package hugolib
+
+import (
+ "bytes"
+ "path/filepath"
+ "testing"
+
+ qt "github.com/frankban/quicktest"
+)
+
+func TestContentFactory(t *testing.T) {
+ t.Parallel()
+
+ c := qt.New(t)
+
+ c.Run("Simple", func(c *qt.C) {
+ workingDir := "/my/work"
+ b := newTestSitesBuilder(c)
+ b.WithWorkingDir(workingDir).WithConfigFile("toml", `
+
+workingDir="/my/work"
+
+[module]
+[[module.mounts]]
+source = 'mcontent/en'
+target = 'content'
+lang = 'en'
+[[module.mounts]]
+source = 'archetypes'
+target = 'archetypes'
+
+`)
+
+ b.WithSourceFile(filepath.Join("mcontent/en/bundle", "index.md"), "")
+
+ b.WithSourceFile(filepath.Join("archetypes", "post.md"), `---
+title: "{{ replace .Name "-" " " | title }}"
+date: {{ .Date }}
+draft: true
+---
+
+Hello World.
+`)
+ b.CreateSites()
+ cf := NewContentFactory(b.H)
+ abs, err := cf.CreateContentPlaceHolder(filepath.FromSlash("mcontent/en/blog/mypage.md"))
+ b.Assert(err, qt.IsNil)
+ b.Assert(abs, qt.Equals, filepath.FromSlash("/my/work/mcontent/en/blog/mypage.md"))
+ b.Build(BuildCfg{SkipRender: true})
+
+ p := b.H.GetContentPage(abs)
+ b.Assert(p, qt.Not(qt.IsNil))
+
+ var buf bytes.Buffer
+ b.Assert(cf.AppplyArchetypeFilename(&buf, p, "", "post.md"), qt.IsNil)
+
+ b.Assert(buf.String(), qt.Contains, `title: "Mypage"`)
+ })
+
+}
diff --git a/hugolib/filesystems/basefs.go b/hugolib/filesystems/basefs.go
index d238d2e03..dcfee34ff 100644
--- a/hugolib/filesystems/basefs.go
+++ b/hugolib/filesystems/basefs.go
@@ -102,6 +102,42 @@ func (b *BaseFs) RelContentDir(filename string) string {
return filename
}
+// AbsProjectContentDir tries to create a TODO1
+func (b *BaseFs) AbsProjectContentDir(filename string) (string, string) {
+ isAbs := filepath.IsAbs(filename)
+ for _, dir := range b.SourceFilesystems.Content.Dirs {
+ meta := dir.Meta()
+ if meta.Module != "project" {
+ continue
+ }
+ if isAbs {
+ if strings.HasPrefix(filename, meta.Filename) {
+ return strings.TrimPrefix(filename, meta.Filename), filename
+ }
+ } else {
+ contentDir := strings.TrimPrefix(strings.TrimPrefix(meta.Filename, meta.BaseDir), filePathSeparator)
+ if strings.HasPrefix(filename, contentDir) {
+ relFilename := strings.TrimPrefix(filename, contentDir)
+ absFilename := filepath.Join(meta.Filename, relFilename)
+ return relFilename, absFilename
+ }
+ }
+
+ }
+
+ if !isAbs {
+ // A filename on the form "posts/mypage.md", put it inside
+ // the first content folder, usually <workDir>/content.
+ // The Dirs are ordered with the most important last, so pick that.
+ contentDirs := b.SourceFilesystems.Content.Dirs
+ firstContentDir := contentDirs[len(contentDirs)-1].Meta().Filename
+ return filename, filepath.Join(firstContentDir, filename)
+
+ }
+
+ return "", ""
+}
+
// ResolveJSConfigFile resolves the JS-related config file to a absolute
// filename. One example of such would be postcss.config.js.
func (fs *BaseFs) ResolveJSConfigFile(name string) string {
diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go
index 27c490cc0..141019a85 100644
--- a/hugolib/hugo_sites.go
+++ b/hugolib/hugo_sites.go
@@ -22,6 +22,8 @@ import (
"sync"
"sync/atomic"
+ "github.com/gohugoio/hugo/hugofs/glob"
+
"github.com/fsnotify/fsnotify"
"github.com/gohugoio/hugo/identity"
@@ -677,6 +679,9 @@ type BuildCfg struct {
// Recently visited URLs. This is used for partial re-rendering.
RecentlyVisited map[string]bool
+ // Can be set to build only with a sub set of the content source.
+ ContentInclusionFilter *glob.FilenameFilter
+
testCounters *testCounters
}
@@ -819,7 +824,7 @@ func (h *HugoSites) Pages() page.Pages {
}
func (h *HugoSites) loadData(fis []hugofs.FileMetaInfo) (err error) {
- spec := source.NewSourceSpec(h.PathSpec, nil)
+ spec := source.NewSourceSpec(h.PathSpec, nil, nil)
h.data = make(map[string]interface{})
for _, fi := range fis {
diff --git a/hugolib/pages_capture_test.go b/hugolib/pages_capture_test.go
index 0fdc73e76..4b2979a0a 100644
--- a/hugolib/pages_capture_test.go
+++ b/hugolib/pages_capture_test.go
@@ -51,7 +51,7 @@ func TestPagesCapture(t *testing.T) {
ps, err := helpers.NewPathSpec(hugofs.NewFrom(fs, cfg), cfg, loggers.NewErrorLogger())
c.Assert(err, qt.IsNil)
- sourceSpec := source.NewSourceSpec(ps, fs)
+ sourceSpec := source.NewSourceSpec(ps, nil, fs)
t.Run("Collect", func(t *testing.T) {
c := qt.New(t)
diff --git a/hugolib/site.go b/hugolib/site.go
index 18c9bfc80..96cf0b93c 100644
--- a/hugolib/site.go
+++ b/hugolib/site.go
@@ -1193,7 +1193,7 @@ func (s *Site) processPartial(config *BuildCfg, init func(config *BuildCfg) erro
filenamesChanged = helpers.UniqueStringsReuse(filenamesChanged)
- if err := s.readAndProcessContent(filenamesChanged...); err != nil {
+ if err := s.readAndProcessContent(*config, filenamesChanged...); err != nil {
return err
}
@@ -1207,7 +1207,7 @@ func (s *Site) process(config BuildCfg) (err error) {
err = errors.Wrap(err, "initialize")
return
}
- if err = s.readAndProcessContent(); err != nil {
+ if err = s.readAndProcessContent(config); err != nil {
err = errors.Wrap(err, "readAndProcessContent")
return
}
@@ -1376,8 +1376,8 @@ func (s *Site) eventToIdentity(e fsnotify.Event) (identity.PathIdentity, bool) {
return identity.PathIdentity{}, false
}
-func (s *Site) readAndProcessContent(filenames ...string) error {
- sourceSpec := source.NewSourceSpec(s.PathSpec, s.BaseFs.Content.Fs)
+func (s *Site) readAndProcessContent(buildConfig BuildCfg, filenames ...string) error {
+ sourceSpec := source.NewSourceSpec(s.PathSpec, buildConfig.ContentInclusionFilter, s.BaseFs.Content.Fs)
proc := newPagesProcessor(s.h, sourceSpec)