summaryrefslogtreecommitdiffstats
path: root/hugolib
diff options
context:
space:
mode:
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2019-09-10 11:26:34 +0200
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>2020-02-18 09:49:42 +0100
commiteada236f87d9669885da1ff647672bb3dc6b4954 (patch)
treea0303f067b2cbe06c55637013dbd7702a551c64f /hugolib
parente5329f13c02b87f0c30f8837759c810cd90ff8da (diff)
Introduce a tree map for all content
This commit introduces a new data structure to store pages and their resources. This data structure is backed by radix trees. This simplies tree operations, makes all pages a bundle, and paves the way for #6310. It also solves a set of annoying issues (see list below). Not a motivation behind this, but this commit also makes Hugo in general a little bit faster and more memory effective (see benchmarks). Especially for partial rebuilds on content edits, but also when taxonomies is in use. ``` name old time/op new time/op delta SiteNew/Bundle_with_image/Edit-16 1.32ms ± 8% 1.00ms ± 9% -24.42% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file/Edit-16 1.28ms ± 0% 0.94ms ± 0% -26.26% (p=0.029 n=4+4) SiteNew/Tags_and_categories/Edit-16 33.9ms ± 2% 21.8ms ± 1% -35.67% (p=0.029 n=4+4) SiteNew/Canonify_URLs/Edit-16 40.6ms ± 1% 37.7ms ± 3% -7.20% (p=0.029 n=4+4) SiteNew/Deep_content_tree/Edit-16 56.7ms ± 0% 51.7ms ± 1% -8.82% (p=0.029 n=4+4) SiteNew/Many_HTML_templates/Edit-16 19.9ms ± 2% 18.3ms ± 3% -7.64% (p=0.029 n=4+4) SiteNew/Page_collections/Edit-16 37.9ms ± 4% 34.0ms ± 2% -10.28% (p=0.029 n=4+4) SiteNew/Bundle_with_image-16 10.7ms ± 0% 10.6ms ± 0% -1.15% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file-16 10.8ms ± 0% 10.7ms ± 0% -1.05% (p=0.029 n=4+4) SiteNew/Tags_and_categories-16 43.2ms ± 1% 39.6ms ± 1% -8.35% (p=0.029 n=4+4) SiteNew/Canonify_URLs-16 47.6ms ± 1% 47.3ms ± 0% ~ (p=0.057 n=4+4) SiteNew/Deep_content_tree-16 73.0ms ± 1% 74.2ms ± 1% ~ (p=0.114 n=4+4) SiteNew/Many_HTML_templates-16 37.9ms ± 0% 38.1ms ± 1% ~ (p=0.114 n=4+4) SiteNew/Page_collections-16 53.6ms ± 1% 54.7ms ± 1% +2.09% (p=0.029 n=4+4) name old alloc/op new alloc/op delta SiteNew/Bundle_with_image/Edit-16 486kB ± 0% 430kB ± 0% -11.47% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file/Edit-16 265kB ± 0% 209kB ± 0% -21.06% (p=0.029 n=4+4) SiteNew/Tags_and_categories/Edit-16 13.6MB ± 0% 8.8MB ± 0% -34.93% (p=0.029 n=4+4) SiteNew/Canonify_URLs/Edit-16 66.5MB ± 0% 63.9MB ± 0% -3.95% (p=0.029 n=4+4) SiteNew/Deep_content_tree/Edit-16 28.8MB ± 0% 25.8MB ± 0% -10.55% (p=0.029 n=4+4) SiteNew/Many_HTML_templates/Edit-16 6.16MB ± 0% 5.56MB ± 0% -9.86% (p=0.029 n=4+4) SiteNew/Page_collections/Edit-16 16.9MB ± 0% 16.0MB ± 0% -5.19% (p=0.029 n=4+4) SiteNew/Bundle_with_image-16 2.28MB ± 0% 2.29MB ± 0% +0.35% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file-16 2.07MB ± 0% 2.07MB ± 0% ~ (p=0.114 n=4+4) SiteNew/Tags_and_categories-16 14.3MB ± 0% 13.2MB ± 0% -7.30% (p=0.029 n=4+4) SiteNew/Canonify_URLs-16 69.1MB ± 0% 69.0MB ± 0% ~ (p=0.343 n=4+4) SiteNew/Deep_content_tree-16 31.3MB ± 0% 31.8MB ± 0% +1.49% (p=0.029 n=4+4) SiteNew/Many_HTML_templates-16 10.8MB ± 0% 10.9MB ± 0% +1.11% (p=0.029 n=4+4) SiteNew/Page_collections-16 21.4MB ± 0% 21.6MB ± 0% +1.15% (p=0.029 n=4+4) name old allocs/op new allocs/op delta SiteNew/Bundle_with_image/Edit-16 4.74k ± 0% 3.86k ± 0% -18.57% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file/Edit-16 4.73k ± 0% 3.85k ± 0% -18.58% (p=0.029 n=4+4) SiteNew/Tags_and_categories/Edit-16 301k ± 0% 198k ± 0% -34.14% (p=0.029 n=4+4) SiteNew/Canonify_URLs/Edit-16 389k ± 0% 373k ± 0% -4.07% (p=0.029 n=4+4) SiteNew/Deep_content_tree/Edit-16 338k ± 0% 262k ± 0% -22.63% (p=0.029 n=4+4) SiteNew/Many_HTML_templates/Edit-16 102k ± 0% 88k ± 0% -13.81% (p=0.029 n=4+4) SiteNew/Page_collections/Edit-16 176k ± 0% 152k ± 0% -13.32% (p=0.029 n=4+4) SiteNew/Bundle_with_image-16 26.8k ± 0% 26.8k ± 0% +0.05% (p=0.029 n=4+4) SiteNew/Bundle_with_JSON_file-16 26.8k ± 0% 26.8k ± 0% +0.05% (p=0.029 n=4+4) SiteNew/Tags_and_categories-16 273k ± 0% 245k ± 0% -10.36% (p=0.029 n=4+4) SiteNew/Canonify_URLs-16 396k ± 0% 398k ± 0% +0.39% (p=0.029 n=4+4) SiteNew/Deep_content_tree-16 317k ± 0% 325k ± 0% +2.53% (p=0.029 n=4+4) SiteNew/Many_HTML_templates-16 146k ± 0% 147k ± 0% +0.98% (p=0.029 n=4+4) SiteNew/Page_collections-16 210k ± 0% 215k ± 0% +2.44% (p=0.029 n=4+4) ``` Fixes #6312 Fixes #6087 Fixes #6738 Fixes #6412 Fixes #6743 Fixes #6875 Fixes #6034 Fixes #6902 Fixes #6173 Fixes #6590
Diffstat (limited to 'hugolib')
-rw-r--r--hugolib/alias.go3
-rw-r--r--hugolib/cascade_test.go193
-rw-r--r--hugolib/content_map.go971
-rw-r--r--hugolib/content_map_page.go998
-rw-r--r--hugolib/content_map_test.go455
-rw-r--r--hugolib/disableKinds_test.go384
-rw-r--r--hugolib/filesystems/basefs.go1
-rw-r--r--hugolib/hugo_modules_test.go5
-rw-r--r--hugolib/hugo_sites.go198
-rw-r--r--hugolib/hugo_sites_build.go45
-rw-r--r--hugolib/hugo_sites_build_test.go11
-rw-r--r--hugolib/page.go162
-rw-r--r--hugolib/page__common.go30
-rw-r--r--hugolib/page__data.go31
-rw-r--r--hugolib/page__meta.go122
-rw-r--r--hugolib/page__new.go174
-rw-r--r--hugolib/page__output.go2
-rw-r--r--hugolib/page__paths.go23
-rw-r--r--hugolib/page__tree.go85
-rw-r--r--hugolib/page_kinds.go1
-rw-r--r--hugolib/page_test.go4
-rw-r--r--hugolib/pagebundler_test.go20
-rw-r--r--hugolib/pagecollections.go489
-rw-r--r--hugolib/pagecollections_test.go268
-rw-r--r--hugolib/pages_capture.go646
-rw-r--r--hugolib/pages_capture_test.go10
-rw-r--r--hugolib/pages_map.go474
-rw-r--r--hugolib/pages_process.go198
-rw-r--r--hugolib/site.go275
-rw-r--r--hugolib/site_benchmark_new_test.go23
-rw-r--r--hugolib/site_output.go25
-rw-r--r--hugolib/site_render.go85
-rw-r--r--hugolib/site_sections_test.go2
-rw-r--r--hugolib/site_test.go21
-rw-r--r--hugolib/taxonomy_test.go179
-rw-r--r--hugolib/template_test.go22
-rw-r--r--hugolib/testhelpers_test.go10
-rw-r--r--hugolib/translations.go16
38 files changed, 4400 insertions, 2261 deletions
diff --git a/hugolib/alias.go b/hugolib/alias.go
index 9eba8b335..b97094ecc 100644
--- a/hugolib/alias.go
+++ b/hugolib/alias.go
@@ -17,7 +17,6 @@ import (
"bytes"
"errors"
"fmt"
- "html/template"
"io"
"path"
"path/filepath"
@@ -32,8 +31,6 @@ import (
"github.com/gohugoio/hugo/tpl"
)
-var defaultAliasTemplates *template.Template
-
type aliasHandler struct {
t tpl.TemplateHandler
log *loggers.Logger
diff --git a/hugolib/cascade_test.go b/hugolib/cascade_test.go
index 9cd402d6a..dd3aa72a6 100644
--- a/hugolib/cascade_test.go
+++ b/hugolib/cascade_test.go
@@ -17,6 +17,7 @@ import (
"bytes"
"fmt"
"path"
+ "strings"
"testing"
qt "github.com/frankban/quicktest"
@@ -60,29 +61,33 @@ func TestCascade(t *testing.T) {
b.Build(BuildCfg{})
b.AssertFileContent("public/index.html", `
- 12|taxonomy|categories/cool/_index.md|Cascade Category|cat.png|categories|HTML-|
- 12|taxonomy|categories/catsect1|catsect1|cat.png|categories|HTML-|
- 12|taxonomy|categories/funny|funny|cat.png|categories|HTML-|
- 12|taxonomyTerm|categories/_index.md|My Categories|cat.png|categories|HTML-|
- 32|taxonomy|categories/sad/_index.md|Cascade Category|sad.png|categories|HTML-|
- 42|taxonomy|tags/blue|blue|home.png|tags|HTML-|
- 42|section|sect3|Cascade Home|home.png|sect3|HTML-|
- 42|taxonomyTerm|tags|Cascade Home|home.png|tags|HTML-|
- 42|page|bundle1/index.md|Cascade Home|home.png|page|HTML-|
- 42|page|p2.md|Cascade Home|home.png|page|HTML-|
- 42|page|sect2/p2.md|Cascade Home|home.png|sect2|HTML-|
- 42|page|sect3/p1.md|Cascade Home|home.png|sect3|HTML-|
- 42|taxonomy|tags/green|green|home.png|tags|HTML-|
- 42|home|_index.md|Home|home.png|page|HTML-|
- 42|page|p1.md|p1|home.png|page|HTML-|
- 42|section|sect1/_index.md|Sect1|sect1.png|stype|HTML-|
- 42|section|sect1/s1_2/_index.md|Sect1_2|sect1.png|stype|HTML-|
- 42|page|sect1/s1_2/p1.md|Sect1_2_p1|sect1.png|stype|HTML-|
- 42|page|sect1/s1_2/p2.md|Sect1_2_p2|sect1.png|stype|HTML-|
- 42|section|sect2/_index.md|Sect2|home.png|sect2|HTML-|
- 42|page|sect2/p1.md|Sect2_p1|home.png|sect2|HTML-|
- 52|page|sect4/p1.md|Cascade Home|home.png|sect4|RSS-|
- 52|section|sect4/_index.md|Sect4|home.png|sect4|RSS-|
+12|taxonomy|categories/cool/_index.md|Cascade Category|cat.png|categories|HTML-|
+12|taxonomy|categories/catsect1|catsect1|cat.png|categories|HTML-|
+12|taxonomy|categories/funny|funny|cat.png|categories|HTML-|
+12|taxonomyTerm|categories/_index.md|My Categories|cat.png|categories|HTML-|
+32|taxonomy|categories/sad/_index.md|Cascade Category|sad.png|categories|HTML-|
+42|taxonomy|tags/blue|blue|home.png|tags|HTML-|
+42|taxonomyTerm|tags|Cascade Home|home.png|tags|HTML-|
+42|section|sectnocontent|Cascade Home|home.png|sectnocontent|HTML-|
+42|section|sect3|Cascade Home|home.png|sect3|HTML-|
+42|page|bundle1/index.md|Cascade Home|home.png|page|HTML-|
+42|page|p2.md|Cascade Home|home.png|page|HTML-|
+42|page|sect2/p2.md|Cascade Home|home.png|sect2|HTML-|
+42|page|sect3/nofrontmatter.md|Cascade Home|home.png|sect3|HTML-|
+42|page|sect3/p1.md|Cascade Home|home.png|sect3|HTML-|
+42|page|sectnocontent/p1.md|Cascade Home|home.png|sectnocontent|HTML-|
+42|section|sectnofrontmatter/_index.md|Cascade Home|home.png|sectnofrontmatter|HTML-|
+42|taxonomy|tags/green|green|home.png|tags|HTML-|
+42|home|_index.md|Home|home.png|page|HTML-|
+42|page|p1.md|p1|home.png|page|HTML-|
+42|section|sect1/_index.md|Sect1|sect1.png|stype|HTML-|
+42|section|sect1/s1_2/_index.md|Sect1_2|sect1.png|stype|HTML-|
+42|page|sect1/s1_2/p1.md|Sect1_2_p1|sect1.png|stype|HTML-|
+42|page|sect1/s1_2/p2.md|Sect1_2_p2|sect1.png|stype|HTML-|
+42|section|sect2/_index.md|Sect2|home.png|sect2|HTML-|
+42|page|sect2/p1.md|Sect2_p1|home.png|sect2|HTML-|
+52|page|sect4/p1.md|Cascade Home|home.png|sect4|RSS-|
+52|section|sect4/_index.md|Sect4|home.png|sect4|RSS-|
`)
// Check that type set in cascade gets the correct layout.
@@ -106,43 +111,131 @@ func TestCascadeEdit(t *testing.T) {
title: P1
---
`
- b := newTestSitesBuilder(t).Running()
- b.WithTemplatesAdded("_default/single.html", `Banner: {{ .Params.banner }}|Layout: {{ .Layout }}|Type: {{ .Type }}|Content: {{ .Content }}`)
- b.WithContent("post/_index.md", `
+
+ indexContentNoCascade := `
+---
+title: Home
+---
+`
+
+ indexContentCascade := `
---
-title: Post
+title: Section
cascade:
banner: post.jpg
layout: postlayout
type: posttype
---
-`)
+`
- b.WithContent("post/dir/_index.md", `
----
-title: Dir
----
-`, "post/dir/p1.md", p1Content)
- b.Build(BuildCfg{})
+ layout := `Banner: {{ .Params.banner }}|Layout: {{ .Layout }}|Type: {{ .Type }}|Content: {{ .Content }}`
+
+ newSite := func(t *testing.T, cascade bool) *sitesBuilder {
+ b := newTestSitesBuilder(t).Running()
+ b.WithTemplates("_default/single.html", layout)
+ b.WithTemplates("_default/list.html", layout)
+ if cascade {
+ b.WithContent("post/_index.md", indexContentCascade)
+ } else {
+ b.WithContent("post/_index.md", indexContentNoCascade)
+ }
+ b.WithContent("post/dir/p1.md", p1Content)
- assert := func() {
- b.Helper()
+ return b
+ }
+
+ t.Run("Edit descendant", func(t *testing.T) {
+ t.Parallel()
+
+ b := newSite(t, true)
+ b.Build(BuildCfg{})
+
+ assert := func() {
+ b.Helper()
+ b.AssertFileContent("public/post/dir/p1/index.html",
+ `Banner: post.jpg|`,
+ `Layout: postlayout`,
+ `Type: posttype`,
+ )
+ }
+
+ assert()
+
+ b.EditFiles("content/post/dir/p1.md", p1Content+"\ncontent edit")
+ b.Build(BuildCfg{})
+
+ assert()
b.AssertFileContent("public/post/dir/p1/index.html",
- `Banner: post.jpg|`,
- `Layout: postlayout`,
- `Type: posttype`,
+ `content edit
+Banner: post.jpg`,
)
- }
+ })
+
+ t.Run("Edit ancestor", func(t *testing.T) {
+ t.Parallel()
- assert()
+ b := newSite(t, true)
+ b.Build(BuildCfg{})
- b.EditFiles("content/post/dir/p1.md", p1Content+"\ncontent edit")
- b.Build(BuildCfg{})
+ b.AssertFileContent("public/post/dir/p1/index.html", `Banner: post.jpg|Layout: postlayout|Type: posttype|Content:`)
- assert()
- b.AssertFileContent("public/post/dir/p1/index.html",
- `content edit`,
- )
+ b.EditFiles("content/post/_index.md", strings.Replace(indexContentCascade, "post.jpg", "edit.jpg", 1))
+
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/post/index.html", `Banner: edit.jpg|Layout: postlayout|Type: posttype|`)
+ b.AssertFileContent("public/post/dir/p1/index.html", `Banner: edit.jpg|Layout: postlayout|Type: posttype|`)
+ })
+
+ t.Run("Edit ancestor, add cascade", func(t *testing.T) {
+ t.Parallel()
+
+ b := newSite(t, true)
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/post/dir/p1/index.html", `Banner: post.jpg`)
+
+ b.EditFiles("content/post/_index.md", indexContentCascade)
+
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/post/index.html", `Banner: post.jpg|Layout: postlayout|Type: posttype|`)
+ b.AssertFileContent("public/post/dir/p1/index.html", `Banner: post.jpg|Layout: postlayout|`)
+ })
+
+ t.Run("Edit ancestor, remove cascade", func(t *testing.T) {
+ t.Parallel()
+
+ b := newSite(t, false)
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/post/dir/p1/index.html", `Banner: |Layout: |`)
+
+ b.EditFiles("content/post/_index.md", indexContentNoCascade)
+
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/post/index.html", `Banner: |Layout: |Type: post|`)
+ b.AssertFileContent("public/post/dir/p1/index.html", `Banner: |Layout: |`)
+ })
+
+ t.Run("Edit ancestor, content only", func(t *testing.T) {
+ t.Parallel()
+
+ b := newSite(t, true)
+ b.Build(BuildCfg{})
+
+ b.EditFiles("content/post/_index.md", indexContentCascade+"\ncontent edit")
+
+ counters := &testCounters{}
+ b.Build(BuildCfg{testCounters: counters})
+ // As we only changed the content, not the cascade front matter, make
+ // only the home page is re-rendered.
+ b.Assert(int(counters.contentRenderCounter), qt.Equals, 1)
+
+ b.AssertFileContent("public/post/index.html", `Banner: post.jpg|Layout: postlayout|Type: posttype|Content: <p>content edit</p>`)
+ b.AssertFileContent("public/post/dir/p1/index.html", `Banner: post.jpg|Layout: postlayout|`)
+ })
}
func newCascadeTestBuilder(t testing.TB, langs []string) *sitesBuilder {
@@ -247,6 +340,12 @@ defaultContentLanguageInSubDir = false
}),
"sect2/p2.md", p(map[string]interface{}{}),
"sect3/p1.md", p(map[string]interface{}{}),
+
+ // No front matter, see #6855
+ "sect3/nofrontmatter.md", `**Hello**`,
+ "sectnocontent/p1.md", `**Hello**`,
+ "sectnofrontmatter/_index.md", `**Hello**`,
+
"sect4/_index.md", p(map[string]interface{}{
"title": "Sect4",
"cascade": map[string]interface{}{
diff --git a/hugolib/content_map.go b/hugolib/content_map.go
new file mode 100644
index 000000000..f0b66d859
--- /dev/null
+++ b/hugolib/content_map.go
@@ -0,0 +1,971 @@
+// Copyright 2019 The Hugo Authors. All rights reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package hugolib
+
+import (
+ "fmt"
+ "path"
+ "path/filepath"
+ "strings"
+ "sync"
+
+ "github.com/gohugoio/hugo/resources/page"
+ "github.com/pkg/errors"
+
+ "github.com/gohugoio/hugo/hugofs/files"
+
+ "github.com/gohugoio/hugo/hugofs"
+
+ radix "github.com/armon/go-radix"
+)
+
+// We store the branch nodes in either the `sections` or `taxonomies` tree
+// with their path as a key; Unix style slashes, a leading slash but no
+// trailing slash.
+//
+// E.g. "/blog" or "/categories/funny"
+//
+// Pages that belongs to a section are stored in the `pages` tree below
+// the section name and a branch separator, e.g. "/blog__hb_". A page is
+// given a key using the path below the section and the base filename with no extension
+// with a leaf separator added.
+//
+// For bundled pages (/mybundle/index.md), we use the folder name.
+//
+// An exmple of a full page key would be "/blog__hb_/page1__hl_"
+//
+// Bundled resources are stored in the `resources` having their path prefixed
+// with the bundle they belong to, e.g.
+// "/blog__hb_/bundle__hl_data.json".
+//
+// The weighted taxonomy entries extracted from page front matter are stored in
+// the `taxonomyEntries` tree below /plural/term/page-key, e.g.
+// "/categories/funny/blog__hb_/bundle__hl_".
+const (
+ cmBranchSeparator = "__hb_"
+ cmLeafSeparator = "__hl_"
+)
+
+// Used to mark ambigous keys in reverse index lookups.
+var ambigousContentNode = &contentNode{}
+
+func newContentMap(cfg contentMapConfig) *contentMap {
+ m := &contentMap{
+ cfg: &cfg,
+ pages: &contentTree{Name: "pages", Tree: radix.New()},
+ sections: &contentTree{Name: "sections", Tree: radix.New()},
+ taxonomies: &contentTree{Name: "taxonomies", Tree: radix.New()},
+ taxonomyEntries: &contentTree{Name: "taxonomyEntries", Tree: radix.New()},
+ resources: &contentTree{Name: "resources", Tree: radix.New()},
+ }
+
+ m.pageTrees = []*contentTree{
+ m.pages, m.sections, m.taxonomies,
+ }
+
+ m.bundleTrees = []*contentTree{
+ m.pages, m.sections, m.taxonomies, m.resources,
+ }
+
+ m.branchTrees = []*contentTree{
+ m.sections, m.taxonomies,
+ }
+
+ addToReverseMap := func(k string, n *contentNode, m map[interface{}]*contentNode) {
+ k = strings.ToLower(k)
+ existing, found := m[k]
+ if found && existing != ambigousContentNode {
+ m[k] = ambigousContentNode
+ } else if !found {
+ m[k] = n
+ }
+ }
+
+ m.pageReverseIndex = &contentTreeReverseIndex{
+ t: []*contentTree{m.pages, m.sections, m.taxonomies},
+ initFn: func(t *contentTree, m map[interface{}]*contentNode) {
+ t.Walk(func(s string, v interface{}) bool {
+ n := v.(*contentNode)
+ if n.p != nil && !n.p.File().IsZero() {
+ meta := n.p.File().FileInfo().Meta()
+ if meta.Path() != meta.PathFile() {
+ // Keep track of the original mount source.
+ mountKey := filepath.ToSlash(filepath.Join(meta.Module(), meta.PathFile()))
+ addToReverseMap(mountKey, n, m)
+ }
+ }
+ k := strings.TrimSuffix(path.Base(s), cmLeafSeparator)
+ addToReverseMap(k, n, m)
+ return false
+ })
+ },
+ }
+
+ return m
+}
+
+type cmInsertKeyBuilder struct {
+ m *contentMap
+
+ err error
+
+ // Builder state
+ tree *contentTree
+ baseKey string // Section or page key
+ key string
+}
+
+func (b cmInsertKeyBuilder) ForPage(s string) *cmInsertKeyBuilder {
+ // TODO2 fmt.Println("ForPage:", s, "baseKey:", b.baseKey, "key:", b.key)
+ baseKey := b.baseKey
+ b.baseKey = s
+
+ if !strings.HasPrefix(s, "/") {
+ s = "/" + s
+ }
+
+ if baseKey != "/" {
+ // Don't repeat the section path in the key.
+ s = strings.TrimPrefix(s, baseKey)
+ }
+
+ switch b.tree {
+ case b.m.sections:
+ b.tree = b.m.pages
+ b.key = baseKey + cmBranchSeparator + s + cmLeafSeparator
+ case b.m.taxonomies:
+ b.key = path.Join(baseKey, s)
+ default:
+ panic("invalid state")
+ }
+
+ return &b
+}
+
+func (b cmInsertKeyBuilder) ForResource(s string) *cmInsertKeyBuilder {
+ // TODO2 fmt.Println("ForResource:", s, "baseKey:", b.baseKey, "key:", b.key)
+
+ s = strings.TrimPrefix(s, "/")
+ s = strings.TrimPrefix(s, strings.TrimPrefix(b.baseKey, "/")+"/")
+
+ switch b.tree {
+ case b.m.pages:
+ b.key = b.key + s
+ case b.m.sections, b.m.taxonomies:
+ b.key = b.key + cmLeafSeparator + s
+ default:
+ panic(fmt.Sprintf("invalid state: %#v", b.tree))
+ }
+ b.tree = b.m.resources
+ return &b
+}
+
+func (b *cmInsertKeyBuilder) Insert(n *contentNode) *cmInsertKeyBuilder {
+ if b.err == nil {
+ b.tree.Insert(cleanTreeKey(b.key), n)
+ }
+ return b
+}
+
+func (b *cmInsertKeyBuilder) DeleteAll() *cmInsertKeyBuilder {
+ if b.err == nil {
+ b.tree.DeletePrefix(cleanTreeKey(b.key))
+ }
+ return b
+}
+
+func (b *cmInsertKeyBuilder) WithFile(fi hugofs.FileMetaInfo) *cmInsertKeyBuilder {
+ b.newTopLevel()
+ m := b.m
+ meta := fi.Meta()
+ p := cleanTreeKey(meta.Path())
+ bundlePath := m.getBundleDir(meta)
+ isBundle := meta.Classifier().IsBundle()
+ if isBundle {
+ panic("not implemented")
+ }
+
+ p, k := b.getBundle(p)
+ if k == "" {
+ b.err = errors.Errorf("no bundle header found for %q", bundlePath)
+ return b
+ }
+
+ id := k + m.reduceKeyPart(p, fi.Meta().Path())
+ b.tree = b.m.resources
+ b.key = id
+ b.baseKey = p
+
+ return b
+}
+
+func (b *cmInsertKeyBuilder) WithSection(s string) *cmInsertKeyBuilder {
+ b.newTopLevel()
+ b.tree = b.m.sections
+ b.baseKey = s
+ b.key = s
+ // TODO2 fmt.Println("WithSection:", s, "baseKey:", b.baseKey, "key:", b.key)
+ return b
+}
+
+func (b *cmInsertKeyBuilder) WithTaxonomy(s string) *cmInsertKeyBuilder {
+ b.newTopLevel()
+ b.tree = b.m.taxonomies
+ b.baseKey = s
+ b.key = s
+ return b
+}
+
+// getBundle gets both the key to the section and the prefix to where to store
+// this page bundle and its resources.
+func (b *cmInsertKeyBuilder) getBundle(s string) (string, string) {
+ m := b.m
+ section, _ := m.getSection(s)
+
+ p := s
+ if section != "/" {
+ p = strings.TrimPrefix(s, section)
+ }
+
+ bundlePathParts := strings.Split(p, "/")[1:]
+ basePath := section + cmBranchSeparator
+
+ // Put it into an existing bundle if found.
+ for i := len(bundlePathParts) - 2; i >= 0; i-- {
+ bundlePath := path.Join(bundlePathParts[:i]...)
+ searchKey := basePath + "/" + bundlePath + cmLeafSeparator
+ if _, found := m.pages.Get(searchKey); found {
+ return section + "/" + bundlePath, searchKey
+ }
+ }
+
+ // Put it into the section bundle.
+ return section, section + cmLeafSeparator
+}
+
<