hugolib: Some more unexports

This commit is contained in:
Bjørn Erik Pedersen 2016-03-25 03:12:03 +01:00
parent 7c81c86bdd
commit 8aec5ee2dd
5 changed files with 33 additions and 34 deletions

View file

@ -252,7 +252,7 @@ func (p Pages) GroupByParamDate(key string, format string, order ...string) (Pag
pdate := func(p1, p2 *Page) bool { pdate := func(p1, p2 *Page) bool {
return p1.GetParam(key).(time.Time).Unix() < p2.GetParam(key).(time.Time).Unix() return p1.GetParam(key).(time.Time).Unix() < p2.GetParam(key).(time.Time).Unix()
} }
PageBy(pdate).Sort(r) pageBy(pdate).Sort(r)
return r return r
} }
formatter := func(p *Page) string { formatter := func(p *Page) string {

View file

@ -23,27 +23,27 @@ var spc = newPageCache()
* Implementation of a custom sorter for Pages * Implementation of a custom sorter for Pages
*/ */
// A PageSorter implements the sort interface for Pages // A pageSorter implements the sort interface for Pages
type PageSorter struct { type pageSorter struct {
pages Pages pages Pages
by PageBy by pageBy
} }
// PageBy is a closure used in the Sort.Less method. // pageBy is a closure used in the Sort.Less method.
type PageBy func(p1, p2 *Page) bool type pageBy func(p1, p2 *Page) bool
// Sort stable sorts the pages given the receiver's sort order. // Sort stable sorts the pages given the receiver's sort order.
func (by PageBy) Sort(pages Pages) { func (by pageBy) Sort(pages Pages) {
ps := &PageSorter{ ps := &pageSorter{
pages: pages, pages: pages,
by: by, // The Sort method's receiver is the function (closure) that defines the sort order. by: by, // The Sort method's receiver is the function (closure) that defines the sort order.
} }
sort.Stable(ps) sort.Stable(ps)
} }
// DefaultPageSort is the default sort for pages in Hugo: // defaultPageSort is the default sort for pages in Hugo:
// Order by Weight, Date, LinkTitle and then full file path. // Order by Weight, Date, LinkTitle and then full file path.
var DefaultPageSort = func(p1, p2 *Page) bool { var defaultPageSort = func(p1, p2 *Page) bool {
if p1.Weight == p2.Weight { if p1.Weight == p2.Weight {
if p1.Date.Unix() == p2.Date.Unix() { if p1.Date.Unix() == p2.Date.Unix() {
if p1.LinkTitle() == p2.LinkTitle() { if p1.LinkTitle() == p2.LinkTitle() {
@ -56,16 +56,16 @@ var DefaultPageSort = func(p1, p2 *Page) bool {
return p1.Weight < p2.Weight return p1.Weight < p2.Weight
} }
func (ps *PageSorter) Len() int { return len(ps.pages) } func (ps *pageSorter) Len() int { return len(ps.pages) }
func (ps *PageSorter) Swap(i, j int) { ps.pages[i], ps.pages[j] = ps.pages[j], ps.pages[i] } func (ps *pageSorter) Swap(i, j int) { ps.pages[i], ps.pages[j] = ps.pages[j], ps.pages[i] }
// Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter. // Less is part of sort.Interface. It is implemented by calling the "by" closure in the sorter.
func (ps *PageSorter) Less(i, j int) bool { return ps.by(ps.pages[i], ps.pages[j]) } func (ps *pageSorter) Less(i, j int) bool { return ps.by(ps.pages[i], ps.pages[j]) }
// Sort sorts the pages by the default sort order defined: // Sort sorts the pages by the default sort order defined:
// Order by Weight, Date, LinkTitle and then full file path. // Order by Weight, Date, LinkTitle and then full file path.
func (p Pages) Sort() { func (p Pages) Sort() {
PageBy(DefaultPageSort).Sort(p) pageBy(defaultPageSort).Sort(p)
} }
// Limit limits the number of pages returned to n. // Limit limits the number of pages returned to n.
@ -83,7 +83,7 @@ func (p Pages) Limit(n int) Pages {
// This may safely be executed in parallel. // This may safely be executed in parallel.
func (p Pages) ByWeight() Pages { func (p Pages) ByWeight() Pages {
key := "pageSort.ByWeight" key := "pageSort.ByWeight"
pages, _ := spc.get(key, p, PageBy(DefaultPageSort).Sort) pages, _ := spc.get(key, p, pageBy(defaultPageSort).Sort)
return pages return pages
} }
@ -100,7 +100,7 @@ func (p Pages) ByTitle() Pages {
return p1.Title < p2.Title return p1.Title < p2.Title
} }
pages, _ := spc.get(key, p, PageBy(title).Sort) pages, _ := spc.get(key, p, pageBy(title).Sort)
return pages return pages
} }
@ -117,7 +117,7 @@ func (p Pages) ByLinkTitle() Pages {
return p1.linkTitle < p2.linkTitle return p1.linkTitle < p2.linkTitle
} }
pages, _ := spc.get(key, p, PageBy(linkTitle).Sort) pages, _ := spc.get(key, p, pageBy(linkTitle).Sort)
return pages return pages
} }
@ -135,7 +135,7 @@ func (p Pages) ByDate() Pages {
return p1.Date.Unix() < p2.Date.Unix() return p1.Date.Unix() < p2.Date.Unix()
} }
pages, _ := spc.get(key, p, PageBy(date).Sort) pages, _ := spc.get(key, p, pageBy(date).Sort)
return pages return pages
} }
@ -153,7 +153,7 @@ func (p Pages) ByPublishDate() Pages {
return p1.PublishDate.Unix() < p2.PublishDate.Unix() return p1.PublishDate.Unix() < p2.PublishDate.Unix()
} }
pages, _ := spc.get(key, p, PageBy(pubDate).Sort) pages, _ := spc.get(key, p, pageBy(pubDate).Sort)
return pages return pages
} }
@ -171,7 +171,7 @@ func (p Pages) ByLength() Pages {
return len(p1.Content) < len(p2.Content) return len(p1.Content) < len(p2.Content)
} }
pages, _ := spc.get(key, p, PageBy(length).Sort) pages, _ := spc.get(key, p, pageBy(length).Sort)
return pages return pages
} }

View file

@ -23,25 +23,25 @@ import (
"github.com/spf13/hugo/helpers" "github.com/spf13/hugo/helpers"
) )
// PathPattern represents a string which builds up a URL from attributes // pathPattern represents a string which builds up a URL from attributes
type PathPattern string type pathPattern string
// PageToPermaAttribute is the type of a function which, given a page and a tag // pageToPermaAttribute is the type of a function which, given a page and a tag
// can return a string to go in that position in the page (or an error) // can return a string to go in that position in the page (or an error)
type PageToPermaAttribute func(*Page, string) (string, error) type pageToPermaAttribute func(*Page, string) (string, error)
// PermalinkOverrides maps a section name to a PathPattern // PermalinkOverrides maps a section name to a PathPattern
type PermalinkOverrides map[string]PathPattern type PermalinkOverrides map[string]pathPattern
// knownPermalinkAttributes maps :tags in a permalink specification to a // knownPermalinkAttributes maps :tags in a permalink specification to a
// function which, given a page and the tag, returns the resulting string // function which, given a page and the tag, returns the resulting string
// to be used to replace that tag. // to be used to replace that tag.
var knownPermalinkAttributes map[string]PageToPermaAttribute var knownPermalinkAttributes map[string]pageToPermaAttribute
var attributeRegexp *regexp.Regexp var attributeRegexp *regexp.Regexp
// validate determines if a PathPattern is well-formed // validate determines if a PathPattern is well-formed
func (pp PathPattern) validate() bool { func (pp pathPattern) validate() bool {
fragments := strings.Split(string(pp[1:]), "/") fragments := strings.Split(string(pp[1:]), "/")
var bail = false var bail = false
for i := range fragments { for i := range fragments {
@ -69,7 +69,7 @@ func (pp PathPattern) validate() bool {
} }
type permalinkExpandError struct { type permalinkExpandError struct {
pattern PathPattern pattern pathPattern
section string section string
err error err error
} }
@ -85,7 +85,7 @@ var (
// Expand on a PathPattern takes a Page and returns the fully expanded Permalink // Expand on a PathPattern takes a Page and returns the fully expanded Permalink
// or an error explaining the failure. // or an error explaining the failure.
func (pp PathPattern) Expand(p *Page) (string, error) { func (pp pathPattern) Expand(p *Page) (string, error) {
if !pp.validate() { if !pp.validate() {
return "", &permalinkExpandError{pattern: pp, section: "<all>", err: errPermalinkIllFormed} return "", &permalinkExpandError{pattern: pp, section: "<all>", err: errPermalinkIllFormed}
} }
@ -184,7 +184,7 @@ func pageToPermalinkSection(p *Page, _ string) (string, error) {
} }
func init() { func init() {
knownPermalinkAttributes = map[string]PageToPermaAttribute{ knownPermalinkAttributes = map[string]pageToPermaAttribute{
"year": pageToPermalinkDate, "year": pageToPermalinkDate,
"month": pageToPermalinkDate, "month": pageToPermalinkDate,
"monthname": pageToPermalinkDate, "monthname": pageToPermalinkDate,

View file

@ -53,7 +53,7 @@ var testdataPermalinks = []struct {
func TestPermalinkValidation(t *testing.T) { func TestPermalinkValidation(t *testing.T) {
for _, item := range testdataPermalinks { for _, item := range testdataPermalinks {
pp := PathPattern(item.spec) pp := pathPattern(item.spec)
have := pp.validate() have := pp.validate()
if have == item.valid { if have == item.valid {
continue continue
@ -77,7 +77,7 @@ func TestPermalinkExpansion(t *testing.T) {
if !item.valid { if !item.valid {
continue continue
} }
pp := PathPattern(item.spec) pp := pathPattern(item.spec)
result, err := pp.Expand(page) result, err := pp.Expand(page)
if err != nil { if err != nil {
t.Errorf("failed to expand page: %s", err) t.Errorf("failed to expand page: %s", err)

View file

@ -447,7 +447,6 @@ func (s *Site) ReBuild(events []fsnotify.Event) error {
for _, ev := range events { for _, ev := range events {
// Need to re-read source // Need to re-read source
if strings.HasPrefix(ev.Name, s.absContentDir()) { if strings.HasPrefix(ev.Name, s.absContentDir()) {
logger.Println("Source changed", ev.Name)
sourceChanged = append(sourceChanged, ev) sourceChanged = append(sourceChanged, ev)
} }
if strings.HasPrefix(ev.Name, s.absLayoutDir()) || strings.HasPrefix(ev.Name, s.absThemeDir()) { if strings.HasPrefix(ev.Name, s.absLayoutDir()) || strings.HasPrefix(ev.Name, s.absThemeDir()) {
@ -803,7 +802,7 @@ func (s *Site) initializeSiteInfo() {
permalinks := make(PermalinkOverrides) permalinks := make(PermalinkOverrides)
for k, v := range viper.GetStringMapString("Permalinks") { for k, v := range viper.GetStringMapString("Permalinks") {
permalinks[k] = PathPattern(v) permalinks[k] = pathPattern(v)
} }
s.Info = SiteInfo{ s.Info = SiteInfo{