aboutsummaryrefslogtreecommitdiffhomepage
path: root/create/content.go
blob: 26eda20310b4fbfac665dbff37d916d404c13819 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
// Copyright 2019 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 create provides functions to create new content.
package create

import (
	"bytes"
	"io"
	"os"
	"path/filepath"
	"strings"

	"github.com/pkg/errors"

	"github.com/gohugoio/hugo/common/hexec"
	"github.com/gohugoio/hugo/hugofs/files"

	"github.com/gohugoio/hugo/hugofs"

	"github.com/gohugoio/hugo/helpers"
	"github.com/gohugoio/hugo/hugolib"
	"github.com/spf13/afero"
	jww "github.com/spf13/jwalterweatherman"
)

// NewContent creates a new content file in the content directory based upon the
// given kind, which is used to lookup an archetype.
func NewContent(
	sites *hugolib.HugoSites, kind, targetPath string) error {
	targetPath = filepath.Clean(targetPath)
	ext := helpers.Ext(targetPath)
	ps := sites.PathSpec
	archetypeFs := ps.BaseFs.SourceFilesystems.Archetypes.Fs
	sourceFs := ps.Fs.Source

	jww.INFO.Printf("attempting to create %q of %q of ext %q", targetPath, kind, ext)

	archetypeFilename, isDir := findArchetype(ps, kind, ext)
	contentPath, s := resolveContentPath(sites, sourceFs, targetPath)

	if isDir {

		langFs, err := hugofs.NewLanguageFs(sites.LanguageSet(), archetypeFs)
		if err != nil {
			return err
		}

		cm, err := mapArcheTypeDir(ps, langFs, archetypeFilename)
		if err != nil {
			return err
		}

		if cm.siteUsed {
			if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
				return err
			}
		}

		name := filepath.Base(targetPath)
		return newContentFromDir(archetypeFilename, sites, sourceFs, cm, name, contentPath)
	}

	// Building the sites can be expensive, so only do it if really needed.
	siteUsed := false

	if archetypeFilename != "" {

		var err error
		siteUsed, err = usesSiteVar(archetypeFs, archetypeFilename)
		if err != nil {
			return err
		}
	}

	if siteUsed {
		if err := sites.Build(hugolib.BuildCfg{SkipRender: true}); err != nil {
			return err
		}
	}

	content, err := executeArcheTypeAsTemplate(s, "", kind, targetPath, archetypeFilename)
	if err != nil {
		return err
	}

	if err := helpers.SafeWriteToDisk(contentPath, bytes.NewReader(content), s.Fs.Source); err != nil {
		return err
	}

	jww.FEEDBACK.Println(contentPath, "created")

	editor := s.Cfg.GetString("newContentEditor")
	if editor != "" {
		jww.FEEDBACK.Printf("Editing %s with %q ...\n", targetPath, editor)

		editorCmd := append(strings.Fields(editor), contentPath)
		cmd, err := hexec.SafeCommand(editorCmd[0], editorCmd[1:]...)
		if err != nil {
			return err
		}
		cmd.Stdin = os.Stdin
		cmd.Stdout = os.Stdout
		cmd.Stderr = os.Stderr

		return cmd.Run()
	}

	return nil
}

func targetSite(sites *hugolib.HugoSites, fi hugofs.FileMetaInfo) *hugolib.Site {
	for _, s := range sites.Sites {
		if fi.Meta().Lang() == s.Language().Lang {
			return s
		}
	}
	return sites.Sites[0]
}

func newContentFromDir(
	archetypeDir string,
	sites *hugolib.HugoSites,
	targetFs afero.Fs,
	cm archetypeMap, name, targetPath string) error {
	for _, f := range cm.otherFiles {
		meta := f.Meta()
		filename := meta.Path()
		// Just copy the file to destination.
		in, err := meta.Open()
		if err != nil {
			return errors.Wrap(err, "failed to open non-content file")
		}

		targetFilename := filepath.Join(targetPath, strings.TrimPrefix(filename, archetypeDir))

		targetDir := filepath.Dir(targetFilename)
		if err := targetFs.MkdirAll(targetDir, 0777); err != nil && !os.IsExist(err) {
			return errors.Wrapf(err, "failed to create target directory for %s:", targetDir)
		}

		out, err := targetFs.Create(targetFilename)
		if err != nil {
			return err
		}

		_, err = io.Copy(out, in)
		if err != nil {
			return err
		}

		in.Close()
		out.Close()
	}

	for _, f := range cm.contentFiles {
		filename := f.Meta().Path()
		s := targetSite(sites, f)
		targetFilename := filepath.Join(targetPath, strings.TrimPrefix(filename, archetypeDir))

		content, err := executeArcheTypeAsTemplate(s, name, archetypeDir, targetFilename, filename)
		if err != nil {
			return errors.Wrap(err, "failed to execute archetype template")
		}

		if err := helpers.SafeWriteToDisk(targetFilename, bytes.NewReader(content), targetFs); err != nil {
			return errors.Wrap(err, "failed to save results")
		}
	}

	jww.FEEDBACK.Println(targetPath, "created")

	return nil
}

type archetypeMap struct {
	// These needs to be parsed and executed as Go templates.
	contentFiles []hugofs.FileMetaInfo
	// These are just copied to destination.
	otherFiles []hugofs.FileMetaInfo
	// If the templates needs a fully built site. This can potentially be
	// expensive, so only do when needed.
	siteUsed bool
}

func mapArcheTypeDir(
	ps *helpers.PathSpec,
	fs afero.Fs,
	archetypeDir string) (archetypeMap, error) {
	var m archetypeMap

	walkFn := func(path string, fi hugofs.FileMetaInfo, err error) error {
		if err != nil {
			return err
		}

		if fi.IsDir() {
			return nil
		}

		fil := fi.(hugofs.FileMetaInfo)

		if files.IsContentFile(path) {
			m.contentFiles = append(m.contentFiles, fil)
			if !m.siteUsed {
				m.siteUsed, err = usesSiteVar(fs, path)
				if err != nil {
					return err
				}
			}
			return nil
		}

		m.otherFiles = append(m.otherFiles, fil)

		return nil
	}

	walkCfg := hugofs.WalkwayConfig{
		WalkFn: walkFn,
		Fs:     fs,
		Root:   archetypeDir,
	}

	w := hugofs.NewWalkway(walkCfg)

	if err := w.Walk(); err != nil {
		return m, errors.Wrapf(err, "failed to walk archetype dir %q", archetypeDir)
	}

	return m, nil
}

func usesSiteVar(fs afero.Fs, filename string) (bool, error) {
	f, err := fs.Open(filename)
	if err != nil {
		return false, errors.Wrap(err, "failed to open archetype file")
	}
	defer f.Close()
	return helpers.ReaderContains(f, []byte(".Site")), nil
}

// Resolve the target content path.
func resolveContentPath(sites *hugolib.HugoSites, fs afero.Fs, targetPath string) (string, *hugolib.Site) {
	targetDir := filepath.Dir(targetPath)
	first := sites.Sites[0]

	var (
		s              *hugolib.Site
		siteContentDir string
	)

	// Try the filename: my-post.en.md
	for _, ss := range sites.Sites {
		if strings.Contains(targetPath, "."+ss.Language().Lang+".") {
			s = ss
			break
		}
	}

	var dirLang string

	for _, dir := range sites.BaseFs.Content.Dirs {
		meta := dir.Meta()
		contentDir := meta.Filename()

		if !strings.HasSuffix(contentDir, helpers.FilePathSeparator) {
			contentDir += helpers.FilePathSeparator
		}

		if strings.HasPrefix(targetPath, contentDir) {
			siteContentDir = contentDir
			dirLang = meta.Lang()
			break
		}
	}

	if s == nil && dirLang != "" {
		for _, ss := range sites.Sites {
			if ss.Lang() == dirLang {
				s = ss
				break
			}
		}
	}

	if s == nil {
		s = first
	}

	if targetDir != "" && targetDir != "." {
		exists, _ := helpers.Exists(targetDir, fs)

		if exists {
			return targetPath, s
		}
	}

	if siteContentDir == "" {
	}

	if siteContentDir != "" {
		pp := filepath.Join(siteContentDir, strings.TrimPrefix(targetPath, siteContentDir))
		return s.PathSpec.AbsPathify(pp), s
	} else {
		var contentDir string
		for _, dir := range sites.BaseFs.Content.Dirs {
			contentDir = dir.Meta().Filename()
			if dir.Meta().Lang() == s.Lang() {
				break
			}
		}
		return s.PathSpec.AbsPathify(filepath.Join(contentDir, targetPath)), s
	}
}

// FindArchetype takes a given kind/archetype of content and returns the path
// to the archetype in the archetype filesystem, blank if none found.
func findArchetype(ps *helpers.PathSpec, kind, ext string) (outpath string, isDir bool) {
	fs := ps.BaseFs.Archetypes.Fs

	var pathsToCheck []string

	if kind != "" {
		pathsToCheck = append(pathsToCheck, kind+ext)
	}
	pathsToCheck = append(pathsToCheck, "default"+ext, "default")

	for _, p := range pathsToCheck {
		fi, err := fs.Stat(p)
		if err == nil {
			return p, fi.IsDir()
		}
	}

	return "", false
}