diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-03-21 17:21:46 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2018-04-02 08:06:21 +0200 |
commit | eb42774e587816b1fbcafbcea59ed65df703882a (patch) | |
tree | fdb62cf17355b47fa485941f3c3fffd604896daa /source/fileInfo.go | |
parent | f27977809ce5d5dce4db41db6323a4ad1b095985 (diff) |
Add support for a content dir set per language
A sample config:
```toml
defaultContentLanguage = "en"
defaultContentLanguageInSubdir = true
[Languages]
[Languages.en]
weight = 10
title = "In English"
languageName = "English"
contentDir = "content/english"
[Languages.nn]
weight = 20
title = "På Norsk"
languageName = "Norsk"
contentDir = "content/norwegian"
```
The value of `contentDir` can be any valid path, even absolute path references. The only restriction is that the content dirs cannot overlap.
The content files will be assigned a language by
1. The placement: `content/norwegian/post/my-post.md` will be read as Norwegian content.
2. The filename: `content/english/post/my-post.nn.md` will be read as Norwegian even if it lives in the English content folder.
The content directories will be merged into a big virtual filesystem with one simple rule: The most specific language file will win.
This means that if both `content/norwegian/post/my-post.md` and `content/english/post/my-post.nn.md` exists, they will be considered duplicates and the version inside `content/norwegian` will win.
Note that translations will be automatically assigned by Hugo by the content file's relative placement, so `content/norwegian/post/my-post.md` will be a translation of `content/english/post/my-post.md`.
If this does not work for you, you can connect the translations together by setting a `translationKey` in the content files' front matter.
Fixes #4523
Fixes #4552
Fixes #4553
Diffstat (limited to 'source/fileInfo.go')
-rw-r--r-- | source/fileInfo.go | 69 |
1 files changed, 55 insertions, 14 deletions
diff --git a/source/fileInfo.go b/source/fileInfo.go index 1cad1c4ee..882ef22a7 100644 --- a/source/fileInfo.go +++ b/source/fileInfo.go @@ -14,12 +14,17 @@ package source import ( + "fmt" "io" "os" "path/filepath" "strings" "sync" + "github.com/spf13/afero" + + "github.com/gohugoio/hugo/hugofs" + "github.com/gohugoio/hugo/helpers" ) @@ -86,7 +91,10 @@ type FileInfo struct { // Absolute filename to the file on disk. filename string - fi os.FileInfo + + sp *SourceSpec + + fi os.FileInfo // Derived from filename ext string // Extension without any "." @@ -104,8 +112,6 @@ type FileInfo struct { uniqueID string - sp *SourceSpec - lazyInit sync.Once } @@ -146,7 +152,6 @@ func (fi *FileInfo) init() { fi.lazyInit.Do(func() { relDir := strings.Trim(fi.relDir, helpers.FilePathSeparator) parts := strings.Split(relDir, helpers.FilePathSeparator) - var section string if (!fi.isLeafBundle && len(parts) == 1) || len(parts) > 1 { section = parts[0] @@ -161,6 +166,19 @@ func (fi *FileInfo) init() { func (sp *SourceSpec) NewFileInfo(baseDir, filename string, isLeafBundle bool, fi os.FileInfo) *FileInfo { + var lang, translationBaseName, relPath string + + if fp, ok := fi.(hugofs.FilePather); ok { + filename = fp.Filename() + baseDir = fp.BaseDir() + relPath = fp.Path() + } + + if fl, ok := fi.(hugofs.LanguageAnnouncer); ok { + lang = fl.Lang() + translationBaseName = fl.TranslationBaseName() + } + dir, name := filepath.Split(filename) if !strings.HasSuffix(dir, helpers.FilePathSeparator) { dir = dir + helpers.FilePathSeparator @@ -175,19 +193,20 @@ func (sp *SourceSpec) NewFileInfo(baseDir, filename string, isLeafBundle bool, f relDir = strings.TrimPrefix(relDir, helpers.FilePathSeparator) - relPath := filepath.Join(relDir, name) + if relPath == "" { + relPath = filepath.Join(relDir, name) + } ext := strings.ToLower(strings.TrimPrefix(filepath.Ext(name), ".")) baseName := helpers.Filename(name) - lang := strings.TrimPrefix(filepath.Ext(baseName), ".") - var translationBaseName string - - if _, ok := sp.Languages[lang]; lang == "" || !ok { - lang = sp.DefaultContentLanguage - translationBaseName = baseName - } else { - translationBaseName = helpers.Filename(baseName) + if translationBaseName == "" { + // This is usyally provided by the filesystem. But this FileInfo is also + // created in a standalone context when doing "hugo new". This is + // an approximate implementation, which is "good enough" in that case. + translationBaseName = strings.TrimSuffix(baseName, ext) + fileLangExt := filepath.Ext(translationBaseName) + translationBaseName = strings.TrimSuffix(translationBaseName, fileLangExt) } f := &FileInfo{ @@ -211,5 +230,27 @@ func (sp *SourceSpec) NewFileInfo(baseDir, filename string, isLeafBundle bool, f // Open implements ReadableFile. func (fi *FileInfo) Open() (io.ReadCloser, error) { - return fi.sp.Fs.Source.Open(fi.Filename()) + f, err := fi.sp.PathSpec.Fs.Source.Open(fi.Filename()) + return f, err +} + +func printFs(fs afero.Fs, path string, w io.Writer) { + if fs == nil { + return + } + afero.Walk(fs, path, func(path string, info os.FileInfo, err error) error { + + if info != nil && !info.IsDir() { + + s := path + if lang, ok := info.(hugofs.LanguageAnnouncer); ok { + s = s + "\t" + lang.Lang() + } + if fp, ok := info.(hugofs.FilePather); ok { + s = s + "\t" + fp.Filename() + } + fmt.Fprintln(w, " ", s) + } + return nil + }) } |