pull/144/head
Henrique Dias 2017-01-16 20:03:44 +00:00
parent fbbf79d757
commit b46dcb6ccb
2 changed files with 47 additions and 16 deletions

View File

@ -82,6 +82,45 @@ func Unmarshal(content []byte) (interface{}, error) {
return data, nil 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 // Content is the block content
type Content struct { type Content struct {
Other interface{} Other interface{}

View File

@ -4,14 +4,13 @@ import (
"bytes" "bytes"
"encoding/json" "encoding/json"
"errors" "errors"
"fmt"
"io/ioutil" "io/ioutil"
"net/http" "net/http"
"path/filepath" "path/filepath"
"strings" "strings"
"github.com/hacdias/caddy-filemanager/config" "github.com/hacdias/caddy-filemanager/config"
"github.com/spf13/hugo/parser" "github.com/hacdias/caddy-filemanager/frontmatter"
) )
// PreProccessPUT is used to update a file that was edited // PreProccessPUT is used to update a file that was edited
@ -83,27 +82,21 @@ func ParseFrontMatterOnlyFile(data interface{}, filename string) ([]byte, error)
} }
// ParseFrontMatter is the frontmatter parser // ParseFrontMatter is the frontmatter parser
func ParseFrontMatter(data interface{}, frontmatter string) ([]byte, error) { func ParseFrontMatter(data interface{}, front string) ([]byte, error) {
var mark rune var mark rune
switch frontmatter { switch front {
case "toml": case "toml":
mark = rune('+') mark = '+'
case "json": case "json":
mark = rune('{') mark = '{'
case "yaml": case "yaml":
mark = rune('-') mark = '-'
default: default:
return []byte{}, errors.New("Can't define the frontmatter.") return nil, errors.New("Unsupported Format provided")
} }
f, err := parser.InterfaceToFrontMatter(data, mark) return frontmatter.Marshal(data, mark)
if err != nil {
return []byte{}, err
}
return f, nil
} }
// ParseCompleteFile parses a complete file // ParseCompleteFile parses a complete file
@ -126,7 +119,6 @@ func ParseCompleteFile(data map[string]interface{}, filename string, frontmatter
front, err := ParseFrontMatter(data, frontmatter) front, err := ParseFrontMatter(data, frontmatter)
if err != nil { if err != nil {
fmt.Println(frontmatter)
return []byte{}, err return []byte{}, err
} }