2015-09-14 13:42:48 +00:00
|
|
|
package frontmatter
|
|
|
|
|
|
|
|
import (
|
2015-09-17 10:32:27 +00:00
|
|
|
"log"
|
2015-09-14 13:42:48 +00:00
|
|
|
"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-17 10:32:27 +00:00
|
|
|
object := new(frontmatter)
|
|
|
|
object.Type = "object"
|
|
|
|
|
|
|
|
return rawToPretty(front, object), nil
|
2015-09-14 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type frontmatter struct {
|
2015-09-15 10:59:48 +00:00
|
|
|
Name string
|
2015-09-17 10:32:27 +00:00
|
|
|
Title string
|
2015-09-15 10:59:48 +00:00
|
|
|
Content interface{}
|
|
|
|
Type string
|
2015-09-17 10:32:27 +00:00
|
|
|
Parent *frontmatter
|
2015-09-14 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 10:32:27 +00:00
|
|
|
func sortByTitle(config []*frontmatter) {
|
|
|
|
keys := make([]string, len(config))
|
|
|
|
positionByTitle := make(map[string]int)
|
2015-09-15 10:59:48 +00:00
|
|
|
|
2015-09-17 10:32:27 +00:00
|
|
|
for index, element := range config {
|
|
|
|
keys[index] = element.Title
|
|
|
|
positionByTitle[element.Title] = index
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Strings(keys)
|
|
|
|
cnf := make([]*frontmatter, len(config))
|
|
|
|
|
|
|
|
for index, title := range keys {
|
|
|
|
cnf[index] = config[positionByTitle[title]]
|
|
|
|
}
|
|
|
|
|
|
|
|
for index := range config {
|
|
|
|
config[index] = cnf[index]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func rawToPretty(config interface{}, parent *frontmatter) interface{} {
|
|
|
|
objects := []*frontmatter{}
|
|
|
|
arrays := []*frontmatter{}
|
|
|
|
fields := []*frontmatter{}
|
2015-09-15 20:28:54 +00:00
|
|
|
|
2015-09-17 10:32:27 +00:00
|
|
|
if parent.Type == "array" {
|
2015-09-15 10:59:48 +00:00
|
|
|
for index, element := range config.([]interface{}) {
|
|
|
|
c := new(frontmatter)
|
|
|
|
c.Parent = parent
|
|
|
|
|
|
|
|
if utils.IsMap(element) {
|
|
|
|
c.Type = "object"
|
2015-09-17 10:32:27 +00:00
|
|
|
|
|
|
|
if parent.Name == "" {
|
|
|
|
c.Name = c.Title
|
|
|
|
} else {
|
|
|
|
c.Name = parent.Name + "[" + c.Name + "]"
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Content = rawToPretty(config.([]interface{})[index], c)
|
|
|
|
objects = append(objects, c)
|
2015-09-15 10:59:48 +00:00
|
|
|
} else if utils.IsSlice(element) {
|
|
|
|
c.Type = "array"
|
2015-09-17 10:32:27 +00:00
|
|
|
c.Name = parent.Name + "[" + c.Name + "]"
|
|
|
|
c.Content = rawToPretty(config.([]interface{})[index], c)
|
|
|
|
|
|
|
|
arrays = append(arrays, c)
|
2015-09-15 10:59:48 +00:00
|
|
|
} else {
|
2015-09-17 10:32:27 +00:00
|
|
|
// TODO: add string, boolean, number
|
|
|
|
c.Type = "string"
|
|
|
|
c.Name = parent.Name + "[]"
|
|
|
|
c.Title = element.(string)
|
|
|
|
c.Content = config.([]interface{})[index]
|
|
|
|
fields = append(fields, c)
|
2015-09-15 10:59:48 +00:00
|
|
|
}
|
|
|
|
}
|
2015-09-17 10:32:27 +00:00
|
|
|
} else if parent.Type == "object" {
|
|
|
|
for name, element := range config.(map[string]interface{}) {
|
|
|
|
c := new(frontmatter)
|
|
|
|
c.Title = name
|
|
|
|
c.Parent = parent
|
2015-09-15 10:59:48 +00:00
|
|
|
|
2015-09-17 10:32:27 +00:00
|
|
|
if utils.IsMap(element) {
|
|
|
|
c.Type = "object"
|
|
|
|
|
|
|
|
if parent.Name == "" {
|
|
|
|
c.Name = c.Title
|
|
|
|
} else {
|
|
|
|
c.Name = parent.Name + "[" + c.Title + "]"
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Content = rawToPretty(config.(map[string]interface{})[name], c)
|
|
|
|
objects = append(objects, c)
|
|
|
|
} else if utils.IsSlice(element) {
|
|
|
|
c.Type = "array"
|
|
|
|
|
|
|
|
if parent.Name == "" {
|
|
|
|
c.Name = name
|
|
|
|
} else {
|
|
|
|
c.Name = parent.Name + "[" + c.Name + "]"
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Content = rawToPretty(config.(map[string]interface{})[c.Title], c)
|
2015-09-15 10:59:48 +00:00
|
|
|
|
2015-09-17 10:32:27 +00:00
|
|
|
arrays = append(arrays, c)
|
|
|
|
} else {
|
|
|
|
// TODO: add string, boolean, number
|
|
|
|
c.Type = "string"
|
|
|
|
|
|
|
|
if parent.Name == "" {
|
|
|
|
c.Name = name
|
|
|
|
} else {
|
|
|
|
c.Name = parent.Name + "[" + name + "]"
|
|
|
|
}
|
2015-09-14 13:42:48 +00:00
|
|
|
|
2015-09-17 10:32:27 +00:00
|
|
|
c.Content = element
|
|
|
|
fields = append(fields, c)
|
|
|
|
}
|
2015-09-14 13:42:48 +00:00
|
|
|
}
|
2015-09-17 10:32:27 +00:00
|
|
|
} else {
|
|
|
|
log.Panic("Parent type not allowed.")
|
2015-09-14 13:42:48 +00:00
|
|
|
}
|
|
|
|
|
2015-09-17 10:32:27 +00:00
|
|
|
sortByTitle(objects)
|
|
|
|
sortByTitle(arrays)
|
|
|
|
sortByTitle(fields)
|
|
|
|
|
|
|
|
settings := []*frontmatter{}
|
|
|
|
settings = append(settings, fields...)
|
|
|
|
settings = append(settings, arrays...)
|
|
|
|
settings = append(settings, objects...)
|
2015-09-14 13:42:48 +00:00
|
|
|
|
2015-09-17 10:32:27 +00:00
|
|
|
return settings
|
2015-09-14 13:42:48 +00:00
|
|
|
}
|