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