From 3037d200cb03dd58eaa95e5ded5cf8e319c45f66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 26 Jul 2015 15:28:56 +0200 Subject: [PATCH] Fix inconsistent front matter handling Fixes #768 --- hugolib/page.go | 4 +- hugolib/page_test.go | 127 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 130 insertions(+), 1 deletion(-) diff --git a/hugolib/page.go b/hugolib/page.go index 2bd19e13e..9b18f1430 100644 --- a/hugolib/page.go +++ b/hugolib/page.go @@ -543,7 +543,9 @@ func (p *Page) update(f interface{}) error { case []interface{}: if len(vvv) > 0 { switch vvv[0].(type) { - case map[interface{}]interface{}: // Proper parsing structured array from yaml based FrontMatter + case map[interface{}]interface{}: // Proper parsing structured array from YAML based FrontMatter + p.Params[loki] = vvv + case map[string]interface{}: // Proper parsing structured array from JSON based FrontMatter p.Params[loki] = vvv default: a := make([]string, len(vvv)) diff --git a/hugolib/page_test.go b/hugolib/page_test.go index 90baecda5..9b613d05f 100644 --- a/hugolib/page_test.go +++ b/hugolib/page_test.go @@ -1,9 +1,11 @@ package hugolib import ( + "fmt" "html/template" "path/filepath" "reflect" + "sort" "strings" "testing" "time" @@ -290,6 +292,104 @@ a_key = "a_value" +++ Front Matter with various frontmatter types` +var PAGE_WITH_CALENDAR_YAML_FRONTMATTER = `--- +type: calendar +weeks: + - + start: "Jan 5" + days: + - activity: class + room: EN1000 + - activity: lab + - activity: class + - activity: lab + - activity: class + - + start: "Jan 12" + days: + - activity: class + - activity: lab + - activity: class + - activity: lab + - activity: exam +--- + +Hi. +` + +var PAGE_WITH_CALENDAR_JSON_FRONTMATTER = `{ + "type": "calendar", + "weeks": [ + { + "start": "Jan 5", + "days": [ + { "activity": "class", "room": "EN1000" }, + { "activity": "lab" }, + { "activity": "class" }, + { "activity": "lab" }, + { "activity": "class" } + ] + }, + { + "start": "Jan 12", + "days": [ + { "activity": "class" }, + { "activity": "lab" }, + { "activity": "class" }, + { "activity": "lab" }, + { "activity": "exam" } + ] + } + ] +} + +Hi. +` + +var PAGE_WITH_CALENDAR_TOML_FRONTMATTER = `+++ +type = "calendar" + +[[weeks]] +start = "Jan 5" + +[[weeks.days]] +activity = "class" +room = "EN1000" + +[[weeks.days]] +activity = "lab" + +[[weeks.days]] +activity = "class" + +[[weeks.days]] +activity = "lab" + +[[weeks.days]] +activity = "class" + +[[weeks]] +start = "Jan 12" + +[[weeks.days]] +activity = "class" + +[[weeks.days]] +activity = "lab" + +[[weeks.days]] +activity = "class" + +[[weeks.days]] +activity = "lab" + +[[weeks.days]] +activity = "exam" ++++ + +Hi. +` + func checkError(t *testing.T, err error, expected string) { if err == nil { t.Fatalf("err is nil. Expected: %s", expected) @@ -574,6 +674,22 @@ func TestShouldRenderContent(t *testing.T) { } } +// Issue #768 +func TestCalendarParamsVariants(t *testing.T) { + pageJSON, _ := NewPage("test/fileJSON.md") + _, _ = pageJSON.ReadFrom(strings.NewReader(PAGE_WITH_CALENDAR_JSON_FRONTMATTER)) + + pageYAML, _ := NewPage("test/fileYAML.md") + _, _ = pageYAML.ReadFrom(strings.NewReader(PAGE_WITH_CALENDAR_YAML_FRONTMATTER)) + + pageTOML, _ := NewPage("test/fileTOML.md") + _, _ = pageTOML.ReadFrom(strings.NewReader(PAGE_WITH_CALENDAR_TOML_FRONTMATTER)) + + assert.True(t, compareObjects(pageJSON.Params, pageYAML.Params)) + assert.True(t, compareObjects(pageJSON.Params, pageTOML.Params)) + +} + func TestDifferentFrontMatterVarTypes(t *testing.T) { page, _ := NewPage("test/file1.md") _, _ = page.ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES)) @@ -751,3 +867,14 @@ func listEqual(left, right []string) bool { return true } + +// TODO(bep) this may be useful for other tests. +func compareObjects(a interface{}, b interface{}) bool { + aStr := strings.Split(fmt.Sprintf("%v", a), "") + sort.Strings(aStr) + + bStr := strings.Split(fmt.Sprintf("%v", b), "") + sort.Strings(bStr) + + return strings.Join(aStr, "") == strings.Join(bStr, "") +}