summaryrefslogtreecommitdiffstats
path: root/hugolib/page__content.go
diff options
context:
space:
mode:
Diffstat (limited to 'hugolib/page__content.go')
-rw-r--r--hugolib/page__content.go74
1 files changed, 48 insertions, 26 deletions
diff --git a/hugolib/page__content.go b/hugolib/page__content.go
index f10c25d7b..1ef31f0f9 100644
--- a/hugolib/page__content.go
+++ b/hugolib/page__content.go
@@ -20,6 +20,7 @@ import (
"fmt"
"html/template"
"io"
+ "path/filepath"
"strconv"
"strings"
"unicode/utf8"
@@ -54,21 +55,31 @@ type pageContentReplacement struct {
source pageparser.Item
}
-func (m *pageMeta) parseFrontMatter(h *HugoSites, pid uint64, sourceKey string) (*contentParseInfo, error) {
- var openSource hugio.OpenReadSeekCloser
- if m.f != nil {
- meta := m.f.FileInfo().Meta()
- openSource = func() (hugio.ReadSeekCloser, error) {
- r, err := meta.Open()
- if err != nil {
- return nil, fmt.Errorf("failed to open file %q: %w", meta.Filename, err)
+func (m *pageMeta) parseFrontMatter(h *HugoSites, pid uint64) (*contentParseInfo, error) {
+ var (
+ sourceKey string
+ openSource hugio.OpenReadSeekCloser
+ hasContent = m.pageConfig.IsFromContentAdapter
+ )
+
+ if m.f != nil && !hasContent {
+ sourceKey = filepath.ToSlash(m.f.Filename())
+ if !hasContent {
+ meta := m.f.FileInfo().Meta()
+ openSource = func() (hugio.ReadSeekCloser, error) {
+ r, err := meta.Open()
+ if err != nil {
+ return nil, fmt.Errorf("failed to open file %q: %w", meta.Filename, err)
+ }
+ return r, nil
}
- return r, nil
}
+ } else if hasContent {
+ openSource = m.pageConfig.Content.ValueAsOpenReadSeekCloser()
}
if sourceKey == "" {
- sourceKey = strconv.Itoa(int(pid))
+ sourceKey = strconv.FormatUint(pid, 10)
}
pi := &contentParseInfo{
@@ -93,6 +104,11 @@ func (m *pageMeta) parseFrontMatter(h *HugoSites, pid uint64, sourceKey string)
pi.itemsStep1 = items
+ if hasContent {
+ // No front matter.
+ return pi, nil
+ }
+
if err := pi.mapFrontMatter(source); err != nil {
return nil, err
}
@@ -418,6 +434,8 @@ func (c *cachedContent) mustSource() []byte {
func (c *contentParseInfo) contentSource(s resource.StaleInfo) ([]byte, error) {
key := c.sourceKey
+ versionv := s.StaleVersion()
+
v, err := c.h.cacheContentSource.GetOrCreate(key, func(string) (*resources.StaleValue[[]byte], error) {
b, err := c.readSourceAll()
if err != nil {
@@ -426,8 +444,8 @@ func (c *contentParseInfo) contentSource(s resource.StaleInfo) ([]byte, error) {
return &resources.StaleValue[[]byte]{
Value: b,
- IsStaleFunc: func() bool {
- return s.IsStale()
+ StaleVersionFunc: func() uint32 {
+ return s.StaleVersion() - versionv
},
}, nil
})
@@ -487,7 +505,7 @@ type contentPlainPlainWords struct {
func (c *cachedContent) contentRendered(ctx context.Context, cp *pageContentOutput) (contentSummary, error) {
ctx = tpl.Context.DependencyScope.Set(ctx, pageDependencyScopeGlobal)
key := c.pi.sourceKey + "/" + cp.po.f.Name
- versionv := cp.contentRenderedVersion
+ versionv := c.version(cp)
v, err := c.pm.cacheContentRendered.GetOrCreate(key, func(string) (*resources.StaleValue[contentSummary], error) {
cp.po.p.s.Log.Trace(logg.StringFunc(func() string {
@@ -504,8 +522,8 @@ func (c *cachedContent) contentRendered(ctx context.Context, cp *pageContentOutp
}
rs := &resources.StaleValue[contentSummary]{
- IsStaleFunc: func() bool {
- return c.IsStale() || cp.contentRenderedVersion != versionv
+ StaleVersionFunc: func() uint32 {
+ return c.version(cp) - versionv
},
}
@@ -565,15 +583,14 @@ func (c *cachedContent) contentRendered(ctx context.Context, cp *pageContentOutp
var result contentSummary // hasVariants bool
if c.pi.hasSummaryDivider {
- isHTML := cp.po.p.m.pageConfig.Markup == "html"
- if isHTML {
+ if cp.po.p.m.pageConfig.ContentMediaType.IsHTML() {
// Use the summary sections as provided by the user.
i := bytes.Index(b, internalSummaryDividerPre)
result.summary = helpers.BytesToHTML(b[:i])
b = b[i+len(internalSummaryDividerPre):]
} else {
- summary, content, err := splitUserDefinedSummaryAndContent(cp.po.p.m.pageConfig.Markup, b)
+ summary, content, err := splitUserDefinedSummaryAndContent(cp.po.p.m.pageConfig.Content.Markup, b)
if err != nil {
cp.po.p.s.Log.Errorf("Failed to set user defined summary for page %q: %s", cp.po.p.pathOrTitle(), err)
} else {
@@ -607,7 +624,7 @@ var setGetContentCallbackInContext = hcontext.NewContextDispatcher[func(*pageCon
func (c *cachedContent) contentToC(ctx context.Context, cp *pageContentOutput) (contentTableOfContents, error) {
key := c.pi.sourceKey + "/" + cp.po.f.Name
- versionv := cp.contentRenderedVersion
+ versionv := c.version(cp)
v, err := c.pm.contentTableOfContents.GetOrCreate(key, func(string) (*resources.StaleValue[contentTableOfContents], error) {
source, err := c.pi.contentSource(c)
@@ -663,7 +680,7 @@ func (c *cachedContent) contentToC(ctx context.Context, cp *pageContentOutput) (
p.pageOutputTemplateVariationsState.Add(1)
}
- isHTML := cp.po.p.m.pageConfig.Markup == "html"
+ isHTML := cp.po.p.m.pageConfig.ContentMediaType.IsHTML()
if !isHTML {
createAndSetToC := func(tocProvider converter.TableOfContentsProvider) {
@@ -713,8 +730,8 @@ func (c *cachedContent) contentToC(ctx context.Context, cp *pageContentOutput) (
return &resources.StaleValue[contentTableOfContents]{
Value: ct,
- IsStaleFunc: func() bool {
- return c.IsStale() || cp.contentRenderedVersion != versionv
+ StaleVersionFunc: func() uint32 {
+ return c.version(cp) - versionv
},
}, nil
})
@@ -725,16 +742,21 @@ func (c *cachedContent) contentToC(ctx context.Context, cp *pageContentOutput) (
return v.Value, nil
}
+func (c *cachedContent) version(cp *pageContentOutput) uint32 {
+ // Both of these gets incremented on change.
+ return c.StaleVersion() + cp.contentRenderedVersion
+}
+
func (c *cachedContent) contentPlain(ctx context.Context, cp *pageContentOutput) (contentPlainPlainWords, error) {
key := c.pi.sourceKey + "/" + cp.po.f.Name
- versionv := cp.contentRenderedVersion
+ versionv := c.version(cp)
v, err := c.pm.cacheContentPlain.GetOrCreateWitTimeout(key, cp.po.p.s.Conf.Timeout(), func(string) (*resources.StaleValue[contentPlainPlainWords], error) {
var result contentPlainPlainWords
rs := &resources.StaleValue[contentPlainPlainWords]{
- IsStaleFunc: func() bool {
- return c.IsStale() || cp.contentRenderedVersion != versionv
+ StaleVersionFunc: func() uint32 {
+ return c.version(cp) - versionv
},
}
@@ -781,7 +803,7 @@ func (c *cachedContent) contentPlain(ctx context.Context, cp *pageContentOutput)
if err != nil {
return nil, err
}
- html := cp.po.p.s.ContentSpec.TrimShortHTML(b.Bytes(), cp.po.p.m.pageConfig.Markup)
+ html := cp.po.p.s.ContentSpec.TrimShortHTML(b.Bytes(), cp.po.p.m.pageConfig.Content.Markup)
result.summary = helpers.BytesToHTML(html)
} else {
var summary string