From 91bb774ae4e129f7ed0624754b31479c960ef774 Mon Sep 17 00:00:00 2001 From: Vas Sudanagunta Date: Thu, 25 Jan 2018 22:54:15 -0500 Subject: Support pages without front matter * Page without front matter now treated same as a page with empty front matter. * Test cases added to cover this and repro issue #4320. * Type safety of front matter code improved. Fixes #4320 --- parser/frontmatter.go | 12 ++++++------ parser/page.go | 7 ++----- 2 files changed, 8 insertions(+), 11 deletions(-) (limited to 'parser') diff --git a/parser/frontmatter.go b/parser/frontmatter.go index ab56b14d1..7560a734a 100644 --- a/parser/frontmatter.go +++ b/parser/frontmatter.go @@ -31,7 +31,7 @@ import ( // FrontmatterType represents a type of frontmatter. type FrontmatterType struct { // Parse decodes content into a Go interface. - Parse func([]byte) (interface{}, error) + Parse func([]byte) (map[string]interface{}, error) markstart, markend []byte // starting and ending delimiters includeMark bool // include start and end mark in output @@ -168,7 +168,7 @@ func DetectFrontMatter(mark rune) (f *FrontmatterType) { // HandleTOMLMetaData unmarshals TOML-encoded datum and returns a Go interface // representing the encoded data structure. -func HandleTOMLMetaData(datum []byte) (interface{}, error) { +func HandleTOMLMetaData(datum []byte) (map[string]interface{}, error) { m := map[string]interface{}{} datum = removeTOMLIdentifier(datum) @@ -198,7 +198,7 @@ func removeTOMLIdentifier(datum []byte) []byte { // HandleYAMLMetaData unmarshals YAML-encoded datum and returns a Go interface // representing the encoded data structure. -func HandleYAMLMetaData(datum []byte) (interface{}, error) { +func HandleYAMLMetaData(datum []byte) (map[string]interface{}, error) { m := map[string]interface{}{} err := yaml.Unmarshal(datum, &m) return m, err @@ -206,7 +206,7 @@ func HandleYAMLMetaData(datum []byte) (interface{}, error) { // HandleJSONMetaData unmarshals JSON-encoded datum and returns a Go interface // representing the encoded data structure. -func HandleJSONMetaData(datum []byte) (interface{}, error) { +func HandleJSONMetaData(datum []byte) (map[string]interface{}, error) { if datum == nil { // Package json returns on error on nil input. // Return an empty map to be consistent with our other supported @@ -214,13 +214,13 @@ func HandleJSONMetaData(datum []byte) (interface{}, error) { return make(map[string]interface{}), nil } - var f interface{} + var f map[string]interface{} err := json.Unmarshal(datum, &f) return f, err } // HandleOrgMetaData unmarshals org-mode encoded datum and returns a Go // interface representing the encoded data structure. -func HandleOrgMetaData(datum []byte) (interface{}, error) { +func HandleOrgMetaData(datum []byte) (map[string]interface{}, error) { return goorgeous.OrgHeaders(datum) } diff --git a/parser/page.go b/parser/page.go index 1537915f4..17378840d 100644 --- a/parser/page.go +++ b/parser/page.go @@ -74,7 +74,7 @@ type Page interface { IsRenderable() bool // Metadata returns the unmarshalled frontmatter data. - Metadata() (interface{}, error) + Metadata() (map[string]interface{}, error) } // page implements the Page interface. @@ -100,16 +100,13 @@ func (p *page) IsRenderable() bool { } // Metadata returns the unmarshalled frontmatter data. -func (p *page) Metadata() (meta interface{}, err error) { +func (p *page) Metadata() (meta map[string]interface{}, err error) { frontmatter := p.FrontMatter() if len(frontmatter) != 0 { fm := DetectFrontMatter(rune(frontmatter[0])) if fm != nil { meta, err = fm.Parse(frontmatter) - if err != nil { - return - } } } return -- cgit v1.2.3