page: Implement frontmatter support

Also drop support for parsing titles from Gemini content.
This commit is contained in:
Adnan Maolood 2021-04-24 13:25:18 -04:00
parent 1996983e96
commit 7853cc5e77
3 changed files with 19 additions and 14 deletions

1
go.mod
View file

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

3
go.sum
View file

@ -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 h1:cokOdA+Jmi5PJGXLlLllQSgYigAEfHXJAERHVMaCc2k=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= 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= 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
View file

@ -1,24 +1,23 @@
package main package main
import ( import (
"log"
pathpkg "path" pathpkg "path"
"regexp"
"strings" "strings"
"time" "time"
"gopkg.in/yaml.v3"
) )
// Page represents a page. // Page represents a page.
type Page struct { type Page struct {
Title string // The title of this page. Title string
Path string // The path to this page. Date time.Time
Date time.Time // The date of the page. Path string `yaml:"-"`
Content string // The content of this page. 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. // NewPage returns a new Page with the given path and content.
func NewPage(path string, content []byte) *Page { func NewPage(path string, content []byte) *Page {
var page Page var page Page
@ -48,11 +47,13 @@ func NewPage(path string, content []byte) *Page {
} }
} }
// Try to parse the title from the contents // Extract frontmatter from content
if submatches := titleRE.FindSubmatch(content); submatches != nil { var frontmatter []byte
page.Title = string(submatches[1]) frontmatter, content = extractFrontmatter(content)
// Remove the title from the contents if len(frontmatter) != 0 {
content = content[len(submatches[0]):] if err := yaml.Unmarshal(frontmatter, &page); err != nil {
log.Println(err)
}
} }
// Remove extension from path // Remove extension from path