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 (
|
|
|
|
"encoding/json"
|
2016-11-18 16:54:57 -05:00
|
|
|
"errors"
|
2016-12-26 16:23:20 -05:00
|
|
|
"io"
|
2014-05-01 13:19:51 -04:00
|
|
|
|
2018-10-20 05:16:18 -04:00
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
2018-02-12 11:39:11 -05:00
|
|
|
|
2017-05-31 06:10:46 -04:00
|
|
|
"github.com/BurntSushi/toml"
|
2016-08-20 15:28:38 -04:00
|
|
|
|
2019-03-24 05:11:16 -04:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2014-05-01 13:19:51 -04:00
|
|
|
)
|
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
const (
|
|
|
|
yamlDelimLf = "---\n"
|
|
|
|
tomlDelimLf = "+++\n"
|
|
|
|
)
|
2014-05-08 18:30:11 -04:00
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
func InterfaceToConfig(in interface{}, format metadecoders.Format, w io.Writer) error {
|
2018-10-20 05:16:18 -04:00
|
|
|
if in == nil {
|
|
|
|
return errors.New("input was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch format {
|
|
|
|
case metadecoders.YAML:
|
|
|
|
b, err := yaml.Marshal(in)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(b)
|
|
|
|
return err
|
|
|
|
|
|
|
|
case metadecoders.TOML:
|
|
|
|
return toml.NewEncoder(w).Encode(in)
|
|
|
|
case metadecoders.JSON:
|
|
|
|
b, err := json.MarshalIndent(in, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write(b)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = w.Write([]byte{'\n'})
|
|
|
|
return err
|
|
|
|
|
|
|
|
default:
|
2019-03-24 05:11:16 -04:00
|
|
|
return errors.New("unsupported Format provided")
|
2018-10-20 05:16:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
func InterfaceToFrontMatter(in interface{}, format metadecoders.Format, w io.Writer) error {
|
2018-10-20 05:16:18 -04:00
|
|
|
if in == nil {
|
|
|
|
return errors.New("input was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch format {
|
|
|
|
case metadecoders.YAML:
|
2018-10-20 11:38:49 -04:00
|
|
|
_, err := w.Write([]byte(yamlDelimLf))
|
2018-10-20 05:16:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
err = InterfaceToConfig(in, format, w)
|
2018-10-20 05:16:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
_, err = w.Write([]byte(yamlDelimLf))
|
2018-10-20 05:16:18 -04:00
|
|
|
return err
|
|
|
|
|
|
|
|
case metadecoders.TOML:
|
2018-10-20 11:38:49 -04:00
|
|
|
_, err := w.Write([]byte(tomlDelimLf))
|
2018-10-20 05:16:18 -04:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
err = InterfaceToConfig(in, format, w)
|
2018-10-20 05:16:18 -04:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 11:38:49 -04:00
|
|
|
_, err = w.Write([]byte("\n" + tomlDelimLf))
|
2018-10-20 05:16:18 -04:00
|
|
|
return err
|
|
|
|
|
|
|
|
default:
|
2018-10-20 11:38:49 -04:00
|
|
|
return InterfaceToConfig(in, format, w)
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
}
|