2015-12-07 13:57:01 -05:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
2014-05-01 13:19:51 -04:00
|
|
|
//
|
2015-11-23 22:16:36 -05:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-05-01 13:19:51 -04:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-23 22:16:36 -05:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-01 13:19:51 -04:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
package parser
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2016-11-18 16:54:57 -05:00
|
|
|
"errors"
|
2014-05-02 01:01:44 -04:00
|
|
|
"strings"
|
2014-05-01 13:19:51 -04:00
|
|
|
|
2016-08-20 15:28:38 -04:00
|
|
|
toml "github.com/pelletier/go-toml"
|
|
|
|
|
2015-01-24 06:30:00 -05:00
|
|
|
"gopkg.in/yaml.v2"
|
2014-05-01 13:19:51 -04:00
|
|
|
)
|
|
|
|
|
2016-03-14 12:52:11 -04:00
|
|
|
type frontmatterType struct {
|
2014-05-01 13:19:51 -04:00
|
|
|
markstart, markend []byte
|
|
|
|
Parse func([]byte) (interface{}, error)
|
|
|
|
includeMark bool
|
|
|
|
}
|
|
|
|
|
2014-05-08 18:30:11 -04:00
|
|
|
func InterfaceToConfig(in interface{}, mark rune) ([]byte, error) {
|
|
|
|
if in == nil {
|
2016-11-18 16:54:57 -05:00
|
|
|
return []byte{}, errors.New("input was nil")
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
|
|
|
|
switch mark {
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(YAMLLead[0]):
|
2014-08-17 21:36:24 -04:00
|
|
|
by, err := yaml.Marshal(in)
|
2014-05-08 18:30:11 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.Write(by)
|
|
|
|
_, err = b.Write([]byte("..."))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(TOMLLead[0]):
|
2016-08-20 15:28:38 -04:00
|
|
|
tree := toml.TreeFromMap(in.(map[string]interface{}))
|
|
|
|
return []byte(tree.String()), nil
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(JSONLead[0]):
|
2014-05-08 18:30:11 -04:00
|
|
|
by, err := json.MarshalIndent(in, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.Write(by)
|
|
|
|
_, err = b.Write([]byte("\n"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
|
|
|
default:
|
2016-11-18 16:54:57 -05:00
|
|
|
return nil, errors.New("Unsupported Format provided")
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
|
|
|
|
if in == nil {
|
2016-11-18 16:54:57 -05:00
|
|
|
return []byte{}, errors.New("input was nil")
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
b := new(bytes.Buffer)
|
|
|
|
|
|
|
|
switch mark {
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(YAMLLead[0]):
|
|
|
|
_, err := b.Write([]byte(YAMLDelimUnix))
|
2014-05-01 13:19:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2014-08-17 21:36:24 -04:00
|
|
|
by, err := yaml.Marshal(in)
|
2014-05-01 13:19:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.Write(by)
|
2016-03-23 09:51:16 -04:00
|
|
|
_, err = b.Write([]byte(YAMLDelimUnix))
|
2014-05-01 13:19:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(TOMLLead[0]):
|
|
|
|
_, err := b.Write([]byte(TOMLDelimUnix))
|
2014-05-01 13:19:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2016-08-20 15:28:38 -04:00
|
|
|
tree := toml.TreeFromMap(in.(map[string]interface{}))
|
|
|
|
b.Write([]byte(tree.String()))
|
2016-03-23 09:51:16 -04:00
|
|
|
_, err = b.Write([]byte("\n" + TOMLDelimUnix))
|
2014-05-01 13:19:51 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
2016-03-23 09:51:16 -04:00
|
|
|
case rune(JSONLead[0]):
|
2014-05-01 13:19:51 -04:00
|
|
|
by, err := json.MarshalIndent(in, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
b.Write(by)
|
|
|
|
_, err = b.Write([]byte("\n"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return b.Bytes(), nil
|
|
|
|
default:
|
2016-11-18 16:54:57 -05:00
|
|
|
return nil, errors.New("Unsupported Format provided")
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-02 01:01:44 -04:00
|
|
|
func FormatToLeadRune(kind string) rune {
|
2014-05-08 18:30:11 -04:00
|
|
|
switch FormatSanitize(kind) {
|
2014-05-02 01:01:44 -04:00
|
|
|
case "yaml":
|
2016-03-23 09:51:16 -04:00
|
|
|
return rune([]byte(YAMLLead)[0])
|
2014-05-02 01:01:44 -04:00
|
|
|
case "json":
|
2016-03-23 09:51:16 -04:00
|
|
|
return rune([]byte(JSONLead)[0])
|
2014-05-02 01:01:44 -04:00
|
|
|
default:
|
2016-03-23 09:51:16 -04:00
|
|
|
return rune([]byte(TOMLLead)[0])
|
2014-05-02 01:01:44 -04:00
|
|
|
}
|
2014-05-08 18:30:11 -04:00
|
|
|
}
|
2014-05-02 01:01:44 -04:00
|
|
|
|
2016-03-14 12:52:11 -04:00
|
|
|
// TODO(bep) move to helpers
|
2014-05-08 18:30:11 -04:00
|
|
|
func FormatSanitize(kind string) string {
|
|
|
|
switch strings.ToLower(kind) {
|
|
|
|
case "yaml", "yml":
|
|
|
|
return "yaml"
|
|
|
|
case "toml", "tml":
|
|
|
|
return "toml"
|
|
|
|
case "json", "js":
|
|
|
|
return "json"
|
|
|
|
default:
|
|
|
|
return "toml"
|
|
|
|
}
|
2014-05-02 01:01:44 -04:00
|
|
|
}
|
|
|
|
|
2016-07-13 17:53:06 -04:00
|
|
|
// DetectFrontMatter detects the type of frontmatter analysing its first character.
|
2016-04-26 16:21:15 -04:00
|
|
|
func DetectFrontMatter(mark rune) (f *frontmatterType) {
|
2014-05-01 13:19:51 -04:00
|
|
|
switch mark {
|
|
|
|
case '-':
|
2016-03-23 09:51:16 -04:00
|
|
|
return &frontmatterType{[]byte(YAMLDelim), []byte(YAMLDelim), HandleYAMLMetaData, false}
|
2014-05-01 13:19:51 -04:00
|
|
|
case '+':
|
2016-03-23 09:51:16 -04:00
|
|
|
return &frontmatterType{[]byte(TOMLDelim), []byte(TOMLDelim), HandleTOMLMetaData, false}
|
2014-05-01 13:19:51 -04:00
|
|
|
case '{':
|
2016-03-14 12:52:11 -04:00
|
|
|
return &frontmatterType{[]byte{'{'}, []byte{'}'}, HandleJSONMetaData, true}
|
2014-05-01 13:19:51 -04:00
|
|
|
default:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func HandleTOMLMetaData(datum []byte) (interface{}, error) {
|
2014-05-01 13:19:51 -04:00
|
|
|
m := map[string]interface{}{}
|
2015-03-11 13:34:57 -04:00
|
|
|
datum = removeTOMLIdentifier(datum)
|
2016-08-20 15:28:38 -04:00
|
|
|
|
|
|
|
tree, err := toml.Load(string(datum))
|
|
|
|
|
|
|
|
if err != nil {
|
2014-05-01 13:19:51 -04:00
|
|
|
return m, err
|
|
|
|
}
|
2016-08-20 15:28:38 -04:00
|
|
|
|
|
|
|
m = tree.ToMap()
|
|
|
|
|
2014-05-01 13:19:51 -04:00
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func removeTOMLIdentifier(datum []byte) []byte {
|
2016-03-23 09:51:16 -04:00
|
|
|
return bytes.Replace(datum, []byte(TOMLDelim), []byte(""), -1)
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func HandleYAMLMetaData(datum []byte) (interface{}, error) {
|
2014-05-01 13:19:51 -04:00
|
|
|
m := map[string]interface{}{}
|
2016-08-30 13:29:48 -04:00
|
|
|
err := yaml.Unmarshal(datum, &m)
|
|
|
|
return m, err
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
|
2015-03-11 13:34:57 -04:00
|
|
|
func HandleJSONMetaData(datum []byte) (interface{}, error) {
|
2014-05-01 13:19:51 -04:00
|
|
|
var f interface{}
|
2016-08-30 13:29:48 -04:00
|
|
|
err := json.Unmarshal(datum, &f)
|
|
|
|
return f, err
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|