2016-03-17 18:30:33 +00:00
|
|
|
// Copyright 2016 The Hugo Authors. All rights reserved.
|
2014-05-02 05:06:01 +00:00
|
|
|
//
|
2015-11-24 03:16:36 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-05-02 05:06:01 +00:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-24 03:16:36 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-02 05:06:01 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2016-04-09 23:36:03 +00:00
|
|
|
// Package create provides functions to create new content.
|
2014-05-02 05:06:01 +00:00
|
|
|
package create
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"os"
|
2014-10-15 02:48:55 +00:00
|
|
|
"os/exec"
|
2014-11-06 15:51:14 +00:00
|
|
|
"path/filepath"
|
2014-05-02 05:06:01 +00:00
|
|
|
|
2017-06-13 16:42:45 +00:00
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/hugolib"
|
2014-05-02 05:06:01 +00:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
|
|
)
|
|
|
|
|
2016-03-17 18:30:33 +00:00
|
|
|
// NewContent creates a new content file in the content directory based upon the
|
|
|
|
// given kind, which is used to lookup an archetype.
|
2017-06-18 17:39:42 +00:00
|
|
|
func NewContent(
|
|
|
|
ps *helpers.PathSpec,
|
|
|
|
siteFactory func(filename string, siteUsed bool) (*hugolib.Site, error), kind, targetPath string) error {
|
2017-06-20 10:46:03 +00:00
|
|
|
ext := helpers.Ext(targetPath)
|
2017-06-18 17:39:42 +00:00
|
|
|
|
2017-06-20 10:46:03 +00:00
|
|
|
jww.INFO.Printf("attempting to create %q of %q of ext %q", targetPath, kind, ext)
|
2014-05-02 05:06:01 +00:00
|
|
|
|
2017-06-20 10:46:03 +00:00
|
|
|
archetypeFilename := findArchetype(ps, kind, ext)
|
2017-06-18 17:39:42 +00:00
|
|
|
|
|
|
|
// Building the sites can be expensive, so only do it if really needed.
|
|
|
|
siteUsed := false
|
2017-06-22 18:30:01 +00:00
|
|
|
|
|
|
|
if archetypeFilename != "" {
|
|
|
|
f, err := ps.Fs.Source.Open(archetypeFilename)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if helpers.ReaderContains(f, []byte(".Site")) {
|
|
|
|
siteUsed = true
|
|
|
|
}
|
2017-06-18 17:39:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
s, err := siteFactory(targetPath, siteUsed)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2014-05-02 05:06:01 +00:00
|
|
|
|
2017-06-18 17:39:42 +00:00
|
|
|
var content []byte
|
2014-05-02 05:06:01 +00:00
|
|
|
|
2017-06-18 17:06:28 +00:00
|
|
|
content, err = executeArcheTypeAsTemplate(s, kind, targetPath, archetypeFilename)
|
2014-05-02 05:06:01 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2016-03-17 18:30:33 +00:00
|
|
|
|
2018-03-21 16:21:46 +00:00
|
|
|
// The site may have multiple content dirs, and we currently do not know which contentDir the
|
|
|
|
// user wants to create this content in. We should improve on this, but we start by testing if the
|
|
|
|
// provided path points to an existing dir. If so, use it as is.
|
|
|
|
var contentPath string
|
|
|
|
var exists bool
|
|
|
|
targetDir := filepath.Dir(targetPath)
|
|
|
|
|
|
|
|
if targetDir != "" && targetDir != "." {
|
|
|
|
exists, _ = helpers.Exists(targetDir, ps.Fs.Source)
|
|
|
|
}
|
|
|
|
|
|
|
|
if exists {
|
|
|
|
contentPath = targetPath
|
|
|
|
} else {
|
|
|
|
contentPath = s.PathSpec.AbsPathify(filepath.Join(s.Cfg.GetString("contentDir"), targetPath))
|
|
|
|
}
|
2016-03-17 18:30:33 +00:00
|
|
|
|
2017-06-18 17:06:28 +00:00
|
|
|
if err := helpers.SafeWriteToDisk(contentPath, bytes.NewReader(content), s.Fs.Source); err != nil {
|
2014-05-02 05:06:01 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-03-24 07:13:05 +00:00
|
|
|
jww.FEEDBACK.Println(contentPath, "created")
|
2016-03-17 18:30:33 +00:00
|
|
|
|
2017-02-05 03:20:06 +00:00
|
|
|
editor := s.Cfg.GetString("newContentEditor")
|
2016-03-17 18:30:33 +00:00
|
|
|
if editor != "" {
|
2017-06-18 17:06:28 +00:00
|
|
|
jww.FEEDBACK.Printf("Editing %s with %q ...\n", targetPath, editor)
|
2016-03-17 18:30:33 +00:00
|
|
|
|
2017-03-24 07:13:05 +00:00
|
|
|
cmd := exec.Command(editor, contentPath)
|
2016-03-17 18:30:33 +00:00
|
|
|
cmd.Stdin = os.Stdin
|
|
|
|
cmd.Stdout = os.Stdout
|
|
|
|
cmd.Stderr = os.Stderr
|
|
|
|
|
|
|
|
return cmd.Run()
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// FindArchetype takes a given kind/archetype of content and returns an output
|
|
|
|
// path for that archetype. If no archetype is found, an empty string is
|
|
|
|
// returned.
|
2017-06-20 10:46:03 +00:00
|
|
|
func findArchetype(ps *helpers.PathSpec, kind, ext string) (outpath string) {
|
2017-06-18 17:39:42 +00:00
|
|
|
search := []string{ps.AbsPathify(ps.Cfg.GetString("archetypeDir"))}
|
2014-05-02 05:06:01 +00:00
|
|
|
|
2017-06-18 17:39:42 +00:00
|
|
|
if ps.Cfg.GetString("theme") != "" {
|
|
|
|
themeDir := filepath.Join(ps.AbsPathify(ps.Cfg.GetString("themesDir")+"/"+ps.Cfg.GetString("theme")), "/archetypes/")
|
|
|
|
if _, err := ps.Fs.Source.Stat(themeDir); os.IsNotExist(err) {
|
|
|
|
jww.ERROR.Printf("Unable to find archetypes directory for theme %q at %q", ps.Cfg.GetString("theme"), themeDir)
|
2014-05-02 05:06:01 +00:00
|
|
|
} else {
|
|
|
|
search = append(search, themeDir)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, x := range search {
|
2014-08-19 00:52:43 +00:00
|
|
|
// If the new content isn't in a subdirectory, kind == "".
|
2014-10-12 13:57:00 +00:00
|
|
|
// Therefore it should be excluded otherwise `is a directory`
|
2017-06-13 16:47:17 +00:00
|
|
|
// error will occur. github.com/gohugoio/hugo/issues/411
|
2017-06-20 10:46:03 +00:00
|
|
|
var pathsToCheck = []string{"default"}
|
2014-08-19 00:52:43 +00:00
|
|
|
|
2017-06-20 10:46:03 +00:00
|
|
|
if ext != "" {
|
|
|
|
if kind != "" {
|
|
|
|
pathsToCheck = append([]string{kind + ext, "default" + ext}, pathsToCheck...)
|
|
|
|
} else {
|
|
|
|
pathsToCheck = append([]string{"default" + ext}, pathsToCheck...)
|
|
|
|
}
|
2014-08-19 00:52:43 +00:00
|
|
|
}
|
2017-06-20 10:46:03 +00:00
|
|
|
|
2014-05-02 05:06:01 +00:00
|
|
|
for _, p := range pathsToCheck {
|
2014-11-06 15:51:14 +00:00
|
|
|
curpath := filepath.Join(x, p)
|
2014-05-02 05:06:01 +00:00
|
|
|
jww.DEBUG.Println("checking", curpath, "for archetypes")
|
2017-06-18 17:39:42 +00:00
|
|
|
if exists, _ := helpers.Exists(curpath, ps.Fs.Source); exists {
|
2014-08-19 00:52:43 +00:00
|
|
|
jww.INFO.Println("curpath: " + curpath)
|
2014-05-02 05:06:01 +00:00
|
|
|
return curpath
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return ""
|
|
|
|
}
|