mirror of
https://github.com/gohugoio/hugo.git
synced 2024-12-29 20:21:25 +00:00
80230f26a3
This commit adds support for theme composition and inheritance in Hugo. With this, it helps thinking about a theme as a set of ordered components: ```toml theme = ["my-shortcodes", "base-theme", "hyde"] ``` The theme definition example above in `config.toml` creates a theme with the 3 components with presedence from left to right. So, Hugo will, for any given file, data entry etc., look first in the project, and then in `my-shortcode`, `base-theme` and lastly `hyde`. Hugo uses two different algorithms to merge the filesystems, depending on the file type: * For `i18n` and `data` files, Hugo merges deeply using the translation id and data key inside the files. * For `static`, `layouts` (templates) and `archetypes` files, these are merged on file level. So the left-most file will be chosen. The name used in the `theme` definition above must match a folder in `/your-site/themes`, e.g. `/your-site/themes/my-shortcodes`. There are plans to improve on this and get a URL scheme so this can be resolved automatically. Also note that a component that is part of a theme can have its own configuration file, e.g. `config.toml`. There are currently some restrictions to what a theme component can configure: * `params` (global and per language) * `menu` (global and per language) * `outputformats` and `mediatypes` The same rules apply here: The left-most param/menu etc. with the same ID will win. There are some hidden and experimental namespace support in the above, which we will work to improve in the future, but theme authors are encouraged to create their own namespaces to avoid naming conflicts. A final note: Themes/components can also have a `theme` definition in their `config.toml` and similar, which is the "inheritance" part of this commit's title. This is currently not supported by the Hugo theme site. We will have to wait for some "auto dependency" feature to be implemented for that to happen, but this can be a powerful feature if you want to create your own theme-variant based on others. Fixes #4460 Fixes #4450
224 lines
5.1 KiB
Go
224 lines
5.1 KiB
Go
package deps
|
|
|
|
import (
|
|
"io/ioutil"
|
|
"log"
|
|
"os"
|
|
"time"
|
|
|
|
"github.com/gohugoio/hugo/config"
|
|
"github.com/gohugoio/hugo/helpers"
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
"github.com/gohugoio/hugo/langs"
|
|
"github.com/gohugoio/hugo/metrics"
|
|
"github.com/gohugoio/hugo/output"
|
|
"github.com/gohugoio/hugo/source"
|
|
"github.com/gohugoio/hugo/tpl"
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
)
|
|
|
|
// Deps holds dependencies used by many.
|
|
// There will be normally only one instance of deps in play
|
|
// at a given time, i.e. one per Site built.
|
|
type Deps struct {
|
|
// The logger to use.
|
|
Log *jww.Notepad `json:"-"`
|
|
|
|
// Used to log errors that may repeat itself many times.
|
|
DistinctErrorLog *helpers.DistinctLogger
|
|
|
|
// The templates to use. This will usually implement the full tpl.TemplateHandler.
|
|
Tmpl tpl.TemplateFinder `json:"-"`
|
|
|
|
// The file systems to use.
|
|
Fs *hugofs.Fs `json:"-"`
|
|
|
|
// The PathSpec to use
|
|
*helpers.PathSpec `json:"-"`
|
|
|
|
// The ContentSpec to use
|
|
*helpers.ContentSpec `json:"-"`
|
|
|
|
// The SourceSpec to use
|
|
SourceSpec *source.SourceSpec `json:"-"`
|
|
|
|
// The configuration to use
|
|
Cfg config.Provider `json:"-"`
|
|
|
|
// The translation func to use
|
|
Translate func(translationID string, args ...interface{}) string `json:"-"`
|
|
|
|
Language *langs.Language
|
|
|
|
// All the output formats available for the current site.
|
|
OutputFormatsConfig output.Formats
|
|
|
|
templateProvider ResourceProvider
|
|
WithTemplate func(templ tpl.TemplateHandler) error `json:"-"`
|
|
|
|
translationProvider ResourceProvider
|
|
|
|
Metrics metrics.Provider
|
|
|
|
// Timeout is configurable in site config.
|
|
Timeout time.Duration
|
|
}
|
|
|
|
// ResourceProvider is used to create and refresh, and clone resources needed.
|
|
type ResourceProvider interface {
|
|
Update(deps *Deps) error
|
|
Clone(deps *Deps) error
|
|
}
|
|
|
|
// TemplateHandler returns the used tpl.TemplateFinder as tpl.TemplateHandler.
|
|
func (d *Deps) TemplateHandler() tpl.TemplateHandler {
|
|
return d.Tmpl.(tpl.TemplateHandler)
|
|
}
|
|
|
|
// LoadResources loads translations and templates.
|
|
func (d *Deps) LoadResources() error {
|
|
// Note that the translations need to be loaded before the templates.
|
|
if err := d.translationProvider.Update(d); err != nil {
|
|
return err
|
|
}
|
|
|
|
if err := d.templateProvider.Update(d); err != nil {
|
|
return err
|
|
}
|
|
|
|
if th, ok := d.Tmpl.(tpl.TemplateHandler); ok {
|
|
th.PrintErrors()
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// New initializes a Dep struct.
|
|
// Defaults are set for nil values,
|
|
// but TemplateProvider, TranslationProvider and Language are always required.
|
|
func New(cfg DepsCfg) (*Deps, error) {
|
|
var (
|
|
logger = cfg.Logger
|
|
fs = cfg.Fs
|
|
)
|
|
|
|
if cfg.TemplateProvider == nil {
|
|
panic("Must have a TemplateProvider")
|
|
}
|
|
|
|
if cfg.TranslationProvider == nil {
|
|
panic("Must have a TranslationProvider")
|
|
}
|
|
|
|
if cfg.Language == nil {
|
|
panic("Must have a Language")
|
|
}
|
|
|
|
if logger == nil {
|
|
logger = jww.NewNotepad(jww.LevelError, jww.LevelError, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
|
|
}
|
|
|
|
if fs == nil {
|
|
// Default to the production file system.
|
|
fs = hugofs.NewDefault(cfg.Language)
|
|
}
|
|
|
|
ps, err := helpers.NewPathSpec(fs, cfg.Language)
|
|
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
contentSpec, err := helpers.NewContentSpec(cfg.Language)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
sp := source.NewSourceSpec(ps, fs.Source)
|
|
|
|
timeoutms := cfg.Language.GetInt("timeout")
|
|
if timeoutms <= 0 {
|
|
timeoutms = 3000
|
|
}
|
|
|
|
distinctErrorLogger := helpers.NewDistinctLogger(logger.ERROR)
|
|
|
|
d := &Deps{
|
|
Fs: fs,
|
|
Log: logger,
|
|
DistinctErrorLog: distinctErrorLogger,
|
|
templateProvider: cfg.TemplateProvider,
|
|
translationProvider: cfg.TranslationProvider,
|
|
WithTemplate: cfg.WithTemplate,
|
|
PathSpec: ps,
|
|
ContentSpec: contentSpec,
|
|
SourceSpec: sp,
|
|
Cfg: cfg.Language,
|
|
Language: cfg.Language,
|
|
Timeout: time.Duration(timeoutms) * time.Millisecond,
|
|
}
|
|
|
|
if cfg.Cfg.GetBool("templateMetrics") {
|
|
d.Metrics = metrics.NewProvider(cfg.Cfg.GetBool("templateMetricsHints"))
|
|
}
|
|
|
|
return d, nil
|
|
}
|
|
|
|
// ForLanguage creates a copy of the Deps with the language dependent
|
|
// parts switched out.
|
|
func (d Deps) ForLanguage(l *langs.Language) (*Deps, error) {
|
|
var err error
|
|
|
|
d.PathSpec, err = helpers.NewPathSpecWithBaseBaseFsProvided(d.Fs, l, d.BaseFs)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
d.ContentSpec, err = helpers.NewContentSpec(l)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
d.Cfg = l
|
|
d.Language = l
|
|
|
|
if err := d.translationProvider.Clone(&d); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
if err := d.templateProvider.Clone(&d); err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &d, nil
|
|
|
|
}
|
|
|
|
// DepsCfg contains configuration options that can be used to configure Hugo
|
|
// on a global level, i.e. logging etc.
|
|
// Nil values will be given default values.
|
|
type DepsCfg struct {
|
|
|
|
// The Logger to use.
|
|
Logger *jww.Notepad
|
|
|
|
// The file systems to use
|
|
Fs *hugofs.Fs
|
|
|
|
// The language to use.
|
|
Language *langs.Language
|
|
|
|
// The configuration to use.
|
|
Cfg config.Provider
|
|
|
|
// Template handling.
|
|
TemplateProvider ResourceProvider
|
|
WithTemplate func(templ tpl.TemplateHandler) error
|
|
|
|
// i18n handling.
|
|
TranslationProvider ResourceProvider
|
|
|
|
// Whether we are in running (server) mode
|
|
Running bool
|
|
}
|