Skip to content

Commit

Permalink
Add more date options to rendering.
Browse files Browse the repository at this point in the history
  • Loading branch information
roblillack committed Jan 6, 2022
1 parent c6dc263 commit 6f6185f
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
4 changes: 3 additions & 1 deletion core/tacker.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"path/filepath"
"sort"
"strings"
"time"

"github.com/cbroglie/mustache"
"gopkg.in/yaml.v2"
Expand All @@ -34,6 +35,7 @@ type Tacker struct {
TagNames map[string]map[string]int
TagIndex *Page
Logger *log.Logger
BuildTime time.Time
}

func NewTacker(dir string) (*Tacker, error) {
Expand Down Expand Up @@ -151,6 +153,7 @@ func (t *Tacker) Log(format string, args ...interface{}) {
}

func (t *Tacker) Tack() error {
t.BuildTime = time.Now()
t.Log("Tacking up %s (%d pages)", t.BaseDir, len(t.Pages))

if _, err := os.Stat(filepath.Join(t.BaseDir, TargetDir)); err != nil && !errors.Is(err, os.ErrNotExist) {
Expand All @@ -162,7 +165,6 @@ func (t *Tacker) Tack() error {
}

for _, page := range t.Pages {
t.Log("%s => %s (template: %s)", page.Permalink(), page.Slug, page.Template)
if err := page.Generate(); err != nil {
return err
}
Expand Down
8 changes: 8 additions & 0 deletions core/template.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@ import (
"io"
"sort"
"strings"
"time"

"github.com/cbroglie/mustache"
)

const RFC822 string = "02 Jan 2006 15:04:05 -0700"

type Template struct {
*mustache.Template
}
Expand All @@ -32,6 +35,8 @@ func PageValues(p *Page, ctx *Page) map[string]interface{} {
data["root"] = p.Root()
if p.Post() {
data["date"] = p.Date.Format("2006-01-02")
data["date_rfc822"] = p.Date.Format(RFC822)
data["date_rfc3339"] = p.Date.Format(time.RFC3339)
data["year"] = p.Date.Format("2006")
data["month"] = p.Date.Format("January")
}
Expand Down Expand Up @@ -137,6 +142,9 @@ func (t *Template) Render(page *Page, w io.Writer) error {
ctx["posts"] = PageListValues(limitPageList(posts, page, "posts_limit"), page)
}

ctx["TACK_BUILD_DATE"] = page.Tacker.BuildTime.Local().Format(time.RFC3339)
ctx["TACK_BUILD_DATE_RFC822"] = page.Tacker.BuildTime.Local().Format(RFC822)

return t.Template.FRender(w, ctx)
}

Expand Down

0 comments on commit 6f6185f

Please sign in to comment.