Skip to content

Commit

Permalink
Move to pagemeta package
Browse files Browse the repository at this point in the history
  • Loading branch information
bep committed Feb 24, 2018
1 parent 352e787 commit 72a3a32
Show file tree
Hide file tree
Showing 6 changed files with 115 additions and 105 deletions.
41 changes: 15 additions & 26 deletions hugolib/page.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"github.com/bep/gitmap"

"github.com/gohugoio/hugo/helpers"
"github.com/gohugoio/hugo/hugolib/pagemeta"
"github.com/gohugoio/hugo/resource"

"github.com/gohugoio/hugo/output"
Expand Down Expand Up @@ -220,10 +221,12 @@ type Page struct {
Keywords []string
Data map[string]interface{}

PageDates
pagemeta.PageDates

Sitemap Sitemap
URLPath
pagemeta.URLPath
frontMatterURL string

permalink string
relPermalink string

Expand Down Expand Up @@ -260,13 +263,6 @@ type Page struct {
targetPathDescriptorPrototype *targetPathDescriptor
}

type PageDates struct {
Date time.Time
Lastmod time.Time
PublishDate time.Time
ExpiryDate time.Time
}

// SearchKeywords implements the related.Document interface needed for fast page searches.
func (p *Page) SearchKeywords(cfg related.IndexConfig) ([]related.Keyword, error) {

Expand Down Expand Up @@ -1123,17 +1119,18 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
mtime = p.Source.FileInfo().ModTime()
}

descriptor := frontMatterDescriptor{
frontmatter: frontmatter,
params: p.params,
dates: &p.PageDates,
pageURLs: &p.URLPath,
baseFilename: p.BaseFileName(), modTime: mtime}
descriptor := pagemeta.FrontMatterDescriptor{
Frontmatter: frontmatter,
Params: p.params,
Dates: &p.PageDates,
PageURLs: &p.URLPath,
BaseFilename: p.BaseFileName(),
ModTime: mtime}

// Handle the date separately
// TODO(bep) we need to "do more" in this area so this can be split up and
// more easily tested without the Page, but the coupling is strong.
err := p.s.frontmatterHandler.handleDates(descriptor)
err := p.s.frontmatterHandler.HandleDates(descriptor)
if err != nil {
p.s.Log.ERROR.Printf("Failed to handle dates for page %q: %s", p.Path(), err)
}
Expand All @@ -1151,7 +1148,7 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
continue
}

if p.s.frontmatterHandler.isDateKey(loki) {
if p.s.frontmatterHandler.IsDateKey(loki) {
continue
}

Expand All @@ -1173,7 +1170,7 @@ func (p *Page) update(frontmatter map[string]interface{}) error {
return fmt.Errorf("Only relative URLs are supported, %v provided", url)
}
p.URLPath.URL = cast.ToString(v)
p.URLPath.frontMatterURL = p.URLPath.URL
p.frontMatterURL = p.URLPath.URL
p.params[loki] = p.URLPath.URL
case "type":
p.contentType = cast.ToString(v)
Expand Down Expand Up @@ -1829,14 +1826,6 @@ func (p *Page) String() string {
return fmt.Sprintf("Page(%q)", p.title)
}

type URLPath struct {
URL string
frontMatterURL string
Permalink string
Slug string
Section string
}

// Scratch returns the writable context associated with this Page.
func (p *Page) Scratch() *Scratch {
if p.scratch == nil {
Expand Down
2 changes: 1 addition & 1 deletion hugolib/page_paths.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ func (p *Page) initTargetPathDescriptor() error {
Sections: p.sections,
UglyURLs: p.s.Info.uglyURLs(p),
Dir: filepath.ToSlash(p.Source.Dir()),
URL: p.URLPath.frontMatterURL,
URL: p.frontMatterURL,
IsMultihost: p.s.owner.IsMultihost(),
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

package hugolib
package pagemeta

import (
"fmt"
Expand All @@ -32,37 +32,37 @@ import (

// TODO(bep) should probably make the date handling chain complete to give people the flexibility they want.

type frontmatterHandler struct {
type FrontmatterHandler struct {
// Ordered chain.
dateHandlers frontMatterFieldHandler

logger *jww.Notepad
}

type frontMatterDescriptor struct {
type FrontMatterDescriptor struct {

// This the Page's front matter.
frontmatter map[string]interface{}
Frontmatter map[string]interface{}

// This is the Page's base filename, e.g. page.md.
baseFilename string
BaseFilename string

// The content file's mod time.
modTime time.Time
ModTime time.Time

// The below are pointers to values on Page and will be updated.

// This is the Page's params.
params map[string]interface{}
Params map[string]interface{}

// This is the Page's dates.
dates *PageDates
Dates *PageDates

// This is the Page's Slug etc.
pageURLs *URLPath
PageURLs *URLPath
}

func (f frontmatterHandler) handleDate(d frontMatterDescriptor) error {
func (f FrontmatterHandler) handleDate(d FrontMatterDescriptor) error {
_, err := f.dateHandlers(d)
return err
}
Expand All @@ -88,70 +88,70 @@ func init() {
allDateFrontMatterKeys["date"] = true
}

func (f frontmatterHandler) handleDates(d frontMatterDescriptor) error {
if d.dates == nil {
func (f FrontmatterHandler) HandleDates(d FrontMatterDescriptor) error {
if d.Dates == nil {
panic("missing dates")
}

err := f.handleDate(d)
if err != nil {
return err
}
d.dates.Lastmod = f.setParamsAndReturnFirstDate(d, lastModFrontMatterKeys)
d.dates.PublishDate = f.setParamsAndReturnFirstDate(d, publishDateFrontMatterKeys)
d.dates.ExpiryDate = f.setParamsAndReturnFirstDate(d, expiryDateFrontMatterKeys)
d.Dates.Lastmod = f.setParamsAndReturnFirstDate(d, lastModFrontMatterKeys)
d.Dates.PublishDate = f.setParamsAndReturnFirstDate(d, publishDateFrontMatterKeys)
d.Dates.ExpiryDate = f.setParamsAndReturnFirstDate(d, expiryDateFrontMatterKeys)

// Hugo really needs a date!
if d.dates.Date.IsZero() {
d.dates.Date = d.dates.PublishDate
if d.Dates.Date.IsZero() {
d.Dates.Date = d.Dates.PublishDate
}

if d.dates.Lastmod.IsZero() {
d.dates.Lastmod = d.dates.Date
if d.Dates.Lastmod.IsZero() {
d.Dates.Lastmod = d.Dates.Date
}

// TODO(bep) date decide vs https://github.com/gohugoio/hugo/issues/3977
if d.dates.PublishDate.IsZero() {
if d.Dates.PublishDate.IsZero() {
//d.dates.PublishDate = d.dates.Date
}

if d.dates.Date.IsZero() {
d.dates.Date = d.dates.Lastmod
if d.Dates.Date.IsZero() {
d.Dates.Date = d.Dates.Lastmod
}

f.setParamIfNotZero("date", d.params, d.dates.Date)
f.setParamIfNotZero("lastmod", d.params, d.dates.Lastmod)
f.setParamIfNotZero("publishdate", d.params, d.dates.PublishDate)
f.setParamIfNotZero("expirydate", d.params, d.dates.ExpiryDate)
f.setParamIfNotZero("date", d.Params, d.Dates.Date)
f.setParamIfNotZero("lastmod", d.Params, d.Dates.Lastmod)
f.setParamIfNotZero("publishdate", d.Params, d.Dates.PublishDate)
f.setParamIfNotZero("expirydate", d.Params, d.Dates.ExpiryDate)

return nil
}

func (f frontmatterHandler) isDateKey(key string) bool {
func (f FrontmatterHandler) IsDateKey(key string) bool {
return allDateFrontMatterKeys[key]
}

func (f frontmatterHandler) setParamIfNotZero(name string, params map[string]interface{}, date time.Time) {
func (f FrontmatterHandler) setParamIfNotZero(name string, params map[string]interface{}, date time.Time) {
if date.IsZero() {
return
}
params[name] = date
}

func (f frontmatterHandler) setParamsAndReturnFirstDate(d frontMatterDescriptor, keys []string) time.Time {
func (f FrontmatterHandler) setParamsAndReturnFirstDate(d FrontMatterDescriptor, keys []string) time.Time {
var date time.Time

for _, key := range keys {
v, found := d.frontmatter[key]
v, found := d.Frontmatter[key]
if found {
currentDate, err := cast.ToTimeE(v)
if err == nil {
d.params[key] = currentDate
d.Params[key] = currentDate
if date.IsZero() {
date = currentDate
}
} else {
d.params[key] = v
d.Params[key] = v
}
}
}
Expand Down Expand Up @@ -183,10 +183,10 @@ func dateAndSlugFromBaseFilename(name string) (time.Time, string) {
return d, slug
}

type frontMatterFieldHandler func(d frontMatterDescriptor) (bool, error)
type frontMatterFieldHandler func(d FrontMatterDescriptor) (bool, error)

func (f frontmatterHandler) newChainedFrontMatterFieldHandler(handlers ...frontMatterFieldHandler) frontMatterFieldHandler {
return func(d frontMatterDescriptor) (bool, error) {
func (f FrontmatterHandler) newChainedFrontMatterFieldHandler(handlers ...frontMatterFieldHandler) frontMatterFieldHandler {
return func(d FrontMatterDescriptor) (bool, error) {
for _, h := range handlers {
// First successful handler wins.
success, err := h(d)
Expand Down Expand Up @@ -258,15 +258,15 @@ func toLowerSlice(in interface{}) []string {
return out
}

func newFrontmatterHandler(logger *jww.Notepad, cfg config.Provider) (frontmatterHandler, error) {
func NewFrontmatterHandler(logger *jww.Notepad, cfg config.Provider) (FrontmatterHandler, error) {

if logger == nil {
logger = jww.NewNotepad(jww.LevelWarn, jww.LevelWarn, os.Stdout, ioutil.Discard, "", log.Ldate|log.Ltime)
}

f := frontmatterHandler{logger: logger}
f := FrontmatterHandler{logger: logger}

handlers := &frontmatterFieldHandlers{logger: logger}
handlers := &frontmatterFieldHandlers{}

/*
Expand Down Expand Up @@ -308,11 +308,10 @@ func newFrontmatterHandler(logger *jww.Notepad, cfg config.Provider) (frontmatte
}

type frontmatterFieldHandlers struct {
logger *jww.Notepad
}

func (f *frontmatterFieldHandlers) defaultDateHandler(d frontMatterDescriptor) (bool, error) {
v, found := d.frontmatter["date"]
func (f *frontmatterFieldHandlers) defaultDateHandler(d FrontMatterDescriptor) (bool, error) {
v, found := d.Frontmatter["date"]
if !found {
return false, nil
}
Expand All @@ -322,30 +321,30 @@ func (f *frontmatterFieldHandlers) defaultDateHandler(d frontMatterDescriptor) (
return false, err
}

d.dates.Date = date
d.Dates.Date = date

return true, nil
}

func (f *frontmatterFieldHandlers) defaultDateFilenameHandler(d frontMatterDescriptor) (bool, error) {
date, slug := dateAndSlugFromBaseFilename(d.baseFilename)
func (f *frontmatterFieldHandlers) defaultDateFilenameHandler(d FrontMatterDescriptor) (bool, error) {
date, slug := dateAndSlugFromBaseFilename(d.BaseFilename)
if date.IsZero() {
return false, nil
}

d.dates.Date = date
d.Dates.Date = date

if _, found := d.frontmatter["slug"]; !found {
if _, found := d.Frontmatter["slug"]; !found {
// Use slug from filename
d.pageURLs.Slug = slug
d.PageURLs.Slug = slug
}

return true, nil
}

func (f *frontmatterFieldHandlers) defaultDateModTimeHandler(d frontMatterDescriptor) (bool, error) {
if !d.modTime.IsZero() {
d.dates.Date = d.modTime
func (f *frontmatterFieldHandlers) defaultDateModTimeHandler(d FrontMatterDescriptor) (bool, error) {
if !d.ModTime.IsZero() {
d.Dates.Date = d.ModTime
return true, nil
}
return false, nil
Expand Down
Loading

0 comments on commit 72a3a32

Please sign in to comment.