Fix inconsistent front matter handling

Fixes #768
This commit is contained in:
Bjørn Erik Pedersen 2015-07-26 15:28:56 +02:00
parent 81e69c416d
commit 3037d200cb
2 changed files with 130 additions and 1 deletions

View File

@ -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))

View File

@ -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, "")
}