| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | // Copyright © 2014 Steve Francia <spf@spf13.com>.
 | 
					
						
							|  |  |  | //
 | 
					
						
							|  |  |  | // Licensed under the Simple Public 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://opensource.org/licenses/Simple-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
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import (
 | 
					
						
							|  |  |  | 	"bytes"
 | 
					
						
							|  |  |  | 	"io/ioutil"
 | 
					
						
							|  |  |  | 	"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"
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	"github.com/spf13/cast"
 | 
					
						
							|  |  |  | 	"github.com/spf13/hugo/helpers"
 | 
					
						
							| 
									
										
										
										
											2014-11-01 11:57:29 -04:00
										 |  |  | 	"github.com/spf13/hugo/hugofs"
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	"github.com/spf13/hugo/hugolib"
 | 
					
						
							|  |  |  | 	"github.com/spf13/hugo/parser"
 | 
					
						
							|  |  |  | 	jww "github.com/spf13/jwalterweatherman"
 | 
					
						
							|  |  |  | 	"github.com/spf13/viper"
 | 
					
						
							|  |  |  | )
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func NewContent(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
										 |  |  | 
 | 
					
						
							|  |  |  | 	location := FindArchetype(kind)
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-12 15:57:00 +02:00
										 |  |  | 	var by []byte
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if location != "" {
 | 
					
						
							|  |  |  | 		by, err = ioutil.ReadFile(location)
 | 
					
						
							|  |  |  | 		if err != nil {
 | 
					
						
							|  |  |  | 			jww.ERROR.Println(err)
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	if location == "" || err != nil {
 | 
					
						
							|  |  |  | 		by = []byte("+++\n title = \"title\"\n draft = true \n+++\n")
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	psr, err := parser.ReadFrom(bytes.NewReader(by))
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	metadata, err := psr.Metadata()
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 	newmetadata, err := cast.ToStringMapE(metadata)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							| 
									
										
										
										
											2014-11-02 17:29:55 -08:00
										 |  |  | 		jww.ERROR.Println("Error processing archetype file:", location)
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-05 23:19:10 +01:00
										 |  |  | 	for k := range newmetadata {
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		switch strings.ToLower(k) {
 | 
					
						
							|  |  |  | 		case "date":
 | 
					
						
							|  |  |  | 			newmetadata[k] = time.Now()
 | 
					
						
							|  |  |  | 		case "title":
 | 
					
						
							|  |  |  | 			newmetadata[k] = helpers.MakeTitle(helpers.Filename(name))
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	caseimatch := func(m map[string]interface{}, key string) bool {
 | 
					
						
							| 
									
										
										
										
											2015-03-05 23:19:10 +01:00
										 |  |  | 		for k := range m {
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 			if strings.ToLower(k) == strings.ToLower(key) {
 | 
					
						
							|  |  |  | 				return true
 | 
					
						
							|  |  |  | 			}
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 		return false
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-01-23 21:56:29 -03:30
										 |  |  | 	if newmetadata == nil {
 | 
					
						
							|  |  |  | 		newmetadata = make(map[string]interface{})
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	if !caseimatch(newmetadata, "date") {
 | 
					
						
							|  |  |  | 		newmetadata["date"] = time.Now()
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if !caseimatch(newmetadata, "title") {
 | 
					
						
							|  |  |  | 		newmetadata["title"] = helpers.MakeTitle(helpers.Filename(name))
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	page, err := hugolib.NewPage(name)
 | 
					
						
							|  |  |  | 	if err != nil {
 | 
					
						
							|  |  |  | 		return err
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-02-08 09:50:01 -07:00
										 |  |  | 	if x := parser.FormatSanitize(viper.GetString("MetaDataFormat")); x == "json" || x == "yaml" || x == "toml" {
 | 
					
						
							| 
									
										
										
										
											2014-05-29 18:40:16 -04:00
										 |  |  | 		newmetadata["date"] = time.Now().Format(time.RFC3339)
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-16 20:20:09 -04:00
										 |  |  | 	//page.Dir = viper.GetString("sourceDir")
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	page.SetSourceMetaData(newmetadata, parser.FormatToLeadRune(viper.GetString("MetaDataFormat")))
 | 
					
						
							| 
									
										
										
										
											2014-10-12 15:57:00 +02:00
										 |  |  | 	page.SetSourceContent(psr.Content())
 | 
					
						
							| 
									
										
										
										
											2014-11-06 09:51:14 -06:00
										 |  |  | 	if err = page.SafeSaveSourceAs(filepath.Join(viper.GetString("contentDir"), name)); err != nil {
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		return
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							| 
									
										
										
										
											2014-11-06 09:51:14 -06:00
										 |  |  | 	jww.FEEDBACK.Println(helpers.AbsPathify(filepath.Join(viper.GetString("contentDir"), name)), "created")
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-10-14 22:48:55 -04:00
										 |  |  | 	editor := viper.GetString("NewContentEditor")
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if editor != "" {
 | 
					
						
							|  |  |  | 		jww.FEEDBACK.Printf("Editing %s in %s.\n", name, editor)
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		cmd := exec.Command(editor, path.Join(viper.GetString("contentDir"), name))
 | 
					
						
							|  |  |  | 		cmd.Stdin = os.Stdin
 | 
					
						
							|  |  |  | 		cmd.Stdout = os.Stdout
 | 
					
						
							|  |  |  | 		cmd.Stderr = os.Stderr
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		if err = cmd.Run(); err != nil {
 | 
					
						
							|  |  |  | 			return
 | 
					
						
							|  |  |  | 		}
 | 
					
						
							|  |  |  | 	}
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 	return nil
 | 
					
						
							|  |  |  | }
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | func FindArchetype(kind string) (outpath string) {
 | 
					
						
							|  |  |  | 	search := []string{helpers.AbsPathify(viper.GetString("archetypeDir"))}
 | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if viper.GetString("theme") != "" {
 | 
					
						
							| 
									
										
										
										
											2014-11-06 09:51:14 -06:00
										 |  |  | 		themeDir := filepath.Join(helpers.AbsPathify("themes/"+viper.GetString("theme")), "/archetypes/")
 | 
					
						
							| 
									
										
										
										
											2014-05-02 01:06:01 -04:00
										 |  |  | 		if _, err := os.Stat(themeDir); os.IsNotExist(err) {
 | 
					
						
							|  |  |  | 			jww.ERROR.Println("Unable to find archetypes directory for theme :", viper.GetString("theme"), "in", themeDir)
 | 
					
						
							|  |  |  | 		} 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")
 | 
					
						
							| 
									
										
										
										
											2014-11-01 11:57:29 -04:00
										 |  |  | 			if exists, _ := helpers.Exists(curpath, hugofs.SourceFs); 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 ""
 | 
					
						
							|  |  |  | }
 |