2015-09-14 13:42:48 +00:00
|
|
|
package frontmatter
|
|
|
|
|
|
|
|
import (
|
|
|
|
"sort"
|
|
|
|
|
|
|
|
"github.com/hacdias/caddy-hugo/utils"
|
|
|
|
"github.com/spf13/hugo/parser"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Pretty creates a new FrontMatter object
|
2015-09-14 21:03:09 +00:00
|
|
|
func Pretty(content []byte) (interface{}, error) {
|
|
|
|
frontType := parser.DetectFrontMatter(rune(content[0]))
|
|
|
|
front, err := frontType.Parse(content)
|
2015-09-14 13:42:48 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return []string{}, err
|
|
|
|
}
|
|
|
|
|
2015-09-15 10:59:48 +00:00
|
|
|
return rawToPretty(front, "", ""), nil
|
2015-09-14 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type frontmatter struct {
|
2015-09-15 10:59:48 +00:00
|
|
|
Name string
|
|
|
|
Content interface{}
|
|
|
|
Parent string
|
|
|
|
Type string
|
2015-09-14 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-15 10:59:48 +00:00
|
|
|
func rawToPretty(config interface{}, master string, parent string) interface{} {
|
|
|
|
if utils.IsSlice(config) {
|
|
|
|
settings := make([]interface{}, len(config.([]interface{})))
|
|
|
|
|
2015-09-15 20:28:54 +00:00
|
|
|
// TODO: improve this function
|
|
|
|
|
2015-09-15 10:59:48 +00:00
|
|
|
for index, element := range config.([]interface{}) {
|
|
|
|
c := new(frontmatter)
|
|
|
|
c.Name = master
|
|
|
|
c.Parent = parent
|
|
|
|
|
|
|
|
if utils.IsMap(element) {
|
|
|
|
c.Type = "object"
|
|
|
|
c.Content = rawToPretty(element, c.Name, "object")
|
|
|
|
} else if utils.IsSlice(element) {
|
|
|
|
c.Type = "array"
|
|
|
|
c.Content = rawToPretty(element, c.Name, "array")
|
|
|
|
} else {
|
|
|
|
c.Type = "text"
|
|
|
|
c.Content = element
|
|
|
|
}
|
|
|
|
|
|
|
|
settings[index] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings
|
|
|
|
}
|
|
|
|
|
2015-09-14 13:42:48 +00:00
|
|
|
var mapsNames []string
|
|
|
|
var stringsNames []string
|
|
|
|
|
|
|
|
for index, element := range config.(map[string]interface{}) {
|
2015-09-15 18:26:18 +00:00
|
|
|
if utils.IsMap(element) || utils.IsSlice(element) {
|
2015-09-14 13:42:48 +00:00
|
|
|
mapsNames = append(mapsNames, index)
|
|
|
|
} else {
|
|
|
|
stringsNames = append(stringsNames, index)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(mapsNames)
|
|
|
|
sort.Strings(stringsNames)
|
|
|
|
names := append(stringsNames, mapsNames...)
|
|
|
|
|
|
|
|
settings := make([]interface{}, len(names))
|
|
|
|
|
|
|
|
for index := range names {
|
|
|
|
c := new(frontmatter)
|
|
|
|
c.Name = names[index]
|
2015-09-15 10:59:48 +00:00
|
|
|
c.Parent = parent
|
2015-09-14 13:42:48 +00:00
|
|
|
|
|
|
|
i := config.(map[string]interface{})[names[index]]
|
|
|
|
|
|
|
|
if utils.IsMap(i) {
|
2015-09-15 10:59:48 +00:00
|
|
|
c.Type = "object"
|
|
|
|
c.Content = rawToPretty(i, c.Name, "object")
|
|
|
|
} else if utils.IsSlice(i) {
|
|
|
|
c.Type = "array"
|
|
|
|
c.Content = rawToPretty(i, c.Name, "array")
|
2015-09-14 13:42:48 +00:00
|
|
|
} else {
|
2015-09-15 10:59:48 +00:00
|
|
|
c.Type = "text"
|
2015-09-14 13:42:48 +00:00
|
|
|
c.Content = i
|
|
|
|
}
|
|
|
|
|
|
|
|
settings[index] = c
|
|
|
|
}
|
|
|
|
|
|
|
|
return settings
|
|
|
|
}
|