1
0
mirror of https://git.sr.ht/~adnano/kiln synced 2024-11-08 10:09:18 +01:00

page: Implement frontmatter support

Also drop support for parsing titles from Gemini content.
This commit is contained in:
adnano 2021-04-24 13:25:18 -04:00
parent 81aac7692d
commit fbf5d3c10d
3 changed files with 19 additions and 14 deletions

1
go.mod

@ -5,4 +5,5 @@ go 1.16
require (
git.sr.ht/~adnano/go-gemini v0.2.0
github.com/BurntSushi/toml v0.3.1
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b // indirect
)

3
go.sum

@ -9,3 +9,6 @@ golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9sn
golang.org/x/text v0.3.3 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b h1:h8qDotaEPuJATrMmW04NCwg7v22aHH28wwpauUhK9Oo=
gopkg.in/yaml.v3 v3.0.0-20210107192922-496545a6307b/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

29
page.go

@ -1,24 +1,23 @@
package main
import (
"log"
pathpkg "path"
"regexp"
"strings"
"time"
"gopkg.in/yaml.v3"
)
// Page represents a page.
type Page struct {
Title string // The title of this page.
Path string // The path to this page.
Date time.Time // The date of the page.
Content string // The content of this page.
Title string
Date time.Time
Path string `yaml:"-"`
Content string `yaml:"-"`
Params map[string]string
}
// titleRe is a regexp to parse titles from Gemini files.
// It also matches the next line if it is empty.
var titleRE = regexp.MustCompile("^# ?([^#\r\n]+)\r?\n?\r?\n?")
// NewPage returns a new Page with the given path and content.
func NewPage(path string, content []byte) *Page {
var page Page
@ -48,11 +47,13 @@ func NewPage(path string, content []byte) *Page {
}
}
// Try to parse the title from the contents
if submatches := titleRE.FindSubmatch(content); submatches != nil {
page.Title = string(submatches[1])
// Remove the title from the contents
content = content[len(submatches[0]):]
// 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