Skip to content

Commit

Permalink
Langs
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Apr 29, 2024
1 parent 5a554c2 commit a882237
Show file tree
Hide file tree
Showing 3 changed files with 112 additions and 44 deletions.
60 changes: 45 additions & 15 deletions hugolib/content_map.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,25 +238,32 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo, whatChanged *whatChanged) error
// TODO1 disabled languages.
s := m.s.h.resolveSite(fi.Meta().Lang)
f := source.NewFileInfo(fi)
h := s.h
if err := func() error {
var contentAdapter *pagesfromdata.PagesFromTemplate
if m.s.h.isRebuild() {
contentAdapter = m.treePagesFromTemplateOptions.Get(pi.Base())
contentAdapter := s.pageMap.treePagesFromTemplateOptions.Get(pi.Base())
var rebuild bool
if contentAdapter != nil {
contentAdapter.Fi = fi
}
if contentAdapter == nil {
rebuild = true
} else {
contentAdapter = pagesfromdata.NewPagesFromTemplate(
pagesfromdata.PagesFromTemplateOptions{
Fi: fi,
TmplFinder: s.TextTmpl(),
TmplExec: s.Tmpl(),
Site: s, // TODO1 wrapper without RegularPages etc.
Fi: fi,
Site: s, // TODO1 wrapper without RegularPages etc.
DepsFromSite: func(s page.Site) pagesfromdata.PagesFromTemplateDeps {
ss := s.(*Site)
return pagesfromdata.PagesFromTemplateDeps{
TmplFinder: ss.TextTmpl(),
TmplExec: ss.Tmpl(),
}
},
DependencyManager: s.Conf.NewIdentityManager("pagesfromdata"),
Watching: s.Conf.Watching(),
HandlePage: func(p pagesfromdata.PageData) error {
HandlePage: func(ss page.Site, p pagesfromdata.PageData) error {
s := ss.(*Site)
pc := p.PageConfig
pc.Path = path.Join(pi.Base(), pc.Path)
ps, pi, err := s.h.newPage(
ps, pi, err := h.newPage(
&pageMeta{
f: f,
s: s,
Expand All @@ -274,30 +281,53 @@ func (m *pageMap) AddFi(fi hugofs.FileMetaInfo, whatChanged *whatChanged) error
return nil
}

n, _, replaced := m.treePages.InsertIntoValuesDimension(pi.Base(), ps)
n, _, replaced := s.pageMap.treePages.InsertIntoValuesDimension(pi.Base(), ps)

if m.s.h.isRebuild() && replaced {
if h.isRebuild() && replaced {
whatChanged.Add(n.GetIdentity())
}

return nil
},
},
)
}

m.treePagesFromTemplateOptions.Insert(pi.Base(), contentAdapter)
s.pageMap.treePagesFromTemplateOptions.Insert(pi.Base(), contentAdapter)

}

if err := contentAdapter.Execute(context.Background()); err != nil {
return err
}

if !rebuild && contentAdapter.BuildState.EnableAllLanguages {
// Clone and insert the adapter for the other sites.
for _, ss := range s.h.Sites {
if s == ss {
continue
}

clone := contentAdapter.CloneForSite(ss)

// Make sure it gets executed for the first time.
if err := clone.Execute(context.Background()); err != nil {
return err
}

// Insert into the correct language tree so it get rebuilt on changes.
ss.pageMap.treePagesFromTemplateOptions.Insert(pi.Base(), clone)

}
}

if m.s.h.isRebuild() {
for _, p := range contentAdapter.BuildState.DeletedPaths {
// TODO1 language, resource etc.
pp := path.Join(pi.Base(), p)
if v, ok := m.treePages.Delete(pp); ok {
whatChanged.Add(v.GetIdentity())
}

}
}
return nil
Expand Down
60 changes: 31 additions & 29 deletions hugolib/pagesfromdata/pagesfromdata.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"context"
"fmt"
"io"
"path/filepath"

"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/helpers"
Expand All @@ -29,13 +30,6 @@ import (
)

type PagesFromDataTemplateContext interface {
// ReturnIfCached checks whether Hugo can use a cached version
// matching the given ETag. If yes, the execution will stop and
// the cached version will be used.
// Note that this method must be called before any call to Add.
// TODO1 assert that ReturnIfCached is called before any call to Add.
ReturnIfCached(eTag any) string

// AddPage adds a new page to the site.
// The first return value will always be an empty string.
AddPage(any) (string, error)
Expand All @@ -61,23 +55,12 @@ type PagesFromDataTemplateContext interface {
var _ PagesFromDataTemplateContext = (*pagesFromDataTemplateContext)(nil)

type pagesFromDataTemplateContext struct {
allLanguages bool
addCalled bool
opts *PagesFromTemplate
opts *PagesFromTemplate

store *maps.Scratch
}

func (p *pagesFromDataTemplateContext) ReturnIfCached(eTag any) string {
if p.addCalled {
panic("ReturnIfCached must be called before any call to Add")
}
// TODO1
return ""
}

func (p *pagesFromDataTemplateContext) AddPage(v any) (string, error) {
p.addCalled = true
var pd PageData
if err := mapstructure.WeakDecode(v, &pd); err != nil {
return "", err
Expand All @@ -95,11 +78,10 @@ func (p *pagesFromDataTemplateContext) AddPage(v any) (string, error) {
return "", err
}

return "", p.opts.HandlePage(pd)
return "", p.opts.HandlePage(p.opts.Site, pd)
}

func (p *pagesFromDataTemplateContext) AddResource(any) (string, error) {
p.addCalled = true
return "", nil
}

Expand All @@ -112,37 +94,48 @@ func (p *pagesFromDataTemplateContext) Store() *maps.Scratch {
}

func (p *pagesFromDataTemplateContext) EnableAllLanguages() string {
p.allLanguages = true
p.opts.BuildState.EnableAllLanguages = true
return ""
}

func NewPagesFromTemplate(opts PagesFromTemplateOptions) *PagesFromTemplate {
return &PagesFromTemplate{
PagesFromTemplateOptions: opts,
PagesFromTemplateDeps: opts.DepsFromSite(opts.Site),
BuildState: &BuildState{
sourceInfosCurrent: make(map[string]*sourceInfo),
},
}
}

type PagesFromTemplateOptions struct {
Fi hugofs.FileMetaInfo
TmplFinder tpl.TemplateParseFinder
TmplExec tpl.TemplateExecutor
Site page.Site
Fi hugofs.FileMetaInfo

Site page.Site
DepsFromSite func(page.Site) PagesFromTemplateDeps

DependencyManager identity.Manager
Watching bool // TODO1 use.
HandlePage func(p PageData) error

Watching bool // TODO1 use.
HandlePage func(s page.Site, p PageData) error
}

type PagesFromTemplateDeps struct {
TmplFinder tpl.TemplateParseFinder
TmplExec tpl.TemplateExecutor
}

type PagesFromTemplate struct {
PagesFromTemplateOptions
PagesFromTemplateDeps
BuildState *BuildState
}

type BuildState struct {
Rebuild bool

EnableAllLanguages bool

// Paths deleted in the current build.
DeletedPaths []string

Expand Down Expand Up @@ -209,6 +202,15 @@ type sourceInfo struct {
hash uint64
}

func (p PagesFromTemplate) CloneForSite(s page.Site) *PagesFromTemplate {
p.PagesFromTemplateOptions.Site = s
p.PagesFromTemplateDeps = p.PagesFromTemplateOptions.DepsFromSite(s)
p.BuildState = &BuildState{
sourceInfosCurrent: make(map[string]*sourceInfo),
}
return &p
}

func (p *PagesFromTemplate) GetDependencyManagerForScope(scope int) identity.Manager {
return p.DependencyManager
}
Expand All @@ -224,7 +226,7 @@ func (p *PagesFromTemplate) Execute(ctx context.Context) error {
}
defer f.Close()

tmpl, err := p.TmplFinder.Parse(p.Fi.Meta().Filename, helpers.ReaderToString(f))
tmpl, err := p.TmplFinder.Parse(filepath.ToSlash(p.Fi.Meta().Filename), helpers.ReaderToString(f))
if err != nil {
return err
}
Expand Down
36 changes: 36 additions & 0 deletions hugolib/pagesfromdata/pagesfromdata_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,46 @@ func TestPagesFromGoTmplMovePage(t *testing.T) {
b.AssertFileContent("public/index.html", "RegularPagesRecursive: p1:p1:/docs/p1|p2:/docs/p2moved|$")
}

func TestPagesFromGoTmplEnableAllLanguages(t *testing.T) {
t.Parallel()

files := `
-- hugo.toml --
defaultContentLanguage = "en"
defaultContentLanguageInSubdir = true
[languages]
[languages.en]
weight = 1
title = "Title"
[languages.fr]
title = "Titre"
weight = 2
-- i18n/en.yaml --
title: Title
-- i18n/fr.yaml --
title: Titre
-- content/docs/_content.gotmpl --
{{ .EnableAllLanguages }}
{{ $title := printf "%s:%s" site.Title (i18n "title") }}
{{ $.AddPage (dict "kind" "page" "path" "p1" "title" $title ) }}
-- layouts/_default/single.html --
Single: {{ .Title }}|{{ .Content }}|
`

b := hugolib.Test(t, files)

b.AssertFileContent("public/en/docs/p1/index.html", "Single: Title:Title||")
b.AssertFileContent("public/fr/docs/p1/index.html", "Single: Titre:Titre||")
}

func TestPagesFromGoTmplRemoveGoTmpl(t *testing.T) {
t.Parallel()
t.Skip("TODO1")
b := hugolib.TestRunning(t, filesPagesFromDataTempleBasic)
b.RemoveFiles("content/docs/_content.gotmpl").Build()
b.AssertFileContent("public/index.html", "RegularPagesRecursive: |$")
}

// TODO1 markup.
// TODO1 resource path must end with a file name.

0 comments on commit a882237

Please sign in to comment.