From fbf5d3c10d8b21bb9416e04790edace840e4a8cb Mon Sep 17 00:00:00 2001 From: adnano Date: Sat, 24 Apr 2021 13:25:18 -0400 Subject: [PATCH] page: Implement frontmatter support Also drop support for parsing titles from Gemini content. --- go.mod | 1 + go.sum | 3 +++ page.go | 29 +++++++++++++++-------------- 3 files changed, 19 insertions(+), 14 deletions(-) diff --git a/go.mod b/go.mod index fea5717..c33777f 100644 --- a/go.mod +++ b/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 ) diff --git a/go.sum b/go.sum index 0e7dd34..93f1d87 100644 --- a/go.sum +++ b/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= diff --git a/page.go b/page.go index a2f54c3..8f2299b 100644 --- a/page.go +++ b/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