Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jan 24, 2023
1 parent eec45c5 commit 098b09e
Show file tree
Hide file tree
Showing 25 changed files with 478 additions and 134 deletions.
2 changes: 2 additions & 0 deletions cache/docs.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
// Package cache contains the differenct cache implementations.
package cache
9 changes: 5 additions & 4 deletions cache/filecache/filecache_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package filecache provides a file based cache for Hugo.
package filecache

import (
Expand Down Expand Up @@ -39,7 +40,7 @@ const (
cacheDirProject = ":cacheDir/:project"
)

var defaultCacheConfig = Config{
var defaultCacheConfig = FileCacheConfig{
MaxAge: -1, // Never expire
Dir: cacheDirProject,
}
Expand All @@ -53,7 +54,7 @@ const (
cacheKeyGetResource = "getresource"
)

type Configs map[string]Config
type Configs map[string]FileCacheConfig

func (c Configs) CacheDirModules() string {
return c[cacheKeyModules].Dir
Expand All @@ -74,13 +75,13 @@ var defaultCacheConfigs = Configs{
MaxAge: -1,
Dir: resourcesGenDir,
},
cacheKeyGetResource: Config{
cacheKeyGetResource: FileCacheConfig{
MaxAge: -1, // Never expire
Dir: cacheDirProject,
},
}

type Config struct {
type FileCacheConfig struct {
// Max age of cache entries in this cache. Any items older than this will
// be removed and not returned from the cache.
// a negative value means forever, 0 means cache is disabled.
Expand Down
149 changes: 71 additions & 78 deletions config/allconfig/allconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,26 @@ import (
"github.com/gohugoio/hugo/config/privacy"
"github.com/gohugoio/hugo/config/security"
"github.com/gohugoio/hugo/config/services"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/markup/markup_config"
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/minifiers"
"github.com/gohugoio/hugo/modules"
"github.com/gohugoio/hugo/navigation"
"github.com/gohugoio/hugo/output"
"github.com/gohugoio/hugo/related"
"github.com/gohugoio/hugo/resources/images"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/resources/page/pagemeta"
"github.com/mitchellh/mapstructure"
"github.com/spf13/afero"
)

type Config2 struct {
type Config struct {
RootConfig

// The build configuration section contains global build-related configuration options.
Build config.Build `mapstructure:"-"`
Build config.BuildConfig `mapstructure:"-"`

// The caches configuration section contains global cache-related configuration options.
// cache:filecache:configs
Expand All @@ -45,58 +50,45 @@ type Config2 struct {
Markup markup_config.Config `mapstructure:"-"`

// Media type configuration.
// TODO1 language.
MediaTypes *config.ConfigNamespace[map[string]media.DocsMediaTypeConfig, media.Types] `mapstructure:"-"`

Imaging *config.ConfigNamespace[images.Imaging, images.ImagingConfigInternal]
// Output format configuration.
// TODO1 language.
Outputformats *config.ConfigNamespace[map[string]output.Format, output.Formats] `mapstructure:"-"`

Imaging *config.ConfigNamespace[images.ImagingConfig, images.ImagingConfigInternal]

// The cascade configuration section contains global the top level front matter cascade configuration options.
Cascade []CascadeEntryConfig `mapstructure:"-"`
Cascade *config.ConfigNamespace[[]page.PageMatcherParamsConfig, map[page.PageMatcher]maps.Params] `mapstructure:"-"`

// Menu configuration.
// TODO1 Per lang.
Menus *config.ConfigNamespace[[]navigation.MenuConfig, navigation.Menus] `mapstructure:"-"`

// Language configuration.
Languages *config.ConfigNamespace[map[string]langs.LanguageConfig, langs.LanguagesConfig] `mapstructure:"-"`

// Front matter configuration.
Frontmatter pagemeta.FrontmatterConfig `mapstructure:"-"`

// Where to put the generated files.
PublishDir string

// Module configuration.
Module modules.Config `mapstructure:"-"`

// Permalink configuration.
Permalinks map[string]string

// The directory to put the generated resources files. This directory should in most situations be considered temporary
// and not be committed to version control. But there may be cached content in here that you want to keep,
// e.g. resources/_gen/images for performance reasons or CSS built from SASS when your CI server doesn't have the full setup.
ResourceDir string

// Minification configuration.
Minify minifiers.MinifyConfig `mapstructure:"-"`

// Sitemap configuration.
Sitemap config.Sitemap
Sitemap config.SitemapConfig

// Taxonomy configuration.
Taxonomies map[string]string

// Related content configuration.
Related related.Config `mapstructure:"-"`
}

// Config holds all configuration options in Hugo.
// TODO(bep) make sure that the keys gets the sensible casing in JSON etc.
type Config struct {

// Language configuration.
Languages map[string]LanguageConfig `mapstructure:"-"`

// Menu configuration.
Menus []MenuConfig `mapstructure:"-"`

// Output format configuration.
Outputformats map[string]OutputFormatConfig `mapstructure:"-"`

// Params configuration.
Params maps.Params

// Privacy configuration.
Privacy privacy.Config `mapstructure:"-"`
Expand All @@ -107,6 +99,17 @@ type Config struct {
// Services configuration.
Services services.Config `mapstructure:"-"`

// Params configuration.
// TODO1 language
Params maps.Params
}

// RootConfig holds all the top-level configuration options in Hugo
type RootConfig struct {

// The base URL of the site.
BaseURL string

// Whether to build content marked as draft.
BuildDrafts bool

Expand Down Expand Up @@ -192,58 +195,22 @@ type Config struct {
TitleCaseStyle string
}

// TODO(bep) move these.
type CascadeEntryConfig struct {
// A Glob pattern matching the content path below /content.
// Expects Unix-styled slashes. Note that this is the virtual path, so it starts at the mount root.
// The matching supports double-asterisks so you can match for patterns like /blog/*/** to match anything from the third level and
Path string

// A Glob pattern matching the Page’s Kind(s), e.g. “{home,section}”.
Kind string

// A Glob pattern matching the Page’s language, e.g. “{en,sv}”.
Lang string

// A Glob pattern matching the build environment, e.g. “{production,development}”
Environment string
}

type OutputFormatConfig struct {
BaseName string
IsPlainText bool
MediaType string
Protocol string
}

type LanguageConfig struct {
LanguageDirection string
Title string
Weight int
Params maps.Params
}

type MenuConfig struct {
Identifier string
Name string
Pre string
URL string
Weight int
}
// TODO(bep) move or remove these.

// FromProvider creates a new Config from the given Provider.
func FromProvider(cfg config.Provider) (all Config2, err error) {
func FromProvider(cfg config.Provider) (all Config, err error) {
// TODO1 per language.

cfg.Set("cacheDir", "/mycachedir")
m := cfg.Get("")
fs := afero.NewMemMapFs()

//litter.Dump(m)
if err = mapstructure.WeakDecode(m, &all); err != nil {
// First decode the top level config.
if err = mapstructure.WeakDecode(m, &all.RootConfig); err != nil {
return
}

// Then all the namespaces.
all.Imaging, err = images.DecodeConfig(cfg.GetStringMap("imaging"))
if err != nil {
return
Expand All @@ -252,7 +219,8 @@ func FromProvider(cfg config.Provider) (all Config2, err error) {
if err != nil {
return
}
all.Build = config.DecodeBuild(cfg)
all.Build = config.DecodeBuildConfig(cfg)

all.Markup, err = markup_config.Decode(cfg)
if err != nil {
return
Expand All @@ -261,6 +229,11 @@ func FromProvider(cfg config.Provider) (all Config2, err error) {
if err != nil {
return
}
all.Outputformats, err = output.DecodeConfig(all.MediaTypes.Config, cfg.Get("outputFormats"))
if err != nil {
return
}

all.Frontmatter, err = pagemeta.DecodeFrontMatterConfig(cfg)
if err != nil {
return
Expand All @@ -277,8 +250,14 @@ func FromProvider(cfg config.Provider) (all Config2, err error) {
return
}

all.Sitemap = config.DecodeSitemap(config.Sitemap{Priority: -1, Filename: "sitemap.xml"}, cfg.GetStringMap("sitemap"))
all.Sitemap = config.DecodeSitemap(config.SitemapConfig{Priority: -1, Filename: "sitemap.xml"}, cfg.GetStringMap("sitemap"))
all.Taxonomies = cfg.GetStringMapString("taxonomies")
all.Params = cfg.GetStringMap("params")

all.Languages, err = langs.DecodeConfig(cfg)
if err != nil {
return
}

// Related config
if cfg.IsSet("related") {
Expand All @@ -294,13 +273,27 @@ func FromProvider(cfg config.Provider) (all Config2, err error) {
}

// Per language (check others)
if cfg.IsSet("cascade") {
/*cascade, err = page.DecodeCascade(cfg.Get("cascade"))
if err != nil {
return
}*/
all.Cascade, err = page.DecodeCascadeConfig(cfg.Get("cascade"))
if err != nil {
return
}

all.Privacy, err = privacy.DecodeConfig(cfg)
if err != nil {
return
}

all.Security, err = security.DecodeConfig(cfg)
if err != nil {
return
}

all.Services, err = services.DecodeConfig(cfg)
if err != nil {
return
}

all.Menus, err = navigation.DecodeConfig(cfg.Get("menus"))

return
}
16 changes: 8 additions & 8 deletions config/commonConfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ import (
jww "github.com/spf13/jwalterweatherman"
)

var DefaultBuild = Build{
var DefaultBuild = BuildConfig{
UseResourceCacheWhen: "fallback",
WriteStats: false,
}

// Build holds some build related configuration.
type Build struct {
// BuildConfig holds some build related configuration.
type BuildConfig struct {
UseResourceCacheWhen string // never, fallback, always. Default is fallback

// When enabled, will collect and write a hugo_stats.json with some build
Expand All @@ -46,7 +46,7 @@ type Build struct {
NoJSConfigInAssets bool
}

func (b Build) UseResourceCache(err error) bool {
func (b BuildConfig) UseResourceCache(err error) bool {
if b.UseResourceCacheWhen == "never" {
return false
}
Expand All @@ -58,7 +58,7 @@ func (b Build) UseResourceCache(err error) bool {
return true
}

func DecodeBuild(cfg Provider) Build {
func DecodeBuildConfig(cfg Provider) BuildConfig {
m := cfg.GetStringMap("build")
b := DefaultBuild
if m == nil {
Expand All @@ -79,14 +79,14 @@ func DecodeBuild(cfg Provider) Build {
return b
}

// Sitemap configures the sitemap to be generated.
type Sitemap struct {
// SitemapConfig configures the sitemap to be generated.
type SitemapConfig struct {
ChangeFreq string
Priority float64
Filename string
}

func DecodeSitemap(prototype Sitemap, input map[string]any) Sitemap {
func DecodeSitemap(prototype SitemapConfig, input map[string]any) SitemapConfig {
for key, value := range input {
switch key {
case "changefreq":
Expand Down
4 changes: 2 additions & 2 deletions config/commonConfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ func TestBuild(t *testing.T) {
"useResourceCacheWhen": "always",
})

b := DecodeBuild(v)
b := DecodeBuildConfig(v)

c.Assert(b.UseResourceCacheWhen, qt.Equals, "always")

v.Set("build", map[string]any{
"useResourceCacheWhen": "foo",
})

b = DecodeBuild(v)
b = DecodeBuildConfig(v)

c.Assert(b.UseResourceCacheWhen, qt.Equals, "fallback")

Expand Down
Loading

0 comments on commit 098b09e

Please sign in to comment.