| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | // Copyright 2016 The Hugo Authors. All rights reserved.
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | //
 | 
					
						
							| 
									
										
										
										
											2015-11-23 22:16:36 -05:00
										 |  |  | // Licensed under the Apache License, Version 2.0 (the "License");
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | // you may not use this file except in compliance with the License.
 | 
					
						
							|  |  |  | // You may obtain a copy of the License at
 | 
					
						
							| 
									
										
										
										
											2015-11-23 22:16:36 -05:00
										 |  |  | // http://www.apache.org/licenses/LICENSE-2.0
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04: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-10 01:36:03 +02:00
										 |  |  | // Package create provides functions to create new content.
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | package create
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"bytes"
 | 
					
						
							|  |  |  | 	"os"
 | 
					
						
							| 
									
										
										
										
											2014-10-14 22:48:55 -04:00
										 |  |  | 	"os/exec"
 | 
					
						
							|  |  |  | 	"path"
 | 
					
						
							| 
									
										
										
										
											2014-11-06 09:51:14 -06:00
										 |  |  | 	"path/filepath"
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	"strings"
 | 
					
						
							|  |  |  | 	"time"
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 	"github.com/spf13/afero"
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	"github.com/spf13/cast"
 | 
					
						
							|  |  |  | 	"github.com/spf13/hugo/helpers"
 | 
					
						
							|  |  |  | 	"github.com/spf13/hugo/hugolib"
 | 
					
						
							|  |  |  | 	"github.com/spf13/hugo/parser"
 | 
					
						
							|  |  |  | 	jww "github.com/spf13/jwalterweatherman"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | // NewContent creates a new content file in the content directory based upon the
 | 
					
						
							|  |  |  | // given kind, which is used to lookup an archetype.
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:55:03 +01:00
										 |  |  | func NewContent(s *hugolib.Site, kind, name string) (err error) {
 | 
					
						
							| 
									
										
										
										
											2014-08-18 19:52:43 -05:00
										 |  |  | 	jww.INFO.Println("attempting to create ", name, "of", kind)
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-05 10:20:06 +07:00
										 |  |  | 	location := FindArchetype(s, kind)
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-12 15:57:00 +02:00
										 |  |  | 	var by []byte
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if location != "" {
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:55:03 +01:00
										 |  |  | 		by, err = afero.ReadFile(s.Fs.Source, location)
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			jww.ERROR.Println(err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if location == "" || err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-11-29 20:18:09 +01:00
										 |  |  | 		by = []byte("+++\ndraft = true \n+++\n")
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	psr, err := parser.ReadFrom(bytes.NewReader(by))
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	metadata, err := createMetadata(psr, name)
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 		jww.ERROR.Printf("Error processing archetype file %s: %s\n", location, err)
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-01-10 10:55:03 +01:00
										 |  |  | 	page, err := s.NewPage(name)
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-05 10:20:06 +07:00
										 |  |  | 	if err = page.SetSourceMetaData(metadata, parser.FormatToLeadRune(s.Cfg.GetString("metaDataFormat"))); err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	page.SetSourceContent(psr.Content())
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-05 10:20:06 +07:00
										 |  |  | 	if err = page.SafeSaveSourceAs(filepath.Join(s.Cfg.GetString("contentDir"), name)); err != nil {
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2017-02-05 10:20:06 +07:00
										 |  |  | 	jww.FEEDBACK.Println(s.PathSpec.AbsPathify(filepath.Join(s.Cfg.GetString("contentDir"), name)), "created")
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-05 10:20:06 +07:00
										 |  |  | 	editor := s.Cfg.GetString("newContentEditor")
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if editor != "" {
 | 
					
						
							|  |  |  | 		jww.FEEDBACK.Printf("Editing %s with %q ...\n", name, editor)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-05 10:20:06 +07:00
										 |  |  | 		cmd := exec.Command(editor, s.PathSpec.AbsPathify(path.Join(s.Cfg.GetString("contentDir"), name)))
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 		cmd.Stdin = os.Stdin
 | 
					
						
							|  |  |  | 		cmd.Stdout = os.Stdout
 | 
					
						
							|  |  |  | 		cmd.Stderr = os.Stderr
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		return cmd.Run()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | // createMetadata generates Metadata for a new page based upon the metadata
 | 
					
						
							|  |  |  | // found in an archetype.
 | 
					
						
							|  |  |  | func createMetadata(archetype parser.Page, name string) (map[string]interface{}, error) {
 | 
					
						
							|  |  |  | 	archMetadata, err := archetype.Metadata()
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	metadata, err := cast.ToStringMapE(archMetadata)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return nil, err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-29 20:18:09 +01:00
										 |  |  | 	var date time.Time
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for k, v := range metadata {
 | 
					
						
							| 
									
										
										
										
											2016-12-15 09:27:30 +01:00
										 |  |  | 		if v == "" {
 | 
					
						
							|  |  |  | 			continue
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2016-11-29 20:18:09 +01:00
										 |  |  | 		lk := strings.ToLower(k)
 | 
					
						
							|  |  |  | 		switch lk {
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		case "date":
 | 
					
						
							| 
									
										
										
										
											2016-11-29 20:18:09 +01:00
										 |  |  | 			date, err = cast.ToTimeE(v)
 | 
					
						
							|  |  |  | 			if err != nil {
 | 
					
						
							|  |  |  | 				return nil, err
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		case "title":
 | 
					
						
							| 
									
										
										
										
											2016-11-29 20:18:09 +01:00
										 |  |  | 			// Use the archetype title as is
 | 
					
						
							| 
									
										
										
										
											2016-11-29 20:32:26 +01:00
										 |  |  | 			metadata[lk] = v
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 	if metadata == nil {
 | 
					
						
							|  |  |  | 		metadata = make(map[string]interface{})
 | 
					
						
							| 
									
										
										
										
											2015-01-23 21:56:29 -03:30
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-29 20:32:26 +01:00
										 |  |  | 	if date.IsZero() {
 | 
					
						
							| 
									
										
										
										
											2016-11-29 20:18:09 +01:00
										 |  |  | 		date = time.Now()
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-11-29 20:32:26 +01:00
										 |  |  | 	if _, ok := metadata["title"]; !ok {
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 		metadata["title"] = helpers.MakeTitle(helpers.Filename(name))
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-05 10:20:06 +07:00
										 |  |  | 	metadata["date"] = date.Format(time.RFC3339)
 | 
					
						
							| 
									
										
										
										
											2014-05-29 18:40:16 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | 	return metadata, nil
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-03-17 13:30:33 -05:00
										 |  |  | // 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-02-05 10:20:06 +07:00
										 |  |  | func FindArchetype(s *hugolib.Site, kind string) (outpath string) {
 | 
					
						
							|  |  |  | 	search := []string{s.PathSpec.AbsPathify(s.Cfg.GetString("archetypeDir"))}
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2017-02-05 10:20:06 +07:00
										 |  |  | 	if s.Cfg.GetString("theme") != "" {
 | 
					
						
							|  |  |  | 		themeDir := filepath.Join(s.PathSpec.AbsPathify(s.Cfg.GetString("themesDir")+"/"+s.Cfg.GetString("theme")), "/archetypes/")
 | 
					
						
							|  |  |  | 		if _, err := s.Fs.Source.Stat(themeDir); os.IsNotExist(err) {
 | 
					
						
							|  |  |  | 			jww.ERROR.Printf("Unable to find archetypes directory for theme %q at %q", s.Cfg.GetString("theme"), themeDir)
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		} else {
 | 
					
						
							|  |  |  | 			search = append(search, themeDir)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for _, x := range search {
 | 
					
						
							| 
									
										
										
										
											2014-08-18 19:52:43 -05:00
										 |  |  | 		// If the new content isn't in a subdirectory, kind == "".
 | 
					
						
							| 
									
										
										
										
											2014-10-12 15:57:00 +02:00
										 |  |  | 		// Therefore it should be excluded otherwise `is a directory`
 | 
					
						
							| 
									
										
										
										
											2014-08-18 19:52:43 -05:00
										 |  |  | 		// error will occur. github.com/spf13/hugo/issues/411
 | 
					
						
							|  |  |  | 		var pathsToCheck []string
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if kind == "" {
 | 
					
						
							|  |  |  | 			pathsToCheck = []string{"default.md", "default"}
 | 
					
						
							|  |  |  | 		} else {
 | 
					
						
							|  |  |  | 			pathsToCheck = []string{kind + ".md", kind, "default.md", "default"}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		for _, p := range pathsToCheck {
 | 
					
						
							| 
									
										
										
										
											2014-11-06 09:51:14 -06:00
										 |  |  | 			curpath := filepath.Join(x, p)
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 			jww.DEBUG.Println("checking", curpath, "for archetypes")
 | 
					
						
							| 
									
										
										
										
											2017-02-05 10:20:06 +07:00
										 |  |  | 			if exists, _ := helpers.Exists(curpath, s.Fs.Source); exists {
 | 
					
						
							| 
									
										
										
										
											2014-08-18 19:52:43 -05:00
										 |  |  | 				jww.INFO.Println("curpath: " + curpath)
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 				return curpath
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	return ""
 | 
					
						
							|  |  |  | }
 |