aboutsummaryrefslogtreecommitdiffhomepage
path: root/create
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <[email protected]>2016-11-29 20:18:09 +0100
committerBjørn Erik Pedersen <[email protected]>2016-11-29 20:18:09 +0100
commit96018ab98c1153c9d882656abf99685f2fb7e0c4 (patch)
tree99347fbdc20f9f2eaf0fb2e954fcaa6a74e83417 /create
parent65d4d96e7f24a27f69acec07ea56d5ab3ddc63c3 (diff)
downloadhugo-96018ab98c1153c9d882656abf99685f2fb7e0c4.tar.gz
hugo-96018ab98c1153c9d882656abf99685f2fb7e0c4.zip
create: Fix archetype title and date handling
Fixes #2750
Diffstat (limited to 'create')
-rw-r--r--create/content.go24
-rw-r--r--create/content_test.go65
2 files changed, 59 insertions, 30 deletions
diff --git a/create/content.go b/create/content.go
index fb9ef7a04..783e21449 100644
--- a/create/content.go
+++ b/create/content.go
@@ -48,7 +48,7 @@ func NewContent(fs afero.Fs, kind, name string) (err error) {
}
}
if location == "" || err != nil {
- by = []byte("+++\n title = \"title\"\n draft = true \n+++\n")
+ by = []byte("+++\ndraft = true \n+++\n")
}
psr, err := parser.ReadFrom(bytes.NewReader(by))
@@ -107,12 +107,19 @@ func createMetadata(archetype parser.Page, name string) (map[string]interface{},
return nil, err
}
- for k := range metadata {
- switch strings.ToLower(k) {
+ var date time.Time
+
+ for k, v := range metadata {
+ lk := strings.ToLower(k)
+ switch lk {
case "date":
- metadata[k] = time.Now()
+ date, err = cast.ToTimeE(v)
+ if err != nil {
+ return nil, err
+ }
case "title":
- metadata[k] = helpers.MakeTitle(helpers.Filename(name))
+ // Use the archetype title as is
+ metadata[lk] = cast.ToString(v)
}
}
@@ -130,15 +137,18 @@ func createMetadata(archetype parser.Page, name string) (map[string]interface{},
}
if !caseimatch(metadata, "date") {
- metadata["date"] = time.Now()
+ date = time.Now()
}
if !caseimatch(metadata, "title") {
metadata["title"] = helpers.MakeTitle(helpers.Filename(name))
}
+ // TOD(bep) what is this?
if x := parser.FormatSanitize(viper.GetString("metaDataFormat")); x == "json" || x == "yaml" || x == "toml" {
- metadata["date"] = time.Now().Format(time.RFC3339)
+ metadata["date"] = date.Format(time.RFC3339)
+ } else {
+ metadata["date"] = date
}
return metadata, nil
diff --git a/create/content_test.go b/create/content_test.go
index 50acc89b4..3d2453b7b 100644
--- a/create/content_test.go
+++ b/create/content_test.go
@@ -16,10 +16,14 @@ package create_test
import (
"os"
"path/filepath"
+ "strings"
"testing"
+ "fmt"
+
"github.com/spf13/afero"
"github.com/spf13/hugo/create"
+ "github.com/spf13/hugo/helpers"
"github.com/spf13/hugo/hugofs"
"github.com/spf13/viper"
)
@@ -33,35 +37,32 @@ func TestNewContent(t *testing.T) {
}
cases := []struct {
- kind string
- path string
- resultStrings []string
+ kind string
+ path string
+ expected []string
}{
- {"post", "post/sample-1.md", []string{`title = "sample 1"`, `test = "test1"`}},
+ {"post", "post/sample-1.md", []string{`title = "Post Arch title"`, `test = "test1"`, "date = \"2015-01-12T19:20:04-07:00\""}},
{"stump", "stump/sample-2.md", []string{`title = "sample 2"`}}, // no archetype file
{"", "sample-3.md", []string{`title = "sample 3"`}}, // no archetype
{"product", "product/sample-4.md", []string{`title = "sample 4"`}}, // empty archetype front matter
}
for i, c := range cases {
+ if i > 0 {
+ break
+ }
err = create.NewContent(hugofs.Source(), c.kind, c.path)
if err != nil {
t.Errorf("[%d] NewContent: %s", i, err)
}
- fname := filepath.Join(os.TempDir(), "content", filepath.FromSlash(c.path))
- _, err = hugofs.Source().Stat(fname)
- if err != nil {
- t.Errorf("[%d] Stat: %s", i, err)
- }
+ fname := filepath.Join("content", filepath.FromSlash(c.path))
+ content := readFileFromFs(t, hugofs.Source(), fname)
- for _, v := range c.resultStrings {
- found, err := afero.FileContainsBytes(hugofs.Source(), fname, []byte(v))
- if err != nil {
- t.Errorf("[%d] FileContainsBytes: %s", i, err)
- }
+ for i, v := range c.expected {
+ found := strings.Contains(content, v)
if !found {
- t.Errorf("content missing from output: %q", v)
+ t.Errorf("[%d] %q missing from output:\n%q", i, v, content)
}
}
}
@@ -70,14 +71,14 @@ func TestNewContent(t *testing.T) {
func initViper() {
viper.Reset()
viper.Set("metaDataFormat", "toml")
- viper.Set("archetypeDir", filepath.Join(os.TempDir(), "archetypes"))
- viper.Set("contentDir", filepath.Join(os.TempDir(), "content"))
- viper.Set("themesDir", filepath.Join(os.TempDir(), "themes"))
+ viper.Set("archetypeDir", "archetypes")
+ viper.Set("contentDir", "content")
+ viper.Set("themesDir", "themes")
viper.Set("theme", "sample")
}
func initFs() error {
- hugofs.SetSource(new(afero.MemMapFs))
+ hugofs.InitMemFs()
perm := os.FileMode(0755)
var err error
@@ -88,7 +89,6 @@ func initFs() error {
filepath.Join("themes", "sample", "archetypes"),
}
for _, dir := range dirs {
- dir = filepath.Join(os.TempDir(), dir)
err = hugofs.Source().Mkdir(dir, perm)
if err != nil {
return err
@@ -101,11 +101,11 @@ func initFs() error {
content string
}{
{
- path: filepath.Join(os.TempDir(), "archetypes", "post.md"),
- content: "+++\ndate = \"2015-01-12T19:20:04-07:00\"\ntitle = \"post arch\"\ntest = \"test1\"\n+++\n",
+ path: filepath.Join("archetypes", "post.md"),
+ content: "+++\ndate = \"2015-01-12T19:20:04-07:00\"\ntitle = \"Post Arch title\"\ntest = \"test1\"\n+++\n",
},
{
- path: filepath.Join(os.TempDir(), "archetypes", "product.md"),
+ path: filepath.Join("archetypes", "product.md"),
content: "+++\n+++\n",
},
} {
@@ -123,3 +123,22 @@ func initFs() error {
return nil
}
+
+// TODO(bep) extract common testing package with this and some others
+func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string {
+ filename = filepath.FromSlash(filename)
+ b, err := afero.ReadFile(fs, filename)
+ if err != nil {
+ // Print some debug info
+ root := strings.Split(filename, helpers.FilePathSeparator)[0]
+ afero.Walk(fs, root, func(path string, info os.FileInfo, err error) error {
+ if info != nil && !info.IsDir() {
+ fmt.Println(" ", path)
+ }
+
+ return nil
+ })
+ t.Fatalf("Failed to read file: %s", err)
+ }
+ return string(b)
+}