Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
// Copyright 2019 The Hugo Authors. All rights reserved.
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// 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 maps
|
|
|
|
|
|
|
|
import (
|
2021-06-09 04:58:18 -04:00
|
|
|
"fmt"
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/spf13/cast"
|
|
|
|
)
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
// Params is a map where all keys are lower case.
|
2022-03-17 17:03:27 -04:00
|
|
|
type Params map[string]any
|
2019-11-21 15:59:38 -05:00
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// KeyParams is an utility struct for the WalkParams method.
|
|
|
|
type KeyParams struct {
|
|
|
|
Key string
|
|
|
|
Params Params
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNested does a lower case and nested search in this map.
|
2019-11-21 15:59:38 -05:00
|
|
|
// It will return nil if none found.
|
2023-01-04 12:24:36 -05:00
|
|
|
// Make all of these methods internal somehow.
|
|
|
|
func (p Params) GetNested(indices ...string) any {
|
2019-11-21 15:59:38 -05:00
|
|
|
v, _, _ := getNested(p, indices)
|
|
|
|
return v
|
|
|
|
}
|
|
|
|
|
2023-05-18 05:05:56 -04:00
|
|
|
// SetParams overwrites values in dst with values in src for common or new keys.
|
2021-06-09 04:58:18 -04:00
|
|
|
// This is done recursively.
|
2023-01-04 12:24:36 -05:00
|
|
|
func SetParams(dst, src Params) {
|
|
|
|
for k, v := range src {
|
|
|
|
vv, found := dst[k]
|
2021-06-09 04:58:18 -04:00
|
|
|
if !found {
|
2023-01-04 12:24:36 -05:00
|
|
|
dst[k] = v
|
2021-06-09 04:58:18 -04:00
|
|
|
} else {
|
|
|
|
switch vvv := vv.(type) {
|
|
|
|
case Params:
|
|
|
|
if pv, ok := v.(Params); ok {
|
2023-01-04 12:24:36 -05:00
|
|
|
SetParams(vvv, pv)
|
2021-06-09 04:58:18 -04:00
|
|
|
} else {
|
2023-01-04 12:24:36 -05:00
|
|
|
dst[k] = v
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
default:
|
2023-01-04 12:24:36 -05:00
|
|
|
dst[k] = v
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-27 07:24:49 -04:00
|
|
|
// IsZero returns true if p is considered empty.
|
|
|
|
func (p Params) IsZero() bool {
|
|
|
|
if p == nil || len(p) == 0 {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(p) > 1 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-03-10 14:18:41 -05:00
|
|
|
for k := range p {
|
2023-05-17 10:29:06 -04:00
|
|
|
return k == MergeStrategyKey
|
2021-06-27 07:24:49 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// MergeParamsWithStrategy transfers values from src to dst for new keys using the merge strategy given.
|
2021-06-09 04:58:18 -04:00
|
|
|
// This is done recursively.
|
2023-01-04 12:24:36 -05:00
|
|
|
func MergeParamsWithStrategy(strategy string, dst, src Params) {
|
|
|
|
dst.merge(ParamsMergeStrategy(strategy), src)
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
|
2023-05-18 05:05:56 -04:00
|
|
|
// MergeParams transfers values from src to dst for new keys using the merge encoded in dst.
|
2021-08-22 05:57:37 -04:00
|
|
|
// This is done recursively.
|
2023-01-04 12:24:36 -05:00
|
|
|
func MergeParams(dst, src Params) {
|
|
|
|
ms, _ := dst.GetMergeStrategy()
|
|
|
|
dst.merge(ms, src)
|
2021-08-22 05:57:37 -04:00
|
|
|
}
|
|
|
|
|
2021-06-09 04:58:18 -04:00
|
|
|
func (p Params) merge(ps ParamsMergeStrategy, pp Params) {
|
|
|
|
ns, found := p.GetMergeStrategy()
|
|
|
|
|
|
|
|
var ms = ns
|
|
|
|
if !found && ps != "" {
|
|
|
|
ms = ps
|
|
|
|
}
|
|
|
|
|
|
|
|
noUpdate := ms == ParamsMergeStrategyNone
|
|
|
|
noUpdate = noUpdate || (ps != "" && ps == ParamsMergeStrategyShallow)
|
|
|
|
|
|
|
|
for k, v := range pp {
|
|
|
|
|
2023-05-17 10:29:06 -04:00
|
|
|
if k == MergeStrategyKey {
|
2021-06-09 04:58:18 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
vv, found := p[k]
|
|
|
|
|
|
|
|
if found {
|
|
|
|
// Key matches, if both sides are Params, we try to merge.
|
|
|
|
if vvv, ok := vv.(Params); ok {
|
|
|
|
if pv, ok := v.(Params); ok {
|
|
|
|
vvv.merge(ms, pv)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if !noUpdate {
|
|
|
|
p[k] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// For internal use.
|
2021-06-09 04:58:18 -04:00
|
|
|
func (p Params) GetMergeStrategy() (ParamsMergeStrategy, bool) {
|
2023-05-17 10:29:06 -04:00
|
|
|
if v, found := p[MergeStrategyKey]; found {
|
2021-06-09 04:58:18 -04:00
|
|
|
if s, ok := v.(ParamsMergeStrategy); ok {
|
|
|
|
return s, true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ParamsMergeStrategyShallow, false
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// For internal use.
|
2021-06-09 04:58:18 -04:00
|
|
|
func (p Params) DeleteMergeStrategy() bool {
|
2023-05-17 10:29:06 -04:00
|
|
|
if _, found := p[MergeStrategyKey]; found {
|
|
|
|
delete(p, MergeStrategyKey)
|
2021-06-09 04:58:18 -04:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// For internal use.
|
|
|
|
func (p Params) SetMergeStrategy(s ParamsMergeStrategy) {
|
2021-06-09 04:58:18 -04:00
|
|
|
switch s {
|
|
|
|
case ParamsMergeStrategyDeep, ParamsMergeStrategyNone, ParamsMergeStrategyShallow:
|
|
|
|
default:
|
|
|
|
panic(fmt.Sprintf("invalid merge strategy %q", s))
|
|
|
|
}
|
2023-05-17 10:29:06 -04:00
|
|
|
p[MergeStrategyKey] = s
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func getNested(m map[string]any, indices []string) (any, string, map[string]any) {
|
2019-11-21 15:59:38 -05:00
|
|
|
if len(indices) == 0 {
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
|
|
|
|
first := indices[0]
|
|
|
|
v, found := m[strings.ToLower(cast.ToString(first))]
|
|
|
|
if !found {
|
2020-10-29 11:22:35 -04:00
|
|
|
if len(indices) == 1 {
|
|
|
|
return nil, first, m
|
|
|
|
}
|
2019-11-21 15:59:38 -05:00
|
|
|
return nil, "", nil
|
2020-10-29 11:22:35 -04:00
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(indices) == 1 {
|
|
|
|
return v, first, m
|
|
|
|
}
|
|
|
|
|
|
|
|
switch m2 := v.(type) {
|
|
|
|
case Params:
|
|
|
|
return getNested(m2, indices[1:])
|
2022-03-17 17:03:27 -04:00
|
|
|
case map[string]any:
|
2019-11-21 15:59:38 -05:00
|
|
|
return getNested(m2, indices[1:])
|
|
|
|
default:
|
|
|
|
return nil, "", nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
// GetNestedParam gets the first match of the keyStr in the candidates given.
|
|
|
|
// It will first try the exact match and then try to find it as a nested map value,
|
|
|
|
// using the given separator, e.g. "mymap.name".
|
|
|
|
// It assumes that all the maps given have lower cased keys.
|
2022-03-17 17:03:27 -04:00
|
|
|
func GetNestedParam(keyStr, separator string, candidates ...Params) (any, error) {
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
keyStr = strings.ToLower(keyStr)
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
// Try exact match first
|
|
|
|
for _, m := range candidates {
|
|
|
|
if v, ok := m[keyStr]; ok {
|
|
|
|
return v, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
keySegments := strings.Split(keyStr, separator)
|
2019-11-21 15:59:38 -05:00
|
|
|
for _, m := range candidates {
|
2023-01-04 12:24:36 -05:00
|
|
|
if v := m.GetNested(keySegments...); v != nil {
|
2019-11-21 15:59:38 -05:00
|
|
|
return v, nil
|
|
|
|
}
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
return nil, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func GetNestedParamFn(keyStr, separator string, lookupFn func(key string) any) (any, string, map[string]any, error) {
|
2020-11-24 08:11:42 -05:00
|
|
|
keySegments := strings.Split(keyStr, separator)
|
2019-11-21 15:59:38 -05:00
|
|
|
if len(keySegments) == 0 {
|
|
|
|
return nil, "", nil, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
first := lookupFn(keySegments[0])
|
|
|
|
if first == nil {
|
2019-07-28 06:31:32 -04:00
|
|
|
return nil, "", nil, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
if len(keySegments) == 1 {
|
|
|
|
return first, keySegments[0], nil, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
switch m := first.(type) {
|
2022-03-17 17:03:27 -04:00
|
|
|
case map[string]any:
|
2019-11-21 15:59:38 -05:00
|
|
|
v, key, owner := getNested(m, keySegments[1:])
|
|
|
|
return v, key, owner, nil
|
|
|
|
case Params:
|
|
|
|
v, key, owner := getNested(m, keySegments[1:])
|
|
|
|
return v, key, owner, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
|
|
|
|
2019-11-21 15:59:38 -05:00
|
|
|
return nil, "", nil, nil
|
Add Hugo Modules
This commit implements Hugo Modules.
This is a broad subject, but some keywords include:
* A new `module` configuration section where you can import almost anything. You can configure both your own file mounts nd the file mounts of the modules you import. This is the new recommended way of configuring what you earlier put in `configDir`, `staticDir` etc. And it also allows you to mount folders in non-Hugo-projects, e.g. the `SCSS` folder in the Bootstrap GitHub project.
* A module consists of a set of mounts to the standard 7 component types in Hugo: `static`, `content`, `layouts`, `data`, `assets`, `i18n`, and `archetypes`. Yes, Theme Components can now include content, which should be very useful, especially in bigger multilingual projects.
* Modules not in your local file cache will be downloaded automatically and even "hot replaced" while the server is running.
* Hugo Modules supports and encourages semver versioned modules, and uses the minimal version selection algorithm to resolve versions.
* A new set of CLI commands are provided to manage all of this: `hugo mod init`, `hugo mod get`, `hugo mod graph`, `hugo mod tidy`, and `hugo mod vendor`.
All of the above is backed by Go Modules.
Fixes #5973
Fixes #5996
Fixes #6010
Fixes #5911
Fixes #5940
Fixes #6074
Fixes #6082
Fixes #6092
2019-05-03 03:16:58 -04:00
|
|
|
}
|
2021-06-09 04:58:18 -04:00
|
|
|
|
|
|
|
// ParamsMergeStrategy tells what strategy to use in Params.Merge.
|
|
|
|
type ParamsMergeStrategy string
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Do not merge.
|
|
|
|
ParamsMergeStrategyNone ParamsMergeStrategy = "none"
|
|
|
|
// Only add new keys.
|
|
|
|
ParamsMergeStrategyShallow ParamsMergeStrategy = "shallow"
|
|
|
|
// Add new keys, merge existing.
|
|
|
|
ParamsMergeStrategyDeep ParamsMergeStrategy = "deep"
|
|
|
|
|
2023-05-17 10:29:06 -04:00
|
|
|
MergeStrategyKey = "_merge"
|
2021-06-09 04:58:18 -04:00
|
|
|
)
|
|
|
|
|
2023-01-04 12:24:36 -05:00
|
|
|
// CleanConfigStringMapString removes any processing instructions from m,
|
|
|
|
// m will never be modified.
|
|
|
|
func CleanConfigStringMapString(m map[string]string) map[string]string {
|
|
|
|
if m == nil || len(m) == 0 {
|
|
|
|
return m
|
|
|
|
}
|
2023-05-17 10:29:06 -04:00
|
|
|
if _, found := m[MergeStrategyKey]; !found {
|
2023-01-04 12:24:36 -05:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
// Create a new map and copy all the keys except the merge strategy key.
|
|
|
|
m2 := make(map[string]string, len(m)-1)
|
|
|
|
for k, v := range m {
|
2023-05-17 10:29:06 -04:00
|
|
|
if k != MergeStrategyKey {
|
2023-01-04 12:24:36 -05:00
|
|
|
m2[k] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return m2
|
|
|
|
}
|
|
|
|
|
|
|
|
// CleanConfigStringMap is the same as CleanConfigStringMapString but for
|
|
|
|
// map[string]any.
|
|
|
|
func CleanConfigStringMap(m map[string]any) map[string]any {
|
|
|
|
if m == nil || len(m) == 0 {
|
|
|
|
return m
|
|
|
|
}
|
2023-05-17 10:29:06 -04:00
|
|
|
if _, found := m[MergeStrategyKey]; !found {
|
2023-01-04 12:24:36 -05:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
// Create a new map and copy all the keys except the merge strategy key.
|
|
|
|
m2 := make(map[string]any, len(m)-1)
|
|
|
|
for k, v := range m {
|
2023-05-17 10:29:06 -04:00
|
|
|
if k != MergeStrategyKey {
|
2023-01-04 12:24:36 -05:00
|
|
|
m2[k] = v
|
|
|
|
}
|
|
|
|
switch v2 := v.(type) {
|
|
|
|
case map[string]any:
|
|
|
|
m2[k] = CleanConfigStringMap(v2)
|
|
|
|
case Params:
|
|
|
|
var p Params = CleanConfigStringMap(v2)
|
|
|
|
m2[k] = p
|
|
|
|
case map[string]string:
|
|
|
|
m2[k] = CleanConfigStringMapString(v2)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
return m2
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-03-17 17:03:27 -04:00
|
|
|
func toMergeStrategy(v any) ParamsMergeStrategy {
|
2021-06-09 04:58:18 -04:00
|
|
|
s := ParamsMergeStrategy(cast.ToString(v))
|
|
|
|
switch s {
|
|
|
|
case ParamsMergeStrategyDeep, ParamsMergeStrategyNone, ParamsMergeStrategyShallow:
|
|
|
|
return s
|
|
|
|
default:
|
|
|
|
return ParamsMergeStrategyDeep
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PrepareParams
|
|
|
|
// * makes all the keys in the given map lower cased and will do so
|
|
|
|
// * This will modify the map given.
|
2021-06-22 03:53:37 -04:00
|
|
|
// * Any nested map[interface{}]interface{}, map[string]interface{},map[string]string will be converted to Params.
|
2021-06-09 04:58:18 -04:00
|
|
|
// * Any _merge value will be converted to proper type and value.
|
|
|
|
func PrepareParams(m Params) {
|
|
|
|
for k, v := range m {
|
|
|
|
var retyped bool
|
|
|
|
lKey := strings.ToLower(k)
|
2023-05-17 10:29:06 -04:00
|
|
|
if lKey == MergeStrategyKey {
|
2021-06-09 04:58:18 -04:00
|
|
|
v = toMergeStrategy(v)
|
|
|
|
retyped = true
|
|
|
|
} else {
|
2021-06-22 03:53:37 -04:00
|
|
|
switch vv := v.(type) {
|
2022-03-17 17:03:27 -04:00
|
|
|
case map[any]any:
|
2021-06-09 04:58:18 -04:00
|
|
|
var p Params = cast.ToStringMap(v)
|
|
|
|
v = p
|
|
|
|
PrepareParams(p)
|
|
|
|
retyped = true
|
2022-03-17 17:03:27 -04:00
|
|
|
case map[string]any:
|
|
|
|
var p Params = v.(map[string]any)
|
2021-06-09 04:58:18 -04:00
|
|
|
v = p
|
|
|
|
PrepareParams(p)
|
|
|
|
retyped = true
|
2021-06-22 03:53:37 -04:00
|
|
|
case map[string]string:
|
|
|
|
p := make(Params)
|
|
|
|
for k, v := range vv {
|
|
|
|
p[k] = v
|
|
|
|
}
|
|
|
|
v = p
|
|
|
|
PrepareParams(p)
|
|
|
|
retyped = true
|
2021-06-09 04:58:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if retyped || k != lKey {
|
|
|
|
delete(m, k)
|
|
|
|
m[lKey] = v
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|