Documentation ¶
Overview ¶
Package pageset defines a set of pages in a Kisipar web site.
TODO: * Rethink array / slice sorting stuff for byPath et al. Benchmark mem! * Tags() []string -> fetch all tags * ByMetaString(key string) -> ordered by this meta string in page * SubsetForTags([]string) -> all having all of these tags * SubsetForMetaString(key,val string) -> all having meta string of this * ByTime (and thus a decent Data func in Page) * Desc and Lower versions of Path sort (ByPathDesc, ByPathLower[Desc]) * Desc and Lower versions of ModTime and Date sorts.
Example ¶
package main import ( "fmt" "github.com/biztos/kisipar/page" "github.com/biztos/kisipar/pageset" ) func main() { // Given a set of pages: p1, _ := page.LoadVirtualString("/here/a.md", "# First!") p2, _ := page.LoadVirtualString("/here/b.md", "# Second!") p3, _ := page.LoadVirtualString("/there/d.md", "# Third!") // ...we create a Pageset: ps, err := pageset.New([]*page.Page{p1, p2, p3}) if err != nil { panic(err) } // ...which then can give us back its Pages in various ways: fmt.Println(ps.Page("/here/a").Title()) for i, p := range ps.ByModTime() { fmt.Println(i, p.Title()) } }
Output: First! 0 Third! 1 Second! 2 First!
Index ¶
- func Reverse(pages []*page.Page) []*page.Page
- type Pageset
- func (ps *Pageset) AddPage(p *page.Page)
- func (ps *Pageset) ByCreated() []*page.Page
- func (ps *Pageset) ByModTime() []*page.Page
- func (ps *Pageset) ByPath() []*page.Page
- func (ps *Pageset) ByTime() []*page.Page
- func (ps *Pageset) Len() int
- func (ps *Pageset) ListedSubset() *Pageset
- func (ps *Pageset) Page(key string) *page.Page
- func (ps *Pageset) PageSlice(pages []*page.Page, start, end int) []*page.Page
- func (ps *Pageset) PathSubset(prefix, trim string) *Pageset
- func (ps *Pageset) RefreshPage(key string) error
- func (ps *Pageset) RemovePage(key string)
- func (ps *Pageset) TagSubset(tag string) *Pageset
- func (ps *Pageset) Tags() []string
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
Types ¶
type Pageset ¶
type Pageset struct {
// contains filtered or unexported fields
}
A Pageset defines a set of Pages that may sorted and manipulated as needed. Pagesets can be as big as an entire Kisipar site, or as small as zero pages. The Pageset expects that its Pages are not manipulated outside of the Pageset context.
Each Page must be unique by extension-stripped Path, meaning a Pageset should not have Pages at both "foo/bar.md" and "foo/bar.txt".
Pages are normally accessed, e.g. in a template, using one of the sorting "By*" methods, which remove unlisted pages.
func New ¶
New creates a Pageset with the provided slice of Pages. Each Page must be unique by extension-stripped path key.
func (*Pageset) AddPage ¶
AddPage adds a Page to the Pageset, and clears the sorting and subset caches. If a Page for the Page's Path exists in the Pageset it is replaced.
func (*Pageset) ByCreated ¶
ByCreated returns an array of the Pageset's Pages sorted by Created time, newest first, as set in the page meta. If no Created time is set, the Page's ModTime is used. Ties (to the nanosecond) are broken by a smart Path comparison. Unlisted Pages are excluded. The result is cached for future use.
func (*Pageset) ByModTime ¶
ByModTime returns an array of the Pageset's Pages sorted by ModTime, newest first. Unlisted Pages are excluded. The result is cached for future use. For pages with exactly the same modification time (to the nanosecond) the secondary sort key is the Path.
func (*Pageset) ByPath ¶
ByPath returns an array of the Pageset's Pages sorted by Path: the first sort key is the length of the Path, the second is whether the page is an index (e.g. "foo/index.md") and the third is the path itself. Unlisted Pages are excluded. The result is cached for future use.
func (*Pageset) ByTime ¶
ByTime returns an array of the Pageset's Pages sorted by the Time function, newest first. Unlisted Pages are excluded. The result is cached for future use. For pages with exactly the same Time() (to the nanosecond) the secondary sort key is the Path.
func (*Pageset) Len ¶
Len returns the number of Pages in the Pageset. Note that this includes Unlisted Pages.
func (*Pageset) ListedSubset ¶
ListedSubset returns a new Pageset containing only those pages that are not marked Unlisted.
func (*Pageset) PageSlice ¶
PageSlice returns a slice of the input pages with the given start and end positions. If start is out of bounds for the slice then an empty slice is returned. If end is out of bounds then it is set to the length of the slice, i.e. to the end. A negative end is omitted, i.e. [start:].
This function is intended for use in templates, e.g. in order to show the first N items of a Pageset:
{{ with .Pageset }}{{ $pp := .Pageslice .ByTime 0 20 }}...{{ end }}
func (*Pageset) PathSubset ¶
PathSubset returns a new Pageset containing those Pages whose Path has the provided prefix. If a trim string is defined then it is trimmed from the beginning of each path before comparison. The subset is cached for future use. NOTE: any change (AddPage, RemovePage, RefreshPage) to the resulting Pageset will *NOT* affect the origin Pageset. Thus in a normal website context, these operations should only be performed on the master Pageset. TODO: this is stupid, let the Pageset know about its trim string in general
func (*Pageset) RefreshPage ¶
RefreshPage refreshes a Page at the given path key, by reloading it, loading it into the Pageset, or removing it if it is no longer on disk. If the page is not found or any filesystem or parse error occurs, the error is returned; nil is returned on success.
func (*Pageset) RemovePage ¶
RemovePage removes the Page at the given normalized path key from the Pageset, and clears the sorting and subset caches.