mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-11-24 03:01:11 +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 (
|
import (
|
||||||
"log"
|
"log"
|
||||||
|
"path/filepath"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -46,13 +47,23 @@ type Page struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewPage(path string, content string) *Page {
|
func NewPage(path string, content string) *Page {
|
||||||
// TODO: Parse date from path name
|
page := &Page{
|
||||||
permalink := "/" + path
|
|
||||||
return &Page{
|
|
||||||
Path: path,
|
Path: path,
|
||||||
Permalink: permalink,
|
Permalink: "/" + path,
|
||||||
Content: content,
|
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).
|
// Section represents a section (i.e., a directory).
|
||||||
|
|
Loading…
Reference in a new issue