mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-12 20:13:59 +02:00
Create a struct with all of Hugo's config options
Primary motivation is documentation, but it will also hopefully simplify the code. Also, * Lower case the default output format names; this is in line with the custom ones (map keys) and how it's treated all the places. This avoids doing `stringds.EqualFold` everywhere. Closes #10896 Closes #10620
This commit is contained in:
@@ -19,6 +19,8 @@ import (
|
||||
"regexp"
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
|
||||
"github.com/gohugoio/hugo/common/hreflect"
|
||||
)
|
||||
|
||||
// Regexp definitions
|
||||
@@ -57,3 +59,58 @@ func (c LowerCaseCamelJSONMarshaller) MarshalJSON() ([]byte, error) {
|
||||
|
||||
return converted, err
|
||||
}
|
||||
|
||||
type ReplacingJSONMarshaller struct {
|
||||
Value any
|
||||
|
||||
KeysToLower bool
|
||||
OmitEmpty bool
|
||||
}
|
||||
|
||||
func (c ReplacingJSONMarshaller) MarshalJSON() ([]byte, error) {
|
||||
converted, err := json.Marshal(c.Value)
|
||||
|
||||
if c.KeysToLower {
|
||||
converted = keyMatchRegex.ReplaceAllFunc(
|
||||
converted,
|
||||
func(match []byte) []byte {
|
||||
return bytes.ToLower(match)
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
if c.OmitEmpty {
|
||||
// It's tricky to do this with a regexp, so convert it to a map, remove zero values and convert back.
|
||||
var m map[string]interface{}
|
||||
err = json.Unmarshal(converted, &m)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
var removeZeroVAlues func(m map[string]any)
|
||||
removeZeroVAlues = func(m map[string]any) {
|
||||
for k, v := range m {
|
||||
if !hreflect.IsTruthful(v) {
|
||||
delete(m, k)
|
||||
} else {
|
||||
switch v.(type) {
|
||||
case map[string]interface{}:
|
||||
removeZeroVAlues(v.(map[string]any))
|
||||
case []interface{}:
|
||||
for _, vv := range v.([]interface{}) {
|
||||
if m, ok := vv.(map[string]any); ok {
|
||||
removeZeroVAlues(m)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
removeZeroVAlues(m)
|
||||
converted, err = json.Marshal(m)
|
||||
|
||||
}
|
||||
|
||||
return converted, err
|
||||
}
|
||||
|
Reference in New Issue
Block a user