Skip to content

Commit

Permalink
Add Page.Contents with scope support
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Aug 20, 2024
1 parent 01008ba commit 7bd37f1
Show file tree
Hide file tree
Showing 17 changed files with 1,336 additions and 379 deletions.
25 changes: 25 additions & 0 deletions common/hugo/hugo.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugo

import (
"context"
"fmt"
"html/template"
"os"
Expand All @@ -29,6 +30,7 @@ import (
"github.com/mitchellh/mapstructure"

"github.com/bep/godartsass/v2"
"github.com/gohugoio/hugo/common/hcontext"
"github.com/gohugoio/hugo/common/hexec"
"github.com/gohugoio/hugo/common/loggers"
"github.com/gohugoio/hugo/hugofs/files"
Expand Down Expand Up @@ -69,6 +71,9 @@ type HugoInfo struct {

conf ConfigProvider
deps []*Dependency

// Context gives access to some of the context scoped variables.
Context Context
}

// Version returns the current version as a comparable version string.
Expand Down Expand Up @@ -127,6 +132,26 @@ func (i HugoInfo) IsMultilingual() bool {
return i.conf.IsMultilingual()
}

type contextKey string

var markupScope = hcontext.NewContextDispatcher[string](contextKey("markupScope"))

type Context struct{}

func (c Context) MarkupScope(ctx context.Context) string {
return GetMarkupScope(ctx)
}

// SetMarkupScope sets the markup scope in the context.
func SetMarkupScope(ctx context.Context, s string) context.Context {
return markupScope.Set(ctx, s)
}

// GetMarkupScope gets the markup scope from the context.
func GetMarkupScope(ctx context.Context) string {
return markupScope.Get(ctx)
}

// ConfigProvider represents the config options that are relevant for HugoInfo.
type ConfigProvider interface {
Environment() string
Expand Down
14 changes: 14 additions & 0 deletions common/hugo/hugo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
package hugo

import (
"context"
"fmt"
"testing"

Expand Down Expand Up @@ -64,6 +65,19 @@ func TestDeprecationLogLevelFromVersion(t *testing.T) {
c.Assert(deprecationLogLevelFromVersion(ver.String()), qt.Equals, logg.LevelError)
}

func TestMarkupScope(t *testing.T) {
c := qt.New(t)

conf := testConfig{environment: "production", workingDir: "/mywork", running: false}
info := NewInfo(conf, nil)

ctx := context.Background()

ctx = SetMarkupScope(ctx, "foo")

c.Assert(info.Context.MarkupScope(ctx), qt.Equals, "foo")
}

type testConfig struct {
environment string
running bool
Expand Down
4 changes: 4 additions & 0 deletions common/types/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,10 @@ type LowHigh struct {
High int
}

func (l LowHigh) IsZero() bool {
return l.Low == 0 && l.High == 0
}

// This is only used for debugging purposes.
var InvocationCounter atomic.Int64

Expand Down
2 changes: 2 additions & 0 deletions helpers/content.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ func TotalWords(s string) int {
}

// TruncateWordsByRune truncates words by runes.
// TODO1 remove me.
func (c *ContentSpec) TruncateWordsByRune(in []string) (string, bool) {
words := make([]string, len(in))
copy(words, in)
Expand Down Expand Up @@ -196,6 +197,7 @@ func (c *ContentSpec) TruncateWordsByRune(in []string) (string, bool) {

// TruncateWordsToWholeSentence takes content and truncates to whole sentence
// limited by max number of words. It also returns whether it is truncated.
// TODO1 remove me.
func (c *ContentSpec) TruncateWordsToWholeSentence(s string) (string, bool) {
var (
wordCount = 0
Expand Down
8 changes: 3 additions & 5 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ var (
pageTypesProvider = resource.NewResourceTypesProvider(media.Builtin.OctetType, pageResourceType)
nopPageOutput = &pageOutput{
pagePerOutputProviders: nopPagePerOutput,
MarkupProvider: page.NopPage,
ContentProvider: page.NopPage,
}
)
Expand Down Expand Up @@ -213,11 +214,8 @@ func (p *pageHeadingsFiltered) page() page.Page {

// For internal use by the related content feature.
func (p *pageState) ApplyFilterToHeadings(ctx context.Context, fn func(*tableofcontents.Heading) bool) related.Document {
r, err := p.m.content.contentToC(ctx, p.pageOutput.pco)
if err != nil {
panic(err)
}
headings := r.tableOfContents.Headings.FilterBy(fn)
fragments := p.pageOutput.pco.c().Fragments(ctx)
headings := fragments.Headings.FilterBy(fn)
return &pageHeadingsFiltered{
pageState: p,
headings: headings,
Expand Down
Loading

0 comments on commit 7bd37f1

Please sign in to comment.