Allow date and slug from filename for leaf bundles
authorJanus <janus@insignificancegalore.net>
Tue, 16 Oct 2018 21:51:48 +0000 (23:51 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Tue, 16 Oct 2018 21:51:48 +0000 (23:51 +0200)
Fixes #4558

hugolib/page.go
hugolib/pagemeta/page_frontmatter.go
source/fileInfo.go
source/fileInfo_test.go

index 8a2864e7f6777c333f10d3ad2c9b25c3bb26d446..0359769e3dfb237afb0abc5d729147317ecce15c 100644 (file)
@@ -1326,7 +1326,7 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
                Params:        p.params,
                Dates:         &p.PageDates,
                PageURLs:      &p.URLPath,
-               BaseFilename:  p.BaseFileName(),
+               BaseFilename:  p.ContentBaseName(),
                ModTime:       mtime,
                GitAuthorDate: gitAuthorDate,
        }
index 88f6f3a11e48b3ae47dc890659ef7f4487f1d73c..f75613e8022899d2b4e66122709eff1fa13e1d3f 100644 (file)
@@ -47,7 +47,8 @@ type FrontMatterDescriptor struct {
        // This the Page's front matter.
        Frontmatter map[string]interface{}
 
-       // This is the Page's base filename, e.g. page.md.
+       // This is the Page's base filename (BaseFilename), e.g. page.md., or
+       // if page is a leaf bundle, the bundle folder name (ContentBaseName).
        BaseFilename string
 
        // The content file's mod time.
index fc55706fe0fe8086991203169df5622e1c00b7d3..ad302f4703cb2b64299270e30effa8e3255193c8 100644 (file)
@@ -70,6 +70,10 @@ type File interface {
        // not even the optional language extension part.
        TranslationBaseName() string
 
+       // ContentBaseName is a either TranslationBaseName or name of containing folder
+       // if file is a leaf bundle.
+       ContentBaseName() string
+
        // UniqueID is the MD5 hash of the file's path and is for most practical applications,
        // Hugo content files being one of them, considered to be unique.
        UniqueID() string
@@ -106,6 +110,7 @@ type FileInfo struct {
        relPath             string
        baseName            string
        translationBaseName string
+       contentBaseName     string
        section             string
        isLeafBundle        bool
 
@@ -144,6 +149,13 @@ func (fi *FileInfo) BaseFileName() string { return fi.baseName }
 // language segement (ie. "page").
 func (fi *FileInfo) TranslationBaseName() string { return fi.translationBaseName }
 
+// ContentBaseName is a either TranslationBaseName or name of containing folder
+// if file is a leaf bundle.
+func (fi *FileInfo) ContentBaseName() string {
+       fi.init()
+       return fi.contentBaseName
+}
+
 // Section returns a file's section.
 func (fi *FileInfo) Section() string {
        fi.init()
@@ -177,11 +189,15 @@ func (fi *FileInfo) init() {
                if (!fi.isLeafBundle && len(parts) == 1) || len(parts) > 1 {
                        section = parts[0]
                }
-
                fi.section = section
 
-               fi.uniqueID = helpers.MD5String(filepath.ToSlash(fi.relPath))
+               if fi.isLeafBundle && len(parts) > 0 {
+                       fi.contentBaseName = parts[len(parts)-1]
+               } else {
+                       fi.contentBaseName = fi.translationBaseName
+               }
 
+               fi.uniqueID = helpers.MD5String(filepath.ToSlash(fi.relPath))
        })
 }
 
index 9d3566240ed2371a79be9afb6194f6f516de81e1..9390c624706c91698f8ee42c2873fed101df2900 100644 (file)
@@ -94,4 +94,17 @@ func TestFileInfoLanguage(t *testing.T) {
 
        assert.Equal("sv", fiSv.Lang())
        assert.Equal("en", fiEn.Lang())
+
+       // test contentBaseName implementation
+       fi := s.NewFileInfo("", "2018-10-01-contentbasename.md", false, nil)
+       assert.Equal("2018-10-01-contentbasename", fi.ContentBaseName())
+
+       fi = s.NewFileInfo("", "2018-10-01-contentbasename.en.md", false, nil)
+       assert.Equal("2018-10-01-contentbasename", fi.ContentBaseName())
+
+       fi = s.NewFileInfo("", filepath.Join("2018-10-01-contentbasename", "index.en.md"), true, nil)
+       assert.Equal("2018-10-01-contentbasename", fi.ContentBaseName())
+
+       fi = s.NewFileInfo("", filepath.Join("2018-10-01-contentbasename", "_index.en.md"), false, nil)
+       assert.Equal("_index", fi.ContentBaseName())
 }