mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
parent
65d4d96e7f
commit
96018ab98c
2 changed files with 59 additions and 30 deletions
|
@ -48,7 +48,7 @@ func NewContent(fs afero.Fs, kind, name string) (err error) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if location == "" || err != nil {
|
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))
|
psr, err := parser.ReadFrom(bytes.NewReader(by))
|
||||||
|
@ -107,12 +107,19 @@ func createMetadata(archetype parser.Page, name string) (map[string]interface{},
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
for k := range metadata {
|
var date time.Time
|
||||||
switch strings.ToLower(k) {
|
|
||||||
|
for k, v := range metadata {
|
||||||
|
lk := strings.ToLower(k)
|
||||||
|
switch lk {
|
||||||
case "date":
|
case "date":
|
||||||
metadata[k] = time.Now()
|
date, err = cast.ToTimeE(v)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
case "title":
|
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") {
|
if !caseimatch(metadata, "date") {
|
||||||
metadata["date"] = time.Now()
|
date = time.Now()
|
||||||
}
|
}
|
||||||
|
|
||||||
if !caseimatch(metadata, "title") {
|
if !caseimatch(metadata, "title") {
|
||||||
metadata["title"] = helpers.MakeTitle(helpers.Filename(name))
|
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" {
|
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
|
return metadata, nil
|
||||||
|
|
|
@ -16,10 +16,14 @@ package create_test
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"fmt"
|
||||||
|
|
||||||
"github.com/spf13/afero"
|
"github.com/spf13/afero"
|
||||||
"github.com/spf13/hugo/create"
|
"github.com/spf13/hugo/create"
|
||||||
|
"github.com/spf13/hugo/helpers"
|
||||||
"github.com/spf13/hugo/hugofs"
|
"github.com/spf13/hugo/hugofs"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
)
|
)
|
||||||
|
@ -35,33 +39,30 @@ func TestNewContent(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
kind string
|
kind string
|
||||||
path string
|
path string
|
||||||
resultStrings []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
|
{"stump", "stump/sample-2.md", []string{`title = "sample 2"`}}, // no archetype file
|
||||||
{"", "sample-3.md", []string{`title = "sample 3"`}}, // no archetype
|
{"", "sample-3.md", []string{`title = "sample 3"`}}, // no archetype
|
||||||
{"product", "product/sample-4.md", []string{`title = "sample 4"`}}, // empty archetype front matter
|
{"product", "product/sample-4.md", []string{`title = "sample 4"`}}, // empty archetype front matter
|
||||||
}
|
}
|
||||||
|
|
||||||
for i, c := range cases {
|
for i, c := range cases {
|
||||||
|
if i > 0 {
|
||||||
|
break
|
||||||
|
}
|
||||||
err = create.NewContent(hugofs.Source(), c.kind, c.path)
|
err = create.NewContent(hugofs.Source(), c.kind, c.path)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("[%d] NewContent: %s", i, err)
|
t.Errorf("[%d] NewContent: %s", i, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
fname := filepath.Join(os.TempDir(), "content", filepath.FromSlash(c.path))
|
fname := filepath.Join("content", filepath.FromSlash(c.path))
|
||||||
_, err = hugofs.Source().Stat(fname)
|
content := readFileFromFs(t, hugofs.Source(), fname)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("[%d] Stat: %s", i, err)
|
|
||||||
}
|
|
||||||
|
|
||||||
for _, v := range c.resultStrings {
|
for i, v := range c.expected {
|
||||||
found, err := afero.FileContainsBytes(hugofs.Source(), fname, []byte(v))
|
found := strings.Contains(content, v)
|
||||||
if err != nil {
|
|
||||||
t.Errorf("[%d] FileContainsBytes: %s", i, err)
|
|
||||||
}
|
|
||||||
if !found {
|
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() {
|
func initViper() {
|
||||||
viper.Reset()
|
viper.Reset()
|
||||||
viper.Set("metaDataFormat", "toml")
|
viper.Set("metaDataFormat", "toml")
|
||||||
viper.Set("archetypeDir", filepath.Join(os.TempDir(), "archetypes"))
|
viper.Set("archetypeDir", "archetypes")
|
||||||
viper.Set("contentDir", filepath.Join(os.TempDir(), "content"))
|
viper.Set("contentDir", "content")
|
||||||
viper.Set("themesDir", filepath.Join(os.TempDir(), "themes"))
|
viper.Set("themesDir", "themes")
|
||||||
viper.Set("theme", "sample")
|
viper.Set("theme", "sample")
|
||||||
}
|
}
|
||||||
|
|
||||||
func initFs() error {
|
func initFs() error {
|
||||||
hugofs.SetSource(new(afero.MemMapFs))
|
hugofs.InitMemFs()
|
||||||
perm := os.FileMode(0755)
|
perm := os.FileMode(0755)
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
|
@ -88,7 +89,6 @@ func initFs() error {
|
||||||
filepath.Join("themes", "sample", "archetypes"),
|
filepath.Join("themes", "sample", "archetypes"),
|
||||||
}
|
}
|
||||||
for _, dir := range dirs {
|
for _, dir := range dirs {
|
||||||
dir = filepath.Join(os.TempDir(), dir)
|
|
||||||
err = hugofs.Source().Mkdir(dir, perm)
|
err = hugofs.Source().Mkdir(dir, perm)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -101,11 +101,11 @@ func initFs() error {
|
||||||
content string
|
content string
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
path: filepath.Join(os.TempDir(), "archetypes", "post.md"),
|
path: filepath.Join("archetypes", "post.md"),
|
||||||
content: "+++\ndate = \"2015-01-12T19:20:04-07:00\"\ntitle = \"post arch\"\ntest = \"test1\"\n+++\n",
|
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",
|
content: "+++\n+++\n",
|
||||||
},
|
},
|
||||||
} {
|
} {
|
||||||
|
@ -123,3 +123,22 @@ func initFs() error {
|
||||||
|
|
||||||
return nil
|
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)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue