aadd first files based on caddy-filemanager
Former-commit-id: 20baeeb41a9555cefc3b31b495e24e907736c443
This commit is contained in:
276
frontmatter/frontmatter.go
Normal file
276
frontmatter/frontmatter.go
Normal file
@@ -0,0 +1,276 @@
|
||||
package frontmatter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"log"
|
||||
"reflect"
|
||||
"sort"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"gopkg.in/yaml.v2"
|
||||
|
||||
"github.com/BurntSushi/toml"
|
||||
"github.com/hacdias/filemanager/utils"
|
||||
|
||||
"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{}
|
||||
Fields []*Block
|
||||
Arrays []*Block
|
||||
Objects []*Block
|
||||
}
|
||||
|
||||
// Block is a block
|
||||
type Block struct {
|
||||
Name string
|
||||
Title string
|
||||
Type string
|
||||
HTMLType string
|
||||
Content *Content
|
||||
Parent *Block
|
||||
}
|
||||
|
||||
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 utils.IsMap(element) {
|
||||
objects = append(objects, handleObjects(element, parent, name))
|
||||
} else if utils.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
|
||||
}
|
||||
58
frontmatter/runes.go
Normal file
58
frontmatter/runes.go
Normal file
@@ -0,0 +1,58 @@
|
||||
package frontmatter
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// HasRune checks if the file has the frontmatter rune
|
||||
func HasRune(file []byte) bool {
|
||||
return strings.HasPrefix(string(file), "---") ||
|
||||
strings.HasPrefix(string(file), "+++") ||
|
||||
strings.HasPrefix(string(file), "{")
|
||||
}
|
||||
|
||||
// AppendRune appends the frontmatter rune to a file
|
||||
func AppendRune(frontmatter []byte, mark rune) []byte {
|
||||
frontmatter = bytes.TrimSpace(frontmatter)
|
||||
|
||||
switch mark {
|
||||
case '-':
|
||||
return []byte("---\n" + string(frontmatter) + "\n---")
|
||||
case '+':
|
||||
return []byte("+++\n" + string(frontmatter) + "\n+++")
|
||||
case '{':
|
||||
return []byte("{\n" + string(frontmatter) + "\n}")
|
||||
}
|
||||
|
||||
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")
|
||||
}
|
||||
}
|
||||
131
frontmatter/runes_test.go
Normal file
131
frontmatter/runes_test.go
Normal file
@@ -0,0 +1,131 @@
|
||||
package frontmatter
|
||||
|
||||
import "testing"
|
||||
|
||||
type hasRuneTest struct {
|
||||
File []byte
|
||||
Return bool
|
||||
}
|
||||
|
||||
var testHasRune = []hasRuneTest{
|
||||
hasRuneTest{
|
||||
File: []byte(`---
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
Sed auctor libero eget ante fermentum commodo.
|
||||
---`),
|
||||
Return: true,
|
||||
},
|
||||
hasRuneTest{
|
||||
File: []byte(`+++
|
||||
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
|
||||
Sed auctor libero eget ante fermentum commodo.
|
||||
+++`),
|
||||
Return: true,
|
||||
},
|
||||
hasRuneTest{
|
||||
File: []byte(`{
|
||||
"json": "Lorem ipsum dolor sit amet"
|
||||
}`),
|
||||
Return: true,
|
||||
},
|
||||
hasRuneTest{
|
||||
File: []byte(`+`),
|
||||
Return: false,
|
||||
},
|
||||
hasRuneTest{
|
||||
File: []byte(`++`),
|
||||
Return: false,
|
||||
},
|
||||
hasRuneTest{
|
||||
File: []byte(`-`),
|
||||
Return: false,
|
||||
},
|
||||
hasRuneTest{
|
||||
File: []byte(`--`),
|
||||
Return: false,
|
||||
},
|
||||
hasRuneTest{
|
||||
File: []byte(`Lorem ipsum`),
|
||||
Return: false,
|
||||
},
|
||||
}
|
||||
|
||||
func TestHasRune(t *testing.T) {
|
||||
for _, test := range testHasRune {
|
||||
if HasRune(test.File) != test.Return {
|
||||
t.Error("Incorrect value on HasRune")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
type appendRuneTest struct {
|
||||
Before []byte
|
||||
After []byte
|
||||
Mark rune
|
||||
}
|
||||
|
||||
var testAppendRuneTest = []appendRuneTest{}
|
||||
|
||||
func TestAppendRune(t *testing.T) {
|
||||
for i, test := range testAppendRuneTest {
|
||||
if !compareByte(AppendRune(test.Before, test.Mark), test.After) {
|
||||
t.Errorf("Incorrect value on AppendRune of Test %d", i)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func compareByte(a, b []byte) bool {
|
||||
if a == nil && b == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
if a == nil || b == nil {
|
||||
return false
|
||||
}
|
||||
|
||||
if len(a) != len(b) {
|
||||
return false
|
||||
}
|
||||
|
||||
for i := range a {
|
||||
if a[i] != b[i] {
|
||||
return false
|
||||
}
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
var testRuneToStringFormat = map[rune]string{
|
||||
'-': "yaml",
|
||||
'+': "toml",
|
||||
'{': "json",
|
||||
'}': "json",
|
||||
'1': "",
|
||||
'a': "",
|
||||
}
|
||||
|
||||
func TestRuneToStringFormat(t *testing.T) {
|
||||
for mark, format := range testRuneToStringFormat {
|
||||
val, _ := RuneToStringFormat(mark)
|
||||
if val != format {
|
||||
t.Errorf("Incorrect value on RuneToStringFormat of %v; want: %s; got: %s", mark, format, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var testStringFormatToRune = map[string]rune{
|
||||
"yaml": '-',
|
||||
"toml": '+',
|
||||
"json": '{',
|
||||
"lorem": '0',
|
||||
}
|
||||
|
||||
func TestStringFormatToRune(t *testing.T) {
|
||||
for format, mark := range testStringFormatToRune {
|
||||
val, _ := StringFormatToRune(format)
|
||||
if val != mark {
|
||||
t.Errorf("Incorrect value on StringFormatToRune of %s; want: %v; got: %v", format, mark, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user