Remove frontmatter interface
Former-commit-id: bd8ab54a13b08f223cc00a5372dc885c5b7b49e6 [formerly 169211b32940ffa9a157bf140579e1946b8df75d] [formerly 1795b434a35c2f9dc3c6940b7555542f62f3d74d [formerly 1ae2afb9985b82c7c101395c8a65fd122200c808]] Former-commit-id: 1ed1a0402184384c37b80d51ef1460d036d5201e [formerly e068d59a1b531635e6ccad4d2203c1ef9eab7c86] Former-commit-id: 391cbcefcd6b9f98b054bc02aec5cf808ccd199f
This commit is contained in:
@@ -1,276 +0,0 @@
|
||||
package frontmatter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/hacdias/filemanager/variables"
|
||||
|
||||
"github.com/spf13/cast"
|
||||
)
|
||||
|
||||
const (
|
||||
mainName = "#MAIN#"
|
||||
objectType = "object"
|
||||
arrayType = "array"
|
||||
)
|
||||
|
||||
var mainTitle = ""
|
||||
|
||||
// Pretty creates a new FrontMatter object
|
||||
func Pretty(content []byte) (*Content, string, error) {
|
||||
data, err := Unmarshal(content)
|
||||
|
||||
if err != nil {
|
||||
return &Content{}, "", err
|
||||
}
|
||||
|
||||
kind := reflect.ValueOf(data).Kind()
|
||||
|
||||
if kind == reflect.Invalid {
|
||||
return &Content{}, "", nil
|
||||
}
|
||||
|
||||
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) {
|
||||
mark := rune(content[0])
|
||||
var data interface{}
|
||||
|
||||
switch mark {
|
||||
case '-':
|
||||
// If it's YAML
|
||||
if err := yaml.Unmarshal(content, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case '+':
|
||||
// If it's TOML
|
||||
content = bytes.Replace(content, []byte("+"), []byte(""), -1)
|
||||
if _, err := toml.Decode(string(content), &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
case '{', '[':
|
||||
// If it's JSON
|
||||
if err := json.Unmarshal(content, &data); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
default:
|
||||
return nil, errors.New("Invalid frontmatter type")
|
||||
}
|
||||
|
||||
return data, nil
|
||||
}
|
||||
|
||||
// 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")
|
||||
}
|
||||
}
|
||||
|
||||
// Content is the block content
|
||||
type Content struct {
|
||||
Other interface{} `json:"other"`
|
||||
Fields []*Block `json:"fields"`
|
||||
Arrays []*Block `json:"arrays"`
|
||||
Objects []*Block `json:"objects"`
|
||||
}
|
||||
|
||||
// Block is a block
|
||||
type Block struct {
|
||||
Name string `json:"name"`
|
||||
Title string `json:"title"`
|
||||
Type string `json:"type"`
|
||||
HTMLType string `json:"htmlType"`
|
||||
Content *Content `json:"content"`
|
||||
Parent *Block `json:"-"`
|
||||
}
|
||||
|
||||
func rawToPretty(config interface{}, parent *Block) *Content {
|
||||
objects := []*Block{}
|
||||
arrays := []*Block{}
|
||||
fields := []*Block{}
|
||||
|
||||
cnf := map[string]interface{}{}
|
||||
kind := reflect.TypeOf(config)
|
||||
|
||||
switch kind {
|
||||
case reflect.TypeOf(map[interface{}]interface{}{}):
|
||||
for key, value := range config.(map[interface{}]interface{}) {
|
||||
cnf[key.(string)] = value
|
||||
}
|
||||
case reflect.TypeOf([]map[string]interface{}{}):
|
||||
for index, value := range config.([]map[string]interface{}) {
|
||||
cnf[strconv.Itoa(index)] = value
|
||||
}
|
||||
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:
|
||||
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))
|
||||
sort.Sort(sortByTitle(arrays))
|
||||
sort.Sort(sortByTitle(objects))
|
||||
return &Content{
|
||||
Fields: fields,
|
||||
Arrays: arrays,
|
||||
Objects: objects,
|
||||
}
|
||||
}
|
||||
|
||||
type sortByTitle []*Block
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
func handleObjects(content interface{}, parent *Block, name string) *Block {
|
||||
c := new(Block)
|
||||
c.Parent = parent
|
||||
c.Type = objectType
|
||||
c.Title = name
|
||||
|
||||
if parent.Name == mainName {
|
||||
c.Name = c.Title
|
||||
} else if parent.Type == arrayType {
|
||||
c.Name = parent.Name + "[" + name + "]"
|
||||
} else {
|
||||
c.Name = parent.Name + "." + c.Title
|
||||
}
|
||||
|
||||
c.Content = rawToPretty(content, c)
|
||||
return c
|
||||
}
|
||||
|
||||
func handleArrays(content interface{}, parent *Block, name string) *Block {
|
||||
c := new(Block)
|
||||
c.Parent = parent
|
||||
c.Type = arrayType
|
||||
c.Title = name
|
||||
|
||||
if parent.Name == mainName {
|
||||
c.Name = name
|
||||
} else {
|
||||
c.Name = parent.Name + "." + name
|
||||
}
|
||||
|
||||
c.Content = rawToPretty(content, c)
|
||||
return c
|
||||
}
|
||||
|
||||
func handleFlatValues(content interface{}, parent *Block, name string) *Block {
|
||||
c := new(Block)
|
||||
c.Parent = parent
|
||||
|
||||
switch content.(type) {
|
||||
case bool:
|
||||
c.Type = "boolean"
|
||||
case int, float32, float64:
|
||||
c.Type = "number"
|
||||
default:
|
||||
c.Type = "string"
|
||||
}
|
||||
|
||||
c.Content = &Content{Other: content}
|
||||
|
||||
switch strings.ToLower(name) {
|
||||
case "description":
|
||||
c.HTMLType = "textarea"
|
||||
case "date", "publishdate":
|
||||
c.HTMLType = "datetime"
|
||||
c.Content = &Content{Other: cast.ToTime(content)}
|
||||
default:
|
||||
c.HTMLType = "text"
|
||||
}
|
||||
|
||||
if parent.Type == arrayType {
|
||||
c.Name = parent.Name + "[]"
|
||||
c.Title = content.(string)
|
||||
} else if parent.Type == objectType {
|
||||
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
|
||||
}
|
||||
@@ -1,7 +1,6 @@
|
||||
package frontmatter
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
@@ -27,31 +26,3 @@ func AppendRune(frontmatter string, mark rune) string {
|
||||
|
||||
return frontmatter
|
||||
}
|
||||
|
||||
// RuneToStringFormat converts the rune to a string with the format
|
||||
func RuneToStringFormat(mark rune) (string, error) {
|
||||
switch mark {
|
||||
case '-':
|
||||
return "yaml", nil
|
||||
case '+':
|
||||
return "toml", nil
|
||||
case '{', '}':
|
||||
return "json", nil
|
||||
default:
|
||||
return "", errors.New("Unsupported format type")
|
||||
}
|
||||
}
|
||||
|
||||
// StringFormatToRune converts the format name to its rune
|
||||
func StringFormatToRune(format string) (rune, error) {
|
||||
switch format {
|
||||
case "yaml":
|
||||
return '-', nil
|
||||
case "toml":
|
||||
return '+', nil
|
||||
case "json":
|
||||
return '{', nil
|
||||
default:
|
||||
return '0', errors.New("Unsupported format type")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user