2015-12-07 19:57:01 +01: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 22:54:57 +01:00
|
|
|
"errors"
|
2016-12-26 15:23:20 -06:00
|
|
|
"io"
|
2014-05-01 13:19:51 -04:00
|
|
|
|
2018-10-20 11:16:18 +02:00
|
|
|
"github.com/gohugoio/hugo/parser/metadecoders"
|
2018-02-12 17:39:11 +01:00
|
|
|
|
2021-07-27 19:07:10 +02:00
|
|
|
toml "github.com/pelletier/go-toml/v2"
|
2016-08-20 20:28:38 +01:00
|
|
|
|
2019-03-24 10:11:16 +01:00
|
|
|
yaml "gopkg.in/yaml.v2"
|
2021-12-02 17:30:36 +01:00
|
|
|
|
|
|
|
xml "github.com/clbanning/mxj/v2"
|
2014-05-01 13:19:51 -04:00
|
|
|
)
|
|
|
|
|
2018-10-20 17:38:49 +02:00
|
|
|
const (
|
|
|
|
yamlDelimLf = "---\n"
|
|
|
|
tomlDelimLf = "+++\n"
|
|
|
|
)
|
2014-05-08 18:30:11 -04:00
|
|
|
|
2022-03-17 22:03:27 +01:00
|
|
|
func InterfaceToConfig(in any, format metadecoders.Format, w io.Writer) error {
|
2018-10-20 11:16:18 +02: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 11:33:30 +02:00
|
|
|
enc := toml.NewEncoder(w)
|
|
|
|
enc.SetIndentTables(true)
|
|
|
|
return enc.Encode(in)
|
2018-10-20 11:16:18 +02: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 17:30:36 +01:00
|
|
|
case metadecoders.XML:
|
|
|
|
b, err := xml.AnyXmlIndent(in, "", "\t", "root")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-10-20 11:16:18 +02:00
|
|
|
|
2021-12-02 17:30:36 +01:00
|
|
|
_, err = w.Write(b)
|
|
|
|
return err
|
2018-10-20 11:16:18 +02:00
|
|
|
default:
|
2019-03-24 10:11:16 +01:00
|
|
|
return errors.New("unsupported Format provided")
|
2018-10-20 11:16:18 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-17 22:03:27 +01:00
|
|
|
func InterfaceToFrontMatter(in any, format metadecoders.Format, w io.Writer) error {
|
2018-10-20 11:16:18 +02:00
|
|
|
if in == nil {
|
|
|
|
return errors.New("input was nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
switch format {
|
|
|
|
case metadecoders.YAML:
|
2018-10-20 17:38:49 +02:00
|
|
|
_, err := w.Write([]byte(yamlDelimLf))
|
2018-10-20 11:16:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 17:38:49 +02:00
|
|
|
err = InterfaceToConfig(in, format, w)
|
2018-10-20 11:16:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 17:38:49 +02:00
|
|
|
_, err = w.Write([]byte(yamlDelimLf))
|
2018-10-20 11:16:18 +02:00
|
|
|
return err
|
|
|
|
|
|
|
|
case metadecoders.TOML:
|
2018-10-20 17:38:49 +02:00
|
|
|
_, err := w.Write([]byte(tomlDelimLf))
|
2018-10-20 11:16:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2018-10-20 17:38:49 +02:00
|
|
|
err = InterfaceToConfig(in, format, w)
|
2018-10-20 11:16:18 +02:00
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-10-03 17:25:38 -07:00
|
|
|
_, err = w.Write([]byte(tomlDelimLf))
|
2018-10-20 11:16:18 +02:00
|
|
|
return err
|
|
|
|
|
|
|
|
default:
|
2018-10-20 17:38:49 +02:00
|
|
|
return InterfaceToConfig(in, format, w)
|
2014-05-01 13:19:51 -04:00
|
|
|
}
|
|
|
|
}
|