2015-12-07 18:57:01 +00:00
|
|
|
// Copyright 2015 The Hugo Authors. All rights reserved.
|
2014-05-01 17:19:51 +00:00
|
|
|
//
|
2015-11-24 03:16:36 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
2014-05-01 17:19:51 +00:00
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-11-24 03:16:36 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2014-05-01 17:19:51 +00: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 21:54:57 +00:00
|
|
|
"errors"
|
2016-12-26 21:23:20 +00:00
|
|
|
"io"
|
2014-05-01 17:19:51 +00:00
|
|
|
|
2018-10-20 09:16:18 +00:00
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
2018-02-12 16:39:11 +00:00
|
|
|
|
2021-07-27 17:07:10 +00:00
|
|
|
toml "github.com/pelletier/go-toml/v2"
|
2016-08-20 19:28:38 +00:00
|
|
|
|
2019-03-24 09:11:16 +00:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2021-12-02 16:30:36 +00:00
|
|
|
|
|
|
|
xml "github.com/clbanning/mxj/v2"
|
2014-05-01 17:19:51 +00:00
|
|
|
)
|
|
|
|
|
2018-10-20 15:38:49 +00:00
|
|
|
const (
|
|
|
|
yamlDelimLf = "---\n"
|
|
|
|
tomlDelimLf = "+++\n"
|
|
|
|
)
|
2014-05-08 22:30:11 +00:00
|
|
|
|
2022-03-17 21:03:27 +00:00
|
|
|
func InterfaceToConfig(in any, format metadecoders.Format, w io.Writer) error {
|
2018-10-20 09:16:18 +00: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:
|
2021-08-04 09:33:30 +00:00
|
|
|
enc := toml.NewEncoder(w)
|
|
|
|
enc.SetIndentTables(true)
|
|
|
|
return enc.Encode(in)
|
2018-10-20 09:16:18 +00:00
|
|
|
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
|
2021-12-02 16:30:36 +00:00
|
|
|
case metadecoders.XML:
|
|
|
|
b, err := xml.AnyXmlIndent(in, "", "\t", "root")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-20 09:16:18 +00:00
|
|
|
|
2021-12-02 16:30:36 +00:00
|
|
|
_, err = w.Write(b)
|
|
|
|
return err
|
2018-10-20 09:16:18 +00:00
|
|
|
default:
|
2019-03-24 09:11:16 +00:00
|
|
|
return errors.New("unsupported Format provided")
|
2018-10-20 09:16:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 21:03:27 +00:00
|
|
|
func InterfaceToFrontMatter(in any, format metadecoders.Format, w io.Writer) error {
|
2018-10-20 09:16:18 +00:00
|
|
|
if in == nil {
|
|
|
|
return errors.New("input was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch format {
|
|
|
|
case metadecoders.YAML:
|
2018-10-20 15:38:49 +00:00
|
|
|
_, err := w.Write([]byte(yamlDelimLf))
|
2018-10-20 09:16:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:38:49 +00:00
|
|
|
err = InterfaceToConfig(in, format, w)
|
2018-10-20 09:16:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:38:49 +00:00
|
|
|
_, err = w.Write([]byte(yamlDelimLf))
|
2018-10-20 09:16:18 +00:00
|
|
|
return err
|
|
|
|
|
|
|
|
case metadecoders.TOML:
|
2018-10-20 15:38:49 +00:00
|
|
|
_, err := w.Write([]byte(tomlDelimLf))
|
2018-10-20 09:16:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:38:49 +00:00
|
|
|
err = InterfaceToConfig(in, format, w)
|
2018-10-20 09:16:18 +00:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 15:38:49 +00:00
|
|
|
_, err = w.Write([]byte("\n" + tomlDelimLf))
|
2018-10-20 09:16:18 +00:00
|
|
|
return err
|
|
|
|
|
|
|
|
default:
|
2018-10-20 15:38:49 +00:00
|
|
|
return InterfaceToConfig(in, format, w)
|
2014-05-01 17:19:51 +00:00
|
|
|
}
|
|
|
|
}
|