1
0
mirror of https://github.com/danog/gojekyll.git synced 2025-01-22 20:21:15 +01:00

Set post.title

This commit is contained in:
Oliver Steele 2017-08-16 16:08:49 -04:00
parent 2ee44cf787
commit bd76718dfc
4 changed files with 23 additions and 10 deletions

View File

@ -31,18 +31,19 @@ func (s defaultStrategy) future(filename string) bool { return fa
type postsStrategy struct{}
func (s postsStrategy) addDate(filename string, fm map[string]interface{}) {
if t, found := utils.FilenameDate(filename); found {
if t, title, found := utils.FilenameDate(filename); found {
fm["date"] = t
fm["title"] = title
}
}
func (s postsStrategy) collectible(filename string) bool {
_, ok := utils.FilenameDate(filename)
_, _, ok := utils.FilenameDate(filename)
return ok
}
func (s postsStrategy) future(filename string) bool {
t, ok := utils.FilenameDate(filename)
t, _, ok := utils.FilenameDate(filename)
return ok && t.After(time.Now())
}

View File

@ -4,4 +4,4 @@
{{ page.title }}
================
I was born on this day.
I was *born* on _this_ day.

View File

@ -9,10 +9,21 @@ import (
// FilenameDate returns the date for a filename that uses Jekyll post convention.
// It also returns a bool indicating whether a date was found.
func FilenameDate(s string) (time.Time, bool) {
layout := "2006-01-02-"
t, err := time.Parse(layout, filepath.Base(s + layout)[:len(layout)])
return t, err == nil
func FilenameDate(s string) (t time.Time, title string, found bool) {
var (
base = TrimExt(filepath.Base(s))
layout = "2006-01-02-"
)
if len(base) < len(layout) {
found = false
return
}
t, err := time.Parse(layout, base[:len(layout)])
if err != nil {
return
}
title, found = base[len(layout):], true
return
}
// MatchList implement Jekyll include: and exclude: configurations.

View File

@ -21,11 +21,12 @@ func TestMustAbs(t *testing.T) {
}
func TestFilenameDate(t *testing.T) {
d, found := FilenameDate("2017-07-02-post.html")
d, title, found := FilenameDate("2017-07-02-post.html")
require.True(t, found)
require.Equal(t, "post", title)
require.Equal(t, timeMustParse("2017-07-02T00:00:00Z"), d)
_, found = FilenameDate("not-post.html")
_, _, found = FilenameDate("not-post.html")
require.False(t, found)
}