mirror of
https://github.com/gohugoio/hugo.git
synced 2025-04-20 16:13:31 +00:00
parent
f0b91852ea
commit
d572071784
3 changed files with 43 additions and 10 deletions
|
@ -14,10 +14,10 @@ import (
|
||||||
"github.com/spf13/hugo/helpers"
|
"github.com/spf13/hugo/helpers"
|
||||||
"github.com/spf13/hugo/hugofs"
|
"github.com/spf13/hugo/hugofs"
|
||||||
"github.com/spf13/hugo/source"
|
"github.com/spf13/hugo/source"
|
||||||
|
jww "github.com/spf13/jwalterweatherman"
|
||||||
"github.com/spf13/viper"
|
"github.com/spf13/viper"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
jww "github.com/spf13/jwalterweatherman"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -52,7 +52,7 @@ func TestMultiSites(t *testing.T) {
|
||||||
|
|
||||||
sites := createMultiTestSites(t)
|
sites := createMultiTestSites(t)
|
||||||
|
|
||||||
err := sites.Build(BuildCfg{skipRender: true})
|
err := sites.Build(BuildCfg{})
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Fatalf("Failed to build sites: %s", err)
|
t.Fatalf("Failed to build sites: %s", err)
|
||||||
|
@ -126,6 +126,11 @@ func TestMultiSites(t *testing.T) {
|
||||||
assert.Equal(t, "fr", frenchPage.Lang())
|
assert.Equal(t, "fr", frenchPage.Lang())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
languageRedirect := readDestination(t, "public/index.html")
|
||||||
|
|
||||||
|
// French is the main content language
|
||||||
|
require.True(t, strings.Contains(languageRedirect, "0; url=http://example.com/blog/fr"), languageRedirect)
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestMultiSitesRebuild(t *testing.T) {
|
func TestMultiSitesRebuild(t *testing.T) {
|
||||||
|
@ -498,7 +503,7 @@ func readFileFromFs(t *testing.T, fs afero.Fs, filename string) string {
|
||||||
// Print some debug info
|
// Print some debug info
|
||||||
root := strings.Split(filename, helpers.FilePathSeparator)[0]
|
root := strings.Split(filename, helpers.FilePathSeparator)[0]
|
||||||
afero.Walk(fs, root, func(path string, info os.FileInfo, err error) error {
|
afero.Walk(fs, root, func(path string, info os.FileInfo, err error) error {
|
||||||
if !info.IsDir() {
|
if info != nil && !info.IsDir() {
|
||||||
fmt.Println(" ", path)
|
fmt.Println(" ", path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -138,6 +138,7 @@ type targetList struct {
|
||||||
pageUgly target.Output
|
pageUgly target.Output
|
||||||
file target.Output
|
file target.Output
|
||||||
alias target.AliasPublisher
|
alias target.AliasPublisher
|
||||||
|
languageAlias target.AliasPublisher
|
||||||
}
|
}
|
||||||
|
|
||||||
type SiteInfo struct {
|
type SiteInfo struct {
|
||||||
|
@ -1398,6 +1399,16 @@ func (s *Site) renderAliases() error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if s.Multilingual.enabled() {
|
||||||
|
mainLang := s.Multilingual.DefaultLang.Lang
|
||||||
|
mainLangURL := helpers.AbsURL(mainLang)
|
||||||
|
jww.DEBUG.Printf("Write redirect to main language %s: %s", mainLang, mainLangURL)
|
||||||
|
if err := s.publishDestAlias(s.languageAliasTarget(), "/", mainLangURL); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2161,6 +2172,11 @@ func (s *Site) aliasTarget() target.AliasPublisher {
|
||||||
return s.targets.alias
|
return s.targets.alias
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (s *Site) languageAliasTarget() target.AliasPublisher {
|
||||||
|
s.initTargetList()
|
||||||
|
return s.targets.languageAlias
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Site) initTargetList() {
|
func (s *Site) initTargetList() {
|
||||||
s.targetListInit.Do(func() {
|
s.targetListInit.Do(func() {
|
||||||
if s.targets.page == nil {
|
if s.targets.page == nil {
|
||||||
|
@ -2185,6 +2201,12 @@ func (s *Site) initTargetList() {
|
||||||
PublishDir: s.absPublishDir(),
|
PublishDir: s.absPublishDir(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if s.targets.languageAlias == nil {
|
||||||
|
s.targets.languageAlias = &target.HTMLRedirectAlias{
|
||||||
|
PublishDir: s.absPublishDir(),
|
||||||
|
AllowRoot: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -2198,7 +2220,12 @@ func (s *Site) writeDestPage(path string, publisher target.Publisher, reader io.
|
||||||
return publisher.Publish(path, reader)
|
return publisher.Publish(path, reader)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AliasPublisher
|
||||||
func (s *Site) writeDestAlias(path string, permalink string) (err error) {
|
func (s *Site) writeDestAlias(path string, permalink string) (err error) {
|
||||||
|
return s.publishDestAlias(s.aliasTarget(), path, permalink)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (s *Site) publishDestAlias(aliasPublisher target.AliasPublisher, path string, permalink string) (err error) {
|
||||||
if viper.GetBool("RelativeURLs") {
|
if viper.GetBool("RelativeURLs") {
|
||||||
// convert `permalink` into URI relative to location of `path`
|
// convert `permalink` into URI relative to location of `path`
|
||||||
baseURL := helpers.SanitizeURLKeepTrailingSlash(viper.GetString("BaseURL"))
|
baseURL := helpers.SanitizeURLKeepTrailingSlash(viper.GetString("BaseURL"))
|
||||||
|
@ -2212,7 +2239,7 @@ func (s *Site) writeDestAlias(path string, permalink string) (err error) {
|
||||||
permalink = filepath.ToSlash(permalink)
|
permalink = filepath.ToSlash(permalink)
|
||||||
}
|
}
|
||||||
jww.DEBUG.Println("creating alias:", path, "redirecting to", permalink)
|
jww.DEBUG.Println("creating alias:", path, "redirecting to", permalink)
|
||||||
return s.aliasTarget().Publish(path, permalink)
|
return aliasPublisher.Publish(path, permalink)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *Site) draftStats() string {
|
func (s *Site) draftStats() string {
|
||||||
|
|
|
@ -45,6 +45,7 @@ type AliasPublisher interface {
|
||||||
type HTMLRedirectAlias struct {
|
type HTMLRedirectAlias struct {
|
||||||
PublishDir string
|
PublishDir string
|
||||||
Templates *template.Template
|
Templates *template.Template
|
||||||
|
AllowRoot bool // for the language redirects
|
||||||
}
|
}
|
||||||
|
|
||||||
func (h *HTMLRedirectAlias) Translate(alias string) (aliasPath string, err error) {
|
func (h *HTMLRedirectAlias) Translate(alias string) (aliasPath string, err error) {
|
||||||
|
@ -56,7 +57,7 @@ func (h *HTMLRedirectAlias) Translate(alias string) (aliasPath string, err error
|
||||||
alias = filepath.Clean(alias)
|
alias = filepath.Clean(alias)
|
||||||
components := strings.Split(alias, helpers.FilePathSeparator)
|
components := strings.Split(alias, helpers.FilePathSeparator)
|
||||||
|
|
||||||
if alias == helpers.FilePathSeparator {
|
if !h.AllowRoot && alias == helpers.FilePathSeparator {
|
||||||
return "", fmt.Errorf("Alias \"%s\" resolves to website root directory", originalAlias)
|
return "", fmt.Errorf("Alias \"%s\" resolves to website root directory", originalAlias)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue