2016-06-28 09:24:02 +00:00
|
|
|
package frontmatter
|
2016-06-23 22:21:44 +00:00
|
|
|
|
|
|
|
import (
|
2016-06-26 22:03:27 +00:00
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
|
|
|
"errors"
|
2016-06-23 22:21:44 +00:00
|
|
|
"log"
|
|
|
|
"reflect"
|
|
|
|
"sort"
|
2016-07-06 09:45:43 +00:00
|
|
|
"strconv"
|
2016-06-23 22:21:44 +00:00
|
|
|
"strings"
|
|
|
|
|
2016-06-26 22:03:27 +00:00
|
|
|
"gopkg.in/yaml.v2"
|
|
|
|
|
|
|
|
"github.com/BurntSushi/toml"
|
2016-06-25 20:57:10 +00:00
|
|
|
"github.com/hacdias/caddy-filemanager/utils/variables"
|
2016-06-23 22:21:44 +00:00
|
|
|
"github.com/spf13/cast"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
mainName = "#MAIN#"
|
|
|
|
objectType = "object"
|
|
|
|
arrayType = "array"
|
|
|
|
)
|
|
|
|
|
|
|
|
var mainTitle = ""
|
|
|
|
|
|
|
|
// Pretty creates a new FrontMatter object
|
2016-06-26 22:03:27 +00:00
|
|
|
func Pretty(content []byte) (*Content, string, error) {
|
2016-06-29 09:40:20 +00:00
|
|
|
data, err := Unmarshal(content)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return &Content{}, "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
kind := reflect.ValueOf(data).Kind()
|
|
|
|
|
|
|
|
object := new(Block)
|
|
|
|
object.Type = objectType
|
|
|
|
object.Name = mainName
|
|
|
|
|
|
|
|
if kind == reflect.Map {
|
|
|
|
object.Type = objectType
|
|
|
|
} else if kind == reflect.Slice || kind == reflect.Array {
|
|
|
|
object.Type = arrayType
|
|
|
|
}
|
|
|
|
|
|
|
|
return rawToPretty(data, object), mainTitle, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unmarshal returns the data of the frontmatter
|
|
|
|
func Unmarshal(content []byte) (interface{}, error) {
|
2016-06-26 22:03:27 +00:00
|
|
|
mark := rune(content[0])
|
|
|
|
var data interface{}
|
|
|
|
|
|
|
|
switch mark {
|
|
|
|
case '-':
|
|
|
|
// If it's YAML
|
|
|
|
if err := yaml.Unmarshal(content, &data); err != nil {
|
2016-06-29 09:40:20 +00:00
|
|
|
return nil, err
|
2016-06-26 22:03:27 +00:00
|
|
|
}
|
|
|
|
case '+':
|
|
|
|
// If it's TOML
|
|
|
|
content = bytes.Replace(content, []byte("+"), []byte(""), -1)
|
|
|
|
if _, err := toml.Decode(string(content), &data); err != nil {
|
2016-06-29 09:40:20 +00:00
|
|
|
return nil, err
|
2016-06-26 22:03:27 +00:00
|
|
|
}
|
|
|
|
case '{', '[':
|
|
|
|
// If it's JSON
|
|
|
|
if err := json.Unmarshal(content, &data); err != nil {
|
2016-06-29 09:40:20 +00:00
|
|
|
return nil, err
|
2016-06-26 22:03:27 +00:00
|
|
|
}
|
|
|
|
default:
|
2016-06-29 09:40:20 +00:00
|
|
|
return nil, errors.New("Invalid frontmatter type.")
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-29 09:40:20 +00:00
|
|
|
return data, nil
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 22:03:27 +00:00
|
|
|
// Content is the block content
|
|
|
|
type Content struct {
|
|
|
|
Other interface{}
|
|
|
|
Fields []*Block
|
|
|
|
Arrays []*Block
|
|
|
|
Objects []*Block
|
|
|
|
}
|
|
|
|
|
|
|
|
// Block is a block
|
|
|
|
type Block struct {
|
2016-06-23 22:21:44 +00:00
|
|
|
Name string
|
|
|
|
Title string
|
|
|
|
Type string
|
|
|
|
HTMLType string
|
2016-06-26 22:03:27 +00:00
|
|
|
Content *Content
|
|
|
|
Parent *Block
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 22:03:27 +00:00
|
|
|
func rawToPretty(config interface{}, parent *Block) *Content {
|
|
|
|
objects := []*Block{}
|
|
|
|
arrays := []*Block{}
|
|
|
|
fields := []*Block{}
|
2016-06-23 22:21:44 +00:00
|
|
|
|
|
|
|
cnf := map[string]interface{}{}
|
2016-06-26 22:03:27 +00:00
|
|
|
kind := reflect.TypeOf(config)
|
2016-06-23 22:21:44 +00:00
|
|
|
|
2016-07-06 09:45:43 +00:00
|
|
|
switch kind {
|
|
|
|
case reflect.TypeOf(map[interface{}]interface{}{}):
|
2016-06-23 22:21:44 +00:00
|
|
|
for key, value := range config.(map[interface{}]interface{}) {
|
|
|
|
cnf[key.(string)] = value
|
|
|
|
}
|
2016-07-06 09:45:43 +00:00
|
|
|
case reflect.TypeOf([]map[string]interface{}{}):
|
|
|
|
for index, value := range config.([]map[string]interface{}) {
|
|
|
|
cnf[strconv.Itoa(index)] = value
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
2016-07-06 09:45:43 +00:00
|
|
|
case reflect.TypeOf([]map[interface{}]interface{}{}):
|
|
|
|
for index, value := range config.([]map[interface{}]interface{}) {
|
|
|
|
cnf[strconv.Itoa(index)] = value
|
|
|
|
}
|
|
|
|
case reflect.TypeOf([]interface{}{}):
|
|
|
|
for index, value := range config.([]interface{}) {
|
|
|
|
cnf[strconv.Itoa(index)] = value
|
|
|
|
}
|
|
|
|
default:
|
2016-06-23 22:21:44 +00:00
|
|
|
cnf = config.(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
for name, element := range cnf {
|
|
|
|
if variables.IsMap(element) {
|
|
|
|
objects = append(objects, handleObjects(element, parent, name))
|
|
|
|
} else if variables.IsSlice(element) {
|
|
|
|
arrays = append(arrays, handleArrays(element, parent, name))
|
|
|
|
} else {
|
|
|
|
if name == "title" && parent.Name == mainName {
|
|
|
|
mainTitle = element.(string)
|
|
|
|
}
|
|
|
|
fields = append(fields, handleFlatValues(element, parent, name))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sort.Sort(sortByTitle(fields))
|
2016-06-26 22:03:27 +00:00
|
|
|
sort.Sort(sortByTitle(arrays))
|
|
|
|
sort.Sort(sortByTitle(objects))
|
|
|
|
return &Content{
|
|
|
|
Fields: fields,
|
|
|
|
Arrays: arrays,
|
|
|
|
Objects: objects,
|
|
|
|
}
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
|
|
|
|
2016-06-26 22:03:27 +00:00
|
|
|
type sortByTitle []*Block
|
2016-06-23 22:21:44 +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)
|
|
|
|
}
|
|
|
|
|
2016-06-26 22:03:27 +00:00
|
|
|
func handleObjects(content interface{}, parent *Block, name string) *Block {
|
|
|
|
c := new(Block)
|
2016-06-23 22:21:44 +00:00
|
|
|
c.Parent = parent
|
|
|
|
c.Type = objectType
|
|
|
|
c.Title = name
|
|
|
|
|
|
|
|
if parent.Name == mainName {
|
|
|
|
c.Name = c.Title
|
|
|
|
} else if parent.Type == arrayType {
|
2016-07-06 09:45:43 +00:00
|
|
|
c.Name = parent.Name + "[" + name + "]"
|
2016-06-23 22:21:44 +00:00
|
|
|
} else {
|
2016-06-27 17:57:54 +00:00
|
|
|
c.Name = parent.Name + "." + c.Title
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Content = rawToPretty(content, c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-06-26 22:03:27 +00:00
|
|
|
func handleArrays(content interface{}, parent *Block, name string) *Block {
|
|
|
|
c := new(Block)
|
2016-06-23 22:21:44 +00:00
|
|
|
c.Parent = parent
|
|
|
|
c.Type = arrayType
|
|
|
|
c.Title = name
|
|
|
|
|
|
|
|
if parent.Name == mainName {
|
|
|
|
c.Name = name
|
|
|
|
} else {
|
2016-06-27 17:57:54 +00:00
|
|
|
c.Name = parent.Name + "." + name
|
2016-06-23 22:21:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
c.Content = rawToPretty(content, c)
|
|
|
|
return c
|
|
|
|
}
|
|
|
|
|
2016-06-26 22:03:27 +00:00
|
|
|
func handleFlatValues(content interface{}, parent *Block, name string) *Block {
|
|
|
|
c := new(Block)
|
2016-06-23 22:21:44 +00:00
|
|
|
c.Parent = parent
|
|
|
|
|
|
|
|
switch reflect.ValueOf(content).Kind() {
|
|
|
|
case reflect.Bool:
|
|
|
|
c.Type = "boolean"
|
|
|
|
case reflect.Int, reflect.Float32, reflect.Float64:
|
|
|
|
c.Type = "number"
|
|
|
|
default:
|
|
|
|
c.Type = "string"
|
|
|
|
}
|
|
|
|
|
2016-06-26 22:03:27 +00:00
|
|
|
c.Content = &Content{Other: content}
|
2016-06-23 22:21:44 +00:00
|
|
|
|
|
|
|
switch strings.ToLower(name) {
|
|
|
|
case "description":
|
|
|
|
c.HTMLType = "textarea"
|
|
|
|
case "date", "publishdate":
|
|
|
|
c.HTMLType = "datetime"
|
2016-06-26 22:03:27 +00:00
|
|
|
c.Content = &Content{Other: cast.ToTime(content)}
|
2016-06-23 22:21:44 +00:00
|
|
|
default:
|
|
|
|
c.HTMLType = "text"
|
|
|
|
}
|
|
|
|
|
|
|
|
if parent.Type == arrayType {
|
|
|
|
c.Name = parent.Name + "[]"
|
|
|
|
c.Title = content.(string)
|
|
|
|
} else if parent.Type == objectType {
|
|
|
|
c.Title = name
|
2016-06-27 17:57:54 +00:00
|
|
|
c.Name = parent.Name + "." + name
|
2016-06-23 22:21:44 +00:00
|
|
|
|
|
|
|
if parent.Name == mainName {
|
|
|
|
c.Name = name
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
log.Panic("Parent type not allowed in handleFlatValues.")
|
|
|
|
}
|
|
|
|
|
|
|
|
return c
|
|
|
|
}
|