Skip to content

Commit

Permalink
Add fragments support to related
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 18, 2023
1 parent 9af78d1 commit b1e9d38
Show file tree
Hide file tree
Showing 53 changed files with 916 additions and 437 deletions.
Binary file added hugolib.test
Binary file not shown.
2 changes: 1 addition & 1 deletion hugolib/content_map_page.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ func (m *pageMap) newPageFromContentNode(n *contentNode, parentBucket *pagesMapB
return nil, err
}

ps.init.Add(func() (any, error) {
ps.init.Add(func(context.Context) (any, error) {
pp, err := newPagePaths(s, ps, metaProvider)
if err != nil {
return nil, err
Expand Down
3 changes: 2 additions & 1 deletion hugolib/embedded_shortcodes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugolib

import (
"context"
"encoding/json"
"fmt"
"html/template"
Expand Down Expand Up @@ -70,7 +71,7 @@ func doTestShortcodeCrossrefs(t *testing.T, relative bool) {

c.Assert(len(s.RegularPages()), qt.Equals, 1)

content, err := s.RegularPages()[0].Content()
content, err := s.RegularPages()[0].Content(context.Background())
c.Assert(err, qt.IsNil)
output := cast.ToString(content)

Expand Down
14 changes: 7 additions & 7 deletions hugolib/hugo_sites.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,15 +194,15 @@ func (h *hugoSitesInit) Reset() {
}

func (h *HugoSites) Data() map[string]any {
if _, err := h.init.data.Do(); err != nil {
if _, err := h.init.data.Do(context.Background()); err != nil {
h.SendError(fmt.Errorf("failed to load data: %w", err))
return nil
}
return h.data
}

func (h *HugoSites) gitInfoForPage(p page.Page) (source.GitInfo, error) {
if _, err := h.init.gitInfo.Do(); err != nil {
if _, err := h.init.gitInfo.Do(context.Background()); err != nil {
return source.GitInfo{}, err
}

Expand All @@ -214,7 +214,7 @@ func (h *HugoSites) gitInfoForPage(p page.Page) (source.GitInfo, error) {
}

func (h *HugoSites) codeownersForPage(p page.Page) ([]string, error) {
if _, err := h.init.gitInfo.Do(); err != nil {
if _, err := h.init.gitInfo.Do(context.Background()); err != nil {
return nil, err
}

Expand Down Expand Up @@ -363,15 +363,15 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
donec: make(chan bool),
}

h.init.data.Add(func() (any, error) {
h.init.data.Add(func(context.Context) (any, error) {
err := h.loadData(h.PathSpec.BaseFs.Data.Dirs)
if err != nil {
return nil, fmt.Errorf("failed to load data: %w", err)
}
return nil, nil
})

h.init.layouts.Add(func() (any, error) {
h.init.layouts.Add(func(context.Context) (any, error) {
for _, s := range h.Sites {
if err := s.Tmpl().(tpl.TemplateManager).MarkReady(); err != nil {
return nil, err
Expand All @@ -380,7 +380,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
return nil, nil
})

h.init.translations.Add(func() (any, error) {
h.init.translations.Add(func(context.Context) (any, error) {
if len(h.Sites) > 1 {
allTranslations := pagesToTranslationsMap(h.Sites)
assignTranslationsToPages(allTranslations, h.Sites)
Expand All @@ -389,7 +389,7 @@ func newHugoSites(cfg deps.DepsCfg, sites ...*Site) (*HugoSites, error) {
return nil, nil
})

h.init.gitInfo.Add(func() (any, error) {
h.init.gitInfo.Add(func(context.Context) (any, error) {
err := h.loadGitInfo()
if err != nil {
return nil, fmt.Errorf("failed to load Git info: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion hugolib/hugo_sites_build.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ func (h *HugoSites) assemble(bcfg *BuildCfg) error {
}

func (h *HugoSites) render(config *BuildCfg) error {
if _, err := h.init.layouts.Do(); err != nil {
if _, err := h.init.layouts.Do(context.Background()); err != nil {
return err
}

Expand Down
2 changes: 1 addition & 1 deletion hugolib/hugo_sites_build_errors_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ line 4

}

func TestErrorNestedShortocde(t *testing.T) {
func TestErrorNestedShortcode(t *testing.T) {
t.Parallel()

files := `
Expand Down
3 changes: 2 additions & 1 deletion hugolib/language_content_dir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugolib

import (
"context"
"fmt"
"os"
"path/filepath"
Expand Down Expand Up @@ -245,7 +246,7 @@ Content.
c.Assert(svP2.Language().Lang, qt.Equals, "sv")
c.Assert(nnP2.Language().Lang, qt.Equals, "nn")

content, _ := nnP2.Content()
content, _ := nnP2.Content(context.Background())
contentStr := cast.ToString(content)
c.Assert(contentStr, qt.Contains, "SVP3-REF: https://example.org/sv/sect/p-sv-3/")
c.Assert(contentStr, qt.Contains, "SVP3-RELREF: /sv/sect/p-sv-3/")
Expand Down
56 changes: 52 additions & 4 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ package hugolib

import (
"bytes"
"context"
"fmt"
"path"
"path/filepath"
Expand All @@ -24,8 +25,10 @@ import (
"go.uber.org/atomic"

"github.com/gohugoio/hugo/identity"
"github.com/gohugoio/hugo/related"

"github.com/gohugoio/hugo/markup/converter"
"github.com/gohugoio/hugo/markup/tableofcontents"

"github.com/gohugoio/hugo/tpl"

Expand Down Expand Up @@ -148,6 +151,51 @@ func (p *pageState) GetIdentity() identity.Identity {
return identity.NewPathIdentity(files.ComponentFolderContent, filepath.FromSlash(p.Pathc()))
}

// Fragments is used internally by the related content feature.
// Note to people reading this: We need will soon revise the whole Content API and
// and expose more ways to manage ToC etc.
// TODO1 move to output
func (p *pageState) Fragments(ctx context.Context) []string {
p.s.initInit(ctx, p.cp.initToC, p)
var refs []string
toc := p.pageOutput.cp.tableOfContents
var addHeadings func(h tableofcontents.Headings)
addHeadings = func(h tableofcontents.Headings) {
for _, v := range h {
if v.ID != "" {
refs = append(refs, v.ID)
}
addHeadings(v.Headings)
}
}
addHeadings(toc.Headings)

return refs
}

type pageFragment struct {
*pageState
fragment string
}

func (p *pageFragment) RelPermalink() string {
return p.pageState.RelPermalink() + "#" + p.fragment
}

func (p *pageFragment) Permalink() string {
return p.pageState.Permalink() + "#" + p.fragment
}

// For equality checks. TODO1
func (p *pageFragment) page() page.Page {
return p.pageState
}

// For internal use by the related content feature.
func (p *pageState) DocumentFragment(s string) related.Document {
return &pageFragment{p, s}
}

func (p *pageState) GitInfo() source.GitInfo {
return p.gitInfo
}
Expand Down Expand Up @@ -351,7 +399,7 @@ func (p *pageState) String() string {
// IsTranslated returns whether this content file is translated to
// other language(s).
func (p *pageState) IsTranslated() bool {
p.s.h.init.translations.Do()
p.s.h.init.translations.Do(context.Background())
return len(p.translations) > 0
}

Expand All @@ -375,13 +423,13 @@ func (p *pageState) TranslationKey() string {

// AllTranslations returns all translations, including the current Page.
func (p *pageState) AllTranslations() page.Pages {
p.s.h.init.translations.Do()
p.s.h.init.translations.Do(context.Background())
return p.allTranslations
}

// Translations returns the translations excluding the current Page.
func (p *pageState) Translations() page.Pages {
p.s.h.init.translations.Do()
p.s.h.init.translations.Do(context.Background())
return p.translations
}

Expand Down Expand Up @@ -461,7 +509,7 @@ func (p *pageState) initOutputFormat(isRenderingSite bool, idx int) error {

// Must be run after the site section tree etc. is built and ready.
func (p *pageState) initPage() error {
if _, err := p.init.Do(); err != nil {
if _, err := p.init.Do(context.Background()); err != nil {
return err
}
return nil
Expand Down
15 changes: 11 additions & 4 deletions hugolib/page__content.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugolib

import (
"context"
"fmt"

"github.com/gohugoio/hugo/output"
Expand All @@ -37,9 +38,9 @@ type pageContent struct {
}

// returns the content to be processed by Goldmark or similar.
func (p pageContent) contentToRender(parsed pageparser.Result, pm *pageContentMap, renderedShortcodes map[string]string) []byte {
func (p pageContent) contentToRender(ctx context.Context, parsed pageparser.Result, pm *pageContentMap, renderedShortcodes map[string]shortcodeRenderer) ([]byte, bool) {
source := parsed.Input()

var hasVariants bool
c := make([]byte, 0, len(source)+(len(source)/10))

for _, it := range pm.items {
Expand All @@ -57,7 +58,13 @@ func (p pageContent) contentToRender(parsed pageparser.Result, pm *pageContentMa
panic(fmt.Sprintf("rendered shortcode %q not found", v.placeholder))
}

c = append(c, []byte(renderedShortcode)...)
// TODO1 name info in err etc.
b, more, err := renderedShortcode.renderShortcode(ctx)
if err != nil {
panic(fmt.Sprintf("failed to render shortcode %q: %s", v.placeholder, err))
}
hasVariants = hasVariants || more
c = append(c, []byte(b)...)

} else {
// Insert the placeholder so we can insert the content after
Expand All @@ -69,7 +76,7 @@ func (p pageContent) contentToRender(parsed pageparser.Result, pm *pageContentMa
}
}

return c
return c, hasVariants
}

func (p pageContent) selfLayoutForOutput(f output.Format) string {
Expand Down
7 changes: 4 additions & 3 deletions hugolib/page__menus.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugolib

import (
"context"
"sync"

"github.com/gohugoio/hugo/navigation"
Expand All @@ -29,21 +30,21 @@ type pageMenus struct {
}

func (p *pageMenus) HasMenuCurrent(menuID string, me *navigation.MenuEntry) bool {
p.p.s.init.menus.Do()
p.p.s.init.menus.Do(context.Background())
p.init()
return p.q.HasMenuCurrent(menuID, me)
}

func (p *pageMenus) IsMenuCurrent(menuID string, inme *navigation.MenuEntry) bool {
p.p.s.init.menus.Do()
p.p.s.init.menus.Do(context.Background())
p.init()
return p.q.IsMenuCurrent(menuID, inme)
}

func (p *pageMenus) Menus() navigation.PageMenus {
// There is a reverse dependency here. initMenus will, once, build the
// site menus and update any relevant page.
p.p.s.init.menus.Do()
p.p.s.init.menus.Do(context.Background())

return p.menus()
}
Expand Down
3 changes: 2 additions & 1 deletion hugolib/page__new.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugolib

import (
"context"
"html/template"
"strings"

Expand Down Expand Up @@ -121,7 +122,7 @@ func newPageFromMeta(
return nil, err
}

ps.init.Add(func() (any, error) {
ps.init.Add(func(context.Context) (any, error) {
pp, err := newPagePaths(metaProvider.s, ps, metaProvider)
if err != nil {
return nil, err
Expand Down
1 change: 1 addition & 0 deletions hugolib/page__output.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ func (p *pageOutput) initContentProvider(cp *pageContentOutput) {

}

// TODO1 remove me
func (p *pageOutput) enablePlaceholders() {
if p.cp != nil {
p.cp.enablePlaceholders()
Expand Down
Loading

0 comments on commit b1e9d38

Please sign in to comment.