filebrowser/frontmatter/frontmatter.go

168 lines
3.7 KiB
Go
Raw Normal View History

2015-09-14 13:42:48 +00:00
package frontmatter
import (
2015-09-17 10:32:27 +00:00
"log"
2015-09-17 14:38:04 +00:00
"reflect"
2015-09-14 13:42:48 +00:00
"sort"
2015-09-18 08:47:02 +00:00
"strings"
2015-09-14 13:42:48 +00:00
2015-10-18 14:10:32 +00:00
"github.com/hacdias/caddy-hugo/utils"
"github.com/spf13/cast"
2015-09-14 13:42:48 +00:00
"github.com/spf13/hugo/parser"
)
2015-09-17 14:38:04 +00:00
const mainName = "#MAIN#"
2016-01-31 22:14:17 +00:00
var mainTitle = ""
2015-09-14 13:42:48 +00:00
// Pretty creates a new FrontMatter object
2016-01-31 22:14:17 +00:00
func Pretty(content []byte) (interface{}, string, error) {
frontType := parser.DetectFrontMatter(rune(content[0]))
front, err := frontType.Parse(content)
2015-09-14 13:42:48 +00:00
if err != nil {
2016-01-31 22:14:17 +00:00
return []string{}, mainTitle, err
2015-09-14 13:42:48 +00:00
}
2015-09-17 10:32:27 +00:00
object := new(frontmatter)
object.Type = "object"
2015-09-17 14:38:04 +00:00
object.Name = mainName
2015-09-17 10:32:27 +00:00
2016-01-31 22:14:17 +00:00
return rawToPretty(front, object), mainTitle, nil
2015-09-14 13:42:48 +00:00
}
type frontmatter struct {
2015-10-24 09:54:10 +00:00
Name string
Title string
Content interface{}
Type string
HTMLType string
Parent *frontmatter
2015-09-14 13:42:48 +00:00
}
2015-09-17 10:32:27 +00:00
func rawToPretty(config interface{}, parent *frontmatter) interface{} {
objects := []*frontmatter{}
arrays := []*frontmatter{}
fields := []*frontmatter{}
2015-09-15 20:28:54 +00:00
2016-01-09 21:19:27 +00:00
cnf := map[string]interface{}{}
if reflect.TypeOf(config) == reflect.TypeOf(map[interface{}]interface{}{}) {
for key, value := range config.(map[interface{}]interface{}) {
cnf[key.(string)] = value
2015-09-15 10:59:48 +00:00
}
2016-01-09 21:19:27 +00:00
} else if reflect.TypeOf(config) == reflect.TypeOf([]interface{}{}) {
for key, value := range config.([]interface{}) {
cnf[string(key)] = value
2015-09-14 13:42:48 +00:00
}
2015-09-17 10:32:27 +00:00
} else {
2016-01-09 21:19:27 +00:00
cnf = config.(map[string]interface{})
}
for name, element := range cnf {
if utils.IsMap(element) {
objects = append(objects, handleObjects(element, parent, name))
} else if utils.IsSlice(element) {
arrays = append(arrays, handleArrays(element, parent, name))
} else {
2016-01-31 22:14:17 +00:00
if name == "title" && parent.Name == mainName {
mainTitle = element.(string)
}
2016-01-09 21:19:27 +00:00
fields = append(fields, handleFlatValues(element, parent, name))
}
2015-09-14 13:42:48 +00:00
}
2015-09-18 08:47:02 +00:00
sort.Sort(sortByTitle(objects))
sort.Sort(sortByTitle(arrays))
sort.Sort(sortByTitle(fields))
2015-09-17 10:32:27 +00:00
settings := []*frontmatter{}
settings = append(settings, fields...)
settings = append(settings, arrays...)
settings = append(settings, objects...)
return settings
2015-09-14 13:42:48 +00:00
}
2015-09-17 14:38:04 +00:00
2015-09-18 08:47:02 +00:00
type sortByTitle []*frontmatter
2015-09-17 14:45:59 +00:00
2015-09-18 08:47:02 +00:00
func (f sortByTitle) Len() int { return len(f) }
func (f sortByTitle) Swap(i, j int) { f[i], f[j] = f[j], f[i] }
func (f sortByTitle) Less(i, j int) bool {
return strings.ToLower(f[i].Name) < strings.ToLower(f[j].Name)
2015-09-17 14:45:59 +00:00
}
2015-09-17 14:38:04 +00:00
func handleObjects(content interface{}, parent *frontmatter, name string) *frontmatter {
c := new(frontmatter)
c.Parent = parent
c.Type = "object"
c.Title = name
if parent.Name == mainName {
c.Name = c.Title
} else {
c.Name = parent.Name + "[" + c.Title + "]"
}
c.Content = rawToPretty(content, c)
return c
}
func handleArrays(content interface{}, parent *frontmatter, name string) *frontmatter {
c := new(frontmatter)
c.Parent = parent
c.Type = "array"
c.Title = name
2015-09-20 12:04:33 +00:00
if parent.Name == mainName {
2015-09-17 14:38:04 +00:00
c.Name = name
} else {
2015-09-20 12:04:33 +00:00
c.Name = parent.Name + "[" + name + "]"
2015-09-17 14:38:04 +00:00
}
c.Content = rawToPretty(content, c)
return c
}
func handleFlatValues(content interface{}, parent *frontmatter, name string) *frontmatter {
c := new(frontmatter)
c.Parent = parent
switch reflect.ValueOf(content).Kind() {
case reflect.Bool:
c.Type = "boolean"
2015-09-18 15:49:53 +00:00
case reflect.Int, reflect.Float32, reflect.Float64:
2015-09-17 14:38:04 +00:00
c.Type = "number"
default:
c.Type = "string"
}
c.Content = content
2015-10-24 09:54:10 +00:00
switch strings.ToLower(name) {
case "description":
c.HTMLType = "textarea"
case "date", "publishdate":
c.HTMLType = "datetime"
c.Content = cast.ToTime(content)
2015-10-24 09:54:10 +00:00
default:
c.HTMLType = "text"
}
2015-09-17 14:38:04 +00:00
if parent.Type == "array" {
c.Name = parent.Name + "[]"
c.Title = content.(string)
} else if parent.Type == "object" {
c.Title = name
c.Name = parent.Name + "[" + name + "]"
if parent.Name == mainName {
c.Name = name
}
} else {
log.Panic("Parent type not allowed in handleFlatValues.")
}
return c
}