mirror of
https://git.sr.ht/~adnano/kiln
synced 2024-11-08 14:19:20 +01:00
fbf5d3c10d
Also drop support for parsing titles from Gemini content.
64 lines
1.3 KiB
Go
64 lines
1.3 KiB
Go
package main
|
|
|
|
import (
|
|
"log"
|
|
pathpkg "path"
|
|
"strings"
|
|
"time"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
// Page represents a page.
|
|
type Page struct {
|
|
Title string
|
|
Date time.Time
|
|
Path string `yaml:"-"`
|
|
Content string `yaml:"-"`
|
|
Params map[string]string
|
|
}
|
|
|
|
// NewPage returns a new Page with the given path and content.
|
|
func NewPage(path string, content []byte) *Page {
|
|
var page Page
|
|
|
|
// Try to parse the date from the page filename
|
|
const layout = "2006-01-02"
|
|
base := pathpkg.Base(path)
|
|
if len(base) >= len(layout) {
|
|
dateStr := base[:len(layout)]
|
|
if time, err := time.Parse(layout, dateStr); err == nil {
|
|
page.Date = time
|
|
// Remove the date from the path
|
|
base = base[len(layout):]
|
|
if len(base) > 0 {
|
|
// Remove a leading dash
|
|
if base[0] == '-' {
|
|
base = base[1:]
|
|
}
|
|
if len(base) > 0 {
|
|
dir := pathpkg.Dir(path)
|
|
if dir == "." {
|
|
dir = ""
|
|
}
|
|
path = pathpkg.Join(dir, base)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
// Extract frontmatter from content
|
|
var frontmatter []byte
|
|
frontmatter, content = extractFrontmatter(content)
|
|
if len(frontmatter) != 0 {
|
|
if err := yaml.Unmarshal(frontmatter, &page); err != nil {
|
|
log.Println(err)
|
|
}
|
|
}
|
|
|
|
// Remove extension from path
|
|
page.Path = "/" + strings.TrimSuffix(path, pathpkg.Ext(path)) + "/"
|
|
page.Content = string(content)
|
|
return &page
|
|
}
|