Misc doc, code refactoring to improve documentation

This commit is contained in:
Bjørn Erik Pedersen
2022-12-30 09:20:58 +01:00
parent 3c51625c71
commit e402d91ee1
47 changed files with 238 additions and 95 deletions

View File

@@ -266,7 +266,7 @@ func (m *pageMap) newResource(fim hugofs.FileMetaInfo, owner *pageState) (resour
}
func (m *pageMap) createSiteTaxonomies() error {
m.s.taxonomies = make(TaxonomyList)
m.s.taxonomies = make(page.TaxonomyList)
var walkErr error
m.taxonomies.Walk(func(s string, v any) bool {
n := v.(*contentNode)
@@ -275,7 +275,7 @@ func (m *pageMap) createSiteTaxonomies() error {
viewName := t.name
if t.termKey == "" {
m.s.taxonomies[viewName.plural] = make(Taxonomy)
m.s.taxonomies[viewName.plural] = make(page.Taxonomy)
} else {
taxonomy := m.s.taxonomies[viewName.plural]
if taxonomy == nil {
@@ -285,7 +285,7 @@ func (m *pageMap) createSiteTaxonomies() error {
m.taxonomyEntries.WalkPrefix(s, func(ss string, v any) bool {
b2 := v.(*contentNode)
info := b2.viewInfo
taxonomy.add(info.termKey, page.NewWeightedPage(info.weight, info.ref.p, n.p))
taxonomy[info.termKey] = append(taxonomy[info.termKey], page.NewWeightedPage(info.weight, info.ref.p, n.p))
return false
})

View File

@@ -20,6 +20,7 @@ import (
"github.com/bep/gitmap"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/source"
)
type gitInfo struct {
@@ -27,11 +28,14 @@ type gitInfo struct {
repo *gitmap.GitRepo
}
func (g *gitInfo) forPage(p page.Page) *gitmap.GitInfo {
func (g *gitInfo) forPage(p page.Page) source.GitInfo {
name := strings.TrimPrefix(filepath.ToSlash(p.File().Filename()), g.contentDir)
name = strings.TrimPrefix(name, "/")
return g.repo.Files[name]
gi, found := g.repo.Files[name]
if !found {
return source.GitInfo{}
}
return source.NewGitInfo(*gi)
}
func newGitInfo(cfg config.Provider) (*gitInfo, error) {

View File

@@ -41,7 +41,6 @@ import (
"github.com/gohugoio/hugo/source"
"github.com/bep/gitmap"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/publisher"
@@ -202,13 +201,13 @@ func (h *HugoSites) Data() map[string]any {
return h.data
}
func (h *HugoSites) gitInfoForPage(p page.Page) (*gitmap.GitInfo, error) {
func (h *HugoSites) gitInfoForPage(p page.Page) (source.GitInfo, error) {
if _, err := h.init.gitInfo.Do(); err != nil {
return nil, err
return source.GitInfo{}, err
}
if h.gitInfo == nil {
return nil, nil
return source.GitInfo{}, nil
}
return h.gitInfo.forPage(p), nil

View File

@@ -31,8 +31,6 @@ import (
"github.com/gohugoio/hugo/hugofs/files"
"github.com/bep/gitmap"
"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/common/herrors"
@@ -150,7 +148,7 @@ func (p *pageState) GetIdentity() identity.Identity {
return identity.NewPathIdentity(files.ComponentFolderContent, filepath.FromSlash(p.Pathc()))
}
func (p *pageState) GitInfo() *gitmap.GitInfo {
func (p *pageState) GitInfo() source.GitInfo {
return p.gitInfo
}

View File

@@ -16,7 +16,6 @@ package hugolib
import (
"sync"
"github.com/bep/gitmap"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/compare"
"github.com/gohugoio/hugo/lazy"
@@ -24,6 +23,7 @@ import (
"github.com/gohugoio/hugo/output"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/resources/resource"
"github.com/gohugoio/hugo/source"
)
type treeRefProvider interface {
@@ -106,7 +106,7 @@ type pageCommon struct {
shortcodeState *shortcodeHandler
// Set if feature enabled and this is in a Git repo.
gitInfo *gitmap.GitInfo
gitInfo source.GitInfo
codeowners []string
// Positional navigation

View File

@@ -404,7 +404,7 @@ func (pm *pageMeta) setMetadata(parentBucket *pagesMapBucket, p *pageState, fron
}
var gitAuthorDate time.Time
if p.gitInfo != nil {
if !p.gitInfo.IsZero() {
gitAuthorDate = p.gitInfo.AuthorDate
}

View File

@@ -110,9 +110,9 @@ type Site struct {
*PageCollections
taxonomies TaxonomyList
taxonomies page.TaxonomyList
Sections Taxonomy
Sections page.Taxonomy
Info *SiteInfo
language *langs.Language
@@ -172,7 +172,7 @@ type Site struct {
init *siteInit
}
func (s *Site) Taxonomies() TaxonomyList {
func (s *Site) Taxonomies() page.TaxonomyList {
s.init.taxonomies.Do()
return s.taxonomies
}
@@ -708,7 +708,7 @@ func (s *SiteInfo) Menus() navigation.Menus {
}
// TODO(bep) type
func (s *SiteInfo) Taxonomies() any {
func (s *SiteInfo) Taxonomies() page.TaxonomyList {
return s.s.Taxonomies()
}

View File

@@ -1,173 +0,0 @@
// 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"
"sort"
"github.com/gohugoio/hugo/compare"
"github.com/gohugoio/hugo/langs"
"github.com/gohugoio/hugo/resources/page"
)
// The TaxonomyList is a list of all taxonomies and their values
// e.g. List['tags'] => TagTaxonomy (from above)
type TaxonomyList map[string]Taxonomy
func (tl TaxonomyList) String() string {
return fmt.Sprintf("TaxonomyList(%d)", len(tl))
}
// A Taxonomy is a map of keywords to a list of pages.
// For example
// TagTaxonomy['technology'] = page.WeightedPages
// TagTaxonomy['go'] = page.WeightedPages
type Taxonomy map[string]page.WeightedPages
// OrderedTaxonomy is another representation of an Taxonomy using an array rather than a map.
// Important because you can't order a map.
type OrderedTaxonomy []OrderedTaxonomyEntry
// getOneOPage returns one page in the taxonomy,
// nil if there is none.
func (t OrderedTaxonomy) getOneOPage() page.Page {
if len(t) == 0 {
return nil
}
return t[0].Pages()[0]
}
// OrderedTaxonomyEntry is similar to an element of a Taxonomy, but with the key embedded (as name)
// e.g: {Name: Technology, page.WeightedPages: TaxonomyPages}
type OrderedTaxonomyEntry struct {
Name string
page.WeightedPages
}
// Get the weighted pages for the given key.
func (i Taxonomy) Get(key string) page.WeightedPages {
return i[key]
}
// Count the weighted pages for the given key.
func (i Taxonomy) Count(key string) int { return len(i[key]) }
func (i Taxonomy) add(key string, w page.WeightedPage) {
i[key] = append(i[key], w)
}
// TaxonomyArray returns an ordered taxonomy with a non defined order.
func (i Taxonomy) TaxonomyArray() OrderedTaxonomy {
ies := make([]OrderedTaxonomyEntry, len(i))
count := 0
for k, v := range i {
ies[count] = OrderedTaxonomyEntry{Name: k, WeightedPages: v}
count++
}
return ies
}
// Alphabetical returns an ordered taxonomy sorted by key name.
func (i Taxonomy) Alphabetical() OrderedTaxonomy {
ia := i.TaxonomyArray()
p := ia.getOneOPage()
if p == nil {
return ia
}
currentSite := p.Site().Current()
coll := langs.GetCollator(currentSite.Language())
coll.Lock()
defer coll.Unlock()
name := func(i1, i2 *OrderedTaxonomyEntry) bool {
return coll.CompareStrings(i1.Name, i2.Name) < 0
}
oiBy(name).Sort(ia)
return ia
}
// ByCount returns an ordered taxonomy sorted by # of pages per key.
// If taxonomies have the same # of pages, sort them alphabetical
func (i Taxonomy) ByCount() OrderedTaxonomy {
count := func(i1, i2 *OrderedTaxonomyEntry) bool {
li1 := len(i1.WeightedPages)
li2 := len(i2.WeightedPages)
if li1 == li2 {
return compare.LessStrings(i1.Name, i2.Name)
}
return li1 > li2
}
ia := i.TaxonomyArray()
oiBy(count).Sort(ia)
return ia
}
// Pages returns the Pages for this taxonomy.
func (ie OrderedTaxonomyEntry) Pages() page.Pages {
return ie.WeightedPages.Pages()
}
// Count returns the count the pages in this taxonomy.
func (ie OrderedTaxonomyEntry) Count() int {
return len(ie.WeightedPages)
}
// Term returns the name given to this taxonomy.
func (ie OrderedTaxonomyEntry) Term() string {
return ie.Name
}
// Reverse reverses the order of the entries in this taxonomy.
func (t OrderedTaxonomy) Reverse() OrderedTaxonomy {
for i, j := 0, len(t)-1; i < j; i, j = i+1, j-1 {
t[i], t[j] = t[j], t[i]
}
return t
}
// A type to implement the sort interface for TaxonomyEntries.
type orderedTaxonomySorter struct {
taxonomy OrderedTaxonomy
by oiBy
}
// Closure used in the Sort.Less method.
type oiBy func(i1, i2 *OrderedTaxonomyEntry) bool
func (by oiBy) Sort(taxonomy OrderedTaxonomy) {
ps := &orderedTaxonomySorter{
taxonomy: taxonomy,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
}
sort.Stable(ps)
}
// Len is part of sort.Interface.
func (s *orderedTaxonomySorter) Len() int {
return len(s.taxonomy)
}
// Swap is part of sort.Interface.
func (s *orderedTaxonomySorter) Swap(i, j int) {
s.taxonomy[i], s.taxonomy[j] = s.taxonomy[j], s.taxonomy[i]
}
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (s *orderedTaxonomySorter) Less(i, j int) bool {
return s.by(&s.taxonomy[i], &s.taxonomy[j])
}