From 9a113da058c5d92c8bc7de4c26b226a2fd4841bb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?= Date: Sun, 17 Feb 2019 10:58:03 +0100 Subject: [PATCH] Fix resource outputformat logic --- hugolib/page_composite.go | 6 ++++ hugolib/pagebundler_handlers.go | 53 ++++++++++++++------------------- hugolib/site_render.go | 19 ++---------- resources/resource_metadata.go | 1 - 4 files changed, 31 insertions(+), 48 deletions(-) diff --git a/hugolib/page_composite.go b/hugolib/page_composite.go index cf4eb76b547..e6e8b20cc87 100644 --- a/hugolib/page_composite.go +++ b/hugolib/page_composite.go @@ -49,6 +49,7 @@ import ( "github.com/gohugoio/hugo/common/collections" "github.com/gohugoio/hugo/common/text" "github.com/gohugoio/hugo/navigation" + "github.com/gohugoio/hugo/resources" "github.com/gohugoio/hugo/resources/page" "github.com/gohugoio/hugo/resources/resource" ) @@ -619,6 +620,11 @@ func (p *pageState) RawContent() string { func (p *pageState) Resources() resource.Resources { p.resourcesInit.Do(func() { + + if len(p.m.resourcesMetadata) > 0 { + resources.AssignMetadata(p.m.resourcesMetadata, p.resources...) + } + sort.SliceStable(p.resources, func(i, j int) bool { ri, rj := p.resources[i], p.resources[j] if ri.ResourceType() < rj.ResourceType() { diff --git a/hugolib/pagebundler_handlers.go b/hugolib/pagebundler_handlers.go index e83d8c6f434..862d0e9c780 100644 --- a/hugolib/pagebundler_handlers.go +++ b/hugolib/pagebundler_handlers.go @@ -16,6 +16,7 @@ package hugolib import ( "errors" "fmt" + "path" "path/filepath" "github.com/gohugoio/hugo/common/hugio" @@ -257,35 +258,6 @@ func (c *contentHandlers) parsePage(h contentHandler) contentHandler { } } } - - /* - - // TOOD(bep) page - sort.SliceStable(p.Resources(), func(i, j int) bool { - if p.resources[i].ResourceType() < p.resources[j].ResourceType() { - return true - } - - p1, ok1 := p.resources[i].(page.Page) - p2, ok2 := p.resources[j].(page.Page) - - if ok1 != ok2 { - return ok2 - } - - if ok1 { - return page.DefaultPageSort(p1, p2) - } - - return p.resources[i].RelPermalink() < p.resources[j].RelPermalink() - }) - - // Assign metadata from front matter if set - if len(p.resourcesMetadata) > 0 { - resources.AssignMetadata(p.resourcesMetadata, p.Resources()...) - } - */ - } return h(ctx) @@ -330,13 +302,34 @@ func (c *contentHandlers) createResource() contentHandler { return notHandled } + // TODO(bep) consolidate with multihost logic + clean up + outputFormats := ctx.parentPage.m.outputFormats() + languageBase := c.s.GetURLLanguageBasePath() + seen := make(map[string]bool) + var targetBasePaths []string + for _, f := range outputFormats { + if !seen[f.Path] { + targetBasePaths = append(targetBasePaths, f.Path) + } + + var lf string + if f.Path == "" { + lf = languageBase + } else { + lf = path.Join(languageBase, f.Path) + } + if !seen[lf] { + targetBasePaths = append(targetBasePaths, lf) + } + } + resource, err := c.s.ResourceSpec.New( resources.ResourceSourceDescriptor{ TargetPathBuilder: ctx.parentPage.subResourceTargetPathFactory, SourceFile: ctx.source, RelTargetFilename: ctx.target, URLBase: c.s.GetURLLanguageBasePath(), - TargetBasePaths: []string{c.s.GetTargetLanguageBasePath()}, + TargetBasePaths: targetBasePaths, }) return handlerResult{err: err, handled: true, result: resource} diff --git a/hugolib/site_render.go b/hugolib/site_render.go index 5eb9a98a9cb..f1b7ed96fdb 100644 --- a/hugolib/site_render.go +++ b/hugolib/site_render.go @@ -113,23 +113,8 @@ func pageRenderer(s *Site, pages <-chan *pageState, results chan<- error, wg *sy continue } - // We only need to re-publish the resources if the output format is different - // from all of the previous (e.g. the "amp" use case). - // TODO(bep) page - shouldRender := true - - /* shouldRender := i == 0 - if i > 0 { - for j := i; j >= 0; j-- { - if f.Path != p.m.configuredOutputFormats[j].Path { - shouldRender = true - } else { - shouldRender = false - } - } - }*/ - - if shouldRender { + if i == 0 { + // This will publish its resources to all output formats paths. if err := p.renderResources(); err != nil { s.SendError(p.errorf(err, "failed to render page resources")) continue diff --git a/resources/resource_metadata.go b/resources/resource_metadata.go index 0830dfc594b..e019133d79f 100644 --- a/resources/resource_metadata.go +++ b/resources/resource_metadata.go @@ -47,7 +47,6 @@ const counterPlaceHolder = ":counter" // The `name` and `title` metadata field support shell-matched collection it got a match in. // See https://golang.org/pkg/path/#Match func AssignMetadata(metadata []map[string]interface{}, resources ...resource.Resource) error { - counters := make(map[string]int) for _, r := range resources {