Rename FrontMatter to Meta
This commit is contained in:
parent
c618d7bc5e
commit
67480bb974
@ -12,7 +12,7 @@ type Document struct {
|
||||
Author string
|
||||
Title string
|
||||
Extends string
|
||||
FrontMatter FrontMatter
|
||||
Meta Meta
|
||||
|
||||
// WORM:
|
||||
environment *Environment
|
||||
@ -45,7 +45,7 @@ func (this *Document) Execute (output io.Writer, data ExecutionData) error {
|
||||
Author: this.Author,
|
||||
Title: this.Title,
|
||||
Extends: this.Extends,
|
||||
FrontMatter: this.FrontMatter,
|
||||
Meta: this.Meta,
|
||||
Body: template.HTML(outputBuilder.String()),
|
||||
},
|
||||
})
|
||||
@ -110,6 +110,6 @@ type ExecutionResult struct {
|
||||
Title string
|
||||
Extends string
|
||||
|
||||
FrontMatter FrontMatter
|
||||
Meta Meta
|
||||
Body template.HTML
|
||||
}
|
||||
|
@ -101,12 +101,12 @@ func (this *Environment) parse (name string, modTime time.Time, input io.Reader)
|
||||
// read entire file and split into front matter and body
|
||||
buffer, err := io.ReadAll(input)
|
||||
if err != nil { return nil, err }
|
||||
frontMatter, body, err := SplitFrontMatter(string(buffer))
|
||||
frontMatter, body, err := SplitMeta(string(buffer))
|
||||
if err != nil { return nil, err }
|
||||
|
||||
// assemble the document struct
|
||||
document := &Document {
|
||||
FrontMatter: frontMatter,
|
||||
Meta: frontMatter,
|
||||
environment: this,
|
||||
name: name,
|
||||
parseTime: time.Now(),
|
||||
|
@ -4,20 +4,20 @@ import "strings"
|
||||
|
||||
const frontMatterRule = "---"
|
||||
|
||||
// FrontMatter represents optional metadata that can occur at the very start of
|
||||
// Meta represents optional metadata that can occur at the very start of
|
||||
// a document.
|
||||
type FrontMatter = map[string] string
|
||||
type Meta = map[string] string
|
||||
|
||||
// SplitFrontMatter parses the front matter (if it exists), returning a map
|
||||
// representing it along with the rest of the input as a string. If there is no
|
||||
// front matter, an empty map will be returned.
|
||||
func SplitFrontMatter (input string) (FrontMatter, string, error) {
|
||||
// SplitMeta parses the metadata (if it exists), returning a map representing it
|
||||
// along with the rest of the input as a string. If there is no metadata, an
|
||||
// empty map will be returned.
|
||||
func SplitMeta (input string) (Meta, string, error) {
|
||||
// i hate crlf!!!!! uwehhh!!!
|
||||
input = strings.ReplaceAll(input, "\r\n", "\n")
|
||||
|
||||
// stop if there is no front matter
|
||||
if !strings.HasPrefix(input, frontMatterRule + "\n") {
|
||||
return FrontMatter { }, input, nil
|
||||
return Meta { }, input, nil
|
||||
}
|
||||
|
||||
// get the start and the end of the front matter
|
||||
@ -28,7 +28,7 @@ func SplitFrontMatter (input string) (FrontMatter, string, error) {
|
||||
}
|
||||
frontMatterRaw := input[:index]
|
||||
body := input[index + len(frontMatterRule) + 2:]
|
||||
frontMatter := make(FrontMatter)
|
||||
frontMatter := make(Meta)
|
||||
|
||||
// iterate over the lines
|
||||
for _, line := range strings.Split(frontMatterRaw, "\n") {
|
||||
|
@ -5,7 +5,7 @@ import "testing"
|
||||
|
||||
const quickBrownFox = "The quick brown fox jumped over the lazy dog."
|
||||
|
||||
func TestSplitFrontMatterLF (test *testing.T) {
|
||||
func TestSplitMetaLF (test *testing.T) {
|
||||
correctBody :=
|
||||
`this is some sample text
|
||||
---
|
||||
@ -13,7 +13,7 @@ theres another hr that shouldnt
|
||||
break anything
|
||||
break: anything
|
||||
`
|
||||
frontMatter, body, err := SplitFrontMatter(
|
||||
frontMatter, body, err := SplitMeta(
|
||||
`---
|
||||
FOO: baR
|
||||
fOoo: Bar
|
||||
@ -33,7 +33,7 @@ Sentence: ` + quickBrownFox + `
|
||||
test.Fatal("body is not correct")
|
||||
}
|
||||
|
||||
test.Log("FRONT MATTER:")
|
||||
test.Log("META:")
|
||||
test.Log(frontMatter)
|
||||
value0, ok := frontMatter["foo"]
|
||||
if !ok { test.Fatal("missing key") }
|
||||
@ -52,9 +52,9 @@ Sentence: ` + quickBrownFox + `
|
||||
if value4 != quickBrownFox { test.Fatal("value is not correct") }
|
||||
}
|
||||
|
||||
func TestSplitFrontMatterCRLF (test *testing.T) {
|
||||
func TestSplitMetaCRLF (test *testing.T) {
|
||||
correctBody := "this is some sample text\r\n---\r\ntheres another hr that shouldnt\r\nbreak anything\r\nbreak: anything\r\n"
|
||||
frontMatter, body, err := SplitFrontMatter(
|
||||
frontMatter, body, err := SplitMeta(
|
||||
"---\r\nFOO: baR\r\n fOoo: Bar\r\nShouldn't break anything: ---\r\n\r\nthis : that\r\nSentence: " + quickBrownFox + "\r\n---\r\n" + correctBody)
|
||||
if err != nil {
|
||||
test.Fatal(err)
|
||||
@ -67,7 +67,7 @@ func TestSplitFrontMatterCRLF (test *testing.T) {
|
||||
test.Fatal("body is not correct")
|
||||
}
|
||||
|
||||
test.Log("FRONT MATTER:")
|
||||
test.Log("META:")
|
||||
test.Log(frontMatter)
|
||||
value0, ok := frontMatter["foo"]
|
||||
if !ok { test.Fatal("missing key") }
|
||||
|
@ -112,10 +112,10 @@ func (this *Handler) serveDocument (
|
||||
recorder.Reset()
|
||||
recorder.Head = res.Header().Clone()
|
||||
}
|
||||
if contentType, ok := document.FrontMatter["content-type"]; ok {
|
||||
if contentType, ok := document.Meta["content-type"]; ok {
|
||||
recorder.Header().Set("Content-Type", contentType)
|
||||
}
|
||||
if status, ok := document.FrontMatter["status"]; ok {
|
||||
if status, ok := document.Meta["status"]; ok {
|
||||
if status, err := strconv.Atoi(status); err == nil {
|
||||
recorder.Status = status
|
||||
}
|
||||
|
@ -55,7 +55,7 @@ func (this *state) funcExecute (name string, data any) (step.ExecutionResult, er
|
||||
Author: document.Author,
|
||||
Title: document.Title,
|
||||
Extends: document.Extends,
|
||||
FrontMatter: document.FrontMatter,
|
||||
Meta: document.Meta,
|
||||
Body: template.HTML(builder.String()),
|
||||
}, nil
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user