1
0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-11-08 14:19:20 +01:00

Parse dates from page filenames

This commit is contained in:
adnano 2020-09-22 17:04:29 -04:00
parent 36aaa50752
commit 40ae714d6d

19
main.go

@ -2,6 +2,7 @@ package main
import (
"log"
"path/filepath"
"time"
)
@ -46,13 +47,23 @@ type Page struct {
}
func NewPage(path string, content string) *Page {
// TODO: Parse date from path name
permalink := "/" + path
return &Page{
page := &Page{
Path: path,
Permalink: permalink,
Permalink: "/" + path,
Content: content,
}
// Try to parse the date from the page filename
const layout = "2006-01-02"
base := filepath.Base(path)
if len(base) >= len(layout) {
dateStr := base[:len(layout)]
if date, err := time.Parse(layout, dateStr); err == nil {
page.Date = date
}
}
return page
}
// Section represents a section (i.e., a directory).