From 9f5a92078a3f388b52d597b5a59af5c933a112d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Fri, 3 May 2019 09:16:58 +0200 Subject: 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 --- langs/i18n/i18n_test.go | 13 +++++++-- langs/i18n/translationProvider.go | 56 ++++++++++++++++++++++----------------- 2 files changed, 43 insertions(+), 26 deletions(-) (limited to 'langs/i18n') diff --git a/langs/i18n/i18n_test.go b/langs/i18n/i18n_test.go index b67cabc55..e08210848 100644 --- a/langs/i18n/i18n_test.go +++ b/langs/i18n/i18n_test.go @@ -17,11 +17,13 @@ import ( "path/filepath" "testing" + "github.com/gohugoio/hugo/modules" + "github.com/gohugoio/hugo/tpl/tplimpl" "github.com/gohugoio/hugo/common/loggers" - "github.com/gohugoio/hugo/htesting" "github.com/gohugoio/hugo/langs" + "github.com/gohugoio/hugo/resources/page" "github.com/spf13/afero" "github.com/spf13/viper" @@ -199,7 +201,7 @@ func newDepsConfig(tp *TranslationProvider, cfg config.Provider, fs *hugofs.Fs) l.Set("i18nDir", "i18n") return deps.DepsCfg{ Language: l, - Site: htesting.NewTestHugoSite(), + Site: page.NewDummyHugoSite(cfg), Cfg: cfg, Fs: fs, Logger: logger, @@ -219,6 +221,13 @@ func getConfig() *viper.Viper { v.Set("assetDir", "assets") v.Set("resourceDir", "resources") v.Set("publishDir", "public") + langs.LoadLanguageSettings(v, nil) + mod, err := modules.CreateProjectModule(v) + if err != nil { + panic(err) + } + v.Set("allModules", modules.Modules{mod}) + return v } diff --git a/langs/i18n/translationProvider.go b/langs/i18n/translationProvider.go index 74e144007..c7b4839ee 100644 --- a/langs/i18n/translationProvider.go +++ b/langs/i18n/translationProvider.go @@ -40,8 +40,7 @@ func NewTranslationProvider() *TranslationProvider { // Update updates the i18n func in the provided Deps. func (tp *TranslationProvider) Update(d *deps.Deps) error { - sp := source.NewSourceSpec(d.PathSpec, d.BaseFs.SourceFilesystems.I18n.Fs) - src := sp.NewFilesystem("") + spec := source.NewSourceSpec(d.PathSpec, nil) i18nBundle := bundle.New() @@ -51,25 +50,33 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error { } var newLangs []string - for _, r := range src.Files() { - currentSpec := language.GetPluralSpec(r.BaseFileName()) - if currentSpec == nil { - // This may is a language code not supported by go-i18n, it may be - // Klingon or ... not even a fake language. Make sure it works. - newLangs = append(newLangs, r.BaseFileName()) + for _, dir := range d.BaseFs.I18n.Dirs { + src := spec.NewFilesystemFromFileMetaInfo(dir) + + files, err := src.Files() + if err != nil { + return err } - } - if len(newLangs) > 0 { - language.RegisterPluralSpec(newLangs, en) - } + for _, r := range files { + currentSpec := language.GetPluralSpec(r.BaseFileName()) + if currentSpec == nil { + // This may is a language code not supported by go-i18n, it may be + // Klingon or ... not even a fake language. Make sure it works. + newLangs = append(newLangs, r.BaseFileName()) + } + } - // The source files are ordered so the most important comes first. Since this is a - // last key win situation, we have to reverse the iteration order. - files := src.Files() - for i := len(files) - 1; i >= 0; i-- { - if err := addTranslationFile(i18nBundle, files[i]); err != nil { - return err + if len(newLangs) > 0 { + language.RegisterPluralSpec(newLangs, en) + } + + // The source files are ordered so the most important comes first. Since this is a + // last key win situation, we have to reverse the iteration order. + for i := len(files) - 1; i >= 0; i-- { + if err := addTranslationFile(i18nBundle, files[i]); err != nil { + return err + } } } @@ -81,8 +88,8 @@ func (tp *TranslationProvider) Update(d *deps.Deps) error { } -func addTranslationFile(bundle *bundle.Bundle, r source.ReadableFile) error { - f, err := r.Open() +func addTranslationFile(bundle *bundle.Bundle, r source.File) error { + f, err := r.FileInfo().Meta().Open() if err != nil { return _errors.Wrapf(err, "failed to open translations file %q:", r.LogicalName()) } @@ -101,14 +108,15 @@ func (tp *TranslationProvider) Clone(d *deps.Deps) error { return nil } -func errWithFileContext(inerr error, r source.ReadableFile) error { - rfi, ok := r.FileInfo().(hugofs.RealFilenameInfo) +func errWithFileContext(inerr error, r source.File) error { + fim, ok := r.FileInfo().(hugofs.FileMetaInfo) if !ok { return inerr } - realFilename := rfi.RealFilename() - f, err := r.Open() + meta := fim.Meta() + realFilename := meta.Filename() + f, err := meta.Open() if err != nil { return inerr } -- cgit v1.2.3