hugo/parser/frontmatter.go

193 lines
4.2 KiB
Go
Raw Normal View History

// Copyright 2015 The Hugo Authors. All rights reserved.
//
2015-11-23 22:16:36 -05:00
// Licensed under the Apache License, Version 2.0 (the "License");
// 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
//
// 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"
"fmt"
"strings"
"github.com/BurntSushi/toml"
"gopkg.in/yaml.v2"
)
2016-03-14 12:52:11 -04:00
type frontmatterType struct {
markstart, markend []byte
Parse func([]byte) (interface{}, error)
includeMark bool
}
func InterfaceToConfig(in interface{}, mark rune) ([]byte, error) {
if in == nil {
return []byte{}, fmt.Errorf("input was nil")
}
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)
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]):
err := toml.NewEncoder(b).Encode(in)
if err != nil {
return nil, err
}
return b.Bytes(), nil
2016-03-23 09:51:16 -04:00
case rune(JSONLead[0]):
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:
return nil, fmt.Errorf("Unsupported Format provided")
}
}
func InterfaceToFrontMatter(in interface{}, mark rune) ([]byte, error) {
if in == nil {
return []byte{}, fmt.Errorf("input was nil")
}
b := new(bytes.Buffer)
switch mark {
2016-03-23 09:51:16 -04:00
case rune(YAMLLead[0]):
_, err := b.Write([]byte(YAMLDelimUnix))
if err != nil {
return nil, err
}
2014-08-17 21:36:24 -04:00
by, err := yaml.Marshal(in)
if err != nil {
return nil, err
}
b.Write(by)
2016-03-23 09:51:16 -04:00
_, err = b.Write([]byte(YAMLDelimUnix))
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))
if err != nil {
return nil, err
}
err = toml.NewEncoder(b).Encode(in)
if err != nil {
return nil, err
}
2016-03-23 09:51:16 -04:00
_, err = b.Write([]byte("\n" + TOMLDelimUnix))
if err != nil {
return nil, err
}
return b.Bytes(), nil
2016-03-23 09:51:16 -04:00
case rune(JSONLead[0]):
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:
return nil, fmt.Errorf("Unsupported Format provided")
}
}
func FormatToLeadRune(kind string) rune {
switch FormatSanitize(kind) {
case "yaml":
2016-03-23 09:51:16 -04:00
return rune([]byte(YAMLLead)[0])
case "json":
2016-03-23 09:51:16 -04:00
return rune([]byte(JSONLead)[0])
default:
2016-03-23 09:51:16 -04:00
return rune([]byte(TOMLLead)[0])
}
}
2016-03-14 12:52:11 -04:00
// TODO(bep) move to helpers
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"
}
}
2016-03-14 12:52:11 -04:00
func detectFrontMatter(mark rune) (f *frontmatterType) {
switch mark {
case '-':
2016-03-23 09:51:16 -04:00
return &frontmatterType{[]byte(YAMLDelim), []byte(YAMLDelim), HandleYAMLMetaData, false}
case '+':
2016-03-23 09:51:16 -04:00
return &frontmatterType{[]byte(TOMLDelim), []byte(TOMLDelim), HandleTOMLMetaData, false}
case '{':
2016-03-14 12:52:11 -04:00
return &frontmatterType{[]byte{'{'}, []byte{'}'}, HandleJSONMetaData, true}
default:
return nil
}
}
func HandleTOMLMetaData(datum []byte) (interface{}, error) {
m := map[string]interface{}{}
datum = removeTOMLIdentifier(datum)
if _, err := toml.Decode(string(datum), &m); err != nil {
return m, err
}
return m, nil
}
func removeTOMLIdentifier(datum []byte) []byte {
2016-03-23 09:51:16 -04:00
return bytes.Replace(datum, []byte(TOMLDelim), []byte(""), -1)
}
func HandleYAMLMetaData(datum []byte) (interface{}, error) {
m := map[string]interface{}{}
2014-08-17 21:36:24 -04:00
if err := yaml.Unmarshal(datum, &m); err != nil {
return m, err
}
return m, nil
}
func HandleJSONMetaData(datum []byte) (interface{}, error) {
var f interface{}
if err := json.Unmarshal(datum, &f); err != nil {
return f, err
}
return f, nil
}