mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-11-23 18:51:10 +00:00
Parse dates from page filenames
This commit is contained in:
parent
36aaa50752
commit
40ae714d6d
19
main.go
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).
|
||||
|
|
Loading…
Reference in a new issue