1
0
mirror of https://github.com/danog/gojekyll.git synced 2024-11-27 07:34:43 +01:00
gojekyll/frontmatter/frontmatter.go
2017-07-07 14:55:24 -04:00

48 lines
1.1 KiB
Go

package frontmatter
import (
"reflect"
"sort"
"strings"
"github.com/osteele/gojekyll/helpers"
"github.com/osteele/liquid/evaluator"
)
// FrontMatter wraps a map to provide interface functions
type FrontMatter map[string]interface{}
const fmMagic = "---\n"
// FileHasFrontMatter returns a bool indicating whether the
// file looks like it has frontmatter.
func FileHasFrontMatter(filename string) (bool, error) {
magic, err := helpers.ReadFileMagic(filename)
if err != nil {
return false, err
}
return string(magic) == fmMagic, nil
}
// SortedStringArray returns a sorts list of strings from a
// frontmatter variable that is either a string (in which case it
// is a ws-separated list of words), or a list of strings.
//
// This is the format for page categories and tags.
func (fm FrontMatter) SortedStringArray(key string) []string {
out := []string{}
field := fm[key]
switch value := field.(type) {
case string:
out = strings.Fields(value)
case []interface{}:
if c, e := evaluator.Convert(value, reflect.TypeOf(out)); e == nil {
out = c.([]string)
}
case []string:
out = value
}
sort.Strings(out)
return out
}