Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support non-md files as archetype files #3619

Merged
merged 1 commit into from
Jun 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 12 additions & 8 deletions create/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,11 @@ import (
func NewContent(
ps *helpers.PathSpec,
siteFactory func(filename string, siteUsed bool) (*hugolib.Site, error), kind, targetPath string) error {
ext := helpers.Ext(targetPath)

jww.INFO.Println("attempting to create ", targetPath, "of", kind)
jww.INFO.Printf("attempting to create %q of %q of ext %q", targetPath, kind, ext)

archetypeFilename := findArchetype(ps, kind)
archetypeFilename := findArchetype(ps, kind, ext)

f, err := ps.Fs.Source.Open(archetypeFilename)
if err != nil {
Expand Down Expand Up @@ -84,7 +85,7 @@ func NewContent(
// 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.
func findArchetype(ps *helpers.PathSpec, kind string) (outpath string) {
func findArchetype(ps *helpers.PathSpec, kind, ext string) (outpath string) {
search := []string{ps.AbsPathify(ps.Cfg.GetString("archetypeDir"))}

if ps.Cfg.GetString("theme") != "" {
Expand All @@ -100,13 +101,16 @@ func findArchetype(ps *helpers.PathSpec, kind string) (outpath string) {
// If the new content isn't in a subdirectory, kind == "".
// Therefore it should be excluded otherwise `is a directory`
// error will occur. github.com/gohugoio/hugo/issues/411
var pathsToCheck []string
var pathsToCheck = []string{"default"}

if kind == "" {
pathsToCheck = []string{"default.md", "default"}
} else {
pathsToCheck = []string{kind + ".md", kind, "default.md", "default"}
if ext != "" {
if kind != "" {
pathsToCheck = append([]string{kind + ext, "default" + ext}, pathsToCheck...)
} else {
pathsToCheck = append([]string{"default" + ext}, pathsToCheck...)
}
}

for _, p := range pathsToCheck {
curpath := filepath.Join(x, p)
jww.DEBUG.Println("checking", curpath, "for archetypes")
Expand Down
5 changes: 5 additions & 0 deletions create/content_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ func TestNewContent(t *testing.T) {
expected []string
}{
{"post", "post/sample-1.md", []string{`title = "Post Arch title"`, `test = "test1"`, "date = \"2015-01-12T19:20:04-07:00\""}},
{"post", "post/org-1.org", []string{`#+title: ORG-1`}},
{"emptydate", "post/sample-ed.md", []string{`title = "Empty Date Arch title"`, `test = "test1"`}},
{"stump", "stump/sample-2.md", []string{`title = "Sample 2"`}}, // no archetype file
{"", "sample-3.md", []string{`title = "Sample 3"`}}, // no archetype
Expand Down Expand Up @@ -111,6 +112,10 @@ func initFs(fs *hugofs.Fs) error {
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("archetypes", "post.org"),
content: "#+title: {{ .BaseFileName | upper }}",
},
{
path: filepath.Join("archetypes", "product.md"),
content: `+++
Expand Down
6 changes: 6 additions & 0 deletions helpers/path.go
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,12 @@ func GetDottedRelativePath(inPath string) string {
return dottedPath
}

// Ext takes a path and returns the extension, including the delmiter, i.e. ".md".
func Ext(in string) string {
_, ext := fileAndExt(in, fpb)
return ext
}

// Filename takes a path, strips out the extension,
// and returns the name of the file.
func Filename(in string) (name string) {
Expand Down