mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-26 22:04:32 +02:00
@@ -2,9 +2,10 @@ package transform
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"errors"
|
||||
"strings"
|
||||
|
||||
"github.com/pkg/errors"
|
||||
|
||||
"github.com/gohugoio/hugo/parser"
|
||||
"github.com/gohugoio/hugo/parser/metadecoders"
|
||||
"github.com/spf13/cast"
|
||||
@@ -34,9 +35,9 @@ func (ns *Namespace) Remarshal(format string, data interface{}) (string, error)
|
||||
return "", err
|
||||
}
|
||||
|
||||
fromFormat, err := detectFormat(from)
|
||||
if err != nil {
|
||||
return "", err
|
||||
fromFormat := metadecoders.FormatFromContentString(from)
|
||||
if fromFormat == "" {
|
||||
return "", errors.New("failed to detect format from content")
|
||||
}
|
||||
|
||||
meta, err := metadecoders.UnmarshalToMap([]byte(from), fromFormat)
|
||||
@@ -56,24 +57,3 @@ func toFormatMark(format string) (metadecoders.Format, error) {
|
||||
|
||||
return "", errors.New("failed to detect target data serialization format")
|
||||
}
|
||||
|
||||
func detectFormat(data string) (metadecoders.Format, error) {
|
||||
jsonIdx := strings.Index(data, "{")
|
||||
yamlIdx := strings.Index(data, ":")
|
||||
tomlIdx := strings.Index(data, "=")
|
||||
|
||||
if jsonIdx != -1 && (yamlIdx == -1 || jsonIdx < yamlIdx) && (tomlIdx == -1 || jsonIdx < tomlIdx) {
|
||||
return metadecoders.JSON, nil
|
||||
}
|
||||
|
||||
if yamlIdx != -1 && (tomlIdx == -1 || yamlIdx < tomlIdx) {
|
||||
return metadecoders.YAML, nil
|
||||
}
|
||||
|
||||
if tomlIdx != -1 {
|
||||
return metadecoders.TOML, nil
|
||||
}
|
||||
|
||||
return "", errors.New("failed to detect data serialization format")
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user