Skip to content

Commit

Permalink
Work
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jan 20, 2023
1 parent eec45c5 commit 84a44d0
Show file tree
Hide file tree
Showing 8 changed files with 378 additions and 76 deletions.
117 changes: 55 additions & 62 deletions config/allconfig/allconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,16 @@ 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"
Expand All @@ -45,12 +49,24 @@ type Config2 struct {
Markup markup_config.Config `mapstructure:"-"`

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

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

Imaging *config.ConfigNamespace[images.Imaging, 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:"-"`
Expand Down Expand Up @@ -80,23 +96,6 @@ type Config2 struct {

// 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 +106,15 @@ type Config struct {
// Services configuration.
Services services.Config `mapstructure:"-"`

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

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

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

Expand Down Expand Up @@ -192,44 +200,7 @@ 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) {
Expand All @@ -239,7 +210,6 @@ func FromProvider(cfg config.Provider) (all Config2, err error) {
m := cfg.Get("")
fs := afero.NewMemMapFs()

//litter.Dump(m)
if err = mapstructure.WeakDecode(m, &all); err != nil {
return
}
Expand All @@ -253,6 +223,7 @@ func FromProvider(cfg config.Provider) (all Config2, err error) {
return
}
all.Build = config.DecodeBuild(cfg)

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

all.Frontmatter, err = pagemeta.DecodeFrontMatterConfig(cfg)
if err != nil {
return
Expand All @@ -279,6 +252,12 @@ func FromProvider(cfg config.Provider) (all Config2, err error) {

all.Sitemap = config.DecodeSitemap(config.Sitemap{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
}
24 changes: 18 additions & 6 deletions config/namespace.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,36 @@ func DecodeNamespace[S, C any](configSource any, buildConfig func(any) (C, any,
}

ns := &ConfigNamespace[S, C]{
External: ext,
SourceHash: h,
Config: c,
SourceStructure: ext,
SourceHash: h,
Config: c,
}

return ns, nil
}

// ConfigNamespace holds a Hugo configuration namespace.
// The construct looks a little odd, but it's built to make the configuration elements
// both self-documenting and contained in a common structure.
type ConfigNamespace[S, C any] struct {
External any
// SourceStructure represents the source configuration with any defaults applied.
// This is used for documentation and printing of the configuration setup to the user.
SourceStructure any

// SourceHash is a hash of the source configuration before any defaults gets applied.
SourceHash string
Config C

// Config is the final configuration as used by Hugo.
Config C
}

// MarshalJSON marshals the source structure.
func (ns *ConfigNamespace[S, C]) MarshalJSON() ([]byte, error) {
return json.Marshal(ns.External)
return json.Marshal(ns.SourceStructure)
}

// Signature returns the signature of the source structure.
// Note that this is for documentation purposes only and SourceStructure may not always be cast to S (it's usually just a map).
func (ns *ConfigNamespace[S, C]) Signature() S {
var s S
return s
Expand Down
2 changes: 1 addition & 1 deletion config/namespace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func TestNamespace(t *testing.T) {

c.Assert(err, qt.IsNil)
c.Assert(ns, qt.Not(qt.IsNil))
c.Assert(ns.External, qt.DeepEquals, map[string]interface{}{"foo": "bar"})
c.Assert(ns.SourceStructure, qt.DeepEquals, map[string]interface{}{"foo": "bar"})
c.Assert(ns.SourceHash, qt.Equals, "14368731254619220105")
c.Assert(ns.Config, qt.DeepEquals, &tstNsExt{Foo: "bar"})
c.Assert(ns.Signature(), qt.DeepEquals, []*tstNsExt(nil))
Expand Down
Loading

0 comments on commit 84a44d0

Please sign in to comment.