diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-03-09 19:19:29 +0100 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2017-03-27 15:43:56 +0200 |
commit | 6bf010fed432e5574e19fd2946ee6397d895950e (patch) | |
tree | 75282ccbd526adc8dba62f9392db282b3bcec49f /hugolib/page_output.go | |
parent | c8fff9501d424882a42f750800d9982ec47df640 (diff) |
hugolib: Refactor/-work the permalink/target path logic
This is a pretty fundamental change in Hugo, but absolutely needed if we should have any hope of getting "multiple outputs" done.
This commit's goal is to say:
* Every file target path is created by `createTargetPath`, i.e. one function for all.
* That function takes every page and site parameter into account, to avoid fragile string parsing to uglify etc. later on.
* The path creation logic has full test coverage.
* All permalinks, paginator URLs etc. are then built on top of that same logic.
Fixes #1252
Fixes #2110
Closes #2374
Fixes #1885
Fixes #3102
Fixes #3179
Fixes #1641
Fixes #1989
Diffstat (limited to 'hugolib/page_output.go')
-rw-r--r-- | hugolib/page_output.go | 40 |
1 files changed, 37 insertions, 3 deletions
diff --git a/hugolib/page_output.go b/hugolib/page_output.go index 45df23388..88386a6d0 100644 --- a/hugolib/page_output.go +++ b/hugolib/page_output.go @@ -14,6 +14,8 @@ package hugolib import ( + "sync" + "github.com/spf13/hugo/output" ) @@ -22,18 +24,50 @@ import ( type PageOutput struct { *Page + // Pagination + paginator *Pager + paginatorInit sync.Once + + // Keep this to create URL/path variations, i.e. paginators. + targetPathDescriptor targetPathDescriptor + outputType output.Type } -func newPageOutput(p *Page, createCopy bool, outputType output.Type) *PageOutput { +func (p *PageOutput) targetPath(addends ...string) (string, error) { + tp, err := p.createTargetPath(p.outputType, addends...) + if err != nil { + return "", err + } + return tp, nil + +} + +func newPageOutput(p *Page, createCopy bool, outputType output.Type) (*PageOutput, error) { if createCopy { + p.initURLs() p = p.copy() } - return &PageOutput{Page: p, outputType: outputType} + + td, err := p.createTargetPathDescriptor(outputType) + + if err != nil { + return nil, err + } + + return &PageOutput{ + Page: p, + outputType: outputType, + targetPathDescriptor: td, + }, nil } // copy creates a copy of this PageOutput with the lazy sync.Once vars reset // so they will be evaluated again, for word count calculations etc. func (p *PageOutput) copy() *PageOutput { - return newPageOutput(p.Page, true, p.outputType) + c, err := newPageOutput(p.Page, true, p.outputType) + if err != nil { + panic(err) + } + return c } |