filebrowser/frontmatter/frontmatter.go

277 lines
5.7 KiB
Go
Raw Normal View History

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-10-22 11:07:19 +00:00
"github.com/hacdias/caddy-filemanager/utils/variables"
2016-10-18 20:30:10 +00:00
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()
2017-01-04 18:19:16 +00:00
if kind == reflect.Invalid {
2017-01-04 18:17:47 +00:00
return &Content{}, "", nil
}
2016-06-29 09:40:20 +00:00
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:
2017-04-25 09:51:48 +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
}
2017-01-16 20:03:44 +00:00
// Marshal encodes the interface in a specific format
func Marshal(data interface{}, mark rune) ([]byte, error) {
b := new(bytes.Buffer)
switch mark {
case '+':
enc := toml.NewEncoder(b)
err := enc.Encode(data)
if err != nil {
return nil, err
}
return b.Bytes(), nil
case '{':
by, err := json.MarshalIndent(data, "", " ")
if err != nil {
return nil, err
}
b.Write(by)
_, err = b.Write([]byte("\n"))
if err != nil {
return nil, err
}
return b.Bytes(), nil
case '-':
by, err := yaml.Marshal(data)
if err != nil {
return nil, err
}
b.Write(by)
_, err = b.Write([]byte("..."))
if err != nil {
return nil, err
}
return b.Bytes(), nil
default:
return nil, errors.New("Unsupported Format provided")
}
}
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 {
2016-10-22 11:07:19 +00:00
if variables.IsMap(element) {
2016-06-23 22:21:44 +00:00
objects = append(objects, handleObjects(element, parent, name))
2016-10-22 11:07:19 +00:00
} else if variables.IsSlice(element) {
2016-06-23 22:21:44 +00:00
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
2016-08-22 21:45:29 +00:00
switch content.(type) {
case bool:
2016-06-23 22:21:44 +00:00
c.Type = "boolean"
2016-08-22 21:45:29 +00:00
case int, float32, float64:
2016-06-23 22:21:44 +00:00
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
}