mirror of
https://github.com/gohugoio/hugo.git
synced 2024-11-07 20:30:36 -05:00
Adding support for more front matter types (int, float & date)
This commit is contained in:
parent
3558e3d6f0
commit
f3c816eabd
3 changed files with 84 additions and 4 deletions
|
@ -70,10 +70,40 @@ func interfaceArrayToStringArray(i interface{}) []string {
|
||||||
return a
|
return a
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func interfaceToFloat64(i interface{}) float64 {
|
||||||
|
switch s := i.(type) {
|
||||||
|
case float64:
|
||||||
|
return s
|
||||||
|
case float32:
|
||||||
|
return float64(s)
|
||||||
|
|
||||||
|
case string:
|
||||||
|
v, err := strconv.ParseFloat(s, 64)
|
||||||
|
if err == nil {
|
||||||
|
return float64(v)
|
||||||
|
} else {
|
||||||
|
errorf("Only Floats are supported for this key\nErr:", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
default:
|
||||||
|
errorf("Only Floats are supported for this key")
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0.0
|
||||||
|
}
|
||||||
|
|
||||||
func interfaceToInt(i interface{}) int {
|
func interfaceToInt(i interface{}) int {
|
||||||
switch s := i.(type) {
|
switch s := i.(type) {
|
||||||
case int:
|
case int:
|
||||||
return s
|
return s
|
||||||
|
case int64:
|
||||||
|
return int(s)
|
||||||
|
case int32:
|
||||||
|
return int(s)
|
||||||
|
case int16:
|
||||||
|
return int(s)
|
||||||
|
case int8:
|
||||||
|
return int(s)
|
||||||
case string:
|
case string:
|
||||||
v, err := strconv.ParseInt(s, 0, 0)
|
v, err := strconv.ParseInt(s, 0, 0)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
|
@ -88,12 +118,23 @@ func interfaceToInt(i interface{}) int {
|
||||||
return 0
|
return 0
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func interfaceToTime(i interface{}) time.Time {
|
||||||
|
switch s := i.(type) {
|
||||||
|
case time.Time:
|
||||||
|
return s
|
||||||
|
default:
|
||||||
|
errorf("Only Time is supported for this key")
|
||||||
|
}
|
||||||
|
|
||||||
|
return *new(time.Time)
|
||||||
|
}
|
||||||
|
|
||||||
func interfaceToString(i interface{}) string {
|
func interfaceToString(i interface{}) string {
|
||||||
switch s := i.(type) {
|
switch s := i.(type) {
|
||||||
case string:
|
case string:
|
||||||
return s
|
return s
|
||||||
default:
|
default:
|
||||||
errorf("Only Strings are supported for this YAML key")
|
errorf("Only Strings are supported for this key")
|
||||||
}
|
}
|
||||||
|
|
||||||
return ""
|
return ""
|
||||||
|
|
|
@ -330,7 +330,8 @@ func (page *Page) update(f interface{}) error {
|
||||||
m := f.(map[string]interface{})
|
m := f.(map[string]interface{})
|
||||||
|
|
||||||
for k, v := range m {
|
for k, v := range m {
|
||||||
switch strings.ToLower(k) {
|
loki := strings.ToLower(k)
|
||||||
|
switch loki {
|
||||||
case "title":
|
case "title":
|
||||||
page.Title = interfaceToString(v)
|
page.Title = interfaceToString(v)
|
||||||
case "description":
|
case "description":
|
||||||
|
@ -368,8 +369,14 @@ func (page *Page) update(f interface{}) error {
|
||||||
default:
|
default:
|
||||||
// If not one of the explicit values, store in Params
|
// If not one of the explicit values, store in Params
|
||||||
switch vv := v.(type) {
|
switch vv := v.(type) {
|
||||||
case string: // handle string values
|
case string:
|
||||||
page.Params[strings.ToLower(k)] = vv
|
page.Params[loki] = vv
|
||||||
|
case int64, int32, int16, int8, int:
|
||||||
|
page.Params[loki] = vv
|
||||||
|
case float64, float32:
|
||||||
|
page.Params[loki] = vv
|
||||||
|
case time.Time:
|
||||||
|
page.Params[loki] = vv
|
||||||
default: // handle array of strings as well
|
default: // handle array of strings as well
|
||||||
switch vvv := vv.(type) {
|
switch vvv := vv.(type) {
|
||||||
case []interface{}:
|
case []interface{}:
|
||||||
|
@ -396,6 +403,12 @@ func (page *Page) GetParam(key string) interface{} {
|
||||||
switch v.(type) {
|
switch v.(type) {
|
||||||
case string:
|
case string:
|
||||||
return interfaceToString(v)
|
return interfaceToString(v)
|
||||||
|
case int64, int32, int16, int8, int:
|
||||||
|
return interfaceToInt(v)
|
||||||
|
case float64, float32:
|
||||||
|
return interfaceToFloat64(v)
|
||||||
|
case time.Time:
|
||||||
|
return interfaceToTime(v)
|
||||||
case []string:
|
case []string:
|
||||||
return v
|
return v
|
||||||
}
|
}
|
||||||
|
|
|
@ -141,6 +141,14 @@ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
|
||||||
`
|
`
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var PAGE_WITH_VARIOUS_FRONTMATTER_TYPES = `+++
|
||||||
|
a_string = "bar"
|
||||||
|
an_integer = 1
|
||||||
|
a_float = 1.3
|
||||||
|
a_date = 1979-05-27T07:32:00Z
|
||||||
|
+++
|
||||||
|
Front Matter with various frontmatter types`
|
||||||
|
|
||||||
func checkError(t *testing.T, err error, expected string) {
|
func checkError(t *testing.T, err error, expected string) {
|
||||||
if err == nil {
|
if err == nil {
|
||||||
t.Fatalf("err is nil. Expected: %s", expected)
|
t.Fatalf("err is nil. Expected: %s", expected)
|
||||||
|
@ -332,6 +340,24 @@ func TestShouldRenderContent(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestDifferentFrontMatterVarTypes(t *testing.T) {
|
||||||
|
page, _ := ReadFrom(strings.NewReader(PAGE_WITH_VARIOUS_FRONTMATTER_TYPES), "test/file1.md")
|
||||||
|
|
||||||
|
dateval, _ := time.Parse(time.RFC3339, "1979-05-27T07:32:00Z")
|
||||||
|
if page.GetParam("a_string") != "bar" {
|
||||||
|
t.Errorf("frontmatter not handling strings correctly should be %s, got: %s", "bar", page.GetParam("a_string"))
|
||||||
|
}
|
||||||
|
if page.GetParam("an_integer") != 1 {
|
||||||
|
t.Errorf("frontmatter not handling ints correctly should be %s, got: %s", "1", page.GetParam("an_integer"))
|
||||||
|
}
|
||||||
|
if page.GetParam("a_float") != 1.3 {
|
||||||
|
t.Errorf("frontmatter not handling floats correctly should be %s, got: %s", 1.3, page.GetParam("a_float"))
|
||||||
|
}
|
||||||
|
if page.GetParam("a_date") != dateval {
|
||||||
|
t.Errorf("frontmatter not handling dates correctly should be %s, got: %s", dateval, page.GetParam("a_date"))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) {
|
func TestDegenerateInvalidFrontMatterLeadingWhitespace(t *testing.T) {
|
||||||
_, err := ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS), "invalid/front/matter/leading/ws")
|
_, err := ReadFrom(strings.NewReader(INVALID_FRONT_MATTER_LEADING_WS), "invalid/front/matter/leading/ws")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|
Loading…
Reference in a new issue