Skip to content

Commit

Permalink
Misc doc, code refactoring to improve documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Jan 4, 2023
1 parent 3c51625 commit e402d91
Show file tree
Hide file tree
Showing 47 changed files with 238 additions and 95 deletions.
16 changes: 10 additions & 6 deletions codegen/methods.go
Expand Up @@ -452,12 +452,16 @@ func collectMethodsRecursive(pkg string, f []*ast.Field) []string {
}

if ident, ok := m.Type.(*ast.Ident); ok && ident.Obj != nil {
// Embedded interface
methodNames = append(
methodNames,
collectMethodsRecursive(
pkg,
ident.Obj.Decl.(*ast.TypeSpec).Type.(*ast.InterfaceType).Methods.List)...)
switch tt := ident.Obj.Decl.(*ast.TypeSpec).Type.(type) {
case *ast.InterfaceType:
// Embedded interface
methodNames = append(
methodNames,
collectMethodsRecursive(
pkg,
tt.Methods.List)...)
}

} else {
// Embedded, but in a different file/package. Return the
// package.Name and deal with that later.
Expand Down
1 change: 1 addition & 0 deletions commands/config.go
Expand Up @@ -126,6 +126,7 @@ type modMount struct {
Lang string `json:"lang,omitempty"`
}

// MarshalJSON is for internal use only.
func (m *modMounts) MarshalJSON() ([]byte, error) {
var mounts []modMount

Expand Down
2 changes: 2 additions & 0 deletions common/docs.go
@@ -0,0 +1,2 @@
// Package common provides common helper functionality for Hugo.
package common
1 change: 1 addition & 0 deletions common/maps/scratch.go
Expand Up @@ -30,6 +30,7 @@ type Scratch struct {

// Scratcher provides a scratching service.
type Scratcher interface {
// Scratch returns a "scratch pad" that can be used to store state.
Scratch() *Scratch
}

Expand Down
11 changes: 11 additions & 0 deletions common/paths/path.go
Expand Up @@ -263,3 +263,14 @@ func (n NamedSlice) String() string {
}
return fmt.Sprintf("%s%s{%s}", n.Name, FilePathSeparator, strings.Join(n.Slice, ","))
}

// DirFile holds the result from path.Split.
type DirFile struct {
Dir string
File string
}

// Used in test.
func (df DirFile) String() string {
return fmt.Sprintf("%s|%s", df.Dir, df.File)
}
2 changes: 2 additions & 0 deletions common/text/position.go
Expand Up @@ -24,6 +24,8 @@ import (
// Positioner represents a thing that knows its position in a text file or stream,
// typically an error.
type Positioner interface {
// Position returns the current position.
// Useful in error logging, e.g. {{ errorf "error in code block: %s" .Position }}.
Position() Position
}

Expand Down
1 change: 1 addition & 0 deletions config/security/whitelist.go
Expand Up @@ -33,6 +33,7 @@ type Whitelist struct {
patternsStrings []string
}

// MarshalJSON is for internal use only.
func (w Whitelist) MarshalJSON() ([]byte, error) {
if w.acceptNone {
return json.Marshal(acceptNoneKeyword)
Expand Down
1 change: 1 addition & 0 deletions hugofs/fileinfo.go
Expand Up @@ -130,6 +130,7 @@ func (f *FileMeta) JoinStat(name string) (FileMetaInfo, error) {

type FileMetaInfo interface {
os.FileInfo
// Meta is for internal use.
Meta() *FileMeta
}

Expand Down
6 changes: 3 additions & 3 deletions hugolib/content_map_page.go
Expand Up @@ -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)
Expand All @@ -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 {
Expand All @@ -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
})
Expand Down
10 changes: 7 additions & 3 deletions hugolib/gitinfo.go
Expand Up @@ -20,18 +20,22 @@ import (
"github.com/bep/gitmap"
"github.com/gohugoio/hugo/config"
"github.com/gohugoio/hugo/resources/page"
"github.com/gohugoio/hugo/source"
)

type gitInfo struct {
contentDir string
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) {
Expand Down
7 changes: 3 additions & 4 deletions hugolib/hugo_sites.go
Expand Up @@ -41,7 +41,6 @@ import (

"github.com/gohugoio/hugo/source"

"github.com/bep/gitmap"
"github.com/gohugoio/hugo/config"

"github.com/gohugoio/hugo/publisher"
Expand Down Expand Up @@ -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
Expand Down
4 changes: 1 addition & 3 deletions hugolib/page.go
Expand Up @@ -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"
Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions hugolib/page__common.go
Expand Up @@ -16,14 +16,14 @@ package hugolib
import (
"sync"

"github.com/bep/gitmap"
"github.com/gohugoio/hugo/common/maps"
"github.com/gohugoio/hugo/compare"
"github.com/gohugoio/hugo/lazy"
"github.com/gohugoio/hugo/navigation"
"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 {
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion hugolib/page__meta.go
Expand Up @@ -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
}

Expand Down
8 changes: 4 additions & 4 deletions hugolib/site.go
Expand Up @@ -110,9 +110,9 @@ type Site struct {

*PageCollections

taxonomies TaxonomyList
taxonomies page.TaxonomyList

Sections Taxonomy
Sections page.Taxonomy
Info *SiteInfo

language *langs.Language
Expand Down Expand Up @@ -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
}
Expand Down Expand Up @@ -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()
}

Expand Down
1 change: 1 addition & 0 deletions langs/language.go
Expand Up @@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package langs contains the language related types and function.
package langs

import (
Expand Down
26 changes: 26 additions & 0 deletions markup/converter/hooks/hooks.go
Expand Up @@ -26,30 +26,56 @@ import (
var _ AttributesOptionsSliceProvider = (*attributes.AttributesHolder)(nil)

type AttributesProvider interface {
// Attributes passed in from Markdown (e.g. { attrName1=attrValue1 attrName2="attr Value 2" }).
Attributes() map[string]any
}

type LinkContext interface {
// The Page being rendered.
Page() any

// The link URL.
Destination() string

// The link title attribute.
Title() string

// The rendered (HTML) text.
Text() hstring.RenderedString

// The plain variant of Text.
PlainText() string
}

type ImageLinkContext interface {
LinkContext

// Returns true if this is a standalone image and the config option
// markup.goldmark.parser.wrapStandAloneImageWithinParagraph is disabled.
IsBlock() bool

// Zero-based ordinal for all the images in the current document.
Ordinal() int
}

// CodeblockContext is the context passed to a code block render hook.
type CodeblockContext interface {
AttributesProvider
text.Positioner

// Chroma highlighting processing options. This will only be filled if Type is a known Chroma Lexer.
Options() map[string]any

// The type of code block. This will be the programming language, e.g. bash, when doing code highlighting.
Type() string

// The text between the code fences.
Inner() string

// Zero-based ordinal for all code blocks in the current document.
Ordinal() int

// The owning Page.
Page() any
}

Expand Down
2 changes: 2 additions & 0 deletions markup/highlight/highlight.go
Expand Up @@ -157,10 +157,12 @@ type HightlightResult struct {
highlighted template.HTML
}

// Wrapped returns the highlighted code wrapped in a <div>, <pre> and <code> tag.
func (h HightlightResult) Wrapped() template.HTML {
return h.highlighted
}

// Inner returns the highlighted code without the wrapping <div>, <pre> and <code> tag, suitable for inline use.
func (h HightlightResult) Inner() template.HTML {
return h.highlighted[h.innerLow:h.innerHigh]
}
Expand Down
1 change: 1 addition & 0 deletions markup/markup.go
Expand Up @@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package markup contains the markup handling (e.g. Markdown).
package markup

import (
Expand Down
1 change: 1 addition & 0 deletions media/mediaType.go
Expand Up @@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package media containes Media Type (MIME type) related types and functions.
package media

import (
Expand Down
3 changes: 2 additions & 1 deletion navigation/menu.go
@@ -1,4 +1,4 @@
// Copyright 2019 The Hugo Authors. All rights reserved.
// Copyright 2023 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.
Expand All @@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package navigation provides the menu functionality.
package navigation

import (
Expand Down
2 changes: 1 addition & 1 deletion navigation/menu_cache.go
Expand Up @@ -10,7 +10,7 @@
// 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 navigation

import (
Expand Down
2 changes: 2 additions & 0 deletions output/outputFormat.go
Expand Up @@ -11,6 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

// Package output contains Output Format types and functions.
package output

import (
Expand Down Expand Up @@ -400,6 +401,7 @@ func (f Format) BaseFilename() string {
}

// MarshalJSON returns the JSON encoding of f.
// For internal use only.
func (f Format) MarshalJSON() ([]byte, error) {
type Alias Format
return json.Marshal(&struct {
Expand Down
15 changes: 15 additions & 0 deletions resources/docs.go
@@ -0,0 +1,15 @@
// Copyright 2023 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 resources contains Resource related types.
package resources

0 comments on commit e402d91

Please sign in to comment.