2021-10-13 06:12:06 +00:00
|
|
|
// 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 (
|
2023-02-11 15:20:24 +00:00
|
|
|
"context"
|
2022-05-02 14:07:52 +00:00
|
|
|
"fmt"
|
2021-10-13 06:12:06 +00:00
|
|
|
"io"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2022-04-26 17:57:04 +00:00
|
|
|
"github.com/gohugoio/hugo/common/htime"
|
2021-10-13 06:12:06 +00:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/source"
|
|
|
|
|
|
|
|
"github.com/gohugoio/hugo/resources/page"
|
|
|
|
|
|
|
|
"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.
|
2022-03-10 13:10:21 +00:00
|
|
|
shortcodeReplacerPre *strings.Replacer
|
|
|
|
shortcodeReplacerPost *strings.Replacer
|
2021-10-13 06:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 13:10:21 +00:00
|
|
|
// ApplyArchetypeFilename archetypeFilename to w as a template using the given Page p as the foundation for the data context.
|
|
|
|
func (f ContentFactory) ApplyArchetypeFilename(w io.Writer, p page.Page, archetypeKind, archetypeFilename string) error {
|
2021-10-13 06:12:06 +00:00
|
|
|
|
|
|
|
fi, err := f.h.SourceFilesystems.Archetypes.Fs.Stat(archetypeFilename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if fi.IsDir() {
|
2022-05-02 14:07:52 +00:00
|
|
|
return fmt.Errorf("archetype directory (%q) not supported", archetypeFilename)
|
2021-10-13 06:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
templateSource, err := afero.ReadFile(f.h.SourceFilesystems.Archetypes.Fs, archetypeFilename)
|
|
|
|
if err != nil {
|
2022-05-02 14:07:52 +00:00
|
|
|
return fmt.Errorf("failed to read archetype file %q: %s: %w", archetypeFilename, err, err)
|
2021-10-13 06:12:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-10 13:10:21 +00:00
|
|
|
return f.ApplyArchetypeTemplate(w, p, archetypeKind, string(templateSource))
|
2021-10-13 06:12:06 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-10 13:10:21 +00:00
|
|
|
// ApplyArchetypeTemplate templateSource to w as a template using the given Page p as the foundation for the data context.
|
|
|
|
func (f ContentFactory) ApplyArchetypeTemplate(w io.Writer, p page.Page, archetypeKind, templateSource string) error {
|
2021-10-13 06:12:06 +00:00
|
|
|
ps := p.(*pageState)
|
|
|
|
if archetypeKind == "" {
|
|
|
|
archetypeKind = p.Type()
|
|
|
|
}
|
|
|
|
|
|
|
|
d := &archetypeFileData{
|
|
|
|
Type: archetypeKind,
|
2022-04-26 17:57:04 +00:00
|
|
|
Date: htime.Now().Format(time.RFC3339),
|
2021-10-13 06:12:06 +00:00
|
|
|
Page: p,
|
|
|
|
File: p.File(),
|
|
|
|
}
|
|
|
|
|
2022-03-10 13:10:21 +00:00
|
|
|
templateSource = f.shortcodeReplacerPre.Replace(templateSource)
|
2021-10-13 06:12:06 +00:00
|
|
|
|
|
|
|
templ, err := ps.s.TextTmpl().Parse("archetype.md", string(templateSource))
|
|
|
|
if err != nil {
|
2022-05-02 14:07:52 +00:00
|
|
|
return fmt.Errorf("failed to parse archetype template: %s: %w", err, err)
|
2021-10-13 06:12:06 +00:00
|
|
|
}
|
|
|
|
|
2023-02-11 15:20:24 +00:00
|
|
|
result, err := executeToString(context.TODO(), ps.s.Tmpl(), templ, d)
|
2021-10-13 06:12:06 +00:00
|
|
|
if err != nil {
|
2022-05-02 14:07:52 +00:00
|
|
|
return fmt.Errorf("failed to execute archetype template: %s: %w", err, err)
|
2021-10-13 06:12:06 +00:00
|
|
|
}
|
|
|
|
|
2022-03-10 13:10:21 +00:00
|
|
|
_, err = io.WriteString(w, f.shortcodeReplacerPost.Replace(result))
|
2021-10-13 06:12:06 +00:00
|
|
|
|
|
|
|
return err
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2021-11-15 09:25:30 +00:00
|
|
|
func (f ContentFactory) SectionFromFilename(filename string) (string, error) {
|
2021-10-13 06:12:06 +00:00
|
|
|
filename = filepath.Clean(filename)
|
2021-11-15 09:25:30 +00:00
|
|
|
rel, _, err := f.h.AbsProjectContentDir(filename)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2021-10-13 06:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
parts := strings.Split(helpers.ToSlashTrimLeading(rel), "/")
|
|
|
|
if len(parts) < 2 {
|
2021-11-15 09:25:30 +00:00
|
|
|
return "", nil
|
2021-10-13 06:12:06 +00:00
|
|
|
}
|
2021-11-15 09:25:30 +00:00
|
|
|
return parts[0], nil
|
2021-10-13 06:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// CreateContentPlaceHolder creates a content placeholder file inside the
|
|
|
|
// best matching content directory.
|
2022-08-31 15:23:31 +00:00
|
|
|
func (f ContentFactory) CreateContentPlaceHolder(filename string, force bool) (string, error) {
|
2021-10-13 06:12:06 +00:00
|
|
|
filename = filepath.Clean(filename)
|
2021-11-15 09:25:30 +00:00
|
|
|
_, abs, err := f.h.AbsProjectContentDir(filename)
|
2022-04-08 13:15:26 +00:00
|
|
|
|
2021-11-15 09:25:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
2021-10-13 06:12:06 +00:00
|
|
|
|
2022-03-10 13:10:21 +00:00
|
|
|
// This will be overwritten later, just write a placeholder to get
|
2021-10-13 06:12:06 +00:00
|
|
|
// the paths correct.
|
|
|
|
placeholder := `---
|
|
|
|
title: "Content Placeholder"
|
|
|
|
_build:
|
|
|
|
render: never
|
|
|
|
list: never
|
|
|
|
publishResources: false
|
|
|
|
---
|
|
|
|
|
|
|
|
`
|
|
|
|
|
2022-08-31 15:23:31 +00:00
|
|
|
if force {
|
|
|
|
return abs, afero.WriteReader(f.h.Fs.Source, abs, strings.NewReader(placeholder))
|
|
|
|
}
|
2021-10-18 17:08:19 +00:00
|
|
|
return abs, afero.SafeWriteReader(f.h.Fs.Source, abs, strings.NewReader(placeholder))
|
2021-10-13 06:12:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewContentFactory creates a new ContentFactory for h.
|
|
|
|
func NewContentFactory(h *HugoSites) ContentFactory {
|
|
|
|
return ContentFactory{
|
|
|
|
h: h,
|
2022-03-10 13:10:21 +00:00
|
|
|
shortcodeReplacerPre: strings.NewReplacer(
|
2021-10-13 06:12:06 +00:00
|
|
|
"{{<", "{x{<",
|
|
|
|
"{{%", "{x{%",
|
|
|
|
">}}", ">}x}",
|
|
|
|
"%}}", "%}x}"),
|
2022-03-10 13:10:21 +00:00
|
|
|
shortcodeReplacerPost: strings.NewReplacer(
|
2021-10-13 06:12:06 +00:00
|
|
|
"{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()
|
|
|
|
}
|