From 67480bb974e629ac0e9696ddb1fbce6f2a1a6631 Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Mon, 9 Dec 2024 02:22:37 -0500 Subject: [PATCH] Rename FrontMatter to Meta --- document.go | 20 ++++++++++---------- environment.go | 4 ++-- frontmatter.go | 16 ++++++++-------- frontmatter_test.go | 12 ++++++------ http/handler.go | 4 ++-- providers/import/import.go | 10 +++++----- 6 files changed, 33 insertions(+), 33 deletions(-) diff --git a/document.go b/document.go index 40e8953..ae85354 100644 --- a/document.go +++ b/document.go @@ -9,10 +9,10 @@ import "html/template" // Document represents a STEP file. type Document struct { - Author string - Title string - Extends string - FrontMatter FrontMatter + Author string + Title string + Extends string + Meta Meta // WORM: environment *Environment @@ -42,11 +42,11 @@ func (this *Document) Execute (output io.Writer, data ExecutionData) error { return parent.Execute(output, ExecutionData { Data: data.Data, Child: &ExecutionResult { - Author: this.Author, - Title: this.Title, - Extends: this.Extends, - FrontMatter: this.FrontMatter, - Body: template.HTML(outputBuilder.String()), + Author: this.Author, + Title: this.Title, + Extends: this.Extends, + 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 } diff --git a/environment.go b/environment.go index cd7b0e6..2f53451 100644 --- a/environment.go +++ b/environment.go @@ -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(), diff --git a/frontmatter.go b/frontmatter.go index 8553de9..503a7c0 100644 --- a/frontmatter.go +++ b/frontmatter.go @@ -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") { diff --git a/frontmatter_test.go b/frontmatter_test.go index e967256..27a8de6 100644 --- a/frontmatter_test.go +++ b/frontmatter_test.go @@ -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") } diff --git a/http/handler.go b/http/handler.go index 68e320f..7547faf 100644 --- a/http/handler.go +++ b/http/handler.go @@ -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 } diff --git a/providers/import/import.go b/providers/import/import.go index accac10..3f0cecd 100644 --- a/providers/import/import.go +++ b/providers/import/import.go @@ -52,11 +52,11 @@ func (this *state) funcExecute (name string, data any) (step.ExecutionResult, er err = document.Execute(&builder, step.ExecutionData { Data: data }) if err != nil { return step.ExecutionResult { }, err } return step.ExecutionResult { - Author: document.Author, - Title: document.Title, - Extends: document.Extends, - FrontMatter: document.FrontMatter, - Body: template.HTML(builder.String()), + Author: document.Author, + Title: document.Title, + Extends: document.Extends, + Meta: document.Meta, + Body: template.HTML(builder.String()), }, nil }