From f94467883b954e9db8f991d8f572f62d8975e464 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B8rn=20Erik=20Pedersen?=
Date: Wed, 27 Nov 2019 13:42:36 +0100
Subject: [PATCH] Add render template hooks for links and images
This commit also revises the change detection for templates used by content files in server mode.
Fixes #6545
Fixes #4663
Closes #6043
---
deps/deps.go | 4 +
docs/content/en/content-management/formats.md | 8 +-
.../en/getting-started/configuration.md | 2 +
.../content/en/getting-started/quick-start.md | 3 +
.../_default/_markup/render-image.html | 2 +
.../layouts/_default/_markup/render-link.html | 1 +
docs/layouts/partials/deleteme.html | 1 +
docs/layouts/shortcodes/deleteme.html | 3 +
helpers/content.go | 4 +-
helpers/general_test.go | 3 +-
hugolib/content_render_hooks_test.go | 214 +++++++++++++
hugolib/filesystems/basefs.go | 49 ++-
hugolib/hugo_modules_test.go | 3 +
hugolib/hugo_sites.go | 39 ++-
hugolib/hugo_sites_build.go | 2 +-
hugolib/page.go | 227 ++++++++++++-
hugolib/page__content.go | 3 +-
hugolib/page__meta.go | 16 +-
hugolib/page__new.go | 32 +-
hugolib/page__output.go | 69 ++--
hugolib/page__per_output.go | 301 +++++++++++-------
hugolib/page_test.go | 7 +-
hugolib/page_unwrap_test.go | 1 +
hugolib/pagebundler_test.go | 1 +
hugolib/pagecollections.go | 10 -
hugolib/shortcode.go | 19 +-
hugolib/shortcode_page.go | 19 ++
hugolib/shortcode_test.go | 7 +-
hugolib/site.go | 123 +++----
hugolib/site_benchmark_new_test.go | 30 ++
hugolib/template_test.go | 87 +++++
hugolib/testhelpers_test.go | 9 +-
identity/identity.go | 131 ++++++++
identity/identity_test.go | 42 +++
markup/asciidoc/convert.go | 5 +
markup/blackfriday/convert.go | 5 +
markup/converter/converter.go | 13 +-
markup/converter/hooks/hooks.go | 57 ++++
markup/goldmark/convert.go | 112 +++++--
markup/goldmark/convert_test.go | 15 +-
markup/goldmark/render_link.go | 208 ++++++++++++
markup/mmark/convert.go | 5 +
markup/org/convert.go | 6 +
markup/pandoc/convert.go | 5 +
markup/rst/convert.go | 5 +
output/layout.go | 38 ++-
output/layout_test.go | 2 +
public/categories/index.xml | 13 +
public/index.xml | 13 +
public/sitemap.xml | 17 +
public/tags/index.xml | 13 +
resources/page/page.go | 5 +-
resources/page/page_nop.go | 8 +-
resources/page/testhelpers_test.go | 6 +-
scripts/fork_go_templates/main.go | 1 +
.../go_templates/texttemplate/exec.go | 2 +-
.../texttemplate/hugo_template.go | 104 +++++-
.../texttemplate/hugo_template_test.go | 22 +-
tpl/partials/partials.go | 6 +-
tpl/template.go | 39 ++-
tpl/template_info.go | 52 ++-
tpl/tplimpl/shortcodes.go | 4 +-
tpl/tplimpl/shortcodes_test.go | 8 +-
tpl/tplimpl/template.go | 175 ++++++----
tpl/tplimpl/templateProvider.go | 9 +-
tpl/tplimpl/template_ast_transformers.go | 166 +++++++---
tpl/tplimpl/template_ast_transformers_test.go | 66 ++--
tpl/tplimpl/template_funcs.go | 24 +-
tpl/tplimpl/template_info_test.go | 7 +-
69 files changed, 2189 insertions(+), 519 deletions(-)
create mode 100644 docs/layouts/_default/_markup/render-image.html
create mode 100644 docs/layouts/_default/_markup/render-link.html
create mode 100644 docs/layouts/partials/deleteme.html
create mode 100644 docs/layouts/shortcodes/deleteme.html
create mode 100644 hugolib/content_render_hooks_test.go
create mode 100644 identity/identity.go
create mode 100644 identity/identity_test.go
create mode 100644 markup/converter/hooks/hooks.go
create mode 100644 markup/goldmark/render_link.go
create mode 100644 public/categories/index.xml
create mode 100644 public/index.xml
create mode 100644 public/sitemap.xml
create mode 100644 public/tags/index.xml
diff --git a/deps/deps.go b/deps/deps.go
index ecbba2e5619..557f036f1fa 100644
--- a/deps/deps.go
+++ b/deps/deps.go
@@ -286,6 +286,10 @@ func (d Deps) ForLanguage(cfg DepsCfg, onCreated func(d *Deps) error) (*Deps, er
return nil, err
}
+ if err != nil {
+ return nil, err
+ }
+
d.Site = cfg.Site
// The resource cache is global so reuse.
diff --git a/docs/content/en/content-management/formats.md b/docs/content/en/content-management/formats.md
index ea056861657..3704ed8b071 100644
--- a/docs/content/en/content-management/formats.md
+++ b/docs/content/en/content-management/formats.md
@@ -23,7 +23,13 @@ You can put any file type into your `/content` directories, but Hugo uses the `m
* [Shortcodes](/content-management/shortcodes/) processed
* Layout applied
-## List of content formats
+{{< deleteme >}}
+
+
+## List of content formats.
+
+
+
The current list of content formats in Hugo:
diff --git a/docs/content/en/getting-started/configuration.md b/docs/content/en/getting-started/configuration.md
index a653c0e0b43..e7b21e007b5 100644
--- a/docs/content/en/getting-started/configuration.md
+++ b/docs/content/en/getting-started/configuration.md
@@ -34,6 +34,8 @@ hugo --config debugconfig.toml
hugo --config a.toml,b.toml,c.toml
```
+FOo.
+
{{% note %}}
Multiple site config files can be specified as a comma-separated string to the `--config` switch.
{{% /note %}}
diff --git a/docs/content/en/getting-started/quick-start.md b/docs/content/en/getting-started/quick-start.md
index 143dc0a4172..2cabdfbb756 100644
--- a/docs/content/en/getting-started/quick-start.md
+++ b/docs/content/en/getting-started/quick-start.md
@@ -24,8 +24,11 @@ This quick start uses `macOS` in the examples. For instructions about how to ins
It is recommended to have [Git](https://git-scm.com/downloads) installed to run this tutorial.
{{% /note %}}
+![Drag Racing](/images/Dragster.jpg "image title")
+![Drag Racing](/images/Dragster2.jpg "image title")
+
## Step 1: Install Hugo
{{% note %}}
diff --git a/docs/layouts/_default/_markup/render-image.html b/docs/layouts/_default/_markup/render-image.html
new file mode 100644
index 00000000000..261b7e53f86
--- /dev/null
+++ b/docs/layouts/_default/_markup/render-image.html
@@ -0,0 +1,2 @@
+{{ $url := "https://upload.wikimedia.org/wikipedia/commons/2/28/Chris_Potter_in_Vienna%2C_2019_1.jpg" | safeURL }}
+
\ No newline at end of file
diff --git a/docs/layouts/_default/_markup/render-link.html b/docs/layouts/_default/_markup/render-link.html
new file mode 100644
index 00000000000..0df3929f6b6
--- /dev/null
+++ b/docs/layouts/_default/_markup/render-link.html
@@ -0,0 +1 @@
+😉😉{{ .Text | safeHTML }} 😉😉
\ No newline at end of file
diff --git a/docs/layouts/partials/deleteme.html b/docs/layouts/partials/deleteme.html
new file mode 100644
index 00000000000..e4df2ce650f
--- /dev/null
+++ b/docs/layouts/partials/deleteme.html
@@ -0,0 +1 @@
+THIS IS PARTIAL!!!
\ No newline at end of file
diff --git a/docs/layouts/shortcodes/deleteme.html b/docs/layouts/shortcodes/deleteme.html
new file mode 100644
index 00000000000..b85b58fc1fc
--- /dev/null
+++ b/docs/layouts/shortcodes/deleteme.html
@@ -0,0 +1,3 @@
+DELETEME PARTIAL:
+
+{{ partial "deleteme" }}
\ No newline at end of file
diff --git a/helpers/content.go b/helpers/content.go
index 4dc4cd413bd..1c780fefe1b 100644
--- a/helpers/content.go
+++ b/helpers/content.go
@@ -25,13 +25,14 @@ import (
"github.com/gohugoio/hugo/common/loggers"
+ "github.com/spf13/afero"
+
"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/markup"
bp "github.com/gohugoio/hugo/bufferpool"
"github.com/gohugoio/hugo/config"
- "github.com/spf13/afero"
"strings"
)
@@ -78,6 +79,7 @@ func NewContentSpec(cfg config.Provider, logger *loggers.Logger, contentFs afero
ContentFs: contentFs,
Logger: logger,
})
+
if err != nil {
return nil, err
}
diff --git a/helpers/general_test.go b/helpers/general_test.go
index 104a4c35def..b45fb0e9b44 100644
--- a/helpers/general_test.go
+++ b/helpers/general_test.go
@@ -21,9 +21,8 @@ import (
"github.com/spf13/viper"
- "github.com/gohugoio/hugo/common/loggers"
-
qt "github.com/frankban/quicktest"
+ "github.com/gohugoio/hugo/common/loggers"
"github.com/spf13/afero"
)
diff --git a/hugolib/content_render_hooks_test.go b/hugolib/content_render_hooks_test.go
new file mode 100644
index 00000000000..b4d927013c0
--- /dev/null
+++ b/hugolib/content_render_hooks_test.go
@@ -0,0 +1,214 @@
+// 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 requiredF 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 "testing"
+
+func TestRenderHooks(t *testing.T) {
+ config := `
+baseURL="https://example.org"
+workingDir="/mywork"
+`
+ b := newTestSitesBuilder(t).WithWorkingDir("/mywork").WithConfigFile("toml", config).Running()
+ b.WithTemplatesAdded("_default/single.html", `{{ .Content }}`)
+ b.WithTemplatesAdded("shortcodes/myshortcode1.html", `{{ partial "mypartial1" }}`)
+ b.WithTemplatesAdded("shortcodes/myshortcode2.html", `{{ partial "mypartial2" }}`)
+ b.WithTemplatesAdded("shortcodes/myshortcode3.html", `SHORT3|`)
+ b.WithTemplatesAdded("shortcodes/myshortcode4.html", `
+
+{{ .Inner | markdownify }}
+
+`)
+ b.WithTemplatesAdded("shortcodes/myshortcode5.html", `
+Inner Inline: {{ .Inner | .Page.RenderString }}
+Inner Block: {{ .Inner | .Page.RenderString (dict "display" "block" ) }}
+`)
+
+ b.WithTemplatesAdded("shortcodes/myshortcode6.html", `.Render: {{ .Page.Render "myrender" }}`)
+ b.WithTemplatesAdded("partials/mypartial1.html", `PARTIAL1`)
+ b.WithTemplatesAdded("partials/mypartial2.html", `PARTIAL2 {{ partial "mypartial3.html" }}`)
+ b.WithTemplatesAdded("partials/mypartial3.html", `PARTIAL3`)
+ b.WithTemplatesAdded("partials/mypartial4.html", `PARTIAL4`)
+ b.WithTemplatesAdded("customview/myrender.html", `myrender: {{ .Title }}|P4: {{ partial "mypartial4" }}`)
+ b.WithTemplatesAdded("_default/_markup/render-link.html", `{{ with .Page }}{{ .Title }}{{ end }}|{{ .Destination | safeURL }}|Title: {{ .Title | safeHTML }}|Text: {{ .Text | safeHTML }}|END`)
+ b.WithTemplatesAdded("docs/_markup/render-link.html", `Link docs section: {{ .Text | safeHTML }}|END`)
+ b.WithTemplatesAdded("_default/_markup/render-image.html", `IMAGE: {{ .Page.Title }}||{{ .Destination | safeURL }}|Title: {{ .Title | safeHTML }}|Text: {{ .Text | safeHTML }}|END`)
+
+ b.WithContent("customview/p1.md", `---
+title: Custom View
+---
+
+{{< myshortcode6 >}}
+
+ `, "blog/p1.md", `---
+title: Cool Page
+---
+
+[First Link](https://www.google.com "Google's Homepage")
+
+{{< myshortcode3 >}}
+
+[Second Link](https://www.google.com "Google's Homepage")
+
+Image:
+
+![Drag Racing](/images/Dragster.jpg "image title")
+
+
+`, "blog/p2.md", `---
+title: Cool Page2
+layout: mylayout
+---
+
+{{< myshortcode1 >}}
+
+[Some Text](https://www.google.com "Google's Homepage")
+
+
+
+`, "blog/p3.md", `---
+title: Cool Page3
+---
+
+{{< myshortcode2 >}}
+
+
+`, "docs/docs1.md", `---
+title: Docs 1
+---
+
+
+[Docs 1](https://www.google.com "Google's Homepage")
+
+
+`, "blog/p4.md", `---
+title: Cool Page With Image
+---
+
+Image:
+
+![Drag Racing](/images/Dragster.jpg "image title")
+
+
+`, "blog/p5.md", `---
+title: Cool Page With Markdownify
+---
+
+{{< myshortcode4 >}}
+Inner Link: [Inner Link](https://www.google.com "Google's Homepage")
+{{< /myshortcode4 >}}
+
+`, "blog/p6.md", `---
+title: With RenderString
+---
+
+{{< myshortcode5 >}}Inner Link: [Inner Link](https://www.gohugo.io "Hugo's Homepage"){{< /myshortcode5 >}}
+
+`)
+ b.Build(BuildCfg{})
+ b.AssertFileContent("public/blog/p1/index.html", `
+Cool Page|https://www.google.com|Title: Google's Homepage|Text: First Link|END
+Text: Second
+SHORT3|
+IMAGE: Cool Page||/images/Dragster.jpg|Title: image title|Text: Drag Racing|END
+`)
+
+ b.AssertFileContent("public/customview/p1/index.html", `.Render: myrender: Custom View|P4: PARTIAL4`)
+ b.AssertFileContent("public/blog/p2/index.html", `PARTIAL`)
+ b.AssertFileContent("public/blog/p3/index.html", `PARTIAL3`)
+ b.AssertFileContent("public/docs/docs1/index.html", `Link docs section: Docs 1|END`)
+ b.AssertFileContent("public/blog/p4/index.html", `IMAGE: Cool Page With Image||/images/Dragster.jpg|Title: image title|Text: Drag Racing|END
`)
+ // The regular markdownify func currently gets regular links.
+ b.AssertFileContent("public/blog/p5/index.html", "Inner Link: Inner Link\n")
+
+ b.AssertFileContent("public/blog/p6/index.html",
+ "Inner Inline: Inner Link: With RenderString|https://www.gohugo.io|Title: Hugo's Homepage|Text: Inner Link|END",
+ "Inner Block: Inner Link: With RenderString|https://www.gohugo.io|Title: Hugo's Homepage|Text: Inner Link|END
",
+ )
+
+ b.EditFiles(
+ "layouts/_default/_markup/render-link.html", `EDITED: {{ .Destination | safeURL }}|`,
+ "layouts/_default/_markup/render-image.html", `IMAGE EDITED: {{ .Destination | safeURL }}|`,
+ "layouts/docs/_markup/render-link.html", `DOCS EDITED: {{ .Destination | safeURL }}|`,
+ "layouts/partials/mypartial1.html", `PARTIAL1_EDITED`,
+ "layouts/partials/mypartial3.html", `PARTIAL3_EDITED`,
+ "layouts/partials/mypartial4.html", `PARTIAL4_EDITED`,
+ "layouts/shortcodes/myshortcode3.html", `SHORT3_EDITED|`,
+ )
+
+ b.Build(BuildCfg{})
+ b.AssertFileContent("public/customview/p1/index.html", `.Render: myrender: Custom View|P4: PARTIAL4_EDITED`)
+ b.AssertFileContent("public/blog/p1/index.html", `EDITED: https://www.google.com|
`, "SHORT3_EDITED|")
+ b.AssertFileContent("public/blog/p2/index.html", `PARTIAL1_EDITED`)
+ b.AssertFileContent("public/blog/p3/index.html", `PARTIAL3_EDITED`)
+ b.AssertFileContent("public/docs/docs1/index.html", `DOCS EDITED: https://www.google.com|
`)
+ b.AssertFileContent("public/blog/p4/index.html", `IMAGE EDITED: /images/Dragster.jpg|`)
+ b.AssertFileContent("public/blog/p6/index.html", "Inner Link: EDITED: https://www.gohugo.io|
")
+
+}
+
+func TestRenderHooksRSS(t *testing.T) {
+
+ b := newTestSitesBuilder(t)
+
+ b.WithTemplates("index.html", `
+{{ $p := site.GetPage "p1.md" }}
+
+P1: {{ $p.Content }}
+
+ `, "index.xml", `
+
+{{ $p2 := site.GetPage "p2.md" }}
+{{ $p3 := site.GetPage "p3.md" }}
+
+P2: {{ $p2.Content }}
+P3: {{ $p3.Content }}
+
+
+ `,
+ "_default/_markup/render-link.html", `html-link: {{ .Destination | safeURL }}|`,
+ "_default/_markup/render-link.rss.xml", `xml-link: {{ .Destination | safeURL }}|`,
+ )
+
+ b.WithContent("p1.md", `---
+title: "p1"
+---
+P1. [I'm an inline-style link](https://www.gohugo.io)
+
+
+`, "p2.md", `---
+title: "p2"
+---
+P1. [I'm an inline-style link](https://www.bep.is)
+
+
+`,
+ "p3.md", `---
+title: "p2"
+outputs: ["rss"]
+---
+P3. [I'm an inline-style link](https://www.example.org)
+
+`,
+ )
+
+ b.Build(BuildCfg{})
+
+ b.AssertFileContent("public/index.html", "P1: P1. html-link: https://www.gohugo.io|
")
+ b.AssertFileContent("public/index.xml", `
+P2: P1. xml-link: https://www.bep.is|
+P3: P3. xml-link: https://www.example.org|
+`)
+
+}
diff --git a/hugolib/filesystems/basefs.go b/hugolib/filesystems/basefs.go
index de6baa130d7..cdc39ce61cb 100644
--- a/hugolib/filesystems/basefs.go
+++ b/hugolib/filesystems/basefs.go
@@ -126,10 +126,28 @@ type SourceFilesystems struct {
StaticDirs []hugofs.FileMetaInfo
}
+// FileSystems returns the FileSystems relevant for the change detection
+// in server mode.
+// Note: This does currently not return any static fs.
+func (s *SourceFilesystems) FileSystems() []*SourceFilesystem {
+ return []*SourceFilesystem{
+ s.Content,
+ s.Data,
+ s.I18n,
+ s.Layouts,
+ s.Archetypes,
+ // TODO(bep) static
+ }
+
+}
+
// A SourceFilesystem holds the filesystem for a given source type in Hugo (data,
// i18n, layouts, static) and additional metadata to be able to use that filesystem
// in server mode.
type SourceFilesystem struct {
+ // Name matches one in files.ComponentFolders
+ Name string
+
// This is a virtual composite filesystem. It expects path relative to a context.
Fs afero.Fs
@@ -275,6 +293,19 @@ func (d *SourceFilesystem) Contains(filename string) bool {
return false
}
+// Path returns the relative path to the given filename if it is a member of
+// of the current filesystem, an empty string if not.
+func (d *SourceFilesystem) Path(filename string) string {
+ for _, dir := range d.Dirs {
+ meta := dir.Meta()
+ if strings.HasPrefix(filename, meta.Filename()) {
+ p := strings.TrimPrefix(strings.TrimPrefix(filename, meta.Filename()), filePathSeparator)
+ return p
+ }
+ }
+ return ""
+}
+
// RealDirs gets a list of absolute paths to directories starting from the given
// path.
func (d *SourceFilesystem) RealDirs(from string) []string {
@@ -349,12 +380,14 @@ func newSourceFilesystemsBuilder(p *paths.Paths, logger *loggers.Logger, b *Base
return &sourceFilesystemsBuilder{p: p, logger: logger, sourceFs: sourceFs, theBigFs: b.theBigFs, result: &SourceFilesystems{}}
}
-func (b *sourceFilesystemsBuilder) newSourceFilesystem(fs afero.Fs, dirs []hugofs.FileMetaInfo) *SourceFilesystem {
+func (b *sourceFilesystemsBuilder) newSourceFilesystem(name string, fs afero.Fs, dirs []hugofs.FileMetaInfo) *SourceFilesystem {
return &SourceFilesystem{
+ Name: name,
Fs: fs,
Dirs: dirs,
}
}
+
func (b *sourceFilesystemsBuilder) Build() (*SourceFilesystems, error) {
if b.theBigFs == nil {
@@ -369,12 +402,12 @@ func (b *sourceFilesystemsBuilder) Build() (*SourceFilesystems, error) {
createView := func(componentID string) *SourceFilesystem {
if b.theBigFs == nil || b.theBigFs.overlayMounts == nil {
- return b.newSourceFilesystem(hugofs.NoOpFs, nil)
+ return b.newSourceFilesystem(componentID, hugofs.NoOpFs, nil)
}
dirs := b.theBigFs.overlayDirs[componentID]
- return b.newSourceFilesystem(afero.NewBasePathFs(b.theBigFs.overlayMounts, componentID), dirs)
+ return b.newSourceFilesystem(componentID, afero.NewBasePathFs(b.theBigFs.overlayMounts, componentID), dirs)
}
@@ -392,14 +425,14 @@ func (b *sourceFilesystemsBuilder) Build() (*SourceFilesystems, error) {
return nil, err
}
- b.result.Data = b.newSourceFilesystem(dataFs, dataDirs)
+ b.result.Data = b.newSourceFilesystem(files.ComponentFolderData, dataFs, dataDirs)
i18nDirs := b.theBigFs.overlayDirs[files.ComponentFolderI18n]
i18nFs, err := hugofs.NewSliceFs(i18nDirs...)
if err != nil {
return nil, err
}
- b.result.I18n = b.newSourceFilesystem(i18nFs, i18nDirs)
+ b.result.I18n = b.newSourceFilesystem(files.ComponentFolderI18n, i18nFs, i18nDirs)
contentDirs := b.theBigFs.overlayDirs[files.ComponentFolderContent]
contentBfs := afero.NewBasePathFs(b.theBigFs.overlayMountsContent, files.ComponentFolderContent)
@@ -409,7 +442,7 @@ func (b *sourceFilesystemsBuilder) Build() (*SourceFilesystems, error) {
return nil, errors.Wrap(err, "create content filesystem")
}
- b.result.Content = b.newSourceFilesystem(contentFs, contentDirs)
+ b.result.Content = b.newSourceFilesystem(files.ComponentFolderContent, contentFs, contentDirs)
b.result.Work = afero.NewReadOnlyFs(b.theBigFs.overlayFull)
@@ -421,13 +454,13 @@ func (b *sourceFilesystemsBuilder) Build() (*SourceFilesystems, error) {
if b.theBigFs.staticPerLanguage != nil {
// Multihost mode
for k, v := range b.theBigFs.staticPerLanguage {
- sfs := b.newSourceFilesystem(v, b.result.StaticDirs)
+ sfs := b.newSourceFilesystem(files.ComponentFolderStatic, v, b.result.StaticDirs)
sfs.PublishFolder = k
ms[k] = sfs
}
} else {
bfs := afero.NewBasePathFs(b.theBigFs.overlayMountsStatic, files.ComponentFolderStatic)
- ms[""] = b.newSourceFilesystem(bfs, b.result.StaticDirs)
+ ms[""] = b.newSourceFilesystem(files.ComponentFolderStatic, bfs, b.result.StaticDirs)
}
return b.result, nil
diff --git a/hugolib/hugo_modules_test.go b/hugolib/hugo_modules_test.go
index 40185e051c2..90044327533 100644
--- a/hugolib/hugo_modules_test.go
+++ b/hugolib/hugo_modules_test.go
@@ -40,6 +40,9 @@ import (
// TODO(bep) this fails when testmodBuilder is also building ...
func TestHugoModules(t *testing.T) {
+ if !isCI() {
+ t.Skip("skip (relative) long running modules test when running locally")
+ }
t.Parallel()
if !isCI() || hugo.GoMinorVersion() < 12 {
diff --git a/hugolib/hugo_sites.go b/hugolib/hugo_sites.go
index c71dcaa5940..526f39fca9a 100644
--- a/hugolib/hugo_sites.go
+++ b/hugolib/hugo_sites.go
@@ -20,6 +20,8 @@ import (
"strings"
"sync"
+ "github.com/gohugoio/hugo/identity"
+
radix "github.com/armon/go-radix"
"github.com/gohugoio/hugo/output"
@@ -411,7 +413,6 @@ func applyDeps(cfg deps.DepsCfg, sites ...*Site) error {
}
d.OutputFormatsConfig = s.outputFormatsConfig
}
-
}
return nil
@@ -806,12 +807,40 @@ func (h *HugoSites) findPagesByKindIn(kind string, inPages page.Pages) page.Page
return h.Sites[0].findPagesByKindIn(kind, inPages)
}
-func (h *HugoSites) findPagesByShortcode(shortcode string) page.Pages {
- var pages page.Pages
+func (h *HugoSites) resetPageStateFromEvents(idset identity.Identities) {
+
for _, s := range h.Sites {
- pages = append(pages, s.findPagesByShortcode(shortcode)...)
+ PAGES:
+ for _, p := range s.rawAllPages {
+ OUTPUTS:
+ for _, po := range p.pageOutputs {
+ if po.cp == nil {
+ continue
+ }
+ for id, _ := range idset {
+ if po.cp.dependencyTracker.Search(id) != nil {
+ po.cp.Reset()
+ p.forceRender = true
+ continue OUTPUTS
+ }
+ }
+ }
+
+ for _, s := range p.shortcodeState.shortcodes {
+ for id, _ := range idset {
+ if idm, ok := s.info.(identity.Manager); ok && idm.Search(id) != nil {
+ for _, po := range p.pageOutputs {
+ if po.cp != nil {
+ po.cp.Reset()
+ }
+ }
+ p.forceRender = true
+ continue PAGES
+ }
+ }
+ }
+ }
}
- return pages
}
// Used in partial reloading to determine if the change is in a bundle.
diff --git a/hugolib/hugo_sites_build.go b/hugolib/hugo_sites_build.go
index a70a19e7c31..d749ff581d5 100644
--- a/hugolib/hugo_sites_build.go
+++ b/hugolib/hugo_sites_build.go
@@ -71,7 +71,7 @@ func (h *HugoSites) Build(config BuildCfg, events ...fsnotify.Event) error {
if conf.whatChanged == nil {
// Assume everything has changed
- conf.whatChanged = &whatChanged{source: true, other: true}
+ conf.whatChanged = &whatChanged{source: true}
}
var prepareErr error
diff --git a/hugolib/page.go b/hugolib/page.go
index 56202f5e0b1..6f7e3c60f43 100644
--- a/hugolib/page.go
+++ b/hugolib/page.go
@@ -23,6 +23,12 @@ import (
"sort"
"strings"
+ "github.com/mitchellh/mapstructure"
+
+ "github.com/gohugoio/hugo/tpl"
+
+ "github.com/gohugoio/hugo/identity"
+
"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/common/maps"
@@ -43,9 +49,11 @@ import (
"github.com/gohugoio/hugo/media"
"github.com/gohugoio/hugo/source"
+ "github.com/spf13/cast"
"github.com/gohugoio/hugo/common/collections"
"github.com/gohugoio/hugo/common/text"
+ "github.com/gohugoio/hugo/markup/converter/hooks"
"github.com/gohugoio/hugo/resources"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/resources/resource"
@@ -59,7 +67,11 @@ var (
var (
pageTypesProvider = resource.NewResourceTypesProvider(media.OctetType, pageResourceType)
- nopPageOutput = &pageOutput{pagePerOutputProviders: nopPagePerOutput}
+ nopPageOutput = &pageOutput{
+ pagePerOutputProviders: nopPagePerOutput,
+ ContentProvider: page.NopPage,
+ TableOfContentsProvider: page.NopPage,
+ }
)
// pageContext provides contextual information about this page, for error
@@ -317,6 +329,105 @@ func (ps *pageState) initCommonProviders(pp pagePaths) error {
return nil
}
+// TODO1 remove
+func (ps *pageState) initOutputFormats() error {
+ canReuse := true
+ if len(ps.pageOutputs) == 0 {
+ return nil
+ }
+
+ if true {
+ return nil
+ }
+
+ c := ps.getContentConverter()
+ if c == nil || !c.Supports(converter.FeatureRenderHooks) {
+ return nil
+ }
+
+ templSet := make(map[identity.Identity]bool)
+
+ for _, o := range ps.pageOutputs {
+ if !o.render || o.cp == nil {
+ continue
+ }
+ h, err := ps.createRenderHooks(o.f)
+ if err != nil {
+ return err
+ }
+ if h == nil {
+ continue
+ }
+ if canReuse {
+ for _, r := range []hooks.LinkRenderer{h.LinkRenderer, h.ImageRenderer} {
+ if !canReuse {
+ break
+ }
+ if r != nil {
+ if len(templSet) != 0 {
+ // There may be a template per output format.
+ // In that case we need to re-render.
+ canReuse = templSet[r.GetIdentity()]
+ }
+ templSet[r.GetIdentity()] = true
+ }
+ }
+ }
+ o.cp.renderHooks = h
+ o.cp.renderHooksHaveVariants = !canReuse
+ }
+
+ return nil
+}
+
+func (p *pageState) createRenderHooks(f output.Format) (*hooks.Render, error) {
+
+ layoutDescriptor := p.getLayoutDescriptor()
+ layoutDescriptor.RenderingHook = true
+ layoutDescriptor.LayoutOverride = false
+ layoutDescriptor.Layout = ""
+
+ layoutDescriptor.Kind = "render-link"
+ linkLayouts, err := p.s.layoutHandler.For(layoutDescriptor, f)
+ if err != nil {
+ return nil, err
+ }
+
+ layoutDescriptor.Kind = "render-image"
+ imageLayouts, err := p.s.layoutHandler.For(layoutDescriptor, f)
+ if err != nil {
+ return nil, err
+ }
+
+ if linkLayouts == nil && imageLayouts == nil {
+ return nil, nil
+ }
+
+ var linkRenderer hooks.LinkRenderer
+ var imageRenderer hooks.LinkRenderer
+
+ if templ, found := p.s.lookupTemplate(linkLayouts...); found {
+ linkRenderer = contentLinkRenderer{
+ templateHandler: p.s.Tmpl,
+ Provider: templ.(tpl.Info),
+ templ: templ,
+ }
+ }
+
+ if templ, found := p.s.lookupTemplate(imageLayouts...); found {
+ imageRenderer = contentLinkRenderer{
+ templateHandler: p.s.Tmpl,
+ Provider: templ.(tpl.Info),
+ templ: templ,
+ }
+ }
+
+ return &hooks.Render{
+ LinkRenderer: linkRenderer,
+ ImageRenderer: imageRenderer,
+ }, nil
+}
+
func (p *pageState) getLayoutDescriptor() output.LayoutDescriptor {
p.layoutDescriptorInit.Do(func() {
var section string
@@ -464,11 +575,77 @@ func (p *pageState) AlternativeOutputFormats() page.OutputFormats {
return o
}
-func (p *pageState) Render(layout ...string) template.HTML {
+type renderStringOpts struct {
+ Display string
+ Markup string
+}
+
+var defualtRenderStringOpts = renderStringOpts{
+ Display: "inline",
+ Markup: "markdown",
+}
+
+// TODO1 option: map: display: inline/block (default inline) + markup (default page.markup)?
+func (p *pageState) RenderString(args ...interface{}) (template.HTML, error) {
+ if len(args) < 1 || len(args) > 2 {
+ return "", errors.New("want 1 or 2 arguments")
+ }
+
+ var s string
+ opts := defualtRenderStringOpts
+ sidx := 1
+
+ if len(args) == 1 {
+ sidx = 0
+ } else {
+ m, ok := args[0].(map[string]interface{})
+ if !ok {
+ return "", errors.New("first argument must be a map")
+ }
+
+ if err := mapstructure.WeakDecode(m, &opts); err != nil {
+ return "", errors.WithMessage(err, "failed to decode options")
+ }
+ }
+
+ var err error
+ s, err = cast.ToStringE(args[sidx])
+ if err != nil {
+ return "", err
+ }
+
+ c, err := p.pageOutput.cp.renderContent([]byte(s), false)
+ if err != nil {
+ return "", p.wrapError(err)
+ }
+
+ b := c.Bytes()
+
+ if opts.Display == "inline" {
+ // We may have to rething this in the future we get other
+ // renderers.
+ b = p.s.ContentSpec.TrimShortHTML(b)
+ }
+
+ return template.HTML(string(b)), nil
+}
+
+func (p *pageState) addDependency(dep identity.Provider) {
+ if !p.s.running() || p.pageOutput.cp == nil {
+ return
+ }
+ p.pageOutput.cp.dependencyTracker.Add(dep)
+}
+
+func (p *pageState) RenderWithTemplateInfo(info tpl.Info, layout ...string) (template.HTML, error) {
+ p.addDependency(info)
+ return p.Render(layout...)
+}
+
+func (p *pageState) Render(layout ...string) (template.HTML, error) {
l, err := p.getLayouts(layout...)
if err != nil {
- p.s.SendError(p.wrapError(errors.Errorf(".Render: failed to resolve layout %v", layout)))
- return ""
+ return "", p.wrapError(errors.Errorf("failed to resolve layout %v", layout))
}
for _, layout := range l {
@@ -479,17 +656,18 @@ func (p *pageState) Render(layout ...string) template.HTML {
// We default to good old HTML.
templ, _ = p.s.Tmpl.Lookup(layout + ".html")
}
+
if templ != nil {
+ p.addDependency(templ.(tpl.Info))
res, err := executeToString(p.s.Tmpl, templ, p)
if err != nil {
- p.s.SendError(p.wrapError(errors.Wrapf(err, ".Render: failed to execute template %q v", layout)))
- return ""
+ return "", p.wrapError(errors.Wrapf(err, "failed to execute template %q v", layout))
}
- return template.HTML(res)
+ return template.HTML(res), nil
}
}
- return ""
+ return "", nil
}
@@ -745,15 +923,34 @@ func (p *pageState) shiftToOutputFormat(isRenderingSite bool, idx int) error {
p.pageOutput.paginator.reset()
}
- if idx > 0 {
- // Check if we can reuse content from one of the previous formats.
- for i := idx - 1; i >= 0; i-- {
- po := p.pageOutputs[i]
- if po.cp != nil && po.cp.reuse {
- p.pageOutput.cp = po.cp
- break
+ // TODO1 move this logic.
+ if isRenderingSite {
+ cp := p.pageOutput.cp
+ if cp == nil {
+
+ // Look for content to reuse.
+ for i := 0; i < len(p.pageOutputs); i++ {
+ if i == idx {
+ continue
+ }
+ po := p.pageOutputs[i]
+
+ if po.cp != nil && po.cp.reuse {
+ cp = po.cp
+ break
+ }
+ }
+ }
+
+ if cp == nil {
+ var err error
+ cp, err = newPageContentOutput(p, p.pageOutput)
+ if err != nil {
+ return err
}
}
+ p.pageOutput.initContentProvider(cp)
+ p.pageOutput.cp = cp
}
for _, r := range p.Resources().ByType(pageResourceType) {
diff --git a/hugolib/page__content.go b/hugolib/page__content.go
index 1919fb17154..013ab3072b7 100644
--- a/hugolib/page__content.go
+++ b/hugolib/page__content.go
@@ -30,8 +30,7 @@ var (
type pageContent struct {
renderable bool
selfLayout string
-
- truncated bool
+ truncated bool
cmap *pageContentMap
diff --git a/hugolib/page__meta.go b/hugolib/page__meta.go
index 1fc69c21826..37e333cb40b 100644
--- a/hugolib/page__meta.go
+++ b/hugolib/page__meta.go
@@ -592,7 +592,7 @@ func (pm *pageMeta) setMetadata(bucket *pagesMapBucket, p *pageState, frontmatte
return nil
}
-func (p *pageMeta) applyDefaultValues() error {
+func (p *pageMeta) applyDefaultValues(ps *pageState) error {
if p.markup == "" {
if !p.File().IsZero() {
// Fall back to file extension
@@ -656,15 +656,19 @@ func (p *pageMeta) applyDefaultValues() error {
return errors.Errorf("no content renderer found for markup %q", p.markup)
}
- cpp, err := cp.New(converter.DocumentContext{
- DocumentID: p.f.UniqueID(),
- DocumentName: p.f.Path(),
- ConfigOverrides: renderingConfigOverrides,
- })
+ cpp, err := cp.New(
+ converter.DocumentContext{
+ Document: newPageForRenderHook(ps),
+ DocumentID: p.f.UniqueID(),
+ DocumentName: p.f.Path(),
+ ConfigOverrides: renderingConfigOverrides,
+ },
+ )
if err != nil {
return err
}
+
p.contentConverter = cpp
}
diff --git a/hugolib/page__new.go b/hugolib/page__new.go
index 99bf305aa58..c6daa36646c 100644
--- a/hugolib/page__new.go
+++ b/hugolib/page__new.go
@@ -112,7 +112,7 @@ func newPageFromMeta(meta map[string]interface{}, metaProvider *pageMeta) (*page
}
}
- if err := metaProvider.applyDefaultValues(); err != nil {
+ if err := metaProvider.applyDefaultValues(ps); err != nil {
return err
}
@@ -134,7 +134,7 @@ func newPageFromMeta(meta map[string]interface{}, metaProvider *pageMeta) (*page
}
makeOut := func(f output.Format, render bool) *pageOutput {
- return newPageOutput(nil, ps, pp, f, render)
+ return newPageOutput(ps, pp, f, render)
}
if ps.m.standalone {
@@ -158,6 +158,10 @@ func newPageFromMeta(meta map[string]interface{}, metaProvider *pageMeta) (*page
return nil, err
}
+ if err := ps.initOutputFormats(); err != nil {
+ return nil, err
+ }
+
return nil, nil
})
@@ -234,7 +238,7 @@ func newPageWithContent(f *fileInfo, s *Site, bundled bool, content resource.Ope
return ps.wrapError(err)
}
- if err := metaProvider.applyDefaultValues(); err != nil {
+ if err := metaProvider.applyDefaultValues(ps); err != nil {
return err
}
@@ -242,10 +246,6 @@ func newPageWithContent(f *fileInfo, s *Site, bundled bool, content resource.Ope
}
ps.init.Add(func() (interface{}, error) {
- reuseContent := ps.renderable && !ps.shortcodeState.hasShortcodes()
-
- // Creates what's needed for each output format.
- contentPerOutput := newPageContentOutput(ps)
pp, err := newPagePaths(s, ps, metaProvider)
if err != nil {
@@ -264,18 +264,18 @@ func newPageWithContent(f *fileInfo, s *Site, bundled bool, content resource.Ope
}
_, render := outputFormatsForPage.GetByName(f.Name)
- var contentProvider *pageContentOutput
- if reuseContent && i > 0 {
- contentProvider = ps.pageOutputs[0].cp
- } else {
- var err error
- contentProvider, err = contentPerOutput(f)
+ po := newPageOutput(ps, pp, f, render)
+
+ // Create a content provider for the first,
+ // we may be able to reuse it.
+ if i == 0 {
+ contentProvider, err := newPageContentOutput(ps, po)
if err != nil {
return nil, err
}
+ po.initContentProvider(contentProvider)
}
- po := newPageOutput(contentProvider, ps, pp, f, render)
ps.pageOutputs[i] = po
created[f.Name] = po
}
@@ -284,6 +284,10 @@ func newPageWithContent(f *fileInfo, s *Site, bundled bool, content resource.Ope
return nil, err
}
+ if err := ps.initOutputFormats(); err != nil {
+ return nil, err
+ }
+
return nil, nil
})
diff --git a/hugolib/page__output.go b/hugolib/page__output.go
index 764c46a937b..183bf010d4e 100644
--- a/hugolib/page__output.go
+++ b/hugolib/page__output.go
@@ -14,13 +14,13 @@
package hugolib
import (
+ "github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/output"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/resources/resource"
)
func newPageOutput(
- cp *pageContentOutput, // may be nil
ps *pageState,
pp pagePaths,
f output.Format,
@@ -45,36 +45,23 @@ func newPageOutput(
paginatorProvider = pag
}
- var (
- contentProvider page.ContentProvider = page.NopPage
- tableOfContentsProvider page.TableOfContentsProvider = page.NopPage
- )
-
- if cp != nil {
- contentProvider = cp
- tableOfContentsProvider = cp
- }
-
providers := struct {
- page.ContentProvider
- page.TableOfContentsProvider
page.PaginatorProvider
resource.ResourceLinksProvider
targetPather
}{
- contentProvider,
- tableOfContentsProvider,
paginatorProvider,
linksProvider,
targetPathsProvider,
}
po := &pageOutput{
- f: f,
- cp: cp,
- pagePerOutputProviders: providers,
- render: render,
- paginator: pag,
+ f: f,
+ pagePerOutputProviders: providers,
+ ContentProvider: page.NopPage,
+ TableOfContentsProvider: page.NopPage,
+ render: render,
+ paginator: pag,
}
return po
@@ -94,16 +81,54 @@ type pageOutput struct {
// used in template(s).
paginator *pagePaginator
- // This interface provides the functionality that is specific for this
+ // These interface provides the functionality that is specific for this
// output format.
pagePerOutputProviders
+ page.ContentProvider
+ page.TableOfContentsProvider
- // This may be nil.
+ // May be nil.
cp *pageContentOutput
}
+func (o *pageOutput) initRenderHooks() error {
+ if o.cp == nil {
+ return nil
+ }
+
+ ps := o.cp.p
+
+ c := ps.getContentConverter()
+ if c == nil || !c.Supports(converter.FeatureRenderHooks) {
+ return nil
+ }
+
+ h, err := ps.createRenderHooks(o.f)
+ if err != nil {
+ return err
+ }
+ if h == nil {
+ return nil
+ }
+
+ o.cp.renderHooks = h
+
+ return nil
+
+}
+
+func (p *pageOutput) initContentProvider(cp *pageContentOutput) {
+ if cp == nil {
+ return
+ }
+ p.ContentProvider = cp
+ p.TableOfContentsProvider = cp
+ p.cp = cp
+}
+
func (p *pageOutput) enablePlaceholders() {
if p.cp != nil {
p.cp.enablePlaceholders()
}
+
}
diff --git a/hugolib/page__per_output.go b/hugolib/page__per_output.go
index d3a32e15c2a..a7ae9a6d6dd 100644
--- a/hugolib/page__per_output.go
+++ b/hugolib/page__per_output.go
@@ -23,6 +23,10 @@ import (
"sync"
"unicode/utf8"
+ "github.com/gohugoio/hugo/identity"
+
+ "github.com/gohugoio/hugo/markup/converter/hooks"
+
"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/lazy"
@@ -58,152 +62,176 @@ var (
}
)
-func newPageContentOutput(p *pageState) func(f output.Format) (*pageContentOutput, error) {
+var pageContentOutputDependenciesID = identity.KeyValueIdentity{Key: "pageOutput", Value: "dependencies"}
+
+func newPageContentOutput(p *pageState, po *pageOutput) (*pageContentOutput, error) {
parent := p.init
- return func(f output.Format) (*pageContentOutput, error) {
- cp := &pageContentOutput{
- p: p,
- f: f,
- }
+ var dependencyTracker identity.Manager
+ if p.s.running() {
+ dependencyTracker = identity.NewManager(pageContentOutputDependenciesID)
+ }
+
+ cp := &pageContentOutput{
+ dependencyTracker: dependencyTracker,
+ p: p,
+ f: po.f,
+ }
- initContent := func() (err error) {
- if p.cmap == nil {
- // Nothing to do.
- return nil
+ initContent := func() (err error) {
+ if p.cmap == nil {
+ // Nothing to do.
+ return nil
+ }
+ defer func() {
+ // See https://github.com/gohugoio/hugo/issues/6210
+ if r := recover(); r != nil {
+ err = fmt.Errorf("%s", r)
+ p.s.Log.ERROR.Printf("[BUG] Got panic:\n%s\n%s", r, string(debug.Stack()))
}
- defer func() {
- // See https://github.com/gohugoio/hugo/issues/6210
- if r := recover(); r != nil {
- err = fmt.Errorf("%s", r)
- p.s.Log.ERROR.Printf("[BUG] Got panic:\n%s\n%s", r, string(debug.Stack()))
- }
- }()
+ }()
- var hasVariants bool
+ if err := po.initRenderHooks(); err != nil {
+ return err
+ }
- cp.contentPlaceholders, hasVariants, err = p.shortcodeState.renderShortcodesForPage(p, f)
- if err != nil {
- return err
- }
+ var hasVariants bool
- if p.render && !hasVariants {
- // We can reuse this for the other output formats
- cp.enableReuse()
- }
+ f := po.f
+ cp.contentPlaceholders, hasVariants, err = p.shortcodeState.renderShortcodesForPage(p, f)
+ if err != nil {
+ return err
+ }
- cp.workContent = p.contentToRender(cp.contentPlaceholders)
+ // TODO1 po.render &&
+ enableReuse := !hasVariants
+ enableReuse = enableReuse && !cp.renderHooksHaveVariants
+
+ if enableReuse {
+ // Reuse this for the other output formats.
+ // We may improve on this, but we really want to avoid re-rendering the content
+ // to all output formats.
+ // The current rule is that if you need output format-aware shortcodes or
+ // content rendering hooks, create a output format-specific template, e.g.
+ // myshortcode.amp.html.
+ cp.enableReuse()
+ }
- isHTML := cp.p.m.markup == "html"
+ cp.workContent = p.contentToRender(cp.contentPlaceholders)
- if p.renderable {
- if !isHTML {
- r, err := cp.renderContent(cp.workContent)
- if err != nil {
- return err
- }
- cp.convertedResult = r
- cp.workContent = r.Bytes()
+ isHTML := cp.p.m.markup == "html"
- if _, ok := r.(converter.TableOfContentsProvider); !ok {
- tmpContent, tmpTableOfContents := helpers.ExtractTOC(cp.workContent)
- cp.tableOfContents = helpers.BytesToHTML(tmpTableOfContents)
- cp.workContent = tmpContent
- }
+ if p.renderable {
+ if !isHTML {
+ r, err := cp.renderContent(cp.workContent, true)
+ if err != nil {
+ return err
}
- if cp.placeholdersEnabled {
- // ToC was accessed via .Page.TableOfContents in the shortcode,
- // at a time when the ToC wasn't ready.
- cp.contentPlaceholders[tocShortcodePlaceholder] = string(cp.tableOfContents)
- }
+ cp.workContent = r.Bytes()
- if p.cmap.hasNonMarkdownShortcode || cp.placeholdersEnabled {
- // There are one or more replacement tokens to be replaced.
- cp.workContent, err = replaceShortcodeTokens(cp.workContent, cp.contentPlaceholders)
- if err != nil {
- return err
- }
+ if tocProvider, ok := r.(converter.TableOfContentsProvider); ok {
+ cfg := p.s.ContentSpec.Converters.GetMarkupConfig()
+ cp.tableOfContents = template.HTML(
+ tocProvider.TableOfContents().ToHTML(
+ cfg.TableOfContents.StartLevel,
+ cfg.TableOfContents.EndLevel,
+ cfg.TableOfContents.Ordered,
+ ),
+ )
+ } else {
+ tmpContent, tmpTableOfContents := helpers.ExtractTOC(cp.workContent)
+ cp.tableOfContents = helpers.BytesToHTML(tmpTableOfContents)
+ cp.workContent = tmpContent
}
+ }
+
+ if cp.placeholdersEnabled {
+ // ToC was accessed via .Page.TableOfContents in the shortcode,
+ // at a time when the ToC wasn't ready.
+ cp.contentPlaceholders[tocShortcodePlaceholder] = string(cp.tableOfContents)
+ }
- if cp.p.source.hasSummaryDivider {
- if isHTML {
- src := p.source.parsed.Input()
+ if p.cmap.hasNonMarkdownShortcode || cp.placeholdersEnabled {
+ // There are one or more replacement tokens to be replaced.
+ cp.workContent, err = replaceShortcodeTokens(cp.workContent, cp.contentPlaceholders)
+ if err != nil {
+ return err
+ }
+ }
- // Use the summary sections as they are provided by the user.
- if p.source.posSummaryEnd != -1 {
- cp.summary = helpers.BytesToHTML(src[p.source.posMainContent:p.source.posSummaryEnd])
- }
+ if cp.p.source.hasSummaryDivider {
+ if isHTML {
+ src := p.source.parsed.Input()
- if cp.p.source.posBodyStart != -1 {
- cp.workContent = src[cp.p.source.posBodyStart:]
- }
+ // Use the summary sections as they are provided by the user.
+ if p.source.posSummaryEnd != -1 {
+ cp.summary = helpers.BytesToHTML(src[p.source.posMainContent:p.source.posSummaryEnd])
+ }
- } else {
- summary, content, err := splitUserDefinedSummaryAndContent(cp.p.m.markup, cp.workContent)
- if err != nil {
- cp.p.s.Log.ERROR.Printf("Failed to set user defined summary for page %q: %s", cp.p.pathOrTitle(), err)
- } else {
- cp.workContent = content
- cp.summary = helpers.BytesToHTML(summary)
- }
+ if cp.p.source.posBodyStart != -1 {
+ cp.workContent = src[cp.p.source.posBodyStart:]
}
- } else if cp.p.m.summary != "" {
- b, err := cp.p.getContentConverter().Convert(
- converter.RenderContext{
- Src: []byte(cp.p.m.summary),
- },
- )
+ } else {
+ summary, content, err := splitUserDefinedSummaryAndContent(cp.p.m.markup, cp.workContent)
if err != nil {
- return err
+ cp.p.s.Log.ERROR.Printf("Failed to set user defined summary for page %q: %s", cp.p.pathOrTitle(), err)
+ } else {
+ cp.workContent = content
+ cp.summary = helpers.BytesToHTML(summary)
}
- html := cp.p.s.ContentSpec.TrimShortHTML(b.Bytes())
- cp.summary = helpers.BytesToHTML(html)
}
+ } else if cp.p.m.summary != "" {
+ b, err := cp.renderContent([]byte(cp.p.m.summary), false)
+ if err != nil {
+ return err
+ }
+ html := cp.p.s.ContentSpec.TrimShortHTML(b.Bytes())
+ cp.summary = helpers.BytesToHTML(html)
}
+ }
- cp.content = helpers.BytesToHTML(cp.workContent)
-
- if !p.renderable {
- err := cp.addSelfTemplate()
- return err
- }
-
- return nil
+ cp.content = helpers.BytesToHTML(cp.workContent)
+ if !p.renderable {
+ err := cp.addSelfTemplate()
+ return err
}
- // Recursive loops can only happen in content files with template code (shortcodes etc.)
- // Avoid creating new goroutines if we don't have to.
- needTimeout := !p.renderable || p.shortcodeState.hasShortcodes()
-
- if needTimeout {
- cp.initMain = parent.BranchWithTimeout(p.s.siteCfg.timeout, func(ctx context.Context) (interface{}, error) {
- return nil, initContent()
- })
- } else {
- cp.initMain = parent.Branch(func() (interface{}, error) {
- return nil, initContent()
- })
- }
+ return nil
- cp.initPlain = cp.initMain.Branch(func() (interface{}, error) {
- cp.plain = helpers.StripHTML(string(cp.content))
- cp.plainWords = strings.Fields(cp.plain)
- cp.setWordCounts(p.m.isCJKLanguage)
+ }
- if err := cp.setAutoSummary(); err != nil {
- return err, nil
- }
+ // Recursive loops can only happen in content files with template code (shortcodes etc.)
+ // Avoid creating new goroutines if we don't have to.
+ needTimeout := !p.renderable || p.shortcodeState.hasShortcodes()
+ needTimeout = needTimeout || cp.renderHooks != nil
- return nil, nil
+ if needTimeout {
+ cp.initMain = parent.BranchWithTimeout(p.s.siteCfg.timeout, func(ctx context.Context) (interface{}, error) {
+ return nil, initContent()
+ })
+ } else {
+ cp.initMain = parent.Branch(func() (interface{}, error) {
+ return nil, initContent()
})
+ }
- return cp, nil
+ cp.initPlain = cp.initMain.Branch(func() (interface{}, error) {
+ cp.plain = helpers.StripHTML(string(cp.content))
+ cp.plainWords = strings.Fields(cp.plain)
+ cp.setWordCounts(p.m.isCJKLanguage)
- }
+ if err := cp.setAutoSummary(); err != nil {
+ return err, nil
+ }
+
+ return nil, nil
+ })
+
+ return cp, nil
}
@@ -211,7 +239,7 @@ func newPageContentOutput(p *pageState) func(f output.Format) (*pageContentOutpu
type pageContentOutput struct {
f output.Format
- // If we can safely reuse this for other output formats.
+ // If we can reuse this for other output formats.
reuse bool
reuseInit sync.Once
@@ -224,10 +252,15 @@ type pageContentOutput struct {
placeholdersEnabled bool
placeholdersEnabledInit sync.Once
+ // May be nil.
+ renderHooks *hooks.Render
+ // Set if there are more than one output format variant
+ renderHooksHaveVariants bool // TODO(bep) reimplement this in another way, consolidate with shortcodes
+
// Content state
- workContent []byte
- convertedResult converter.Result
+ workContent []byte
+ dependencyTracker identity.Manager // Set in server mode.
// Temporary storage of placeholders mapped to their content.
// These are shortcodes etc. Some of these will need to be replaced
@@ -248,6 +281,21 @@ type pageContentOutput struct {
readingTime int
}
+func (p *pageContentOutput) trackDependency(id identity.Provider) {
+ if p.dependencyTracker != nil {
+ p.dependencyTracker.Add(id)
+ }
+}
+
+func (p *pageContentOutput) Reset() {
+ if p.dependencyTracker != nil {
+ p.dependencyTracker.Reset()
+ }
+ p.p.initOutputFormats()
+ p.initMain.Reset()
+ p.initPlain.Reset()
+}
+
func (p *pageContentOutput) Content() (interface{}, error) {
if p.p.s.initInit(p.initMain, p.p) {
return p.content, nil
@@ -290,10 +338,6 @@ func (p *pageContentOutput) Summary() template.HTML {
func (p *pageContentOutput) TableOfContents() template.HTML {
p.p.s.initInit(p.initMain, p.p)
- if tocProvider, ok := p.convertedResult.(converter.TableOfContentsProvider); ok {
- cfg := p.p.s.ContentSpec.Converters.GetMarkupConfig()
- return template.HTML(tocProvider.TableOfContents().ToHTML(cfg.TableOfContents.StartLevel, cfg.TableOfContents.EndLevel, cfg.TableOfContents.Ordered))
- }
return p.tableOfContents
}
@@ -331,12 +375,25 @@ func (p *pageContentOutput) setAutoSummary() error {
}
-func (cp *pageContentOutput) renderContent(content []byte) (converter.Result, error) {
- return cp.p.getContentConverter().Convert(
+func (cp *pageContentOutput) renderContent(content []byte, renderTOC bool) (converter.Result, error) {
+ c := cp.p.getContentConverter()
+ r, err := c.Convert(
converter.RenderContext{
- Src: content,
- RenderTOC: true,
+ Src: content,
+ RenderTOC: renderTOC,
+ RenderHooks: cp.renderHooks,
})
+
+ if err == nil {
+ if ids, ok := r.(identity.IdentitiesProvider); ok {
+ for _, v := range ids.GetIdentities() {
+ cp.trackDependency(v)
+ }
+ }
+ }
+
+ return r, err
+
}
func (p *pageContentOutput) setWordCounts(isCJKLanguage bool) {
@@ -392,9 +449,7 @@ func (p *pageContentOutput) enableReuse() {
// these will be shifted out when rendering a given output format.
type pagePerOutputProviders interface {
targetPather
- page.ContentProvider
page.PaginatorProvider
- page.TableOfContentsProvider
resource.ResourceLinksProvider
}
diff --git a/hugolib/page_test.go b/hugolib/page_test.go
index dc8bc821c15..f4bf3ac0040 100644
--- a/hugolib/page_test.go
+++ b/hugolib/page_test.go
@@ -93,12 +93,6 @@ Summary Next Line. {{