2017-01-10 04:55:03 -05:00
|
|
|
package deps
|
|
|
|
|
|
|
|
import (
|
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2017-06-13 12:42:45 -04:00
|
|
|
"github.com/gohugoio/hugo/config"
|
|
|
|
"github.com/gohugoio/hugo/helpers"
|
|
|
|
"github.com/gohugoio/hugo/hugofs"
|
|
|
|
"github.com/gohugoio/hugo/output"
|
|
|
|
"github.com/gohugoio/hugo/tpl"
|
2017-01-10 04:55:03 -05:00
|
|
|
jww "github.com/spf13/jwalterweatherman"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Deps holds dependencies used by many.
|
|
|
|
// There will be normally be 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:"-"`
|
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
// The templates to use. This will usually implement the full tpl.TemplateHandler.
|
|
|
|
Tmpl tpl.TemplateFinder `json:"-"`
|
2017-01-10 04:55:03 -05:00
|
|
|
|
|
|
|
// The file systems to use.
|
|
|
|
Fs *hugofs.Fs `json:"-"`
|
|
|
|
|
|
|
|
// The PathSpec to use
|
|
|
|
*helpers.PathSpec `json:"-"`
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
// The ContentSpec to use
|
|
|
|
*helpers.ContentSpec `json:"-"`
|
|
|
|
|
|
|
|
// The configuration to use
|
|
|
|
Cfg config.Provider `json:"-"`
|
|
|
|
|
|
|
|
// The translation func to use
|
|
|
|
Translate func(translationID string, args ...interface{}) string `json:"-"`
|
|
|
|
|
|
|
|
Language *helpers.Language
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-04-04 08:10:20 -04:00
|
|
|
// All the output formats available for the current site.
|
|
|
|
OutputFormatsConfig output.Formats
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
templateProvider ResourceProvider
|
2017-03-27 14:43:49 -04:00
|
|
|
WithTemplate func(templ tpl.TemplateHandler) error `json:"-"`
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
translationProvider ResourceProvider
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
|
|
|
|
2017-03-09 08:18:12 -05:00
|
|
|
// ResourceProvider is used to create and refresh, and clone resources needed.
|
2017-02-04 22:20:06 -05:00
|
|
|
type ResourceProvider interface {
|
2017-01-10 04:55:03 -05:00
|
|
|
Update(deps *Deps) error
|
|
|
|
Clone(deps *Deps) error
|
|
|
|
}
|
|
|
|
|
2017-04-30 15:52:56 -04:00
|
|
|
func (d *Deps) TemplateHandler() tpl.TemplateHandler {
|
|
|
|
return d.Tmpl.(tpl.TemplateHandler)
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
if err := d.templateProvider.Update(d); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-30 15:52:56 -04:00
|
|
|
|
|
|
|
if th, ok := d.Tmpl.(tpl.TemplateHandler); ok {
|
|
|
|
th.PrintErrors()
|
|
|
|
}
|
2017-02-04 22:20:06 -05:00
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
func New(cfg DepsCfg) (*Deps, error) {
|
2017-01-10 04:55:03 -05:00
|
|
|
var (
|
|
|
|
logger = cfg.Logger
|
|
|
|
fs = cfg.Fs
|
|
|
|
)
|
|
|
|
|
|
|
|
if cfg.TemplateProvider == nil {
|
|
|
|
panic("Must have a TemplateProvider")
|
|
|
|
}
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
if cfg.TranslationProvider == nil {
|
|
|
|
panic("Must have a TranslationProvider")
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
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 {
|
2017-02-04 22:20:06 -05:00
|
|
|
// Default to the production file system.
|
|
|
|
fs = hugofs.NewDefault(cfg.Language)
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
ps, err := helpers.NewPathSpec(fs, cfg.Language)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
d := &Deps{
|
2017-02-04 22:20:06 -05:00
|
|
|
Fs: fs,
|
|
|
|
Log: logger,
|
|
|
|
templateProvider: cfg.TemplateProvider,
|
|
|
|
translationProvider: cfg.TranslationProvider,
|
|
|
|
WithTemplate: cfg.WithTemplate,
|
2017-03-25 09:37:04 -04:00
|
|
|
PathSpec: ps,
|
2017-02-04 22:20:06 -05:00
|
|
|
ContentSpec: helpers.NewContentSpec(cfg.Language),
|
|
|
|
Cfg: cfg.Language,
|
|
|
|
Language: cfg.Language,
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
|
|
|
|
2017-03-25 09:37:04 -04:00
|
|
|
return d, nil
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// ForLanguage creates a copy of the Deps with the language dependent
|
|
|
|
// parts switched out.
|
|
|
|
func (d Deps) ForLanguage(l *helpers.Language) (*Deps, error) {
|
2017-03-25 09:37:04 -04:00
|
|
|
var err error
|
|
|
|
|
|
|
|
d.PathSpec, err = helpers.NewPathSpec(d.Fs, l)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-10 04:55:03 -05:00
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
d.ContentSpec = helpers.NewContentSpec(l)
|
|
|
|
d.Cfg = l
|
|
|
|
d.Language = l
|
|
|
|
|
|
|
|
if err := d.translationProvider.Clone(&d); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
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 *helpers.Language
|
|
|
|
|
2017-02-04 22:20:06 -05:00
|
|
|
// The configuration to use.
|
|
|
|
Cfg config.Provider
|
|
|
|
|
2017-01-10 04:55:03 -05:00
|
|
|
// Template handling.
|
2017-02-04 22:20:06 -05:00
|
|
|
TemplateProvider ResourceProvider
|
2017-03-27 14:43:49 -04:00
|
|
|
WithTemplate func(templ tpl.TemplateHandler) error
|
2017-02-04 22:20:06 -05:00
|
|
|
|
|
|
|
// i18n handling.
|
|
|
|
TranslationProvider ResourceProvider
|
2017-01-10 04:55:03 -05:00
|
|
|
}
|