pageset

package
v0.0.0-...-cf5c4a9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 22, 2023 License: MIT Imports: 6 Imported by: 0

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

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func Reverse

func Reverse(pages []*page.Page) []*page.Page

Reverse reverses a slice of Pages, returning a new slice.

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

func New(pages []*page.Page) (*Pageset, error)

New creates a Pageset with the provided slice of Pages. Each Page must be unique by extension-stripped path key.

func (*Pageset) AddPage

func (ps *Pageset) AddPage(p *page.Page)

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

func (ps *Pageset) ByCreated() []*page.Page

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

func (ps *Pageset) ByModTime() []*page.Page

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

func (ps *Pageset) ByPath() []*page.Page

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

func (ps *Pageset) ByTime() []*page.Page

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

func (ps *Pageset) Len() int

Len returns the number of Pages in the Pageset. Note that this includes Unlisted Pages.

func (*Pageset) ListedSubset

func (ps *Pageset) ListedSubset() *Pageset

ListedSubset returns a new Pageset containing only those pages that are not marked Unlisted.

func (*Pageset) Page

func (ps *Pageset) Page(key string) *page.Page

Page returns the Page at the given path key, or nil if none is defined.

func (*Pageset) PageSlice

func (ps *Pageset) PageSlice(pages []*page.Page, start, end int) []*page.Page

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

func (ps *Pageset) PathSubset(prefix, trim string) *Pageset

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

func (ps *Pageset) RefreshPage(key string) error

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

func (ps *Pageset) RemovePage(key string)

RemovePage removes the Page at the given normalized path key from the Pageset, and clears the sorting and subset caches.

func (*Pageset) TagSubset

func (ps *Pageset) TagSubset(tag string) *Pageset

TagSubset returns a new Pageset containing only those pages that contain the given tag, case-insensitively. The result is cached for future use.

func (*Pageset) Tags

func (ps *Pageset) Tags() []string

Tags returns the unique lowercase tags found in the Pageset's Pages, alpha-sorted. Unlisted pages are ignored. The result is cached for future use.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL